Skip to content

Commit f7a9809

Browse files
Merge pull request #4977 from ipfs/fix/4973-1
only resolve dnslinks once in the gateway
2 parents d6086fb + e9928f7 commit f7a9809

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

core/corehttp/gateway_test.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"io/ioutil"
7+
"math"
78
"net/http"
89
"net/http/httptest"
910
"strings"
@@ -31,11 +32,25 @@ var emptyDir = "/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
3132
type mockNamesys map[string]path.Path
3233

3334
func (m mockNamesys) Resolve(ctx context.Context, name string, opts ...nsopts.ResolveOpt) (value path.Path, err error) {
34-
p, ok := m[name]
35-
if !ok {
36-
return "", namesys.ErrResolveFailed
35+
cfg := nsopts.DefaultResolveOpts()
36+
for _, o := range opts {
37+
o(cfg)
3738
}
38-
return p, nil
39+
depth := cfg.Depth
40+
if depth == nsopts.UnlimitedDepth {
41+
depth = math.MaxUint64
42+
}
43+
for depth > 0 && strings.HasPrefix(name, "/ipns/") {
44+
depth--
45+
46+
var ok bool
47+
value, ok = m[name]
48+
if !ok {
49+
return "", namesys.ErrResolveFailed
50+
}
51+
name = value.String()
52+
}
53+
return value, nil
3954
}
4055

4156
func (m mockNamesys) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
@@ -130,6 +145,10 @@ func TestGatewayGet(t *testing.T) {
130145
t.Fatal(err)
131146
}
132147
ns["/ipns/example.com"] = path.FromString("/ipfs/" + k)
148+
ns["/ipns/working.example.com"] = path.FromString("/ipfs/" + k)
149+
ns["/ipns/double.example.com"] = path.FromString("/ipns/working.example.com")
150+
ns["/ipns/triple.example.com"] = path.FromString("/ipns/double.example.com")
151+
ns["/ipns/broken.example.com"] = path.FromString("/ipns/" + k)
133152

134153
t.Log(ts.URL)
135154
for _, test := range []struct {
@@ -145,6 +164,13 @@ func TestGatewayGet(t *testing.T) {
145164
{"localhost:5001", "/ipns/%0D%0A%0D%0Ahello", http.StatusNotFound, "ipfs resolve -r /ipns/%0D%0A%0D%0Ahello: " + namesys.ErrResolveFailed.Error() + "\n"},
146165
{"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
147166
{"example.com", "/", http.StatusOK, "fnord"},
167+
168+
{"working.example.com", "/", http.StatusOK, "fnord"},
169+
{"double.example.com", "/", http.StatusOK, "fnord"},
170+
{"triple.example.com", "/", http.StatusOK, "fnord"},
171+
{"working.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/working.example.com/ipfs/" + k + ": no link named \"ipfs\" under " + k + "\n"},
172+
{"broken.example.com", "/", http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
173+
{"broken.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/ipfs/" + k + ": " + namesys.ErrResolveFailed.Error() + "\n"},
148174
} {
149175
var c http.Client
150176
r, err := http.NewRequest("GET", ts.URL+test.path, nil)

core/corehttp/ipns_hostname.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"net/http"
77
"strings"
88

9-
"github.com/ipfs/go-ipfs/core"
9+
core "github.com/ipfs/go-ipfs/core"
10+
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
1011

1112
isd "gx/ipfs/QmZmmuAXgX73UQmX1jRKjTGmjzq24Jinqkq8vzkBtno4uX/go-is-domain"
1213
)
@@ -24,7 +25,7 @@ func IPNSHostnameOption() ServeOption {
2425
host := strings.SplitN(r.Host, ":", 2)[0]
2526
if len(host) > 0 && isd.IsDomain(host) {
2627
name := "/ipns/" + host
27-
if _, err := n.Namesys.Resolve(ctx, name); err == nil {
28+
if _, err := n.Namesys.Resolve(ctx, name, nsopts.Depth(1)); err == nil {
2829
r.Header["X-Ipns-Original-Path"] = []string{r.URL.Path}
2930
r.URL.Path = name + r.URL.Path
3031
}

0 commit comments

Comments
 (0)