Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 45bf988

Browse files
author
Lars Gierth
committedNov 21, 2015
gateway: harden path prefix, only allow paths, omit URIs
License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>
1 parent 93c2309 commit 45bf988

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed
 

‎core/corehttp/gateway_handler.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,11 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
9797
// It will be prepended to links in directory listings and the index.html redirect.
9898
prefix := ""
9999
if prefixHdr := r.Header["X-Ipfs-Gateway-Prefix"]; len(prefixHdr) > 0 {
100-
log.Debugf("X-Ipfs-Gateway-Prefix: %s", prefixHdr[0])
101-
prefix = prefixHdr[0]
100+
prfx := prefixHdr[0]
101+
if strings.HasPrefix(prfx, "/") {
102+
log.Debugf("X-Ipfs-Gateway-Prefix: %s", prfx)
103+
prefix = prfx
104+
}
102105
}
103106

104107
// IPNSHostnameOption might have constructed an IPNS path using the Host header.

‎core/corehttp/gateway_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,35 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
396396
if !strings.Contains(s, "<a href=\"/prefix/file.txt\">") {
397397
t.Fatalf("expected file in directory listing")
398398
}
399+
400+
// make request to directory listing with illegal prefix
401+
req, err = http.NewRequest("GET", ts.URL, nil)
402+
if err != nil {
403+
t.Fatal(err)
404+
}
405+
req.Host = "example.net"
406+
req.Header.Set("X-Ipfs-Gateway-Prefix", "http://evil.com")
407+
408+
res, err = doWithoutRedirect(req)
409+
if err != nil {
410+
t.Fatal(err)
411+
}
412+
413+
// expect correct backlinks without illegal prefix
414+
body, err = ioutil.ReadAll(res.Body)
415+
if err != nil {
416+
t.Fatalf("error reading response: %s", err)
417+
}
418+
s = string(body)
419+
t.Logf("body: %s\n", string(body))
420+
421+
if !strings.Contains(s, "Index of /") {
422+
t.Fatalf("expected a path in directory listing")
423+
}
424+
if !strings.Contains(s, "<a href=\"/\">") {
425+
t.Fatalf("expected backlink in directory listing")
426+
}
427+
if !strings.Contains(s, "<a href=\"/file.txt\">") {
428+
t.Fatalf("expected file in directory listing")
429+
}
399430
}

0 commit comments

Comments
 (0)
Please sign in to comment.