fix(security): sandbox SVG served by the media route
`/sidebar/file` served `.svg` as `image/svg+xml` with no `Content-Disposition`,
no `X-Content-Type-Options` and no CSP. An SVG is a scriptable DOCUMENT, so
navigating to that URL directly (a link, `window.open`, a popup opened by a
previewed page) executes its `<script>` in the GUI's origin — which has
same-origin access to `/sidebar/api/*`, the exact surface the request-trust
fence is there to keep a browsed page away from.
The sibling `/sidebar/html` route already defends this (its own comment calls
the sandbox directive "the previewer's security boundary even for top-level
loads"); the media route simply never got the same treatment, because most of
what it serves is inert.
Fix: mirror the html route for the scriptable type only. When the resolved
media type is `image/svg+xml`, add
content-security-policy: sandbox; object-src 'none'
x-content-type-options: nosniff
referrer-policy: no-referrer
Scope note: `<img src>` embedding is unaffected — CSP applies to the document,
not to an image reference — so the editor's image preview (viewers.tsx renders
`<img>`) and markdown image rewriting keep working unchanged. The guard is
type-scoped, so PNG/JPEG/PDF responses are byte-identical to before.
Verified:
- `smoke.spec.ts` drives the real `/sidebar/file` route: the new case asserts
200 + `image/svg+xml` + a `sandbox` CSP + `nosniff`, and is RED on the
pre-change source (the CSP header is absent). A companion case pins that a
PNG response gains no CSP, so the change cannot silently widen.
- the test helper now surfaces response headers (`invokeGetFull`), reusing the
existing `invokeGet` for every other caller.
- typecheck / lint / build clean.
This commit is contained in:
1 parent
1fcf43ccbe
commit
c7c9e3c9d8
2 files changed
+64
-2
No files matched your search
@@ -991,6 +991,16 @@ export function apply(ctx: Context, config?: SidebarConfig): void {
|
||||
// Raw bytes either way (binary-safe); ?download=1 switches the
|
||||
// disposition so the browser saves the file instead of showing it.
|
||||
const headers: Record<string, string> = { 'content-type': type, 'cache-control': 'no-cache' }
|
||||
// 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 (url.searchParams.get('download') === '1') {
|
||||
headers['content-disposition'] = `attachment; filename*=UTF-8''${encodeURIComponent(basename(path))}`
|
||||
}
|
||||
|
||||
+54
-2
@@ -546,10 +546,18 @@ describe('session cwd resolution over the API route', () => {
|
||||
}
|
||||
|
||||
const invokeGet = async (route: SidebarWebRoute, url: string): Promise<{ status: number; body: string }> => {
|
||||
const out: { status: number; body: string } = { status: 200, body: '' }
|
||||
return (await invokeGetFull(route, url))
|
||||
}
|
||||
|
||||
/** invokeGet plus the response headers (for header contracts). */
|
||||
const invokeGetFull = async (route: SidebarWebRoute, url: string): Promise<{ status: number; body: string; headers: Record<string, string> }> => {
|
||||
const out: { status: number; body: string; headers: Record<string, string> } = { status: 200, body: '', headers: {} }
|
||||
const req = { method: 'GET', url, headers: { host: '127.0.0.1:3080' } } as never
|
||||
const res = {
|
||||
writeHead: (status: number) => { out.status = status },
|
||||
writeHead: (status: number, headers?: Record<string, string>) => {
|
||||
out.status = status
|
||||
if (headers !== undefined) out.headers = headers
|
||||
},
|
||||
end: (chunk: unknown) => { out.body += String(chunk ?? '') },
|
||||
} as never
|
||||
await route.handler(req, res)
|
||||
@@ -763,6 +771,50 @@ describe('session cwd resolution over the API route', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('serves an SVG with a sandboxing CSP so a direct navigation cannot run its script', async () => {
|
||||
// An SVG is a scriptable document. Served bare on the GUI origin, opening
|
||||
// this URL directly runs its <script> with same-origin access to
|
||||
// /sidebar/api/*. The html route already sandboxes; the media route must
|
||||
// match. <img> embedding is unaffected (CSP applies to documents).
|
||||
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-svg-security-'))
|
||||
const workspace = join(root, 'workspace')
|
||||
mkdirSync(workspace)
|
||||
const svgPath = join(workspace, 'logo.svg')
|
||||
writeFileSync(svgPath, '<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>')
|
||||
try {
|
||||
const routes = mountAll({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
|
||||
const media = routes.find(route => route.path === '/sidebar/file')!
|
||||
const res = await invokeGetFull(media, `/sidebar/file?sessionId=security&path=${encodeURIComponent(svgPath)}`)
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers['content-type']).toBe('image/svg+xml')
|
||||
expect(res.headers['content-security-policy']).toContain('sandbox')
|
||||
expect(res.headers['x-content-type-options']).toBe('nosniff')
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('leaves an ordinary image response free of the SVG sandbox headers', async () => {
|
||||
// The guard is scoped to the scriptable type: a PNG must not gain a
|
||||
// sandbox directive (nothing to sandbox, and it would be a behaviour
|
||||
// change for existing image previews).
|
||||
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-png-security-'))
|
||||
const workspace = join(root, 'workspace')
|
||||
mkdirSync(workspace)
|
||||
const pngPath = join(workspace, 'pixel.png')
|
||||
writeFileSync(pngPath, Buffer.from([0x89, 0x50, 0x4e, 0x47]))
|
||||
try {
|
||||
const routes = mountAll({ sessions: { get: () => ({ header: { cwd: workspace } }) } })
|
||||
const media = routes.find(route => route.path === '/sidebar/file')!
|
||||
const res = await invokeGetFull(media, `/sidebar/file?sessionId=security&path=${encodeURIComponent(pngPath)}`)
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers['content-type']).toBe('image/png')
|
||||
expect(res.headers['content-security-policy']).toBeUndefined()
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('keeps fs.tree missing-path failures as fs errors', async () => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-security-'))
|
||||
const workspace = join(root, 'workspace')
|
||||
|
||||
Reference in new issue
Block a user