Skip to content

Commit

Permalink
fix: add referer on redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 27, 2022
1 parent 8e9f414 commit 100d80e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
11 changes: 10 additions & 1 deletion packages/edge-gateway/src/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,14 @@ export async function ipfsGet(request, env) {
`https://${nCid}.${env.IPFS_GATEWAY_HOSTNAME}${redirectPath}${redirectQueryString}`
)

return Response.redirect(url, 302)
const headers = new Headers(request.headers)
headers.set('Referrer-Policy', 'unsafe-url')
headers.set('Location', url.toString())
headers.set('Referer', request.headers.get('Referer'))

return new Response(undefined, {
status: 302,
statusText: 'Found',
headers,
})
}
30 changes: 24 additions & 6 deletions packages/edge-gateway/test/ipfs-path.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,83 +29,101 @@ test('should resolve a cid v0 with IPFS canonical resolution', async (t) => {
const { mf } = t.context

const response = await mf.dispatchFetch(
'https://localhost:8787/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR'
'https://localhost:8787/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR',
{ headers: { Referer: 'localhost:8787' } }
)
await response.waitUntil()
t.is(response.status, 302)
t.is(
response.headers.get('location'),
'https://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi.ipfs.localhost:8787/'
)
t.is(response.headers.get('referrer-policy'), 'unsafe-url')
t.is(response.headers.get('referer'), 'localhost:8787')
})

test('should resolve a cid v1 with IPFS canonical resolution', async (t) => {
const { mf } = t.context

const response = await mf.dispatchFetch(
'https://localhost:8787/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq'
'https://localhost:8787/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq',
{ headers: { Referer: 'localhost:8787' } }
)
await response.waitUntil()
t.is(response.status, 302)
t.is(
response.headers.get('location'),
'https://bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq.ipfs.localhost:8787/'
)
t.is(response.headers.get('referrer-policy'), 'unsafe-url')
t.is(response.headers.get('referer'), 'localhost:8787')
})

test('should resolve a cid and path with IPFS canonical resolution', async (t) => {
const { mf } = t.context

const response = await mf.dispatchFetch(
'https://localhost:8787/ipfs/bafybeifvsmjgbhck72pabliifeo35cew5yhxujfqjxg4g32vr3yv24h6zu/path/file.txt'
'https://localhost:8787/ipfs/bafybeifvsmjgbhck72pabliifeo35cew5yhxujfqjxg4g32vr3yv24h6zu/path/file.txt',
{ headers: { Referer: 'localhost:8787' } }
)
await response.waitUntil()
t.is(response.status, 302)
t.is(
response.headers.get('location'),
'https://bafybeifvsmjgbhck72pabliifeo35cew5yhxujfqjxg4g32vr3yv24h6zu.ipfs.localhost:8787/path/file.txt'
)
t.is(response.headers.get('referrer-policy'), 'unsafe-url')
t.is(response.headers.get('referer'), 'localhost:8787')
})

test('should resolve a cid and path with IPFS canonical resolution when subdomain also used', async (t) => {
const { mf } = t.context

const response = await mf.dispatchFetch(
'https://bafybeifvsmjgbhck72pabliifeo35cew5yhxujfqjxg4g32vr3yv24h6zu.ipfs.localhost:8787/ipfs/bafybeifvsmjgbhck72pabliifeo35cew5yhxujfqjxg4g32vr3yv24h6zu/path/file.txt'
'https://bafybeifvsmjgbhck72pabliifeo35cew5yhxujfqjxg4g32vr3yv24h6zu.ipfs.localhost:8787/ipfs/bafybeifvsmjgbhck72pabliifeo35cew5yhxujfqjxg4g32vr3yv24h6zu/path/file.txt',
{ headers: { Referer: 'localhost:8787' } }
)
await response.waitUntil()
t.is(response.status, 302)
t.is(
response.headers.get('location'),
'https://bafybeifvsmjgbhck72pabliifeo35cew5yhxujfqjxg4g32vr3yv24h6zu.ipfs.localhost:8787/path/file.txt'
)
t.is(response.headers.get('referrer-policy'), 'unsafe-url')
t.is(response.headers.get('referer'), 'localhost:8787')
})

test('should resolve a cid IPFS canonical resolution keeping query parameters', async (t) => {
const { mf } = t.context
const queryString = '?key=value'

const response = await mf.dispatchFetch(
`https://localhost:8787/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq${queryString}`
`https://localhost:8787/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq${queryString}`,
{ headers: { Referer: 'localhost:8787' } }
)
await response.waitUntil()
t.is(response.status, 302)
t.is(
response.headers.get('location'),
`https://bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq.ipfs.localhost:8787/${queryString}`
)
t.is(response.headers.get('referrer-policy'), 'unsafe-url')
t.is(response.headers.get('referer'), 'localhost:8787')
})

test('should resolve a cid IPFS canonical resolution with same path as IPFS path', async (t) => {
const { mf } = t.context

const response = await mf.dispatchFetch(
'https://localhost:8787/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq'
'https://localhost:8787/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq',
{ headers: { Referer: 'localhost:8787' } }
)
await response.waitUntil()
t.is(response.status, 302)
t.is(
response.headers.get('location'),
'https://bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq.ipfs.localhost:8787/ipfs/bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq'
)
t.is(response.headers.get('referrer-policy'), 'unsafe-url')
t.is(response.headers.get('referer'), 'localhost:8787')
})

0 comments on commit 100d80e

Please sign in to comment.