fix(studio-server): a preview asset whose read cannot be recorded answers not found (#8385)

This commit is contained in:
Miguel Ángel
2026-11-21 19:59:08 +00:00
committed by GitHub
parent e87babf261
commit 22b394c663
6 changed files with 98 additions and 6 deletions
@@ -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
+14 -1
View File
@@ -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-"));
@@ -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 () => {
@@ -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();
@@ -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<typeof import("../helpers/previewReads.js")>()),
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);
});
+5 -1
View File
@@ -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);