Skip to content

Commit

Permalink
fix: keep invalid paths as 400
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 23, 2023
1 parent be6e09c commit 404f74a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 149 deletions.
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/ipfs/go-libipfs v0.4.0
github.com/ipfs/go-merkledag v0.9.0
github.com/ipfs/go-namesys v0.7.0
github.com/ipfs/go-path v0.3.0
github.com/ipfs/go-path v0.3.1-0.20230223124933-986db2269d7d
github.com/ipfs/go-unixfs v0.4.3
github.com/ipfs/go-unixfsnode v1.5.2
github.com/ipfs/interface-go-ipfs-core v0.10.0
Expand Down
70 changes: 3 additions & 67 deletions examples/go.sum

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
ipld "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
"github.com/ipfs/go-namesys"
"github.com/ipfs/go-path"
"github.com/ipfs/go-path/resolver"
coreiface "github.com/ipfs/interface-go-ipfs-core"
ipath "github.com/ipfs/interface-go-ipfs-core/path"
Expand Down Expand Up @@ -560,7 +561,9 @@ func webRequestError(w http.ResponseWriter, err *requestError) {
}

func webError(w http.ResponseWriter, err error, defaultCode int) {
if isErrNotFound(err) {
if errors.Is(err, path.ErrInvalidPath{}) {
webErrorWithCode(w, err, http.StatusBadRequest)
} else if isErrNotFound(err) {
webErrorWithCode(w, err, http.StatusNotFound)
} else if errors.Is(err, ErrGatewayTimeout) {
webErrorWithCode(w, err, http.StatusGatewayTimeout)
Expand Down
41 changes: 21 additions & 20 deletions gateway/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ipfs/go-libipfs/files"
iface "github.com/ipfs/interface-go-ipfs-core"
ipath "github.com/ipfs/interface-go-ipfs-core/path"
"github.com/tj/assert"
)

func TestEtagMatch(t *testing.T) {
Expand Down Expand Up @@ -70,24 +71,31 @@ func (api *errorMockAPI) ResolvePath(ctx context.Context, ip ipath.Path) (ipath.
return nil, api.err
}

func TestGatewayBadRequestInvalidPath(t *testing.T) {
api, _ := newMockAPI(t)
ts := newTestServer(t, api)
t.Logf("test server url: %s", ts.URL)

req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipfs/QmInvalid/Path", nil)
assert.Nil(t, err)

res, err := ts.Client().Do(req)
assert.Nil(t, err)

assert.Equal(t, http.StatusBadRequest, res.StatusCode)
}

func TestGatewayTimeoutBubblingFromAPI(t *testing.T) {
api := &errorMockAPI{err: fmt.Errorf("the mock api has timed out: %w", ErrGatewayTimeout)}
ts := newTestServer(t, api)
t.Logf("test server url: %s", ts.URL)

req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipns/en.wikipedia-on-ipfs.org", nil)
if err != nil {
t.Fatal(err)
}
assert.Nil(t, err)

res, err := ts.Client().Do(req)
if err != nil {
t.Fatal(err)
}

if res.StatusCode != http.StatusGatewayTimeout {
t.Fatalf("expected 504, got %d", res.StatusCode)
}
assert.Nil(t, err)
assert.Equal(t, http.StatusGatewayTimeout, res.StatusCode)
}

func TestBadGatewayBubblingFromAPI(t *testing.T) {
Expand All @@ -96,16 +104,9 @@ func TestBadGatewayBubblingFromAPI(t *testing.T) {
t.Logf("test server url: %s", ts.URL)

req, err := http.NewRequest(http.MethodGet, ts.URL+"/ipns/en.wikipedia-on-ipfs.org", nil)
if err != nil {
t.Fatal(err)
}
assert.Nil(t, err)

res, err := ts.Client().Do(req)
if err != nil {
t.Fatal(err)
}

if res.StatusCode != http.StatusBadGateway {
t.Fatalf("expected 504, got %d", res.StatusCode)
}
assert.Nil(t, err)
assert.Equal(t, http.StatusBadGateway, res.StatusCode)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/ipfs/go-merkledag v0.9.0
github.com/ipfs/go-metrics-interface v0.0.1
github.com/ipfs/go-namesys v0.7.0
github.com/ipfs/go-path v0.3.0
github.com/ipfs/go-path v0.3.1-0.20230223124933-986db2269d7d
github.com/ipfs/go-peertaskqueue v0.8.1
github.com/ipfs/go-unixfs v0.3.1
github.com/ipfs/go-unixfsnode v1.5.1
Expand All @@ -56,6 +56,7 @@ require (
github.com/prometheus/client_golang v1.14.0
github.com/samber/lo v1.36.0
github.com/stretchr/testify v1.8.1
github.com/tj/assert v0.0.3
go.opencensus.io v0.24.0
go.opentelemetry.io/otel v1.7.0
go.opentelemetry.io/otel/trace v1.7.0
Expand Down
Loading

0 comments on commit 404f74a

Please sign in to comment.