Skip to content

fix: JS caching via Access-Control-Expose-Headers #8984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ func GatewayOption(writable bool, paths ...string) ServeOption {

headers[ACEHeadersName] = cleanHeaderSet(
append([]string{
"Content-Length",
Copy link
Member Author

@lidel lidel May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also included Content-Length because it was not implicitly safelisted initially by the browsers (whatwg/fetch#622, whatwg/fetch#626) and still, the current browser behavior is not consistent.

Click to expand - Firefox 100 and Chromium 1001

Works on localhost

Start go-ipfs 0.12.2 and 0.13-rc1, then go to https://example.com/ and try to access the header from js, by typing in browser console:

$ fetch('http://127.0.0.1:8080/ipfs/bafybeib4zuumguq4cgkt7caddeukb4ysijnehvntekrtu5afmet5ujvlka/package.json').then(response => console.table(Object.fromEntries(response.headers))) 
cache-control public, max-age=29030400, immutable
content-length 4674
content-type application/json
last-modified Thu, 01 Jan 1970 00:00:01 GMT

👉 when local gateway is used, fetch is able to access content-length

Fails on ipfs.io

At the same time, JS running on https://example.com is unable to read content-length if ipfs.io is used:

$ fetch('https://ipfs.io/ipfs/bafybeib4zuumguq4cgkt7caddeukb4ysijnehvntekrtu5afmet5ujvlka/package.json').then(response => console.table(Object.fromEntries(response.headers))) 
cache-control public, max-age=29030400, immutable
content-type application/json
last-modified Thu, 01 Jan 1970 00:00:01 GMT

Manually including it on our own safelist solves the problem.

"Content-Range",
"X-Chunked-Output",
"X-Stream-Output",
"X-Ipfs-Path",
"X-Ipfs-Roots",
}, headers[ACEHeadersName]...))

var gateway http.Handler = newGatewayHandler(GatewayConfig{
Expand Down
13 changes: 11 additions & 2 deletions test/sharness/t0112-gateway-cors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ test_expect_success "GET response for Gateway resource looks good" '
grep "< Access-Control-Allow-Origin: \*" curl_output &&
grep "< Access-Control-Allow-Methods: GET" curl_output &&
grep "< Access-Control-Allow-Headers: Range" curl_output &&
grep "< Access-Control-Expose-Headers: Content-Range" curl_output
grep "< Access-Control-Expose-Headers: Content-Range" curl_output &&
grep "< Access-Control-Expose-Headers: Content-Length" curl_output &&
grep "< Access-Control-Expose-Headers: X-Ipfs-Path" curl_output &&
grep "< Access-Control-Expose-Headers: X-Ipfs-Roots" curl_output
'

# HTTP OPTIONS Request
Expand All @@ -40,7 +43,10 @@ test_expect_success "OPTIONS response for Gateway resource looks good" '
grep "< Access-Control-Allow-Origin: \*" curl_output &&
grep "< Access-Control-Allow-Methods: GET" curl_output &&
grep "< Access-Control-Allow-Headers: Range" curl_output &&
grep "< Access-Control-Expose-Headers: Content-Range" curl_output
grep "< Access-Control-Expose-Headers: Content-Range" curl_output &&
grep "< Access-Control-Expose-Headers: Content-Length" curl_output &&
grep "< Access-Control-Expose-Headers: X-Ipfs-Path" curl_output &&
grep "< Access-Control-Expose-Headers: X-Ipfs-Roots" curl_output
'

test_kill_ipfs_daemon
Expand All @@ -63,6 +69,9 @@ test_expect_success "Access-Control-Allow-Headers extends" '
grep "< Access-Control-Allow-Headers: Range" curl_output &&
grep "< Access-Control-Allow-Headers: X-Custom1" curl_output &&
grep "< Access-Control-Expose-Headers: Content-Range" curl_output &&
grep "< Access-Control-Expose-Headers: Content-Length" curl_output &&
grep "< Access-Control-Expose-Headers: X-Ipfs-Path" curl_output &&
grep "< Access-Control-Expose-Headers: X-Ipfs-Roots" curl_output &&
grep "< Access-Control-Expose-Headers: X-Custom2" curl_output
'

Expand Down