diff --git a/src/index.ts b/src/index.ts
index 7d037fb0..b340ae55 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1846,22 +1846,21 @@ export function apply(ctx: Context, config?: SidebarConfig): void {
const type = mediaTypeForPath(path)
// Raw bytes either way (binary-safe); ?download=1 switches the
// disposition so the browser saves the file instead of showing it.
+ // Only scriptable DOCUMENT types get an opaque-origin CSP. An inert
+ // image/binary keeps its existing response surface: the CSP sandbox
+ // does nothing for an
, and a directly opened image must not gain
+ // a needless behaviour change. nosniff + no-referrer are safe everywhere.
+ const documentType = type === 'text/html' || type === 'application/xhtml+xml' || type === 'image/svg+xml'
const headers: Record = {
'content-type': type,
'cache-control': 'no-cache',
- 'content-security-policy': "sandbox allow-scripts allow-popups allow-downloads allow-modals; object-src 'none'",
'x-content-type-options': 'nosniff',
'referrer-policy': 'no-referrer',
}
- // An SVG is a scriptable DOCUMENT, not an inert image: navigating to
- // this URL directly would run its ')
@@ -182,6 +182,22 @@ describe('/sidebar/file streams without a size cap', () => {
expect(await response.text()).toContain('window.fixture')
})
+ it.each(['png', 'bin'])('keeps an inert preview free of the CSP sandbox (%s)', async extension => {
+ // The guard is scoped to scriptable documents: a PNG/unknown binary must
+ // not gain a sandbox directive (nothing to sandbox, and it would be a
+ // behaviour change for existing image previews). nosniff is the real
+ // protection there, and it stays on every response.
+ const ws = workspace('inert-preview')
+ const target = join(ws, `preview.${extension}`)
+ writeFileSync(target, '')
+ const { origin } = await serve(ws)
+ const response = await fetch(fileUrl(origin, ws, target))
+ expect(response.status).toBe(200)
+ expect(response.headers.get('content-security-policy')).toBeNull()
+ expect(response.headers.get('x-content-type-options')).toBe('nosniff')
+ expect(response.headers.get('referrer-policy')).toBe('no-referrer')
+ })
+
it('answers the JSON error envelope when the file cannot be opened', async () => {
const ws = workspace('missing')
const { origin } = await serve(ws)