Skip to content

Commit 0fb2020

Browse files
authored
Merge pull request #5564 from ipfs/fix/5369
don't use the domain name as a filename in /ipns/a.com
2 parents 6ff0fe0 + f498459 commit 0fb2020

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

core/corehttp/gateway_handler.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
268268
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename*=UTF-8''%s", url.PathEscape(urlFilename)))
269269
name = urlFilename
270270
} else {
271-
name = gopath.Base(urlPath)
271+
name = getFilename(urlPath)
272272
}
273273
i.serveFile(w, r, name, modtime, dr)
274274
return
@@ -626,3 +626,11 @@ func webErrorWithCode(w http.ResponseWriter, message string, err error, code int
626626
func internalWebError(w http.ResponseWriter, err error) {
627627
webErrorWithCode(w, "internalWebError", err, http.StatusInternalServerError)
628628
}
629+
630+
func getFilename(s string) string {
631+
if (strings.HasPrefix(s, ipfsPathPrefix) || strings.HasPrefix(s, ipnsPathPrefix)) && strings.Count(gopath.Clean(s), "/") <= 2 {
632+
// Don't want to treat ipfs.io in /ipns/ipfs.io as a filename.
633+
return ""
634+
}
635+
return gopath.Base(s)
636+
}

core/corehttp/gateway_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ func TestGatewayGet(t *testing.T) {
153153
ns["/ipns/double.example.com"] = path.FromString("/ipns/working.example.com")
154154
ns["/ipns/triple.example.com"] = path.FromString("/ipns/double.example.com")
155155
ns["/ipns/broken.example.com"] = path.FromString("/ipns/" + k)
156+
// We picked .man because:
157+
// 1. It's a valid TLD.
158+
// 2. Go treats it as the file extension for "man" files (even though
159+
// nobody actually *uses* this extension, AFAIK).
160+
//
161+
// Unfortunately, this may not work on all platforms as file type
162+
// detection is platform dependent.
163+
ns["/ipns/example.man"] = path.FromString("/ipfs/" + k)
156164

157165
t.Log(ts.URL)
158166
for _, test := range []struct {
@@ -175,6 +183,8 @@ func TestGatewayGet(t *testing.T) {
175183
{"working.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/working.example.com/ipfs/" + k + ": no link by that name\n"},
176184
{"broken.example.com", "/", http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
177185
{"broken.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/ipfs/" + k + ": " + namesys.ErrResolveFailed.Error() + "\n"},
186+
// This test case ensures we don't treat the TLD as a file extension.
187+
{"example.man", "/", http.StatusOK, "fnord"},
178188
} {
179189
var c http.Client
180190
r, err := http.NewRequest("GET", ts.URL+test.path, nil)
@@ -190,6 +200,10 @@ func TestGatewayGet(t *testing.T) {
190200
continue
191201
}
192202
defer resp.Body.Close()
203+
contentType := resp.Header.Get("Content-Type")
204+
if contentType != "text/plain; charset=utf-8" {
205+
t.Errorf("expected content type to be text/plain, got %s", contentType)
206+
}
193207
if resp.StatusCode != test.status {
194208
t.Errorf("got %d, expected %d from %s", resp.StatusCode, test.status, urlstr)
195209
continue

0 commit comments

Comments
 (0)