From 38815514df3be45977ead5d9c0c48db4d7b7cc92 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 20 Mar 2023 11:03:19 +0100 Subject: [PATCH] feat: return 400 on /ipfs/invalid-cid (#205) --- examples/go.mod | 2 +- examples/go.sum | 4 ++-- gateway/errors.go | 3 +++ gateway/gateway_test.go | 2 +- gateway/handler_test.go | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/go.mod b/examples/go.mod index c059028cc..e4232893d 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/gogo/protobuf v1.3.2 github.com/ipfs/go-blockservice v0.5.0 - github.com/ipfs/go-cid v0.3.2 + github.com/ipfs/go-cid v0.4.0 github.com/ipfs/go-datastore v0.6.0 github.com/ipfs/go-fetcher v1.6.1 github.com/ipfs/go-ipfs-blockstore v1.2.0 diff --git a/examples/go.sum b/examples/go.sum index d53d3aea5..575afa97e 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -392,8 +392,8 @@ github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= -github.com/ipfs/go-cid v0.3.2 h1:OGgOd+JCFM+y1DjWPmVH+2/4POtpDzwcr7VgnB7mZXc= -github.com/ipfs/go-cid v0.3.2/go.mod h1:gQ8pKqT/sUxGY+tIwy1RPpAojYu7jAyCp5Tz1svoupw= +github.com/ipfs/go-cid v0.4.0 h1:a4pdZq0sx6ZSxbCizebnKiMCx/xI/aBBFlB73IgH4rA= +github.com/ipfs/go-cid v0.4.0/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= github.com/ipfs/go-datastore v0.4.0/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= diff --git a/gateway/errors.go b/gateway/errors.go index 47f40cf5b..3fd18e277 100644 --- a/gateway/errors.go +++ b/gateway/errors.go @@ -8,6 +8,7 @@ import ( "strconv" "time" + "github.com/ipfs/go-cid" ipld "github.com/ipfs/go-ipld-format" "github.com/ipfs/go-path/resolver" ) @@ -137,6 +138,8 @@ func webError(w http.ResponseWriter, err error, defaultCode int) { // Handle status code switch { + case errors.Is(err, &cid.ErrInvalidCid{}): + code = http.StatusBadRequest case isErrNotFound(err): code = http.StatusNotFound case errors.Is(err, context.DeadlineExceeded): diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index 2b9ef0c01..4fad1527a 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -342,7 +342,7 @@ func TestGatewayGet(t *testing.T) { }{ {"127.0.0.1:8080", "/", http.StatusNotFound, "404 page not found\n"}, {"127.0.0.1:8080", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"}, - {"127.0.0.1:8080", "/ipfs/this-is-not-a-cid", http.StatusInternalServerError, "failed to resolve /ipfs/this-is-not-a-cid: invalid path \"/ipfs/this-is-not-a-cid\": invalid CID: illegal base32 data at input byte 3\n"}, + {"127.0.0.1:8080", "/ipfs/this-is-not-a-cid", http.StatusBadRequest, "failed to resolve /ipfs/this-is-not-a-cid: invalid path \"/ipfs/this-is-not-a-cid\": invalid CID: invalid cid: illegal base32 data at input byte 3\n"}, {"127.0.0.1:8080", k.String(), http.StatusOK, "fnord"}, {"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusInternalServerError, "failed to resolve /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"}, {"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusInternalServerError, "failed to resolve /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"}, diff --git a/gateway/handler_test.go b/gateway/handler_test.go index 99f70c4d7..cd67ba9e4 100644 --- a/gateway/handler_test.go +++ b/gateway/handler_test.go @@ -74,7 +74,7 @@ func (api *errorMockAPI) ResolvePath(ctx context.Context, ip ipath.Path) (ipath. return nil, api.err } -func TestGatewayInternalServerErrorInvalidPath(t *testing.T) { +func TestGatewayBadRequestInvalidPath(t *testing.T) { api, _ := newMockAPI(t) ts := newTestServer(t, api) t.Logf("test server url: %s", ts.URL) @@ -85,7 +85,7 @@ func TestGatewayInternalServerErrorInvalidPath(t *testing.T) { res, err := ts.Client().Do(req) assert.Nil(t, err) - assert.Equal(t, http.StatusInternalServerError, res.StatusCode) + assert.Equal(t, http.StatusBadRequest, res.StatusCode) } func TestErrorBubblingFromAPI(t *testing.T) { diff --git a/go.mod b/go.mod index a48d98dfd..51ffc5db3 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/google/uuid v1.3.0 github.com/gorilla/mux v1.8.0 github.com/ipfs/go-blockservice v0.5.0 - github.com/ipfs/go-cid v0.3.2 + github.com/ipfs/go-cid v0.4.0 github.com/ipfs/go-datastore v0.6.0 github.com/ipfs/go-detect-race v0.0.1 github.com/ipfs/go-fetcher v1.6.1 diff --git a/go.sum b/go.sum index ede1a802c..8c4127c1f 100644 --- a/go.sum +++ b/go.sum @@ -360,8 +360,8 @@ github.com/ipfs/go-cid v0.0.4/go.mod h1:4LLaPOQwmk5z9LBgQnpkivrx8BJjUyGwTXCd5Xfj github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog= github.com/ipfs/go-cid v0.0.6/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I= -github.com/ipfs/go-cid v0.3.2 h1:OGgOd+JCFM+y1DjWPmVH+2/4POtpDzwcr7VgnB7mZXc= -github.com/ipfs/go-cid v0.3.2/go.mod h1:gQ8pKqT/sUxGY+tIwy1RPpAojYu7jAyCp5Tz1svoupw= +github.com/ipfs/go-cid v0.4.0 h1:a4pdZq0sx6ZSxbCizebnKiMCx/xI/aBBFlB73IgH4rA= +github.com/ipfs/go-cid v0.4.0/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk= github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE= github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw= github.com/ipfs/go-datastore v0.4.0/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA=