Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix: limit SW registration to content root #2682

Merged
merged 2 commits into from
Feb 3, 2020
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
script:
- npx aegir build --bundlesize
- npx aegir dep-check -- -i wrtc -i electron-webrtc
- npm run lint
- npx aegir lint

- stage: test
name: chrome
Expand Down
5 changes: 5 additions & 0 deletions src/http/gateway/resources/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ module.exports = {
// add trailing slash for directories with implicit index.html
return h.redirect(`${path}/`).permanent(true)
}
if (request.headers['service-worker'] === 'script') {
// Disallow Service Worker registration on /ipfs scope
// https://github.com/ipfs/go-ipfs/issues/4025
if (path.match(/^\/ip[nf]s\/[^/]+$/)) throw Boom.badRequest('navigator.serviceWorker: registration is not allowed for this scope')
}

// Support If-None-Match & Etag (Conditional Requests from RFC7232)
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
Expand Down
14 changes: 13 additions & 1 deletion test/gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('HTTP Gateway', function () {
expect(res.headers.suborigin).to.equal(undefined)
})

it('400 for request with invalid argument', async () => {
it('returns 400 for request with invalid argument', async () => {
const res = await gateway.inject({
method: 'GET',
url: '/ipfs/invalid'
Expand All @@ -118,6 +118,18 @@ describe('HTTP Gateway', function () {
expect(res.headers.suborigin).to.equal(undefined)
})

it('returns 400 for service worker registration outside of an IPFS content root', async () => {
const res = await gateway.inject({
method: 'GET',
url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o?filename=sw.js',
headers: { 'Service-Worker': 'script' }
})

// Expect 400 Bad Request
// https://github.com/ipfs/go-ipfs/issues/4025#issuecomment-342250616
expect(res.statusCode).to.equal(400)
})

it('valid CIDv0', async () => {
const res = await gateway.inject({
method: 'GET',
Expand Down