From 3cdffaa2bdfd9bfb7f712e547be24de0dfed8331 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 21 Nov 2023 13:04:32 +0100 Subject: [PATCH 1/3] fix(gateway): do not act on template errors --- gateway/assets/test/main.go | 6 +----- gateway/handler.go | 6 ++---- gateway/handler_codec.go | 10 +++------- gateway/handler_unixfs_dir.go | 1 - 4 files changed, 6 insertions(+), 17 deletions(-) diff --git a/gateway/assets/test/main.go b/gateway/assets/test/main.go index c38c72057..8072f0000 100644 --- a/gateway/assets/test/main.go +++ b/gateway/assets/test/main.go @@ -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() { diff --git a/gateway/handler.go b/gateway/handler.go index ab9b46ebd..181236b76 100644 --- a/gateway/handler.go +++ b/gateway/handler.go @@ -912,13 +912,11 @@ func (i *handler) handleSuperfluousNamespace(w http.ResponseWriter, r *http.Requ // - redirects to intendedURL after a short delay w.WriteHeader(http.StatusBadRequest) - if err := redirectTemplate.Execute(w, redirectTemplateData{ + _ = redirectTemplate.Execute(w, redirectTemplateData{ 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) - } + }) return true } diff --git a/gateway/handler_codec.go b/gateway/handler_codec.go index 617eb9396..b6a738367 100644 --- a/gateway/handler_codec.go +++ b/gateway/handler_codec.go @@ -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 diff --git a/gateway/handler_unixfs_dir.go b/gateway/handler_unixfs_dir.go index 678c51aba..366514085 100644 --- a/gateway/handler_unixfs_dir.go +++ b/gateway/handler_unixfs_dir.go @@ -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 } From af84598ecfccfd2e9c3158e69adf520d91b249ee Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 21 Nov 2023 13:20:31 +0100 Subject: [PATCH 2/3] docs: changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db6859abf..749bbdb06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 5bfcf4f95ed19f691e3ecaad99602f18d8c8840b Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 22 Nov 2023 10:26:25 +0100 Subject: [PATCH 3/3] feat: write error on template execution error --- gateway/assets/test/main.go | 5 ++++- gateway/errors.go | 5 ++++- gateway/handler.go | 5 ++++- gateway/handler_codec.go | 3 +++ gateway/handler_unixfs_dir.go | 1 + 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/gateway/assets/test/main.go b/gateway/assets/test/main.go index 8072f0000..c03074dab 100644 --- a/gateway/assets/test/main.go +++ b/gateway/assets/test/main.go @@ -157,7 +157,10 @@ func runTemplate(w http.ResponseWriter, filename string, data interface{}) { http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError) return } - _ = tpl.Execute(w, data) + err = tpl.Execute(w, data) + if err != nil { + _, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err))) + } } func main() { diff --git a/gateway/errors.go b/gateway/errors.go index 4487f0c2e..4e4bb6823 100644 --- a/gateway/errors.go +++ b/gateway/errors.go @@ -165,7 +165,7 @@ func webError(w http.ResponseWriter, r *http.Request, c *Config, err error, defa if acceptsHTML { w.Header().Set("Content-Type", "text/html") w.WriteHeader(code) - _ = assets.ErrorTemplate.Execute(w, assets.ErrorTemplateData{ + err = assets.ErrorTemplate.Execute(w, assets.ErrorTemplateData{ GlobalData: assets.GlobalData{ Menu: c.Menu, }, @@ -173,6 +173,9 @@ func webError(w http.ResponseWriter, r *http.Request, c *Config, err error, defa StatusText: http.StatusText(code), Error: err.Error(), }) + if err != nil { + _, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err))) + } } else { http.Error(w, err.Error(), code) } diff --git a/gateway/handler.go b/gateway/handler.go index 181236b76..29a816b7a 100644 --- a/gateway/handler.go +++ b/gateway/handler.go @@ -912,11 +912,14 @@ func (i *handler) handleSuperfluousNamespace(w http.ResponseWriter, r *http.Requ // - redirects to intendedURL after a short delay w.WriteHeader(http.StatusBadRequest) - _ = redirectTemplate.Execute(w, redirectTemplateData{ + err = redirectTemplate.Execute(w, redirectTemplateData{ RedirectURL: intendedURL, SuggestedPath: intendedPath.String(), ErrorMsg: fmt.Sprintf("invalid path: %q should be %q", r.URL.Path, intendedPath.String()), }) + if err != nil { + _, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err))) + } return true } diff --git a/gateway/handler_codec.go b/gateway/handler_codec.go index b6a738367..89bff966e 100644 --- a/gateway/handler_codec.go +++ b/gateway/handler_codec.go @@ -203,6 +203,9 @@ func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r * CodecHex: fmt.Sprintf("0x%x", uint64(cidCodec)), Node: parseNode(blockCid, blockData), }) + if err != nil { + _, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err))) + } return err == nil } diff --git a/gateway/handler_unixfs_dir.go b/gateway/handler_unixfs_dir.go index 366514085..098a77b6a 100644 --- a/gateway/handler_unixfs_dir.go +++ b/gateway/handler_unixfs_dir.go @@ -212,6 +212,7 @@ 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 { + _, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err))) return false }