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

Commit 109549f

Browse files
committed
fix: limit SW registration to content root
Introduces hardening proposed in: ipfs/kubo#4025 (comment) License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent 0565f47 commit 109549f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/http/gateway/resources/gateway.js

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ module.exports = {
9292
// add trailing slash for directories with implicit index.html
9393
return h.redirect(`${path}/`).permanent(true)
9494
}
95+
if (request.headers['service-worker'] === 'script') {
96+
// Disallow Service Worker registration on /ipfs scope
97+
// https://github.com/ipfs/go-ipfs/issues/4025
98+
if (path.match(/^\/ip[nf]s\/[^/]+$/)) throw Boom.badRequest('navigator.serviceWorker: registration is not allowed for this scope')
99+
}
95100

96101
// Support If-None-Match & Etag (Conditional Requests from RFC7232)
97102
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag

test/gateway/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('HTTP Gateway', function () {
104104
expect(res.headers.suborigin).to.equal(undefined)
105105
})
106106

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

120+
it('returns 400 for service worker registration outside of an IPFS content root', async () => {
121+
const res = await gateway.inject({
122+
method: 'GET',
123+
url: '/ipfs/QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o?filename=sw.js',
124+
headers: { 'Service-Worker': 'script' }
125+
})
126+
127+
// Expect 400 Bad Request
128+
// https://github.com/ipfs/go-ipfs/issues/4025#issuecomment-342250616
129+
expect(res.statusCode).to.equal(400)
130+
})
131+
120132
it('valid CIDv0', async () => {
121133
const res = await gateway.inject({
122134
method: 'GET',

0 commit comments

Comments
 (0)