Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gateway): do not act on template errors #515

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ The following emojis are used to highlight certain changes:

### Fixed

* `boxo/gateway`: a panic (which is recovered) could sporadically be triggered inside a CAR request, if the right [conditions were met](https://github.com/ipfs/boxo/pull/511).
* `boxo/gateway`
* a panic (which is recovered) could sporadically be triggered inside a CAR request, if the right [conditions were met](https://github.com/ipfs/boxo/pull/511).
* no longer emits `http: superfluous response.WriteHeader` warnings when an error happens.

### Security

Expand Down
6 changes: 1 addition & 5 deletions gateway/assets/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,7 @@ func runTemplate(w http.ResponseWriter, filename string, data interface{}) {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
err = tpl.Execute(w, data)
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute template: %s", err), http.StatusInternalServerError)
return
}
_ = tpl.Execute(w, data)
}

func main() {
Expand Down
6 changes: 2 additions & 4 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,13 +912,11 @@
// - redirects to intendedURL after a short delay

w.WriteHeader(http.StatusBadRequest)
if err := redirectTemplate.Execute(w, redirectTemplateData{
_ = redirectTemplate.Execute(w, redirectTemplateData{

Check warning on line 915 in gateway/handler.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler.go#L915

Added line #L915 was not covered by tests
RedirectURL: intendedURL,
SuggestedPath: intendedPath.String(),
ErrorMsg: fmt.Sprintf("invalid path: %q should be %q", r.URL.Path, intendedPath.String()),
}); err != nil {
i.webError(w, r, fmt.Errorf("failed to redirect when fixing superfluous namespace: %w", err), http.StatusBadRequest)
lidel marked this conversation as resolved.
Show resolved Hide resolved
}
})

Check warning on line 919 in gateway/handler.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler.go#L919

Added line #L919 was not covered by tests

return true
}
Expand Down
10 changes: 3 additions & 7 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,16 @@ func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *
w.Header().Del("Cache-Control")

cidCodec := mc.Code(resolvedPath.RootCid().Prefix().Codec)
if err := assets.DagTemplate.Execute(w, assets.DagTemplateData{
err = assets.DagTemplate.Execute(w, assets.DagTemplateData{
GlobalData: i.getTemplateGlobalData(r, contentPath),
Path: contentPath.String(),
CID: resolvedPath.RootCid().String(),
CodecName: cidCodec.String(),
CodecHex: fmt.Sprintf("0x%x", uint64(cidCodec)),
Node: parseNode(blockCid, blockData),
}); err != nil {
err = fmt.Errorf("failed to generate HTML listing for this DAG: try fetching raw block with ?format=raw: %w", err)
i.webError(w, r, err, http.StatusInternalServerError)
return false
}
})

return true
return err == nil
}

// parseNode does a best effort attempt to parse this request's block such that
Expand Down
1 change: 0 additions & 1 deletion gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
rq.logger.Debugw("request processed", "tplDataDNSLink", globalData.DNSLink, "tplDataSize", size, "tplDataBackLink", backLink, "tplDataHash", hash)

if err := assets.DirectoryTemplate.Execute(w, tplData); err != nil {
i.webError(w, r, err, http.StatusInternalServerError)
return false
}

Expand Down
Loading