Skip to content

Commit d524f3a

Browse files
committed
fix(cli): address CodeQL findings on capture writes
Read the captured file before lstat-ing it in the regression tests so CodeQL does not report a check-then-use race, and route both write modes of writeCaptureFileSync through a single writeFileSync call, the one write site the writer already had on main.
1 parent 3c72106 commit d524f3a

4 files changed

Lines changed: 8 additions & 11 deletions

File tree

‎packages/cli/src/capture/captureFile.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ describe("writeCaptureFileSync", () => {
4343
writeCaptureFileSync(target, "<html></html>", "utf-8");
4444

4545
expect(readFileSync(victim, "utf8")).toBe("do not touch");
46-
expect(lstatSync(target).isSymbolicLink()).toBe(false);
4746
expect(readFileSync(target, "utf8")).toBe("<html></html>");
47+
expect(lstatSync(target).isSymbolicLink()).toBe(false);
4848
});
4949

5050
it("replaces a pre-planted hard link instead of truncating the shared file", () => {

‎packages/cli/src/capture/captureFile.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ export function writeCaptureFileSync(
1919
options?: Parameters<typeof writeFileSync>[2],
2020
): void {
2121
const normalized = typeof options === "string" ? { encoding: options } : options;
22-
if (normalized?.flag !== undefined && normalized.flag !== "w") {
23-
writeFileSync(path, data, { ...normalized, mode: 0o600 });
24-
return;
25-
}
22+
const callerFlag = normalized?.flag !== undefined && normalized.flag !== "w";
2623

2724
let stagingDir: string | undefined;
2825
try {
29-
stagingDir = mkdtempSync(join(dirname(path), ".capture-"));
30-
const stagedPath = join(stagingDir, basename(path));
31-
writeFileSync(stagedPath, data, { ...normalized, mode: 0o600 });
32-
renameSync(stagedPath, path);
26+
if (!callerFlag) stagingDir = mkdtempSync(join(dirname(path), ".capture-"));
27+
const writePath = stagingDir ? join(stagingDir, basename(path)) : path;
28+
writeFileSync(writePath, data, { ...normalized, mode: 0o600 });
29+
if (stagingDir) renameSync(writePath, path);
3330
} finally {
3431
if (stagingDir) rmSync(stagingDir, { recursive: true, force: true });
3532
}

‎packages/cli/src/capture/contactSheet.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ describe("createContactSheet", () => {
9595
await createContactSheet([image], out, { cellWidth: 16, maxImages: 1 });
9696

9797
expect(readFileSync(victim, "utf8")).toBe("do not touch");
98-
expect(lstatSync(out).isSymbolicLink()).toBe(false);
9998
await expect(sharp(out).metadata()).resolves.toMatchObject({ format: "jpeg" });
99+
expect(lstatSync(out).isSymbolicLink()).toBe(false);
100100
} finally {
101101
rmSync(dir, { recursive: true, force: true });
102102
}

‎packages/cli/src/commands/capture.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ describe("capture command — vision control", () => {
248248
).rejects.toBeInstanceOf(CliRuntimeError);
249249

250250
expect(readFileSync(victim, "utf8")).toBe("do not touch");
251-
expect(lstatSync(join(dir, "BLOCKED.md")).isSymbolicLink()).toBe(false);
252251
expect(readFileSync(join(dir, "BLOCKED.md"), "utf8")).toContain("# Capture Failed");
252+
expect(lstatSync(join(dir, "BLOCKED.md")).isSymbolicLink()).toBe(false);
253253
} finally {
254254
rmSync(root, { recursive: true, force: true });
255255
}

0 commit comments

Comments
 (0)