Files
hyperframes/packages/cli/src/capture/mediaCapture.test.ts
Leonel RivasandClaude Opus 5.5 42eb6e9ad2 fix(cli): replace pre-planted links when writing capture files (#4298)
* fix(cli): replace pre-planted links when writing capture files

writeCaptureFileSync wrote through whatever sat at the target path, so a
symlink or hard link planted in the capture directory redirected the write to
its target. Stage each file in a private directory next to the target and
rename it over the target instead, the same publication pattern the font
cache uses since #3669. Rename swaps the directory entry, so a planted link is
replaced rather than followed, while intentional overwrites such as the
deadline partial bundle keep working. Cleanup only removes the staging
directory this call created. A caller's own flag (wx for meta.json) is passed
through unchanged.

Two capture writes bypassed the writer and stayed exposed: the contact sheets
(sharp's toFile opens the path itself) and the BLOCKED.md failure note. Route
both through writeCaptureFileSync.

Fixes #4243

* 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.

* fix(cli): route media previews through the capture writer

The Lottie and video previews in mediaCapture.ts still let Puppeteer write
the PNG itself via page.screenshot({ path }), which opens whatever sits at
that path, so a pre-planted link there redirected the write. Take the
screenshot as a buffer and write it with writeCaptureFileSync, like
screenshotCapture.ts already does.

Also restructure writeCaptureFileSync so its only write is the same
writeFileSync(path, ...) call main has. Publication now stages through a
separate helper that calls back into the writer with O_EXCL (wx) on a
fresh staging path, then renames over the target. Behavior is unchanged,
and CodeQL keeps tracking the existing network-data-to-file finding on
that line instead of reporting it as new.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y2KDat7mo5Dr8wFkFzobyb

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 06:24:21 +01:00

302 lines
10 KiB
TypeScript

import {
existsSync,
lstatSync,
mkdirSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { Browser, Page } from "puppeteer-core";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
captureVideoManifest,
remainingVideoDownloadTimeoutMs,
renderLottiePreviews,
saveLottieAnimations,
} from "./mediaCapture.js";
const tempDirs: string[] = [];
function tempDir(): string {
const dir = mkdtempSync(join(tmpdir(), "hf-media-budget-"));
tempDirs.push(dir);
return dir;
}
afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
for (const dir of tempDirs) rmSync(dir, { recursive: true, force: true });
tempDirs.length = 0;
});
describe("Lottie capture budget", () => {
it("does not start another Lottie fetch after the live budget expires", async () => {
const dir = tempDir();
let remainingMs = 10_000;
const fetchMock = vi.fn(async () => {
remainingMs = 0;
return new Response(JSON.stringify({ w: 100, h: 100, layers: [] }), { status: 200 });
});
vi.stubGlobal("fetch", fetchMock);
const saved = await saveLottieAnimations(
[{ url: "https://one.example/a.json" }, { url: "https://two.example/b.json" }],
dir,
{ remainingMs: () => remainingMs },
);
expect(saved).toBe(1);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("does not open another Lottie preview page after the live budget expires", async () => {
const dir = tempDir();
const lottieDir = join(dir, "assets", "lottie");
mkdirSync(join(dir, "extracted"), { recursive: true });
mkdirSync(lottieDir, { recursive: true });
const lottie = JSON.stringify({ w: 100, h: 100, fr: 30, ip: 0, op: 30, layers: [] });
writeFileSync(join(lottieDir, "a.json"), lottie);
writeFileSync(join(lottieDir, "b.json"), lottie);
let remainingMs = 10_000;
const previewPage = {
setRequestInterception: vi.fn(async () => undefined),
on: vi.fn(),
setViewport: vi.fn(async () => undefined),
setContent: vi.fn(async () => undefined),
evaluate: vi.fn(async () => undefined),
waitForFunction: vi.fn(async () => undefined),
screenshot: vi.fn(async () => undefined),
close: vi.fn(async () => undefined),
};
const newPage = vi.fn(async () => {
remainingMs = 0;
return previewPage;
});
const browser = { newPage } as unknown as Browser;
await renderLottiePreviews(browser, lottieDir, dir, { remainingMs: () => remainingMs });
expect(newPage).toHaveBeenCalledTimes(1);
expect(previewPage.setViewport).not.toHaveBeenCalled();
expect(previewPage.screenshot).not.toHaveBeenCalled();
});
it("omits a preview path when the budget expires after Lottie readiness", async () => {
const dir = tempDir();
const lottieDir = join(dir, "assets", "lottie");
mkdirSync(join(dir, "extracted"), { recursive: true });
mkdirSync(lottieDir, { recursive: true });
writeFileSync(
join(lottieDir, "logo.json"),
JSON.stringify({
nm: "Logo",
w: 100,
h: 100,
fr: 30,
ip: 0,
op: 30,
layers: [],
}),
);
const screenshot = vi.fn(async () => undefined);
const previewPage = {
setRequestInterception: vi.fn(async () => undefined),
on: vi.fn(),
setViewport: vi.fn(async () => undefined),
setContent: vi.fn(async () => undefined),
evaluate: vi.fn(async () => undefined),
waitForFunction: vi.fn(async () => undefined),
screenshot,
close: vi.fn(async () => undefined),
};
const browser = { newPage: vi.fn(async () => previewPage) } as unknown as Browser;
let budgetChecks = 0;
await renderLottiePreviews(browser, lottieDir, dir, {
remainingMs: () => (++budgetChecks < 4 ? 10_000 : 0),
});
const manifest = JSON.parse(
readFileSync(join(dir, "extracted", "lottie-manifest.json"), "utf-8"),
);
expect(screenshot).not.toHaveBeenCalled();
expect(manifest).toHaveLength(1);
expect(manifest[0]).toMatchObject({
file: "assets/lottie/logo.json",
name: "Logo",
width: 100,
height: 100,
});
expect(manifest[0]).not.toHaveProperty("preview");
expect(existsSync(join(lottieDir, "previews", "logo-preview.png"))).toBe(false);
});
});
describe("video capture live budget", () => {
it("does not preview or download when the budget expires during DOM sampling", async () => {
vi.useFakeTimers();
vi.setSystemTime(0);
const dir = tempDir();
mkdirSync(join(dir, "extracted"), { recursive: true });
const descriptor = {
src: "https://video.example/hero.mp4",
filename: "hero.mp4",
width: 640,
height: 360,
sourceWidth: 640,
sourceHeight: 360,
top: 0,
left: 0,
heading: "Hero",
caption: "Demo",
ariaLabel: "",
};
const screenshot = vi.fn(async () => Buffer.from("preview"));
const evaluate = vi.fn(async (expression: unknown) =>
typeof expression === "function" ? { x: 0, y: 0, width: 640, height: 360 } : [descriptor],
);
const page = { evaluate, screenshot } as unknown as Page;
const fetchMock = vi.fn(
async () =>
new Response(Buffer.alloc(2048), {
status: 200,
headers: { "content-type": "video/mp4" },
}),
);
vi.stubGlobal("fetch", fetchMock);
const capture = captureVideoManifest(page, dir, () => {}, {
sampleMs: 10_000,
downloadBudgetMs: 10_000,
remainingMs: () => Math.max(0, 1_000 - Date.now()),
});
await vi.runAllTimersAsync();
await capture;
expect(screenshot).not.toHaveBeenCalled();
expect(fetchMock).not.toHaveBeenCalled();
});
});
describe("remainingVideoDownloadTimeoutMs", () => {
it("caps a video request to the remaining capture budget", () => {
expect(remainingVideoDownloadTimeoutMs(1_000, 5_000, 4_500)).toBe(1_500);
});
it("returns zero after the aggregate budget is exhausted", () => {
expect(remainingVideoDownloadTimeoutMs(1_000, 5_000, 6_001)).toBe(0);
});
it("retains the existing per-request ceiling when more budget remains", () => {
expect(remainingVideoDownloadTimeoutMs(1_000, 300_000, 2_000)).toBe(120_000);
});
});
describe("Lottie capture rejects unsafe persistence", () => {
it("does not write corrupt archives as raw Lottie files", async () => {
const dir = tempDir();
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response("not a zip")),
);
expect(await saveLottieAnimations([{ url: "https://public.example/bad.lottie" }], dir)).toBe(0);
expect(readdirSync(dir)).toEqual([]);
});
it("does not publish truthy non-array layers", async () => {
const dir = tempDir();
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response('{"w":100,"h":100,"layers":true}')),
);
expect(await saveLottieAnimations([{ url: "https://public.example/bad.json" }], dir)).toBe(0);
expect(readdirSync(dir)).toEqual([]);
});
});
describe("media previews replace pre-planted links", () => {
const PNG = Buffer.from("\x89PNG preview");
it.skipIf(process.platform === "win32")(
"writes a Lottie preview without following a pre-planted symlink",
async () => {
const dir = tempDir();
const lottieDir = join(dir, "assets", "lottie");
const previewPath = join(lottieDir, "previews", "logo-preview.png");
const victim = join(dir, "victim.txt");
mkdirSync(join(dir, "extracted"), { recursive: true });
mkdirSync(join(lottieDir, "previews"), { recursive: true });
writeFileSync(
join(lottieDir, "logo.json"),
JSON.stringify({ w: 100, h: 100, fr: 30, ip: 0, op: 30, layers: [] }),
);
writeFileSync(victim, "do not touch");
symlinkSync(victim, previewPath);
const previewPage = {
setRequestInterception: vi.fn(async () => undefined),
on: vi.fn(),
setViewport: vi.fn(async () => undefined),
setContent: vi.fn(async () => undefined),
evaluate: vi.fn(async () => undefined),
waitForFunction: vi.fn(async () => undefined),
screenshot: vi.fn(async () => PNG),
close: vi.fn(async () => undefined),
};
const browser = { newPage: vi.fn(async () => previewPage) } as unknown as Browser;
await renderLottiePreviews(browser, lottieDir, dir);
expect(readFileSync(victim, "utf8")).toBe("do not touch");
expect(readFileSync(previewPath)).toEqual(PNG);
expect(lstatSync(previewPath).isSymbolicLink()).toBe(false);
},
);
it.skipIf(process.platform === "win32")(
"writes a video preview without following a pre-planted symlink",
async () => {
const dir = tempDir();
const previewPath = join(dir, "assets", "videos", "previews", "video-0-preview.png");
const victim = join(dir, "victim.txt");
mkdirSync(join(dir, "extracted"), { recursive: true });
mkdirSync(join(dir, "assets", "videos", "previews"), { recursive: true });
writeFileSync(victim, "do not touch");
symlinkSync(victim, previewPath);
const descriptor = {
src: "https://video.example/hero.mp4",
filename: "hero.mp4",
width: 640,
height: 360,
sourceWidth: 640,
sourceHeight: 360,
top: 0,
left: 0,
heading: "Hero",
caption: "Demo",
ariaLabel: "",
};
const evaluate = vi.fn(async (expression: unknown) =>
typeof expression === "function" ? { x: 0, y: 0, width: 640, height: 360 } : [descriptor],
);
const page = { evaluate, screenshot: vi.fn(async () => PNG) } as unknown as Page;
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response(null, { status: 404 })),
);
await captureVideoManifest(page, dir, () => {});
expect(readFileSync(victim, "utf8")).toBe("do not touch");
expect(readFileSync(previewPath)).toEqual(PNG);
expect(lstatSync(previewPath).isSymbolicLink()).toBe(false);
},
);
});