From 22b394c663ff1cd1bfe8b92209cc1a202cbcc465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 19:59:08 +0000 Subject: [PATCH] fix(studio-server): a preview asset whose read cannot be recorded answers not found (#8385) --- .../workflows/studio-server-windows-files.yml | 11 ++++- packages/core/src/safePath.eisdir.test.ts | 15 ++++++- .../src/helpers/projectDisk.eisdir.test.ts | 15 ++++++- .../src/helpers/projectDisk.test.ts | 15 ++++++- .../src/routes/preview.recordClosed.test.ts | 42 +++++++++++++++++++ packages/studio-server/src/routes/preview.ts | 6 ++- 6 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 packages/studio-server/src/routes/preview.recordClosed.test.ts diff --git a/.github/workflows/studio-server-windows-files.yml b/.github/workflows/studio-server-windows-files.yml index a1784fa0d8..31f918b045 100644 --- a/.github/workflows/studio-server-windows-files.yml +++ b/.github/workflows/studio-server-windows-files.yml @@ -1,12 +1,17 @@ name: Studio server files on Windows -# A file another program holds open, or a read-only one, must not read as missing or as a server error. +# A file another program holds open, or a read-only one, must not read as missing or as a server error; a link to a +# private file is refused on Windows as elsewhere. on: pull_request: paths: - packages/studio-server/src/routes/files.ts - packages/studio-server/src/routes/files.heldFile.test.ts - packages/core/src/renameWithRetry.ts + - packages/studio-server/src/helpers/projectDisk.ts + - packages/studio-server/src/helpers/projectDisk*.test.ts + - packages/core/src/safePath.ts + - packages/core/src/safePath.eisdir.test.ts - .github/workflows/studio-server-windows-files.yml permissions: @@ -25,5 +30,7 @@ jobs: - run: bun install --frozen-lockfile --ignore-scripts --linker=hoisted - run: bun run --filter '@hyperframes/{parsers,lint,studio-server}' build - run: bun run --cwd packages/core build - - run: npx vitest run src/routes/files.heldFile.test.ts + - run: npx vitest run src/routes/files.heldFile.test.ts src/helpers/projectDisk working-directory: packages/studio-server + - run: npx vitest run src/safePath.eisdir.test.ts + working-directory: packages/core diff --git a/packages/core/src/safePath.eisdir.test.ts b/packages/core/src/safePath.eisdir.test.ts index eb51ff7d85..c47b53416a 100644 --- a/packages/core/src/safePath.eisdir.test.ts +++ b/packages/core/src/safePath.eisdir.test.ts @@ -39,7 +39,20 @@ it("still resolves paths on a volume whose native realpath refuses with EISDIR", // fails without the fix: off the event loop a link on such a volume never resolved, so the private-file check read it // under its innocuous spelling. -it.skipIf(process.platform === "win32")( +// Windows makes a link only with the right to (an admin, or Developer Mode); without it these cases can't be set up. +const canLink = (() => { + const dir = mkdtempSync(join(tmpdir(), "link-probe-")); + try { + symlinkSync(join(dir, "a"), join(dir, "b")); + return true; + } catch { + return false; + } finally { + rmSync(dir, { recursive: true, force: true }); + } +})(); + +it.skipIf(!canLink)( "resolves a link off the event loop on a volume whose native realpath refuses with EISDIR", async () => { const base = mkdtempSync(join(tmpdir(), "safepath-eisdir-")); diff --git a/packages/studio-server/src/helpers/projectDisk.eisdir.test.ts b/packages/studio-server/src/helpers/projectDisk.eisdir.test.ts index e2fc4086df..c80fa4bfe2 100644 --- a/packages/studio-server/src/helpers/projectDisk.eisdir.test.ts +++ b/packages/studio-server/src/helpers/projectDisk.eisdir.test.ts @@ -19,7 +19,20 @@ afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); -describe.skipIf(process.platform === "win32")("the off-loop private-file check", () => { +// Windows makes a link only with the right to (an admin, or Developer Mode); without it these cases can't be set up. +const canLink = (() => { + const dir = mkdtempSync(join(tmpdir(), "link-probe-")); + try { + symlinkSync(join(dir, "a"), join(dir, "b")); + return true; + } catch { + return false; + } finally { + rmSync(dir, { recursive: true, force: true }); + } +})(); + +describe.skipIf(!canLink)("the off-loop private-file check", () => { // fails without the fix: native realpath failed at every level, so the check compared the link's own spelling and // served the project's .env under an innocuous name. it("refuses an innocuously named link to a private file on a volume whose native realpath refuses", async () => { diff --git a/packages/studio-server/src/helpers/projectDisk.test.ts b/packages/studio-server/src/helpers/projectDisk.test.ts index edf63271f4..428b8f81c8 100644 --- a/packages/studio-server/src/helpers/projectDisk.test.ts +++ b/packages/studio-server/src/helpers/projectDisk.test.ts @@ -16,7 +16,20 @@ function project(): string { return dir; } -describe.skipIf(process.platform === "win32")("the off-loop private-file check", () => { +// Windows makes a link only with the right to (an admin, or Developer Mode); without it these cases can't be set up. +const canLink = (() => { + const dir = mkdtempSync(join(tmpdir(), "link-probe-")); + try { + symlinkSync(join(dir, "a"), join(dir, "b")); + return true; + } catch { + return false; + } finally { + rmSync(dir, { recursive: true, force: true }); + } +})(); + +describe.skipIf(!canLink)("the off-loop private-file check", () => { // fails without the fix: a path that is there but will not resolve (a link loop) was judged by its own spelling. it("refuses a path that is there but will not resolve", async () => { const dir = project(); diff --git a/packages/studio-server/src/routes/preview.recordClosed.test.ts b/packages/studio-server/src/routes/preview.recordClosed.test.ts new file mode 100644 index 0000000000..46c1671ca0 --- /dev/null +++ b/packages/studio-server/src/routes/preview.recordClosed.test.ts @@ -0,0 +1,42 @@ +// @vitest-environment node +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, expect, it, vi } from "vitest"; +import type { StudioApiAdapter } from "../types"; + +// A path that stops resolving between the private-file check and the read's record (a link swapped for a loop). +vi.mock("../helpers/previewReads.js", async (importOriginal) => ({ + ...(await importOriginal()), + recordPreviewReadOff: () => Promise.reject(Object.assign(new Error("ELOOP"), { code: "ELOOP" })), +})); + +const { createStudioApi } = await import("../createStudioApi"); +let projectDir = ""; +afterEach(() => rmSync(projectDir, { recursive: true, force: true })); + +const adapter = (dir: string): StudioApiAdapter => ({ + listProjects: () => [], + resolveProject: async (id: string) => ({ id, dir }), + bundle: async () => null, + lint: async () => ({ findings: [] }), + runtimeUrl: "/api/runtime.js", + rendersDir: () => "/tmp/renders", + startRender: () => ({ + id: "job-1", + status: "rendering", + progress: 0, + outputPath: "/tmp/out.mp4", + }), +}); + +// fails without the fix: the record's failure escaped the route, which answered a server error for the file. +it("an asset whose path stops resolving before its read is recorded answers not found", async () => { + projectDir = mkdtempSync(join(tmpdir(), "preview-record-")); + mkdirSync(join(projectDir, "assets")); + writeFileSync(join(projectDir, "assets", "notes.txt"), "hello"); + const res = await createStudioApi(adapter(projectDir)).request( + "http://localhost/projects/demo/preview/assets/notes.txt", + ); + expect(res.status).toBe(404); +}); diff --git a/packages/studio-server/src/routes/preview.ts b/packages/studio-server/src/routes/preview.ts index 30540c509d..632d75e6ab 100644 --- a/packages/studio-server/src/routes/preview.ts +++ b/packages/studio-server/src/routes/preview.ts @@ -785,7 +785,11 @@ export function registerPreviewRoutes( if (!file || (await isPrivateProjectFileOff(project.dir, file))) { return c.text("not found", 404); } - await recordPreviewReadOff(project.dir, file); + const recorded = await recordPreviewReadOff(project.dir, file).then( + () => true, + () => false, + ); + if (!recorded) return c.text("not found", 404); const stat = await statOrNull(file); if (!stat?.isFile()) { return c.text("not found", 404);