From 9df954a23160d246491838f8d70b388e33a40cfd Mon Sep 17 00:00:00 2001 From: evgyur Date: Thu, 5 Nov 2026 06:45:54 +0300 Subject: [PATCH] fix: sandbox file previews and reserve archive capacity [skip ci] --- src/archive-route.ts | 81 +++++++++++++++++++-------------- src/index.ts | 8 +++- tests/archive.spec.ts | 21 +++++++++ tests/file-stream-route.spec.ts | 15 ++++++ 4 files changed, 90 insertions(+), 35 deletions(-) diff --git a/src/archive-route.ts b/src/archive-route.ts index a4cd9f8a..b95590c8 100644 --- a/src/archive-route.ts +++ b/src/archive-route.ts @@ -215,6 +215,8 @@ export function createArchiveTasks(options: ArchiveTaskOptions = {}): ArchiveTas const tasks = new Map() /** Running children, by task id (teardown kills them). */ const children = new Map() + let resolvingArchives = 0 + let disposed = false const spawner = options.spawn ?? undefined const tempPathOf = (id: string, ext: string): string => @@ -245,40 +247,45 @@ export function createArchiveTasks(options: ArchiveTaskOptions = {}): ArchiveTas sweep(Date.now()) let building = 0 for (const task of tasks.values()) if (task.state === 'building') building += 1 - if (building >= ARCHIVE_MAX_BUILDING) { + if (disposed) throw new SidebarError('unavailable', 'archive service disposed', 503) + if (building + resolvingArchives >= ARCHIVE_MAX_BUILDING) { throw new SidebarError('bad-request', `too many archives are being built (max ${ARCHIVE_MAX_BUILDING})`, 409) } - const resolve = options.resolveTool ?? resolveArchiveTool - // One call either way: `spawn` has a default, so passing `undefined` - // selects the real implementation without a second code path. - const spec = await resolve(spawner) - if (spec === undefined) { - // A NEW wire code, and deliberately its own: "this machine has no zip - // and no tar" is not a failure of the selection, and the client shows - // it as an actionable sentence rather than a raw English message. - throw new SidebarError( - 'unavailable', - 'no archive tool is available: neither zip nor tar could be run', - 501, - ) - } - const plan = planArchive(paths, spec) - const id = `ar-${randomUUID()}` - const finalName = archiveFileName(name, spec.ext) - const tempPath = tempPathOf(id, spec.ext) - const task: ArchiveTask = { - id, - sessionId, - name: finalName, - state: 'building', - bytes: 0, - expiresAt: Number.POSITIVE_INFINITY, - } - tasks.set(id, task) - // Detached on purpose: the POST answers as soon as the id exists, and the - // task table (not the request) owns the error from here on. - void pack(id, task, plan, tempPath, spawner) - return { id, name: finalName, entries: paths.length } + resolvingArchives += 1 + try { + const resolve = options.resolveTool ?? resolveArchiveTool + // One call either way: `spawn` has a default, so passing `undefined` + // selects the real implementation without a second code path. + const spec = await resolve(spawner) + if (disposed) throw new SidebarError('unavailable', 'archive service disposed', 503) + if (spec === undefined) { + // A NEW wire code, and deliberately its own: "this machine has no zip + // and no tar" is not a failure of the selection, and the client shows + // it as an actionable sentence rather than a raw English message. + throw new SidebarError( + 'unavailable', + 'no archive tool is available: neither zip nor tar could be run', + 501, + ) + } + const plan = planArchive(paths, spec) + const id = `ar-${randomUUID()}` + const finalName = archiveFileName(name, spec.ext) + const tempPath = tempPathOf(id, spec.ext) + const task: ArchiveTask = { + id, + sessionId, + name: finalName, + state: 'building', + bytes: 0, + expiresAt: Number.POSITIVE_INFINITY, + } + tasks.set(id, task) + // Detached on purpose: the POST answers as soon as the id exists, and the + // task table (not the request) owns the error from here on. + void pack(id, task, plan, tempPath, spawner) + return { id, name: finalName, entries: paths.length } + } finally { resolvingArchives -= 1 } }, status(id, sessionId) { sweep(Date.now()) @@ -302,6 +309,7 @@ export function createArchiveTasks(options: ArchiveTaskOptions = {}): ArchiveTas return tasks.size }, dispose() { + disposed = true for (const child of children.values()) { try { child.kill() @@ -338,17 +346,22 @@ export function createArchiveTasks(options: ArchiveTaskOptions = {}): ArchiveTas children.delete(id) } let handle: Awaited> | undefined + let ownsTemp = false try { - handle = await open(tempPath, 'w') + handle = await open(tempPath, 'wx') + ownsTemp = true + if (disposed) throw new Error('archive service disposed') const result = await runArchiveTool(plan, handle.fd, spawner, child => { children.set(id, child) }) await handle.close() handle = undefined + if (disposed) throw new Error('archive service disposed') if (result.code !== 0) { settle(result.stderr === '' ? `${plan.spec.bin} exited with code ${result.code}` : result.stderr) await rm(tempPath, { force: true }).catch(() => {}) return } const info = await stat(tempPath).catch(() => undefined) + if (disposed) throw new Error('archive service disposed') if (info === undefined) { settle(`${plan.spec.bin} produced no archive`) return @@ -362,7 +375,7 @@ export function createArchiveTasks(options: ArchiveTaskOptions = {}): ArchiveTas await handle?.close().catch(() => {}) task.bytes = await stat(tempPath).then(info => info.size).catch(() => 0) settle(error instanceof Error ? error.message : String(error)) - await rm(tempPath, { force: true }).catch(() => {}) + if (ownsTemp) await rm(tempPath, { force: true }).catch(() => {}) } } } diff --git a/src/index.ts b/src/index.ts index 996976c9..ea05329c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1846,7 +1846,13 @@ 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. - const headers: Record = { 'content-type': type, 'cache-control': 'no-cache' } + 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 ') + const { origin } = await serve(ws) + const response = await fetch(fileUrl(origin, ws, target)) + expect(response.status).toBe(200) + const policy = response.headers.get('content-security-policy') ?? '' + expect(policy).toContain('sandbox') + expect(policy).not.toContain('allow-same-origin') + expect(response.headers.get('x-content-type-options')).toBe('nosniff') + expect(response.headers.get('referrer-policy')).toBe('no-referrer') + expect(await response.text()).toContain('window.fixture') + }) + it('answers the JSON error envelope when the file cannot be opened', async () => { const ws = workspace('missing') const { origin } = await serve(ws)