Skip to content

Commit f1d0c2e

Browse files
authored
fix(producer): pin lint entry reads to checked descriptors (#3734)
* fix(producer): pin lint entry reads to checked descriptors * test(producer): replace lint entry paths portably
1 parent e5b3514 commit f1d0c2e

4 files changed

Lines changed: 214 additions & 5 deletions

File tree

‎.github/workflows/windows-render.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ jobs:
479479
shell: pwsh
480480
run: bun run --cwd packages/studio-server test -- src/helpers/waveform.test.ts src/helpers/waveform.file-race.test.ts --maxWorkers=2
481481

482+
- name: Verify producer lint entry reads on Windows
483+
if: matrix.lane == 'studio-engine-cli'
484+
shell: pwsh
485+
run: bunx vitest run packages/producer/src/services/hyperframeLint.file-race.test.ts --maxWorkers=2
486+
482487
- name: Run runtime contract test
483488
if: matrix.runtime_contract
484489
shell: pwsh

‎packages/producer/scripts/test-classification.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const INTEGRATION_TEST_FILES = new Set([
2121
"src/services/fileServer.test.ts",
2222
"src/services/fileServer.file-race.test.ts",
2323
"src/services/healthWorker.test.ts",
24+
"src/services/hyperframeLint.file-race.test.ts",
2425
"src/services/assetMediaType.test.ts",
2526
"src/services/htmlCompiler.mediaType.test.ts",
2627
"src/services/htmlCompiler.naturalDuration.test.ts",
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { join, resolve } from "node:path";
3+
import { tmpdir } from "node:os";
4+
import { execFileSync } from "node:child_process";
5+
import { prepareHyperframeLintBody } from "./hyperframeLint.js";
6+
7+
const hooks = vi.hoisted(() => {
8+
const state: {
9+
swap?: () => void;
10+
target: string;
11+
descriptors: Set<number>;
12+
failStat: boolean;
13+
failRead: boolean;
14+
} = {
15+
target: "",
16+
descriptors: new Set(),
17+
failStat: false,
18+
failRead: false,
19+
};
20+
return state;
21+
});
22+
vi.mock("node:fs", async (importOriginal) => {
23+
const fs = await importOriginal<typeof import("node:fs")>();
24+
const swap = () => {
25+
const run = hooks.swap;
26+
hooks.swap = undefined;
27+
run?.();
28+
};
29+
return {
30+
...fs,
31+
openSync: (...args: Parameters<typeof fs.openSync>) => {
32+
const fd = fs.openSync(...args);
33+
hooks.descriptors.add(fd);
34+
return fd;
35+
},
36+
statSync: (...args: Parameters<typeof fs.statSync>) => {
37+
const stat = fs.statSync(...args);
38+
if (args[0] === hooks.target) swap();
39+
return stat;
40+
},
41+
fstatSync: (...args: Parameters<typeof fs.fstatSync>) => {
42+
if (hooks.failStat) throw new Error("stat failure");
43+
const stat = fs.fstatSync(...args);
44+
swap();
45+
return stat;
46+
},
47+
readFileSync: (...args: Parameters<typeof fs.readFileSync>) => {
48+
if (hooks.failRead) throw new Error("read failure");
49+
return fs.readFileSync(...args);
50+
},
51+
closeSync: (fd: number) => {
52+
fs.closeSync(fd);
53+
hooks.descriptors.delete(fd);
54+
},
55+
};
56+
});
57+
vi.mock("@hyperframes/lint", () => ({ lintHyperframeHtml: vi.fn() }));
58+
const fs = await vi.importActual<typeof import("node:fs")>("node:fs");
59+
let root: string;
60+
let projectDir: string;
61+
beforeEach(() => {
62+
root = fs.mkdtempSync(join(tmpdir(), "hf-lint-entry-"));
63+
projectDir = join(root, "project");
64+
fs.mkdirSync(projectDir);
65+
Object.assign(hooks, { target: "", swap: undefined, failStat: false, failRead: false });
66+
});
67+
afterEach(() => {
68+
const leaked = [...hooks.descriptors];
69+
for (const fd of leaked) fs.closeSync(fd);
70+
hooks.descriptors.clear();
71+
fs.rmSync(root, { recursive: true, force: true });
72+
expect(leaked).toEqual([]);
73+
});
74+
function writeEntry(name: string, html = "<html>checked</html>") {
75+
const path = join(projectDir, name);
76+
fs.mkdirSync(resolve(path, ".."), { recursive: true });
77+
fs.writeFileSync(path, html);
78+
return path;
79+
}
80+
function prepare(entryFile?: string) {
81+
return prepareHyperframeLintBody({ projectDir, entryFile });
82+
}
83+
function expected(entryFile: string, html = "<html>checked</html>") {
84+
return { prepared: { entryFile, html, source: "projectDir" } };
85+
}
86+
87+
describe("project entry reads", () => {
88+
it.each(["preferred.html", "index.html", "src/index.html"])(
89+
"reads the checked file when %s is replaced",
90+
(entry) => {
91+
hooks.target = writeEntry(entry);
92+
hooks.swap = () => {
93+
fs.renameSync(hooks.target, join(root, "original.html"));
94+
fs.writeFileSync(hooks.target, "<html>unchecked</html>");
95+
};
96+
expect(prepare(entry === "preferred.html" ? entry : undefined)).toEqual(expected(entry));
97+
expect(hooks.swap).toBeUndefined();
98+
},
99+
);
100+
it("prefers requested entries and accepts empty HTML", () => {
101+
writeEntry("preferred.html", "");
102+
writeEntry("index.html");
103+
expect(prepare("preferred.html")).toEqual(expected("preferred.html", ""));
104+
});
105+
it.each(["missing", "directory", "dangling", "non-directory-parent"])(
106+
"falls back from a %s entry",
107+
(kind) => {
108+
writeEntry("index.html");
109+
let preferred = "preferred.html";
110+
if (kind === "directory") fs.mkdirSync(join(projectDir, preferred));
111+
if (kind === "dangling")
112+
fs.symlinkSync(join(root, "absent"), join(projectDir, preferred), "file");
113+
if (kind === "non-directory-parent") {
114+
writeEntry("parent");
115+
preferred = "parent/file.html";
116+
}
117+
expect(prepare(preferred)).toEqual(expected("index.html"));
118+
},
119+
);
120+
it.skipIf(process.platform === "win32")("skips a FIFO without waiting for a writer", () => {
121+
execFileSync("mkfifo", [join(projectDir, "pipe")]);
122+
writeEntry("index.html");
123+
expect(prepare("pipe")).toEqual(expected("index.html"));
124+
});
125+
it("keeps project and entry symlinks working", () => {
126+
const target = join(root, "linked.html");
127+
fs.writeFileSync(target, "linked");
128+
fs.symlinkSync(target, join(projectDir, "index.html"), "file");
129+
const linkedProject = join(root, "linked-project");
130+
fs.symlinkSync(projectDir, linkedProject, process.platform === "win32" ? "junction" : "dir");
131+
expect(prepareHyperframeLintBody({ projectDir: linkedProject })).toEqual(
132+
expected("index.html", "linked"),
133+
);
134+
});
135+
it("rejects a sibling directory sharing the project name prefix", () => {
136+
const sibling = join(root, "project-other");
137+
fs.mkdirSync(sibling);
138+
fs.writeFileSync(join(sibling, "index.html"), "outside");
139+
expect(prepare("../project-other/index.html")).toEqual({
140+
error: "Entry file must stay inside project directory: ../project-other/index.html",
141+
});
142+
});
143+
it("preserves absent-project and absent-entry errors", () => {
144+
expect(prepare()).toEqual({
145+
error: `No HTML entry file found in project directory: ${join(projectDir, "index.html")}`,
146+
});
147+
fs.rmdirSync(projectDir);
148+
expect(prepare()).toEqual({ error: `Project directory not found: ${projectDir}` });
149+
});
150+
it.each(["stat", "read"])("propagates %s failures and closes the descriptor", (failure) => {
151+
writeEntry("index.html");
152+
hooks.failStat = failure === "stat";
153+
hooks.failRead = failure === "read";
154+
expect(() => prepare()).toThrow(`${failure} failure`);
155+
expect(hooks.descriptors.size).toBe(0);
156+
});
157+
it("keeps inline HTML and files-payload entry selection unchanged", () => {
158+
expect(prepareHyperframeLintBody({ html: "", entryFile: " custom.html " })).toEqual({
159+
prepared: { html: "", entryFile: "custom.html", source: "html" },
160+
});
161+
expect(
162+
prepareHyperframeLintBody({ files: { "src/index.html": "source", "index.html": "" } }),
163+
).toEqual({ prepared: { html: "source", entryFile: "src/index.html", source: "files" } });
164+
});
165+
});

‎packages/producer/src/services/hyperframeLint.ts‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { existsSync, readFileSync, statSync } from "node:fs";
2-
import { resolve, join } from "node:path";
1+
import {
2+
existsSync,
3+
readFileSync,
4+
statSync,
5+
openSync,
6+
fstatSync,
7+
closeSync,
8+
constants,
9+
} from "node:fs";
10+
import { resolve, join, relative, isAbsolute, sep } from "node:path";
311
import { lintHyperframeHtml, type HyperframeLintResult } from "@hyperframes/lint";
412

513
export interface PreparedHyperframeLintInput {
@@ -38,6 +46,30 @@ function pickEntryFile(files: Record<string, string>, preferredEntryFile?: strin
3846
return null;
3947
}
4048

49+
function readEntryHtml(filePath: string): string | null {
50+
let fd: number;
51+
try {
52+
// Opening a named pipe must not wait for a writer before fstat can reject it.
53+
fd = openSync(filePath, constants.O_RDONLY | constants.O_NONBLOCK);
54+
} catch (error) {
55+
if (
56+
error instanceof Error &&
57+
"code" in error &&
58+
(error.code === "ENOENT" || error.code === "ENOTDIR")
59+
)
60+
return null;
61+
// Classify platform-specific directory/socket errors only after open fails.
62+
if (!statSync(filePath, { throwIfNoEntry: false })?.isFile()) return null;
63+
throw error;
64+
}
65+
try {
66+
if (!fstatSync(fd).isFile()) return null;
67+
return readFileSync(fd, "utf-8");
68+
} finally {
69+
closeSync(fd);
70+
}
71+
}
72+
4173
function readProjectEntryFile(
4274
projectDir: string,
4375
preferredEntryFile?: string,
@@ -53,13 +85,19 @@ function readProjectEntryFile(
5385

5486
for (const entryFile of entryCandidates) {
5587
const absoluteEntryPath = resolve(absProjectDir, entryFile);
56-
if (!absoluteEntryPath.startsWith(absProjectDir)) {
88+
const entryRelativePath = relative(absProjectDir, absoluteEntryPath);
89+
if (
90+
entryRelativePath === ".." ||
91+
entryRelativePath.startsWith(`..${sep}`) ||
92+
isAbsolute(entryRelativePath)
93+
) {
5794
return { error: `Entry file must stay inside project directory: ${entryFile}` };
5895
}
59-
if (existsSync(absoluteEntryPath) && statSync(absoluteEntryPath).isFile()) {
96+
const html = readEntryHtml(absoluteEntryPath);
97+
if (html !== null) {
6098
return {
6199
entryFile,
62-
html: readFileSync(absoluteEntryPath, "utf-8"),
100+
html,
63101
source: "projectDir",
64102
};
65103
}

0 commit comments

Comments
 (0)