Skip to content

Commit

Permalink
Add CSP to Image Optimization API
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Aug 13, 2021
1 parent d2551bb commit b638509
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/next/server/image-optimizer.ts
Expand Up @@ -525,6 +525,8 @@ function setResponseHeaders(
res.setHeader('Content-Disposition', `inline; filename="${fileName}"`)
}

res.setHeader('Content-Security-Policy', `script-src 'none'; sandbox;`)

return { finished: false }
}

Expand Down
14 changes: 14 additions & 0 deletions test/integration/image-component/default/pages/xss-svg.js
@@ -0,0 +1,14 @@
import React from 'react'
import Image from 'next/image'

const Page = () => {
return (
<div>
<h1>SVG with a script tag attempting XSS</h1>
<Image id="img" src="/xss.svg" width="100" height="100" />
<p id="msg">safe</p>
</div>
)
}

export default Page
9 changes: 9 additions & 0 deletions test/integration/image-component/default/public/xss.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Expand Up @@ -229,6 +229,28 @@ function runTests(mode) {
}
})

it('should not execute scripts inside svg image', async () => {
let browser
try {
browser = await webdriver(appPort, '/xss-svg')
await browser.eval(`document.getElementById("img").scrollIntoView()`)
expect(await browser.elementById('img').getAttribute('src')).toContain(
'xss.svg'
)
expect(await browser.elementById('msg').text()).toBe('safe')

browser = await webdriver(
appPort,
'/_next/image?url=%2Fxss.svg&w=256&q=75'
)
expect(await browser.elementById('msg').text()).toBe('safe')
} finally {
if (browser) {
await browser.close()
}
}
})

it('should work when using flexbox', async () => {
let browser
try {
Expand Down

0 comments on commit b638509

Please sign in to comment.