@cryptotaxi247 / kubo / commits / a6687744c

fix(gw): no backlink when listing root dir

Closes #9071

Marcin Rataj committed Jul 18, 2022 at 23:41 UTC a6687744c703c5c020f4c004ca73f024c3bae4f7
5 files changed +27 -13
assets/dir-index-html/dir-index.html
+3 -1
@@ -3,7 +3,7 @@
3 <html lang="en">
4 <head>
5 <meta charset="utf-8" />
6 -<meta name="description" content="A directory of files hosted on the distributed, decentralized web using IPFS">
6 +<meta name="description" content="A directory of content-addressed files hosted on IPFS">
7 <meta property="og:title" content="Files on IPFS">
8 <meta property="og:description" content="{{ .Path }}">
9 <meta property="og:type" content="website">
@@ -62,6 +62,7 @@
62 </div>
63 <div class="table-responsive">
64 <table>
65 + {{ if .BackLink }}
66 <tr>
67 <td class="type-icon">
68 <div class="ipfs-_blank">&nbsp;</div>
@@ -72,6 +73,7 @@
73 <td></td>
74 <td></td>
75 </tr>
76 + {{ end }}
77 {{ range .Listing }}
78 <tr>
79 <td class="type-icon">
assets/dir-index-html/src/dir-index.html
+3 -1
@@ -3,7 +3,7 @@
3 <html lang="en">
4 <head>
5 <meta charset="utf-8" />
6 -<meta name="description" content="A directory of files hosted on the distributed, decentralized web using IPFS">
6 +<meta name="description" content="A directory of content-addressed files hosted on IPFS">
7 <meta property="og:title" content="Files on IPFS">
8 <meta property="og:description" content="{{ .Path }}">
9 <meta property="og:type" content="website">
@@ -61,6 +61,7 @@
61 </div>
62 <div class="table-responsive">
63 <table>
64 + {{ if .BackLink }}
65 <tr>
66 <td class="type-icon">
67 <div class="ipfs-_blank">&nbsp;</div>
@@ -71,6 +72,7 @@
72 <td></td>
73 <td></td>
74 </tr>
75 + {{ end }}
76 {{ range .Listing }}
77 <tr>
78 <td class="type-icon">
core/corehttp/gateway_handler_unixfs_dir.go
+4 -2
@@ -152,11 +152,13 @@ func (i *gatewayHandler) serveDirectory(ctx context.Context, w http.ResponseWrit
152 // don't go further up than /ipfs/$hash/
153 pathSplit := path.SplitList(contentPath.String())
154 switch {
155 - // keep backlink
155 + // skip backlink when listing a content root
156 case len(pathSplit) == 3: // url: /ipfs/$hash
157 + backLink = ""
158
158 - // keep backlink
159 + // skip backlink when listing a content root
160 case len(pathSplit) == 4 && pathSplit[3] == "": // url: /ipfs/$hash/
161 + backLink = ""
162
163 // add the correct link depending on whether the path ends with a slash
164 default:
core/corehttp/gateway_test.go
+2 -2
@@ -529,8 +529,8 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
529 if !matchPathOrBreadcrumbs(s, "/") {
530 t.Fatalf("expected a path in directory listing")
531 }
532 - if !strings.Contains(s, "<a href=\"/\">") {
533 - t.Fatalf("expected backlink in directory listing")
532 + if strings.Contains(s, "<a href=\"/\">") {
533 + t.Fatalf("expected no backlink in directory listing of the root CID")
534 }
535 if !strings.Contains(s, "<a href=\"/file.txt\">") {
536 t.Fatalf("expected file in directory listing")
test/sharness/t0115-gateway-dir-listing.sh
+15 -7
@@ -37,10 +37,10 @@ test_expect_success "Add the test directory" '
37 ## Test dir listing on path gateway (eg. 127.0.0.1:8080/ipfs/)
38 ## ============================================================================
39
40 -test_expect_success "path gw: backlink on root CID should point at self" '
40 +test_expect_success "path gw: backlink on root CID should be hidden" '
41 curl -sD - http://127.0.0.1:$GWAY_PORT/ipfs/${DIR_CID}/ > list_response &&
42 test_should_contain "Index of" list_response &&
43 - test_should_contain "<a href=\"/ipfs/$DIR_CID/\">..</a>" list_response
43 + test_should_not_contain "<a href=\"/ipfs/$DIR_CID/\">..</a>" list_response
44 '
45
46 test_expect_success "path gw: Etag should be present" '
@@ -53,7 +53,7 @@ test_expect_success "path gw: breadcrumbs should point at /ipfs namespace mounte
53 test_should_contain "/ipfs/<a href=\"/ipfs/$DIR_CID\">$DIR_CID</a>/<a href=\"/ipfs/$DIR_CID/%C4%85\">ą</a>/<a href=\"/ipfs/$DIR_CID/%C4%85/%C4%99\">ę</a>" list_response
54 '
55
56 -test_expect_success "path gw: backlink should point at parent directory" '
56 +test_expect_success "path gw: backlink on subdirectory should point at parent directory" '
57 test_should_contain "<a href=\"/ipfs/$DIR_CID/%C4%85/%C4%99/..\">..</a>" list_response
58 '
59
@@ -72,10 +72,10 @@ test_expect_success "path gw: hash column should be a CID link with filename par
72 DIR_HOSTNAME="${DIR_CID}.ipfs.localhost"
73 # note: we skip DNS lookup by running curl with --resolve $DIR_HOSTNAME:127.0.0.1
74
75 -test_expect_success "path gw: backlink on root CID should point origin root" '
75 +test_expect_success "path gw: backlink on root CID should be hidden" '
76 curl -sD - --resolve $DIR_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DIR_HOSTNAME:$GWAY_PORT/ > list_response &&
77 test_should_contain "Index of" list_response &&
78 - test_should_contain "<a href=\"/\">..</a>" list_response
78 + test_should_not_contain "<a href=\"/\">..</a>" list_response
79 '
80
81 test_expect_success "path gw: Etag should be present" '
@@ -84,6 +84,10 @@ test_expect_success "path gw: Etag should be present" '
84 test_should_contain "Etag: \"DirIndex-" list_response
85 '
86
87 +test_expect_success "path gw: backlink on subdirectory should point at parent directory" '
88 + test_should_contain "<a href=\"/%C4%85/%C4%99/..\">..</a>" list_response
89 +'
90 +
91 test_expect_success "subdomain gw: breadcrumbs should leverage path-based router mounted on the parent domain" '
92 test_should_contain "/ipfs/<a href=\"//localhost:$GWAY_PORT/ipfs/$DIR_CID\">$DIR_CID</a>/<a href=\"//localhost:$GWAY_PORT/ipfs/$DIR_CID/%C4%85\">ą</a>/<a href=\"//localhost:$GWAY_PORT/ipfs/$DIR_CID/%C4%85/%C4%99\">ę</a>" list_response
93 '
@@ -111,10 +115,10 @@ test_launch_ipfs_daemon
115 # (go tests and sharness tests should be kept in sync)
116 # - we skip DNS lookup by running curl with --resolve $DNSLINK_HOSTNAME:127.0.0.1
117
114 -test_expect_success "dnslink gw: backlink on root CID should point origin root" '
118 +test_expect_success "dnslink gw: backlink on root CID should be hidden" '
119 curl -v -sD - --resolve $DNSLINK_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DNSLINK_HOSTNAME:$GWAY_PORT/ > list_response &&
120 test_should_contain "Index of" list_response &&
117 - test_should_contain "<a href=\"/\">..</a>" list_response
121 + test_should_not_contain "<a href=\"/\">..</a>" list_response
122 '
123
124 test_expect_success "dnslink gw: Etag should be present" '
@@ -123,6 +127,10 @@ test_expect_success "dnslink gw: Etag should be present" '
127 test_should_contain "Etag: \"DirIndex-" list_response
128 '
129
130 +test_expect_success "dnslink gw: backlink on subdirectory should point at parent directory" '
131 + test_should_contain "<a href=\"/%C4%85/%C4%99/..\">..</a>" list_response
132 +'
133 +
134 test_expect_success "dnslink gw: breadcrumbs should point at content root mounted at dnslink origin" '
135 test_should_contain "/ipns/<a href=\"//$DNSLINK_HOSTNAME:$GWAY_PORT/\">website.example.com</a>/<a href=\"//$DNSLINK_HOSTNAME:$GWAY_PORT/%C4%85\">ą</a>/<a href=\"//$DNSLINK_HOSTNAME:$GWAY_PORT/%C4%85/%C4%99\">ę</a>" list_response
136 '