From c7c9e3c9d82c7273d207e31dbf72edff7eda6012 Mon Sep 17 00:00:00 2001 From: 6699ll <6699ll@users.noreply.github.com> Date: Tue, 22 Sep 2026 02:48:43 +0800 Subject: [PATCH] fix(security): sandbox SVG served by the media route MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `/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 `') + 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')