diff --git a/gateway/handler_codec.go b/gateway/handler_codec.go index f18bba3e2..567d271d5 100644 --- a/gateway/handler_codec.go +++ b/gateway/handler_codec.go @@ -172,6 +172,11 @@ func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r * if r.URL.RawQuery != "" { suffix = suffix + "?" + url.PathEscape(r.URL.RawQuery) } + // Re-escape path instead of reusing RawPath to avod mix of lawer + // and upper hex that may come from RawPath. + if strings.IndexRune(requestURI.RawPath, '%') != -1 { + requestURI.RawPath = "" + } // /ipfs/cid/foo?bar must be redirected to /ipfs/cid/foo/?bar redirectURL := requestURI.EscapedPath() + suffix http.Redirect(w, r, redirectURL, http.StatusMovedPermanently) diff --git a/gateway/handler_unixfs_dir.go b/gateway/handler_unixfs_dir.go index 04771e3ca..26713ac1c 100644 --- a/gateway/handler_unixfs_dir.go +++ b/gateway/handler_unixfs_dir.go @@ -49,9 +49,15 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r * if r.URL.RawQuery != "" { suffix = suffix + "?" + url.PathEscape(r.URL.RawQuery) } + // Re-escape path instead of reusing RawPath to avod mix of lawer + // and upper hex that may come from RawPath. + if strings.IndexRune(requestURI.RawPath, '%') != -1 { + requestURI.RawPath = "" + } // /ipfs/cid/foo?bar must be redirected to /ipfs/cid/foo/?bar redirectURL := requestURI.EscapedPath() + suffix rq.logger.Debugw("directory location moved permanently", "status", http.StatusMovedPermanently) + http.Redirect(w, r, redirectURL, http.StatusMovedPermanently) return true }