From c4a4167aace1c3aba115ba8505207dfdbc6413f0 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Tue, 17 Nov 2026 22:27:57 +0000 Subject: [PATCH] fix(cli): check's face pass reads only media the page may load (#8116) The text-over-face pass now treats a page-named source as missing when it is a private project file, by its name or through a link, or is not a regular file. Before any ffprobe or ffmpeg run, it refuses a document, pipe or playlist that names files outside its folder or private in it. Every probe and decode then uses render's demuxer list and a 30 s limit. measure reads files named on the command line as before. --- .../cli/src/commands/snapshotHeldVideos.ts | 11 ++-- packages/cli/src/measure/face.test.ts | 7 +++ packages/cli/src/measure/face.ts | 47 ++++++++++----- packages/cli/src/measure/media.test.ts | 23 ++++++++ packages/cli/src/measure/media.ts | 34 +++++++++-- packages/cli/src/utils/checkBrowser.test.ts | 33 ++++++++++- packages/cli/src/utils/checkBrowser.ts | 24 +++++--- .../utils/checkPipeline.textOverFace.test.ts | 54 +++++++++++++++++ packages/cli/src/utils/textOverFace.test.ts | 59 +++++++++++++++++++ packages/cli/src/utils/textOverFace.ts | 17 ++++-- 10 files changed, 265 insertions(+), 44 deletions(-) diff --git a/packages/cli/src/commands/snapshotHeldVideos.ts b/packages/cli/src/commands/snapshotHeldVideos.ts index 89701a7604..63c41a0c2d 100644 --- a/packages/cli/src/commands/snapshotHeldVideos.ts +++ b/packages/cli/src/commands/snapshotHeldVideos.ts @@ -2,11 +2,8 @@ import { existsSync } from "node:fs"; import { isAbsolute, relative, resolve } from "node:path"; import { parseHTML } from "linkedom"; import { MEDIA_RENDER_ID_ATTR } from "@hyperframes/core"; -import { - isNotMediaPayload, - isPrivateUnderAssetRoots, - type CaptureVideoMetadataHint, -} from "@hyperframes/engine"; +import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path"; +import { isNotMediaPayload, type CaptureVideoMetadataHint } from "@hyperframes/engine"; import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; import { findFFprobe } from "../browser/ffmpeg.js"; import { runCancellableProcess } from "../utils/cancellableProcess.js"; @@ -36,7 +33,7 @@ export function projectLocalFile(projectDir: string, src: string): string | null const file = resolve(projectDir, path); const rel = relative(projectDir, file); const inside = !rel.startsWith("..") && !isAbsolute(rel) && existsSync(file); - return inside && !isPrivateUnderAssetRoots(file, [projectDir]) ? file : null; + return inside && !isPrivateFileUnder(projectDir, file, isStudioPrivatePath) ? file : null; } catch { return null; } @@ -52,7 +49,7 @@ export async function projectVideoFile( const PROBE_TIMEOUT_MS = 30_000; -async function readableAsMediaWithinProbeLimit(path: string): Promise { +export async function readableAsMediaWithinProbeLimit(path: string): Promise { let timer: ReturnType | undefined; const deadline = new Promise((done) => { timer = setTimeout(() => done(false), PROBE_TIMEOUT_MS); diff --git a/packages/cli/src/measure/face.test.ts b/packages/cli/src/measure/face.test.ts index 9f012f3d9d..39ea37f792 100644 --- a/packages/cli/src/measure/face.test.ts +++ b/packages/cli/src/measure/face.test.ts @@ -130,6 +130,13 @@ describe.skipIf(!media)("readSamples", () => { expect(Buffer.compare(f, raw.subarray(i * 80 * 48, (i + 1) * 80 * 48))).toBe(0), ); }); + + it("reads the same frames from a file a composition names", () => { + const ks = [1, 40, 75, 210]; + expect(readSamples(probeVideo(video, { guarded: true }), ks, 80, 48)).toEqual( + readSamples(probeVideo(video), ks, 80, 48), + ); + }); }); describe.skipIf(!opencv)("faceBoxes", () => { diff --git a/packages/cli/src/measure/face.ts b/packages/cli/src/measure/face.ts index 8f26ffeb84..f5ddfe54a3 100644 --- a/packages/cli/src/measure/face.ts +++ b/packages/cli/src/measure/face.ts @@ -1,7 +1,16 @@ // How big the face is and where, ported from hf-media's face_size: OpenCV's frontal and profile Haar cascades on // evenly spaced frames, graded by face height over frame height. A regenerated presenter keeps the likeness from // FACE_HOLDS up; below FACE_FLOOR a video model cannot carry one at all (the harness's face-size eval, 2026-10-08). -import { frameAt, round, runFfmpeg, sumOf, type VideoInfo } from "./media.js"; +import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { + GUARDED_RUN_MS, + ffmpegBin, + frameAt, + round, + runFfmpeg, + sumOf, + type VideoInfo, +} from "./media.js"; import { greyMat, loadOpenCv } from "./opencv.js"; const FACE_HOLDS = 0.25; @@ -65,21 +74,27 @@ export function readSamples(info: VideoInfo, ks: number[], w: number, h: number) ...(w !== info.width || h !== info.height ? [`scale=${w}:${h}`] : []), "format=gray", ]; - const raw = runFfmpeg([ - ...seek, - "-i", - info.path, - "-an", - "-vf", - filters.join(","), - "-fps_mode", - "passthrough", - "-frames:v", - String(run.length), - "-f", - "rawvideo", - "-", - ]); + const raw = runFfmpeg( + [ + ...seek, + ...(info.guarded ? mediaInputFormatArgs(ffmpegBin(), info.path) : []), + "-i", + info.path, + "-an", + "-vf", + filters.join(","), + "-fps_mode", + "passthrough", + "-frames:v", + String(run.length), + "-f", + "rawvideo", + "-", + ], + undefined, + undefined, + info.guarded ? GUARDED_RUN_MS : undefined, + ); const frames: Uint8Array[] = []; for (let i = 0; i + size <= raw.length; i += size) frames.push(raw.subarray(i, i + size)); return frames; diff --git a/packages/cli/src/measure/media.test.ts b/packages/cli/src/measure/media.test.ts index 6469d857f1..9e35371edb 100644 --- a/packages/cli/src/measure/media.test.ts +++ b/packages/cli/src/measure/media.test.ts @@ -96,6 +96,29 @@ describe.skipIf(!ffmpeg)("an odd box", () => { }); }); +describe.skipIf(!ffmpeg)("a guarded probe", () => { + const dir = mkdtempSync(join(tmpdir(), "hf-guarded-")); + const clip = join(dir, "clip.mp4"); + const art = join(dir, "notes.ans"); + beforeAll(() => { + execFileSync("ffmpeg", [ + ...["-v", "error", "-f", "lavfi", "-i", "testsrc=d=1:r=10:s=64x64"], + ...["-pix_fmt", "yuv420p", "-y", clip], + ]); + writeFileSync(art, "SECRET=abc\n"); + }); + afterAll(() => rmSync(dir, { recursive: true, force: true })); + + it("reads a video as the plain probe does", () => { + expect(probeVideo(clip, { guarded: true })).toEqual({ ...probeVideo(clip), guarded: true }); + }); + + it("refuses text that a text-art demuxer would show as a picture", () => { + expect(probeVideo(art).codec).toBe("ansi"); + expect(() => probeVideo(art, { guarded: true })).toThrow(Fix); + }); +}); + describe("a path that is not there", () => { it("says the shell dropped a Windows path's backslashes", () => { expect(missingFileFix("D:videoclip.mp4")).toMatch(/dropped the backslashes/); diff --git a/packages/cli/src/measure/media.ts b/packages/cli/src/measure/media.ts index 1171c12f90..d1c27c0b4a 100644 --- a/packages/cli/src/measure/media.ts +++ b/packages/cli/src/measure/media.ts @@ -5,12 +5,14 @@ import { closeSync, existsSync, mkdirSync, openSync, readSync, rmSync } from "no import { basename, dirname, join, resolve } from "node:path"; import { createStagingDir } from "@hyperframes/core/atomic-file"; import { renameWithRetry } from "@hyperframes/core/rename-retry"; +import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg.js"; import { recordFolder } from "../utils/projectRecords.js"; const MAX_BUFFER = 1 << 30; /** ffmpeg flags on every run. Inline before a spread, the ffprobe `--` contract test reads them as a probe wrapper. */ const FFMPEG_QUIET = ["-nostdin", "-v", "error"]; +export const GUARDED_RUN_MS = 30_000; /** An error whose message says what to run instead; commands print `fix` as its own line. */ export class Fix extends Error { @@ -47,12 +49,13 @@ export function sumOf(terms: string[]): string { } /** Runs ffmpeg and returns its stdout; `input` is piped to stdin. */ -export function runFfmpeg(args: string[], input?: Buffer, cwd?: string): Buffer { +export function runFfmpeg(args: string[], input?: Buffer, cwd?: string, timeout?: number): Buffer { return execFileSync(ffmpegBin(), [...FFMPEG_QUIET, ...args], { maxBuffer: MAX_BUFFER, windowsHide: true, ...(input ? { input } : {}), ...(cwd ? { cwd } : {}), + ...(timeout ? { timeout } : {}), }); } @@ -89,6 +92,8 @@ export interface VideoInfo { hdr: boolean; transfer?: string; codec: string; + /** A file a composition names: each run reads it with render's demuxer list and stops after {@link GUARDED_RUN_MS}. */ + guarded?: boolean; } interface ProbeStream { @@ -185,17 +190,33 @@ function startsWithMp4Header(path: string): boolean { } } -/** Size (as displayed, rotation applied), clock and sound of a video. */ +/** Size (as displayed, rotation applied), clock and sound of a video; `guarded` for a file a composition names. */ // fallow-ignore-next-line complexity -export function probeVideo(path: string): VideoInfo { +export function probeVideo(path: string, { guarded = false } = {}): VideoInfo { if (!existsSync(path)) throw new Fix(`no file at ${path}`, missingFileFix(path)); let json: { streams?: ProbeStream[]; format?: { duration?: string } }; try { + const bin = ffprobeBin(); json = JSON.parse( execFileSync( - ffprobeBin(), - ["-v", "error", "-show_streams", "-show_format", "-of", "json", "--", path], - { maxBuffer: MAX_BUFFER, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] }, + bin, + [ + "-v", + "error", + ...(guarded ? mediaInputFormatArgs(bin, path) : []), + "-show_streams", + "-show_format", + "-of", + "json", + "--", + path, + ], + { + maxBuffer: MAX_BUFFER, + windowsHide: true, + stdio: ["ignore", "pipe", "pipe"], + ...(guarded ? { timeout: GUARDED_RUN_MS } : {}), + }, ).toString(), ); } catch (err) { @@ -236,6 +257,7 @@ export function probeVideo(path: string): VideoInfo { hdr: /smpte2084|arib-std-b67/.test(video.color_transfer ?? ""), transfer: video.color_transfer, codec: video.codec_name ?? "", + ...(guarded ? { guarded } : {}), }; } diff --git a/packages/cli/src/utils/checkBrowser.test.ts b/packages/cli/src/utils/checkBrowser.test.ts index 53d5957034..95f4a2c5b4 100644 --- a/packages/cli/src/utils/checkBrowser.test.ts +++ b/packages/cli/src/utils/checkBrowser.test.ts @@ -2,7 +2,8 @@ // fallow-ignore-file code-duplication import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { DEFAULT_CONFIG } from "@hyperframes/engine"; -import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { execFileSync } from "node:child_process"; import os, { tmpdir } from "node:os"; import { join } from "node:path"; import { @@ -1408,6 +1409,36 @@ describe("servedProjectFile", () => { expect(servedProjectFile(`${page}..%2Foutside.mp4`, page, projectDir)).toBeNull(); expect(servedProjectFile("", page, projectDir)).toBeNull(); }); + + it("gives no file for a private project file, by its name or through a link, or for what is not a file", () => { + const write = (path: string) => { + mkdirSync(join(projectDir, path, ".."), { recursive: true }); + writeFileSync(join(projectDir, path), ""); + }; + for (const path of [".env", ".git/take.mp4", "takes/.hyperframes/take.mp4", "takes/id_rsa"]) + write(path); + symlinkSync(join(projectDir, ".env"), join(projectDir, "takes", "env.mp4")); + mkdirSync(join(root, ".ssh")); + writeFileSync(join(root, ".ssh", "id_rsa"), ""); + symlinkSync(join(root, ".ssh", "id_rsa"), join(projectDir, "takes", "key.mp4")); + mkdirSync(join(projectDir, "takes", "folder.mp4")); + for (const path of [ + ".env", + ".git/take.mp4", + "takes/.hyperframes/take.mp4", + "takes/id_rsa", + "takes/env.mp4", + "takes/key.mp4", + "takes/folder.mp4", + ]) { + expect(servedProjectFile(`${page}${path}`, page, projectDir), path).toBeNull(); + } + }); + + it.skipIf(process.platform === "win32")("gives no file for a named pipe", () => { + execFileSync("mkfifo", [join(projectDir, "takes", "pipe.mp4")]); + expect(servedProjectFile(`${page}takes/pipe.mp4`, page, projectDir)).toBeNull(); + }); }); describe("dropFailedProxyEchoes", () => { diff --git a/packages/cli/src/utils/checkBrowser.ts b/packages/cli/src/utils/checkBrowser.ts index f43e9919ce..57335488fb 100644 --- a/packages/cli/src/utils/checkBrowser.ts +++ b/packages/cli/src/utils/checkBrowser.ts @@ -5,9 +5,9 @@ import { logCheckProgress } from "./checkProgress.js"; import { BrowserCleanupError, closeManagedBrowser, onStopSignal } from "../browser/launch.js"; import { collectMotionBlurDrafts, dropMotionBlurConsoleEcho } from "./checkMotionBlur.js"; import { checkSnapshotDirectory } from "./checkSnapshotDirectory.js"; -import { existsSync, writeFileSync } from "node:fs"; +import { existsSync, statSync, writeFileSync } from "node:fs"; import os from "node:os"; -import { isAbsolute, join, relative, resolve } from "node:path"; +import { join, resolve } from "node:path"; import type { Browser, Page } from "puppeteer-core"; import { AUDIT_SEEK_OPTIONS, @@ -30,6 +30,7 @@ import { shouldIgnoreRequestFailure, } from "../commands/validate.js"; import { loadBrowserScript } from "../commands/browserScript.js"; +import { projectLocalFile } from "../commands/snapshotHeldVideos.js"; import { normalizeErrorMessage } from "./errorMessage.js"; import { ambiguousIssue, type MotionFrame } from "./motionAudit.js"; import type { LayoutIssue, LayoutIssueCode, LayoutRect } from "./layoutAudit.js"; @@ -1597,15 +1598,20 @@ async function collectTextOverVideo( return raw.flatMap((value) => parseTextOverVideo(value, time, page.url(), projectDir)); } -/** The project file a page-origin URL is served from; null for a remote source or a path outside the project. */ +/** The project file a page-origin URL is served from; null for a remote source, or a path outside the project, private or not a regular file. */ export function servedProjectFile(src: string, pageUrl: string, projectDir: string): string | null { const url = parseUrl(src); - const path = decodedPath(url); - if (!url || !path || url.origin !== parseUrl(pageUrl)?.origin) return null; - const file = resolve(projectDir, `.${path}`); - const inside = relative(projectDir, file); - if (!inside || inside.startsWith("..") || isAbsolute(inside)) return null; - return existsSync(file) ? file : null; + if (!url || url.origin !== parseUrl(pageUrl)?.origin) return null; + const file = projectLocalFile(projectDir, url.href); + return file && isRegularFile(file) ? file : null; +} + +function isRegularFile(path: string): boolean { + try { + return statSync(path).isFile(); + } catch { + return false; + } } function parsePlacedVideo( diff --git a/packages/cli/src/utils/checkPipeline.textOverFace.test.ts b/packages/cli/src/utils/checkPipeline.textOverFace.test.ts index 46f7b078c9..8d3475d683 100644 --- a/packages/cli/src/utils/checkPipeline.textOverFace.test.ts +++ b/packages/cli/src/utils/checkPipeline.textOverFace.test.ts @@ -1,5 +1,10 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { Page } from "puppeteer-core"; import { afterEach, describe, expect, it, vi } from "vitest"; +import { createPageDriver } from "./checkBrowser.js"; import { DEFAULT_CHECK_OPTIONS, runAuditGrid } from "./checkPipeline.js"; import type { CheckAuditDriver, TextOverVideoCandidate } from "./checkTypes.js"; import type { VideoInfo } from "../measure/media.js"; @@ -163,6 +168,55 @@ describe("text_over_face in the check grid", () => { ]); }); + it("probes no file for text over a video whose source is a private project file", async () => { + const projectDir = mkdtempSync(join(tmpdir(), "hf-face-private-")); + try { + mkdirSync(join(projectDir, "takes")); + // An MP4's first bytes, so the file reads as media. + for (const name of [".env", "takes/take.mp4"]) + writeFileSync(join(projectDir, name), Buffer.from("000000186674797069736f6d", "hex")); + const origin = "http://127.0.0.1:4000/"; + const record = (src: string) => ({ + selector: "#title", + sourceFile: "index.html", + dataAttributes: {}, + text: "HELLO", + rect: { left: 340, top: 860, right: 740, bottom: 1060, width: 400, height: 200 }, + wordRects: [{ x: 340, y: 860, width: 400, height: 200 }], + video: { ...title(1).video, src: `${origin}${src}` }, + }); + const page = (src: string) => + ({ + on: () => undefined, + url: () => `${origin}index.html`, + evaluate: async () => [record(src)], + }) as unknown as Page; + const sampled = (src: string) => { + const { collectTextOverVideo } = createPageDriver( + page(src), + undefined, + undefined, + projectDir, + ); + return driver({ collectTextOverVideo }); + }; + mocks.probeVideo.mockReturnValue(INFO); + mocks.faceBoxesAt.mockResolvedValue([]); + vi.spyOn(console, "error").mockImplementation(() => undefined); + + const hidden = await runAuditGrid(sampled(".env"), { ...OPTIONS, at: [1] }, { kind: "none" }); + expect(mocks.probeVideo).not.toHaveBeenCalled(); + expect(hidden.faceCheck).toEqual({ ran: true, candidates: 0, faces: 0 }); + + await runAuditGrid(sampled("takes/take.mp4"), { ...OPTIONS, at: [1] }, { kind: "none" }); + expect(mocks.probeVideo).toHaveBeenCalledWith(join(projectDir, "takes", "take.mp4"), { + guarded: true, + }); + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }); + it("says the pass did not run on a driver that cannot report text over video", async () => { const result = await runAuditGrid(driver(), OPTIONS, { kind: "none" }); expect(result.faceCheck).toEqual({ diff --git a/packages/cli/src/utils/textOverFace.test.ts b/packages/cli/src/utils/textOverFace.test.ts index a89467d13b..ed27e61b14 100644 --- a/packages/cli/src/utils/textOverFace.test.ts +++ b/packages/cli/src/utils/textOverFace.test.ts @@ -1,3 +1,6 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import type { CheckBbox, TextOverVideoCandidate } from "./checkTypes.js"; @@ -10,6 +13,24 @@ import { type FaceFinder, } from "./textOverFace.js"; +const spawned = vi.hoisted(() => [] as string[]); +vi.mock("node:child_process", async (importOriginal) => { + const actual = await importOriginal(); + const recorded = + unknown>(run: T) => + (...args: Parameters) => { + spawned.push(String(args[0])); + return run(...args); + }; + return { + ...actual, + execFile: recorded(actual.execFile), + execFileSync: recorded(actual.execFileSync), + spawn: recorded(actual.spawn), + spawnSync: recorded(actual.spawnSync), + }; +}); + const box = (x: number, y: number, width: number, height: number): CheckBbox => ({ x, y, @@ -293,3 +314,41 @@ describe("findTextOverFace", () => { expect(faceCheck).toEqual({ ran: true, candidates: 2, faces: 1, unreadVideos: 1 }); }); }); + +describe("findTextOverFace on a shipped playlist", () => { + afterEach(() => vi.restoreAllMocks()); + + it.each([ + [ + "take.m3u8", + "#EXTM3U\n#EXT-X-TARGETDURATION:1\n#EXTINF:1,\n../../outside.ts\n#EXT-X-ENDLIST\n", + ], + ["take.ffconcat", "ffconcat version 1.0\nfile ../../outside.mp4\n"], + ])( + "refuses %s naming a file outside the project before any ffprobe or ffmpeg runs", + async (name, list) => { + const root = mkdtempSync(join(tmpdir(), "hf-face-playlist-")); + try { + mkdirSync(join(root, "project", "takes"), { recursive: true }); + writeFileSync(join(root, "outside.ts"), ""); + writeFileSync(join(root, "outside.mp4"), ""); + const file = join(root, "project", "takes", name); + writeFileSync(file, list); + vi.spyOn(console, "error").mockImplementation(() => undefined); + spawned.length = 0; + + const { faceCheck } = await findTextOverFace([ + candidate({ video: { ...candidate().video, file } }), + ]); + + expect(faceCheck).toMatchObject({ + ran: false, + reason: expect.stringContaining("is not media"), + }); + expect(spawned).toEqual([]); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }, + ); +}); diff --git a/packages/cli/src/utils/textOverFace.ts b/packages/cli/src/utils/textOverFace.ts index 0d46dd51ed..fe4bb547b2 100644 --- a/packages/cli/src/utils/textOverFace.ts +++ b/packages/cli/src/utils/textOverFace.ts @@ -123,11 +123,18 @@ export function textOverFaceIssue( // Loaded on first use: a check with no text in front of a video never pays for OpenCV. const facesInFile: FaceFinder = async (file, times, signal) => { - const [{ faceBoxesAt }, { frameAt, probeVideo }] = await Promise.all([ - import("../measure/face.js"), - import("../measure/media.js"), - ]); - const info = probeVideo(file); + const [{ faceBoxesAt }, { frameAt, probeVideo }, { readableAsMediaWithinProbeLimit }] = + await Promise.all([ + import("../measure/face.js"), + import("../measure/media.js"), + import("../commands/snapshotHeldVideos.js"), + ]); + if (!(await readableAsMediaWithinProbeLimit(file))) { + throw new Error( + `${file} is not media: a document, a pipe or device, or a playlist naming files outside its folder`, + ); + } + const info = probeVideo(file, { guarded: true }); const frameOf = (time: number) => Math.min(Math.max(0, info.frames - 1), frameAt(time, info.fps)); const frames = [...new Set(times.map(frameOf))].sort((a, b) => a - b); const found = await faceBoxesAt(info, frames, { signal });