fix(preview): keep the CSP sandbox scoped to scriptable documents (#1123)

This commit is contained in:
Menghuan1918 committed 2026-11-08 11:47:24 +08:00
1 parent 641bf969ca
commit f3ff9d39df
2 files changed
+26 -11

No files matched your search

+9 -10
View File
@@ -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 <img>, 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<string, string> = {
'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 <script> in the GUI's origin, with
// same-origin access to /sidebar/api/*. The sibling html route already
// sandboxes for exactly this reason; mirror it here. The CSP applies
// to the document, so <img src> embedding is unaffected.
if (type === 'image/svg+xml') {
headers['content-security-policy'] = "sandbox; object-src 'none'"
headers['x-content-type-options'] = 'nosniff'
headers['referrer-policy'] = 'no-referrer'
if (documentType) {
headers['content-security-policy'] = type === 'image/svg+xml'
? "sandbox; object-src 'none'"
: "sandbox allow-scripts allow-popups allow-downloads allow-modals; object-src 'none'"
}
if (url.searchParams.get('download') === '1') {
headers['content-disposition'] = `attachment; filename*=UTF-8''${encodeURIComponent(basename(path))}`
+17 -1
View File
@@ -167,7 +167,7 @@ describe('/sidebar/file streams without a size cap', () => {
expect(response.headers.get('referrer-policy')).toBe('no-referrer')
})
it.each(['html', 'htm', 'HTML', 'bin'])('keeps arbitrary file previews in an opaque origin (%s)', async extension => {
it.each(['html', 'htm', 'HTML'])('keeps scriptable file previews in an opaque origin (%s)', async extension => {
const ws = workspace('opaque-preview')
const target = join(ws, `preview.${extension}`)
writeFileSync(target, '<script>window.fixture = true</script>')
@@ -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, '<script>window.fixture = true</script>')
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)