Skip to content

Commit ed9d229

Browse files
MichaelMurelidel
authored andcommitted
fix(gateway): ipld.ErrNotFound should result in a 404 (ipfs#630)
* fix(gateway): ipld.ErrNotFound should result in a 404 In case of blockstore restriction like with --offline or similar restriction, returning a 500 is inappropriate. It's also going back to a previous gateway behavior (at least in kubo v0.22, possibly later). * refactor(gateway): match 410 before 404s just a precaution to ensure 410 takes precedence, until we address ipfs#591 * docs: clarify 404 use case --------- Co-authored-by: Marcin Rataj <[email protected]>
1 parent 8bcdca1 commit ed9d229

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The following emojis are used to highlight certain changes:
2626

2727
### Fixed
2828

29+
- `boxo/gateway` now correctly returns 404 Status Not Found instead of 500 when the requested content cannot be found due to offline exchange, gateway running in no-fetch (non-recursive) mode, or a similar restriction that only serves a specific set of CIDs.
2930
- `bitswap/client` fix memory leak in BlockPresenceManager due to unlimited map growth.
3031

3132
### Security

gateway/errors.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ipfs/boxo/path"
1414
"github.com/ipfs/boxo/path/resolver"
1515
"github.com/ipfs/go-cid"
16+
ipld "github.com/ipfs/go-ipld-format"
1617
"github.com/ipld/go-ipld-prime/datamodel"
1718
"github.com/ipld/go-ipld-prime/schema"
1819
)
@@ -185,10 +186,10 @@ func webError(w http.ResponseWriter, r *http.Request, c *Config, err error, defa
185186
switch {
186187
case errors.Is(err, &cid.ErrInvalidCid{}):
187188
code = http.StatusBadRequest
188-
case isErrNotFound(err):
189-
code = http.StatusNotFound
190189
case isErrContentBlocked(err):
191190
code = http.StatusGone
191+
case isErrNotFound(err):
192+
code = http.StatusNotFound
192193
case errors.Is(err, context.DeadlineExceeded):
193194
code = http.StatusGatewayTimeout
194195
}
@@ -226,6 +227,10 @@ func isErrNotFound(err error) bool {
226227
return true
227228
}
228229

230+
if ipld.IsNotFound(err) {
231+
return true
232+
}
233+
229234
// Checks if err is of a type that does not implement the .Is interface and
230235
// cannot be directly compared to. Therefore, errors.Is cannot be used.
231236
for {

gateway/gateway_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ func TestErrorBubblingFromBackend(t *testing.T) {
924924
})
925925
}
926926

927-
testError("500 Not Found from IPLD", &ipld.ErrNotFound{}, http.StatusInternalServerError)
927+
testError("404 Not Found from IPLD", &ipld.ErrNotFound{}, http.StatusNotFound)
928928
testError("404 Not Found from path resolver", &resolver.ErrNoLink{}, http.StatusNotFound)
929929
testError("502 Bad Gateway", ErrBadGateway, http.StatusBadGateway)
930930
testError("504 Gateway Timeout", ErrGatewayTimeout, http.StatusGatewayTimeout)

0 commit comments

Comments
 (0)