Skip to content

Commit 7992025

Browse files
committed
fix(gw): 404 when a valid DAG is missing link
1 parent cf82ae5 commit 7992025

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

core/corehttp/gateway_handler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
cid "github.com/ipfs/go-cid"
2121
files "github.com/ipfs/go-ipfs-files"
22+
ipld "github.com/ipfs/go-ipld-format"
2223
dag "github.com/ipfs/go-merkledag"
2324
mfs "github.com/ipfs/go-mfs"
2425
path "github.com/ipfs/go-path"
@@ -389,8 +390,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
389390
logger.Debugw("serve pretty 404 if present")
390391
return
391392
}
392-
393-
webError(w, "ipfs resolve -r "+debugStr(contentPath.String()), err, http.StatusNotFound)
393+
webError(w, "ipfs resolve -r "+debugStr(contentPath.String()), err, http.StatusBadRequest)
394394
return
395395
}
396396

@@ -782,6 +782,8 @@ func webError(w http.ResponseWriter, message string, err error, defaultCode int)
782782
webErrorWithCode(w, message, err, http.StatusNotFound)
783783
} else if err == routing.ErrNotFound {
784784
webErrorWithCode(w, message, err, http.StatusNotFound)
785+
} else if ipld.IsNotFound(err) {
786+
webErrorWithCode(w, message, err, http.StatusNotFound)
785787
} else if err == context.DeadlineExceeded {
786788
webErrorWithCode(w, message, err, http.StatusRequestTimeout)
787789
} else {

core/corehttp/gateway_handler_unixfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (i *gatewayHandler) serveUnixFS(ctx context.Context, w http.ResponseWriter,
2222
// Handling UnixFS
2323
dr, err := i.api.Unixfs().Get(ctx, resolvedPath)
2424
if err != nil {
25-
webError(w, "ipfs cat "+html.EscapeString(contentPath.String()), err, http.StatusNotFound)
25+
webError(w, "ipfs cat "+html.EscapeString(contentPath.String()), err, http.StatusBadRequest)
2626
return
2727
}
2828
defer dr.Close()

core/corehttp/gateway_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,17 @@ func TestGatewayGet(t *testing.T) {
235235
{"127.0.0.1:8080", "/", http.StatusNotFound, "404 page not found\n"},
236236
{"127.0.0.1:8080", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"},
237237
{"127.0.0.1:8080", k.String(), http.StatusOK, "fnord"},
238-
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusNotFound, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
239-
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"},
238+
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusBadRequest, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
239+
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusBadRequest, "ipfs resolve -r /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"},
240240
{"127.0.0.1:8080", "/ipns/example.com", http.StatusOK, "fnord"},
241241
{"example.com", "/", http.StatusOK, "fnord"},
242242

243243
{"working.example.com", "/", http.StatusOK, "fnord"},
244244
{"double.example.com", "/", http.StatusOK, "fnord"},
245245
{"triple.example.com", "/", http.StatusOK, "fnord"},
246246
{"working.example.com", k.String(), http.StatusNotFound, "ipfs resolve -r /ipns/working.example.com" + k.String() + ": no link named \"ipfs\" under " + k.Cid().String() + "\n"},
247-
{"broken.example.com", "/", http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
248-
{"broken.example.com", k.String(), http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com" + k.String() + ": " + namesys.ErrResolveFailed.Error() + "\n"},
247+
{"broken.example.com", "/", http.StatusBadRequest, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
248+
{"broken.example.com", k.String(), http.StatusBadRequest, "ipfs resolve -r /ipns/broken.example.com" + k.String() + ": " + namesys.ErrResolveFailed.Error() + "\n"},
249249
// This test case ensures we don't treat the TLD as a file extension.
250250
{"example.man", "/", http.StatusOK, "fnord"},
251251
} {

test/sharness/t0110-gateway.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ test_expect_success "GET IPFS directory with index.html and trailing slash retur
8888
test_should_contain \"hello i am a webpage\" response_with_slash
8989
"
9090

91-
test_expect_success "GET IPFS nonexistent file returns code expected (404)" '
91+
test_expect_success "GET IPFS nonexistent file returns 404 (Not Found)" '
9292
test_curl_resp_http_code "http://127.0.0.1:$port/ipfs/$HASH2/pleaseDontAddMe" "HTTP/1.1 404 Not Found"
9393
'
9494

95+
test_expect_success "GET IPFS invalid CID returns 400 (Bad Request)" '
96+
test_curl_resp_http_code "http://127.0.0.1:$port/ipfs/QmInvalid/pleaseDontAddMe" "HTTP/1.1 400 Bad Request"
97+
'
98+
9599
# https://github.com/ipfs/go-ipfs/issues/8230
96100
test_expect_success "GET IPFS inlined zero-length data object returns ok code (200)" '
97101
curl -sD - "http://127.0.0.1:$port/ipfs/bafkqaaa" > empty_ok_response &&
@@ -105,6 +109,10 @@ test_expect_success "GET /ipfs/ipfs/{cid} returns redirect to the valid path" '
105109
test_should_contain "<link rel=\"canonical\" href=\"/ipfs/bafkqaaa?query=to-remember\" />" response_with_double_ipfs_ns
106110
'
107111

112+
test_expect_success "GET invalid IPNS root returns 400 (Bad Request)" '
113+
test_curl_resp_http_code "http://127.0.0.1:$port/ipns/QmInvalid/pleaseDontAddMe" "HTTP/1.1 400 Bad Request"
114+
'
115+
108116
test_expect_failure "GET IPNS path succeeds" '
109117
ipfs name publish --allow-offline "$HASH" &&
110118
PEERID=$(ipfs config Identity.PeerID) &&

0 commit comments

Comments
 (0)