Skip to content

Commit 3c9b71d

Browse files
committed
fix: return http.StatusInternalServerError by default if cannot resolve
1 parent e7aa6a1 commit 3c9b71d

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

examples/go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,19 @@ require (
4949
github.com/ipfs/go-bitfield v1.0.0 // indirect
5050
github.com/ipfs/go-block-format v0.1.1 // indirect
5151
github.com/ipfs/go-datastore v0.6.0 // indirect
52+
github.com/ipfs/go-ipfs-chunker v0.0.1 // indirect
5253
github.com/ipfs/go-ipfs-ds-help v1.1.0 // indirect
5354
github.com/ipfs/go-ipfs-exchange-interface v0.2.0 // indirect
5455
github.com/ipfs/go-ipfs-files v0.3.0 // indirect
56+
github.com/ipfs/go-ipfs-posinfo v0.0.1 // indirect
5557
github.com/ipfs/go-ipfs-redirects-file v0.1.1 // indirect
5658
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
5759
github.com/ipfs/go-ipld-cbor v0.0.6 // indirect
5860
github.com/ipfs/go-ipld-legacy v0.1.1 // indirect
5961
github.com/ipfs/go-log v1.0.5 // indirect
6062
github.com/ipfs/go-log/v2 v2.5.1 // indirect
6163
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
64+
github.com/ipfs/go-mfs v0.2.1 // indirect
6265
github.com/ipfs/go-verifcid v0.0.2 // indirect
6366
github.com/ipld/go-car v0.5.0 // indirect
6467
github.com/jbenet/goprocess v0.1.4 // indirect
@@ -97,6 +100,7 @@ require (
97100
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
98101
github.com/whyrusleeping/cbor v0.0.0-20171005072247-63513f603b11 // indirect
99102
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa // indirect
103+
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect
100104
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
101105
go.opencensus.io v0.24.0 // indirect
102106
go.opentelemetry.io/otel v1.12.0 // indirect

examples/go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,11 @@ github.com/ipfs/go-merkledag v0.9.0 h1:DFC8qZ96Dz1hMT7dtIpcY524eFFDiEWAF8hNJHWW2
467467
github.com/ipfs/go-merkledag v0.9.0/go.mod h1:bPHqkHt5OZ0p1n3iqPeDiw2jIBkjAytRjS3WSBwjq90=
468468
github.com/ipfs/go-metrics-interface v0.0.1 h1:j+cpbjYvu4R8zbleSs36gvB7jR+wsL2fGD6n0jO4kdg=
469469
github.com/ipfs/go-metrics-interface v0.0.1/go.mod h1:6s6euYU4zowdslK0GKHmqaIZ3j/b/tL7HTWtJ4VPgWY=
470+
github.com/ipfs/go-mfs v0.2.1 h1:5jz8+ukAg/z6jTkollzxGzhkl3yxm022Za9f2nL5ab8=
471+
github.com/ipfs/go-mfs v0.2.1/go.mod h1:Woj80iuw4ajDnIP6+seRaoHpPsc9hmL0pk/nDNDWP88=
470472
github.com/ipfs/go-namesys v0.7.0 h1:xqosk71GIVRkFDtF2UNRcXn4LdNeo7tzuy8feHD6NbU=
471473
github.com/ipfs/go-namesys v0.7.0/go.mod h1:KYSZBVZG3VJC34EfqqJPG7T48aWgxseoMPAPA5gLyyQ=
474+
github.com/ipfs/go-path v0.2.1/go.mod h1:NOScsVgxfC/eIw4nz6OiGwK42PjaSJ4Y/ZFPn1Xe07I=
472475
github.com/ipfs/go-path v0.3.0 h1:tkjga3MtpXyM5v+3EbRvOHEoo+frwi4oumw5K+KYWyA=
473476
github.com/ipfs/go-path v0.3.0/go.mod h1:NOScsVgxfC/eIw4nz6OiGwK42PjaSJ4Y/ZFPn1Xe07I=
474477
github.com/ipfs/go-peertaskqueue v0.1.0/go.mod h1:Jmk3IyCcfl1W3jTW3YpghSwSEC6IJ3Vzz/jUmWw8Z0U=

gateway/gateway_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,17 @@ func TestGatewayGet(t *testing.T) {
349349
{"127.0.0.1:8080", "/", http.StatusNotFound, "404 page not found\n"},
350350
{"127.0.0.1:8080", "/" + k.Cid().String(), http.StatusNotFound, "404 page not found\n"},
351351
{"127.0.0.1:8080", k.String(), http.StatusOK, "fnord"},
352-
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusBadRequest, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
353-
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusBadRequest, "ipfs resolve -r /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"},
352+
{"127.0.0.1:8080", "/ipns/nxdomain.example.com", http.StatusInternalServerError, "ipfs resolve -r /ipns/nxdomain.example.com: " + namesys.ErrResolveFailed.Error() + "\n"},
353+
{"127.0.0.1:8080", "/ipns/%0D%0A%0D%0Ahello", http.StatusInternalServerError, "ipfs resolve -r /ipns/\\r\\n\\r\\nhello: " + namesys.ErrResolveFailed.Error() + "\n"},
354354
{"127.0.0.1:8080", "/ipns/example.com", http.StatusOK, "fnord"},
355355
{"example.com", "/", http.StatusOK, "fnord"},
356356

357357
{"working.example.com", "/", http.StatusOK, "fnord"},
358358
{"double.example.com", "/", http.StatusOK, "fnord"},
359359
{"triple.example.com", "/", http.StatusOK, "fnord"},
360360
{"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"},
361-
{"broken.example.com", "/", http.StatusBadRequest, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
362-
{"broken.example.com", k.String(), http.StatusBadRequest, "ipfs resolve -r /ipns/broken.example.com" + k.String() + ": " + namesys.ErrResolveFailed.Error() + "\n"},
361+
{"broken.example.com", "/", http.StatusInternalServerError, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
362+
{"broken.example.com", k.String(), http.StatusInternalServerError, "ipfs resolve -r /ipns/broken.example.com" + k.String() + ": " + namesys.ErrResolveFailed.Error() + "\n"},
363363
// This test case ensures we don't treat the TLD as a file extension.
364364
{"example.man", "/", http.StatusOK, "fnord"},
365365
} {

gateway/handler.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import (
1818
cid "github.com/ipfs/go-cid"
1919
ipld "github.com/ipfs/go-ipld-format"
2020
logging "github.com/ipfs/go-log"
21+
dag "github.com/ipfs/go-merkledag"
22+
mfs "github.com/ipfs/go-mfs"
23+
"github.com/ipfs/go-namesys"
24+
path "github.com/ipfs/go-path"
2125
"github.com/ipfs/go-path/resolver"
2226
coreiface "github.com/ipfs/interface-go-ipfs-core"
2327
ipath "github.com/ipfs/interface-go-ipfs-core/path"
@@ -743,6 +747,10 @@ func (i *handler) handlePathResolution(w http.ResponseWriter, r *http.Request, r
743747
case coreiface.ErrOffline:
744748
webError(w, "ipfs resolve -r "+debugStr(contentPath.String()), err, http.StatusServiceUnavailable)
745749
return nil, nil, false
750+
case namesys.ErrResolveFailed:
751+
// Note: webError will replace http.StatusBadRequest with StatusNotFound or StatusRequestTimeout if necessary
752+
webError(w, "ipfs resolve -r "+debugStr(contentPath.String()), err, http.StatusInternalServerError)
753+
return nil, nil, false
746754
default:
747755
// The path can't be resolved.
748756
if isUnixfsResponseFormat(responseFormat) {
@@ -767,7 +775,7 @@ func (i *handler) handlePathResolution(w http.ResponseWriter, r *http.Request, r
767775
}
768776
}
769777

770-
// Note: webError will replace http.StatusBadRequest with StatusNotFound if necessary
778+
// Note: webError will replace http.StatusBadRequest with StatusNotFound or StatusRequestTimeout if necessary
771779
webError(w, "ipfs resolve -r "+debugStr(contentPath.String()), err, http.StatusBadRequest)
772780
return nil, nil, false
773781
}

go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/ipfs/go-log/v2 v2.5.1
3232
github.com/ipfs/go-merkledag v0.9.0
3333
github.com/ipfs/go-metrics-interface v0.0.1
34+
github.com/ipfs/go-mfs v0.2.1
3435
github.com/ipfs/go-namesys v0.7.0
3536
github.com/ipfs/go-path v0.3.0
3637
github.com/ipfs/go-peertaskqueue v0.8.0
@@ -81,8 +82,10 @@ require (
8182
github.com/ipfs/bbloom v0.0.4 // indirect
8283
github.com/ipfs/go-bitfield v1.0.0 // indirect
8384
github.com/ipfs/go-block-format v0.1.1 // indirect
85+
github.com/ipfs/go-ipfs-chunker v0.0.1 // indirect
8486
github.com/ipfs/go-ipfs-ds-help v1.1.0 // indirect
8587
github.com/ipfs/go-ipfs-files v0.3.0 // indirect
88+
github.com/ipfs/go-ipfs-posinfo v0.0.1 // indirect
8689
github.com/ipfs/go-ipfs-pq v0.0.2 // indirect
8790
github.com/ipfs/go-ipld-cbor v0.0.6 // indirect
8891
github.com/ipfs/go-ipld-legacy v0.1.1 // indirect
@@ -119,6 +122,7 @@ require (
119122
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
120123
github.com/whyrusleeping/cbor v0.0.0-20171005072247-63513f603b11 // indirect
121124
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa // indirect
125+
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect
122126
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
123127
go.uber.org/atomic v1.10.0 // indirect
124128
golang.org/x/crypto v0.5.0 // indirect

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,11 @@ github.com/ipfs/go-merkledag v0.9.0 h1:DFC8qZ96Dz1hMT7dtIpcY524eFFDiEWAF8hNJHWW2
473473
github.com/ipfs/go-merkledag v0.9.0/go.mod h1:bPHqkHt5OZ0p1n3iqPeDiw2jIBkjAytRjS3WSBwjq90=
474474
github.com/ipfs/go-metrics-interface v0.0.1 h1:j+cpbjYvu4R8zbleSs36gvB7jR+wsL2fGD6n0jO4kdg=
475475
github.com/ipfs/go-metrics-interface v0.0.1/go.mod h1:6s6euYU4zowdslK0GKHmqaIZ3j/b/tL7HTWtJ4VPgWY=
476+
github.com/ipfs/go-mfs v0.2.1 h1:5jz8+ukAg/z6jTkollzxGzhkl3yxm022Za9f2nL5ab8=
477+
github.com/ipfs/go-mfs v0.2.1/go.mod h1:Woj80iuw4ajDnIP6+seRaoHpPsc9hmL0pk/nDNDWP88=
476478
github.com/ipfs/go-namesys v0.7.0 h1:xqosk71GIVRkFDtF2UNRcXn4LdNeo7tzuy8feHD6NbU=
477479
github.com/ipfs/go-namesys v0.7.0/go.mod h1:KYSZBVZG3VJC34EfqqJPG7T48aWgxseoMPAPA5gLyyQ=
480+
github.com/ipfs/go-path v0.2.1/go.mod h1:NOScsVgxfC/eIw4nz6OiGwK42PjaSJ4Y/ZFPn1Xe07I=
478481
github.com/ipfs/go-path v0.3.0 h1:tkjga3MtpXyM5v+3EbRvOHEoo+frwi4oumw5K+KYWyA=
479482
github.com/ipfs/go-path v0.3.0/go.mod h1:NOScsVgxfC/eIw4nz6OiGwK42PjaSJ4Y/ZFPn1Xe07I=
480483
github.com/ipfs/go-peertaskqueue v0.1.0/go.mod h1:Jmk3IyCcfl1W3jTW3YpghSwSEC6IJ3Vzz/jUmWw8Z0U=

0 commit comments

Comments
 (0)