diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 113fe52b5ff5..93fe5c82e580 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -134,6 +134,7 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { urlPath := r.URL.Path + escapedURLPath := r.URL.EscapedPath() // If the gateway is behind a reverse proxy and mounted at a sub-path, // the prefix header can be set to signal this sub-path. @@ -173,12 +174,12 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr case nil: case coreiface.ErrOffline: if !i.node.OnlineMode() { - webError(w, "ipfs resolve -r "+urlPath, err, http.StatusServiceUnavailable) + webError(w, "ipfs resolve -r "+escapedUrlPath, err, http.StatusServiceUnavailable) return } fallthrough default: - webError(w, "ipfs resolve -r "+urlPath, err, http.StatusNotFound) + webError(w, "ipfs resolve -r "+escapedUrlPath, err, http.StatusNotFound) return } @@ -191,7 +192,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr case coreiface.ErrIsDir: dir = true default: - webError(w, "ipfs cat "+urlPath, err, http.StatusNotFound) + webError(w, "ipfs cat "+escapedUrlPath, err, http.StatusNotFound) return } @@ -278,7 +279,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ixnd, err := dirr.Find(ctx, "index.html") switch { case err == nil: - log.Debugf("found index.html link for %s", urlPath) + log.Debugf("found index.html link for %s", escapedUrlPath) dirwithoutslash := urlPath[len(urlPath)-1] != '/' goget := r.URL.Query().Get("go-get") == "1" diff --git a/core/corehttp/gateway_test.go b/core/corehttp/gateway_test.go index da60e8a65ac4..3a4760e71746 100644 --- a/core/corehttp/gateway_test.go +++ b/core/corehttp/gateway_test.go @@ -140,6 +140,7 @@ func TestGatewayGet(t *testing.T) { {"localhost:5001", "/" + k, http.StatusNotFound, "404 page not found\n"}, {"localhost:5001", "/ipfs/" + k, http.StatusOK, "fnord"}, {"localhost:5001", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"}, + {"localhost:5001", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys.ErrResolveFailed.Error() + "\n"}, {"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"}, {"example.com", "/", http.StatusOK, "fnord"}, } {