don't use the domain name as a filename in /ipns/a.com
fixes #5369 License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Oct 4, 2018 at 15:36 UTC
47e7f693ebc22f9f7b5e1876fd16b31c46a83307
2 files changed
+15
-1
core/corehttp/gateway_handler.go
+9
-1
@@ -266,7 +266,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
266
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename*=UTF-8''%s", url.PathEscape(urlFilename)))
267
name = urlFilename
268
} else {
269
- name = gopath.Base(urlPath)
269
+ name = getFilename(urlPath)
270
}
271
i.serveFile(w, r, name, modtime, dr)
272
return
@@ -624,3 +624,11 @@ func webErrorWithCode(w http.ResponseWriter, message string, err error, code int
624
func internalWebError(w http.ResponseWriter, err error) {
625
webErrorWithCode(w, "internalWebError", err, http.StatusInternalServerError)
626
}
627
+
628
+func getFilename(s string) string {
629
+ if (strings.HasPrefix(s, ipfsPathPrefix) || strings.HasPrefix(s, ipnsPathPrefix)) && strings.Count(gopath.Clean(s), "/") <= 2 {
630
+ // Don't want to treat ipfs.io in /ipns/ipfs.io as a filename.
631
+ return ""
632
+ }
633
+ return gopath.Base(s)
634
+}
core/corehttp/gateway_test.go
+6
@@ -153,6 +153,7 @@ func TestGatewayGet(t *testing.T) {
153
ns["/ipns/double.example.com"] = path.FromString("/ipns/working.example.com")
154
ns["/ipns/triple.example.com"] = path.FromString("/ipns/double.example.com")
155
ns["/ipns/broken.example.com"] = path.FromString("/ipns/" + k)
156
+ ns["/ipns/example.man"] = path.FromString("/ipfs/" + k)
157
158
t.Log(ts.URL)
159
for _, test := range []struct {
@@ -175,6 +176,7 @@ func TestGatewayGet(t *testing.T) {
176
{"working.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/working.example.com/ipfs/" + k + ": no link by that name\n"},
177
{"broken.example.com", "/", http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/: " + namesys.ErrResolveFailed.Error() + "\n"},
178
{"broken.example.com", "/ipfs/" + k, http.StatusNotFound, "ipfs resolve -r /ipns/broken.example.com/ipfs/" + k + ": " + namesys.ErrResolveFailed.Error() + "\n"},
179
+ {"example.man", "/", http.StatusOK, "fnord"},
180
} {
181
var c http.Client
182
r, err := http.NewRequest("GET", ts.URL+test.path, nil)
@@ -190,6 +192,10 @@ func TestGatewayGet(t *testing.T) {
192
continue
193
}
194
defer resp.Body.Close()
195
+ contentType := resp.Header.Get("Content-Type")
196
+ if contentType != "text/plain; charset=utf-8" {
197
+ t.Errorf("expected content type to be text/plain, got %s", contentType)
198
+ }
199
if resp.StatusCode != test.status {
200
t.Errorf("got %d, expected %d from %s", resp.StatusCode, test.status, urlstr)
201
continue