From a6687744c703c5c020f4c004ca73f024c3bae4f7 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Mon, 18 Jul 2022 23:41:55 +0200 Subject: [PATCH] fix(gw): no backlink when listing root dir Closes #9071 --- assets/dir-index-html/dir-index.html | 4 +++- assets/dir-index-html/src/dir-index.html | 4 +++- core/corehttp/gateway_handler_unixfs_dir.go | 6 ++++-- core/corehttp/gateway_test.go | 4 ++-- test/sharness/t0115-gateway-dir-listing.sh | 22 ++++++++++++++------- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/assets/dir-index-html/dir-index.html b/assets/dir-index-html/dir-index.html index 49ac2bb1f..a2d662d19 100644 --- a/assets/dir-index-html/dir-index.html +++ b/assets/dir-index-html/dir-index.html @@ -3,7 +3,7 @@ - + @@ -62,6 +62,7 @@
+ {{ if .BackLink }} + {{ end }} {{ range .Listing }}
 
@@ -72,6 +73,7 @@
diff --git a/assets/dir-index-html/src/dir-index.html b/assets/dir-index-html/src/dir-index.html index 376c4cd77..1bbbb0a9a 100644 --- a/assets/dir-index-html/src/dir-index.html +++ b/assets/dir-index-html/src/dir-index.html @@ -3,7 +3,7 @@ - + @@ -61,6 +61,7 @@
+ {{ if .BackLink }} + {{ end }} {{ range .Listing }}
 
@@ -71,6 +72,7 @@
diff --git a/core/corehttp/gateway_handler_unixfs_dir.go b/core/corehttp/gateway_handler_unixfs_dir.go index 30e1b5ec7..4b8b7bc1f 100644 --- a/core/corehttp/gateway_handler_unixfs_dir.go +++ b/core/corehttp/gateway_handler_unixfs_dir.go @@ -152,11 +152,13 @@ func (i *gatewayHandler) serveDirectory(ctx context.Context, w http.ResponseWrit // don't go further up than /ipfs/$hash/ pathSplit := path.SplitList(contentPath.String()) switch { - // keep backlink + // skip backlink when listing a content root case len(pathSplit) == 3: // url: /ipfs/$hash + backLink = "" - // keep backlink + // skip backlink when listing a content root case len(pathSplit) == 4 && pathSplit[3] == "": // url: /ipfs/$hash/ + backLink = "" // add the correct link depending on whether the path ends with a slash default: diff --git a/core/corehttp/gateway_test.go b/core/corehttp/gateway_test.go index 5f7cf0cb5..5ac27341d 100644 --- a/core/corehttp/gateway_test.go +++ b/core/corehttp/gateway_test.go @@ -529,8 +529,8 @@ func TestIPNSHostnameBacklinks(t *testing.T) { if !matchPathOrBreadcrumbs(s, "/") { t.Fatalf("expected a path in directory listing") } - if !strings.Contains(s, "") { - t.Fatalf("expected backlink in directory listing") + if strings.Contains(s, "") { + t.Fatalf("expected no backlink in directory listing of the root CID") } if !strings.Contains(s, "") { t.Fatalf("expected file in directory listing") diff --git a/test/sharness/t0115-gateway-dir-listing.sh b/test/sharness/t0115-gateway-dir-listing.sh index 91ab8afe1..bd634388d 100755 --- a/test/sharness/t0115-gateway-dir-listing.sh +++ b/test/sharness/t0115-gateway-dir-listing.sh @@ -37,10 +37,10 @@ test_expect_success "Add the test directory" ' ## Test dir listing on path gateway (eg. 127.0.0.1:8080/ipfs/) ## ============================================================================ -test_expect_success "path gw: backlink on root CID should point at self" ' +test_expect_success "path gw: backlink on root CID should be hidden" ' curl -sD - http://127.0.0.1:$GWAY_PORT/ipfs/${DIR_CID}/ > list_response && test_should_contain "Index of" list_response && - test_should_contain ".." list_response + test_should_not_contain ".." list_response ' 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 test_should_contain "/ipfs/$DIR_CID/ą/ę" list_response ' -test_expect_success "path gw: backlink should point at parent directory" ' +test_expect_success "path gw: backlink on subdirectory should point at parent directory" ' test_should_contain ".." list_response ' @@ -72,10 +72,10 @@ test_expect_success "path gw: hash column should be a CID link with filename par DIR_HOSTNAME="${DIR_CID}.ipfs.localhost" # note: we skip DNS lookup by running curl with --resolve $DIR_HOSTNAME:127.0.0.1 -test_expect_success "path gw: backlink on root CID should point origin root" ' +test_expect_success "path gw: backlink on root CID should be hidden" ' curl -sD - --resolve $DIR_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DIR_HOSTNAME:$GWAY_PORT/ > list_response && test_should_contain "Index of" list_response && - test_should_contain ".." list_response + test_should_not_contain ".." list_response ' test_expect_success "path gw: Etag should be present" ' @@ -84,6 +84,10 @@ test_expect_success "path gw: Etag should be present" ' test_should_contain "Etag: \"DirIndex-" list_response ' +test_expect_success "path gw: backlink on subdirectory should point at parent directory" ' + test_should_contain ".." list_response +' + test_expect_success "subdomain gw: breadcrumbs should leverage path-based router mounted on the parent domain" ' test_should_contain "/ipfs/$DIR_CID/ą/ę" list_response ' @@ -111,10 +115,10 @@ test_launch_ipfs_daemon # (go tests and sharness tests should be kept in sync) # - we skip DNS lookup by running curl with --resolve $DNSLINK_HOSTNAME:127.0.0.1 -test_expect_success "dnslink gw: backlink on root CID should point origin root" ' +test_expect_success "dnslink gw: backlink on root CID should be hidden" ' curl -v -sD - --resolve $DNSLINK_HOSTNAME:$GWAY_PORT:127.0.0.1 http://$DNSLINK_HOSTNAME:$GWAY_PORT/ > list_response && test_should_contain "Index of" list_response && - test_should_contain ".." list_response + test_should_not_contain ".." list_response ' test_expect_success "dnslink gw: Etag should be present" ' @@ -123,6 +127,10 @@ test_expect_success "dnslink gw: Etag should be present" ' test_should_contain "Etag: \"DirIndex-" list_response ' +test_expect_success "dnslink gw: backlink on subdirectory should point at parent directory" ' + test_should_contain ".." list_response +' + test_expect_success "dnslink gw: breadcrumbs should point at content root mounted at dnslink origin" ' test_should_contain "/ipns/website.example.com/ą/ę" list_response '