Files
hyperframes/packages/cli/src/capture/partialCapture.test.ts
T
Leonel RivasandMiguel Angel Simon Sierra d2cdad418a fix(cli): refuse capture folders that resolve outside the output root (#4789)
* fix(cli): refuse capture directories that resolve outside the output root

writeCaptureFileSync guards the final path entry, but a directory in the
path that is itself a planted symlink (assets -> ~/somewhere) still carried
the staging directory and the rename into wherever it pointed, with a
filename the captured page chose.

Every capture directory is now created through ensureCaptureDirSync(root,
dir), which walks from the output root one level at a time and refuses any
directory whose real path is not the one it has under the root, before
creating anything inside it. The root itself stays trusted as given, so a
symlinked root like /tmp on macOS keeps working.

Fixes #4304

* fix(cli): vet assets/svgs before the SVG contact sheet writes into it

Under --skip-assets, downloadAssets never runs, so nothing checked
assets/svgs before createSvgContactSheet wrote its thumbnails and
contact-sheet.jpg into it; a planted assets/svgs link received them.
The sheet now runs ensureCaptureDirSync on the directory first, and a
refusal just skips the sheet.

A dangling or looping directory link is now refused with the same
message instead of a raw ENOENT/ELOOP, and a comment records why the
check uses the JS realpathSync rather than realpathSync.native.

Refs #4304

* fix(cli): vet capture --video's download folder

Video mode created assets/videos (or capture/assets/videos in a W2H
project) with a plain mkdirSync, so a planted assets or capture link
carried the downloaded clip outside the project. It now goes through
ensureCaptureDirSync with the project folder as the trusted root, and
a refused folder is reported before anything is downloaded.

* fix(cli): warn when a best-effort capture phase refuses a folder

Folder refusals now throw CaptureDirRefusedError. The contact sheets
and the video manifest still skip on any error, but a refusal adds a
warning so a planted link doesn't go unnoticed.

saveLottieAnimations now vets assets/lottie itself, taking the output
root like renderLottiePreviews, and every folder check in the capture
pipeline is pinned by a test.

* style(cli): trim the ensureCaptureDirSync doc comment to two lines

---------

Co-authored-by: Miguel Angel Simon Sierra <miguel.sierra@heygen.com>
2026-10-02 05:53:31 -07:00

83 lines
2.8 KiB
TypeScript

import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
readdirSync,
rmSync,
symlinkSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { createPartialCaptureState, writePartialCaptureBundle } from "./partialCapture.js";
import { CAPTURE_PHASE_SCHEMA } from "./types.js";
const temporaryDirectories: string[] = [];
afterEach(() => {
for (const directory of temporaryDirectories.splice(0))
rmSync(directory, { recursive: true, force: true });
});
describe("writePartialCaptureBundle", () => {
it("writes tokens collected before the deadline", () => {
const outputDir = mkdtempSync(join(tmpdir(), "hyperframes-partial-capture-"));
temporaryDirectories.push(outputDir);
const opts = { url: "https://example.com", outputDir };
const state = createPartialCaptureState(opts);
state.tokens.title = "Collected before deadline";
state.tokens.colors = ["#ABCDEF"];
const result = writePartialCaptureBundle(opts, state, {
schema: CAPTURE_PHASE_SCHEMA,
phase: "complete",
status: "degraded",
remainingMs: null,
reason: "deadline",
});
expect(result.ok).toBe(false);
expect(result.lastPhase.reason).toBe("deadline");
const tokensPath = join(outputDir, "extracted", "tokens.json");
expect(existsSync(tokensPath)).toBe(true);
expect(JSON.parse(readFileSync(tokensPath, "utf8"))).toMatchObject({
title: "Collected before deadline",
colors: ["#ABCDEF"],
});
expect(
JSON.parse(readFileSync(join(outputDir, "extracted", "design-styles.json"), "utf8")),
).toEqual(state.designStyles);
expect(readFileSync(join(outputDir, "extracted", "page.html"), "utf8")).toBe(state.pageHtml);
expect(JSON.parse(readFileSync(join(outputDir, "meta.json"), "utf8"))).toMatchObject({
partial: true,
});
});
// Creating symlinks needs elevated rights on Windows.
it.skipIf(process.platform === "win32")(
"refuses to write through a planted extracted/ symlink (#4304)",
() => {
const root = mkdtempSync(join(tmpdir(), "hyperframes-partial-capture-"));
temporaryDirectories.push(root);
const outputDir = join(root, "capture");
const outside = join(root, "outside");
mkdirSync(outputDir);
mkdirSync(outside);
symlinkSync(outside, join(outputDir, "extracted"));
const opts = { url: "https://example.com", outputDir };
expect(() =>
writePartialCaptureBundle(opts, createPartialCaptureState(opts), {
schema: CAPTURE_PHASE_SCHEMA,
phase: "complete",
status: "degraded",
remainingMs: null,
reason: "deadline",
}),
).toThrow(/outside the capture directory/);
expect(readdirSync(outside)).toEqual([]);
},
);
});