diff --git a/src/index.ts b/src/index.ts index 9598be87..509f7484 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 = { '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 ') + 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')