Skip to content

Commit

Permalink
add content security policy header to SVG responses (#10642)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s authored Oct 27, 2024
1 parent cc90c19 commit 6243039
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/base-service/legacy-result-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function streamFromString(str) {

function sendSVG(res, askres, end) {
askres.setHeader('Content-Type', 'image/svg+xml;charset=utf-8')
askres.setHeader('Content-Security-Policy', "script-src 'none';")
askres.setHeader('Content-Length', Buffer.byteLength(res, 'utf8'))
end(null, { template: streamFromString(res) })
}
Expand Down
21 changes: 21 additions & 0 deletions core/server/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ describe('The server', function () {
expect(() => JSON.parse(body)).not.to.throw()
})

describe('Content Security Policy', function () {
it('should disable javascript when serving SVG content (no extension)', async function () {
const { headers } = await got(`${baseUrl}:fruit-apple-green`)
expect(headers['content-security-policy']).to.equal(
"script-src 'none';",
)
})

it('should disable javascript when serving SVG content (with extension)', async function () {
const { headers } = await got(`${baseUrl}:fruit-apple-green.svg`)
expect(headers['content-security-policy']).to.equal(
"script-src 'none';",
)
})

it('should not send content security headers when serving JSON content', async function () {
const { headers } = await got(`${baseUrl}:fruit-apple-green.json`)
expect(headers).not.to.have.property('content-security-policy')
})
})

it('should preserve label case', async function () {
const { statusCode, body } = await got(`${baseUrl}:fRuiT-apple-green.svg`)
expect(statusCode).to.equal(200)
Expand Down

0 comments on commit 6243039

Please sign in to comment.