Skip to content

Commit b638509

Browse files
committed
Add CSP to Image Optimization API
1 parent d2551bb commit b638509

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

packages/next/server/image-optimizer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ function setResponseHeaders(
525525
res.setHeader('Content-Disposition', `inline; filename="${fileName}"`)
526526
}
527527

528+
res.setHeader('Content-Security-Policy', `script-src 'none'; sandbox;`)
529+
528530
return { finished: false }
529531
}
530532

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react'
2+
import Image from 'next/image'
3+
4+
const Page = () => {
5+
return (
6+
<div>
7+
<h1>SVG with a script tag attempting XSS</h1>
8+
<Image id="img" src="/xss.svg" width="100" height="100" />
9+
<p id="msg">safe</p>
10+
</div>
11+
)
12+
}
13+
14+
export default Page
Lines changed: 9 additions & 0 deletions
Loading

test/integration/image-component/default/test/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,28 @@ function runTests(mode) {
229229
}
230230
})
231231

232+
it('should not execute scripts inside svg image', async () => {
233+
let browser
234+
try {
235+
browser = await webdriver(appPort, '/xss-svg')
236+
await browser.eval(`document.getElementById("img").scrollIntoView()`)
237+
expect(await browser.elementById('img').getAttribute('src')).toContain(
238+
'xss.svg'
239+
)
240+
expect(await browser.elementById('msg').text()).toBe('safe')
241+
242+
browser = await webdriver(
243+
appPort,
244+
'/_next/image?url=%2Fxss.svg&w=256&q=75'
245+
)
246+
expect(await browser.elementById('msg').text()).toBe('safe')
247+
} finally {
248+
if (browser) {
249+
await browser.close()
250+
}
251+
}
252+
})
253+
232254
it('should work when using flexbox', async () => {
233255
let browser
234256
try {

0 commit comments

Comments
 (0)