Skip to content

Commit d32a74a

Browse files
author
Lars Gierth
committed
gateway: fix further resolution of dnslink failures
A while ago, we noticed that if IpnsHostname failed to look up a dnslink for a hostname in a Host header, it wouldn't respond with an error, but instead just skip the Host header part and carry on. In the issue were this was flagged, the example was: `_dnslink.tr.wikipedia-on-ipfs.org => /ipns/QmSomeKey` This key had stopped to resolve, and thus the resolution of /ipns/tr.wikipedia-on-ipfs.org would fail. Now you'd expect an error response, but instead it'd happily continue processing the request, disregarding the Host header, and allowing requests to e.g. http://tr.wikipedia-on-ipfs.org/ipns/libp2p.io to succeed. A previous patch (#4977) tried to work around this issue by limiting the recursion depth of Host: header dnslink resolutions to 1. In this patch we start to correctly handle the initial resolution error and stop processing the request, and we remove the earlier workaround. License: MIT Signed-off-by: Lars Gierth <[email protected]>
1 parent e7938a1 commit d32a74a

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

core/corehttp/gateway_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ func TestGatewayGet(t *testing.T) {
169169
{"double.example.com", "/", http.StatusOK, "fnord"},
170170
{"triple.example.com", "/", http.StatusOK, "fnord"},
171171
{"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"},
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: " + namesys.ErrResolveFailed.Error() + "\n"},
174174
} {
175175
var c http.Client
176176
r, err := http.NewRequest("GET", ts.URL+test.path, nil)

core/corehttp/ipns_hostname.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package corehttp
22

33
import (
44
"context"
5+
"fmt"
56
"net"
67
"net/http"
78
"strings"
89

910
core "github.com/ipfs/go-ipfs/core"
10-
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
1111

1212
isd "gx/ipfs/QmZmmuAXgX73UQmX1jRKjTGmjzq24Jinqkq8vzkBtno4uX/go-is-domain"
1313
)
@@ -25,10 +25,15 @@ func IPNSHostnameOption() ServeOption {
2525
host := strings.SplitN(r.Host, ":", 2)[0]
2626
if len(host) > 0 && isd.IsDomain(host) {
2727
name := "/ipns/" + host
28-
if _, err := n.Namesys.Resolve(ctx, name, nsopts.Depth(1)); err == nil {
29-
r.Header.Set("X-Ipns-Original-Path", r.URL.Path)
30-
r.URL.Path = name + r.URL.Path
28+
_, err := n.Namesys.Resolve(ctx, name)
29+
if err != nil {
30+
w.WriteHeader(404)
31+
fmt.Fprintf(w, "ipfs resolve -r %s: %s\n", name, err)
32+
return
3133
}
34+
35+
r.Header.Set("X-Ipns-Original-Path", r.URL.Path)
36+
r.URL.Path = name + r.URL.Path
3237
}
3338
childMux.ServeHTTP(w, r)
3439
})

0 commit comments

Comments
 (0)