From ead110aab9e4b43befdbe21001deac168c09e04b Mon Sep 17 00:00:00 2001 From: Vance Ingalls Date: Mon, 16 Nov 2026 00:35:56 -0800 Subject: [PATCH] fix(engine,producer,cli): media inputs refuse formats that open other files (#7931) * fix(engine,producer): media inputs refuse formats that open other files Composition media read by ffprobe/ffmpeg now passes a format allowlist on every input: the demuxers the installed ffmpeg reports, minus the ones that open other files or URLs on their own (DASH, IMF and similar). hls and concat stay allowed, since ffmpeg carries the list into every file they open. The HDR window, alpha-plane, GIF, HEIC, audio-sidecar and snapshot reads that skipped it now use it. The not-media check skips ID3v2 tags before it looks at the first byte, refuses a playlist entry that is a document, and refuses JSON or non-SVG markup as an src. Co-Authored-By: Claude Opus 5.5 * fix(engine,parsers,core,lint,cli): format allowlist reads old listings and covers every media probe The demuxer listing is read from the binary that enforces the flag, accepts ffmpeg 4's two-column output, and a failed listing is asked again next time. The lint HEVC probe, the grade analyzer and the snapshot probes use it too. A nested playlist entry is refused only for a real document signature, so encrypted segments and keys are kept; an image whose prolog outruns the sniff window, or that is UTF-16, is no longer refused. Co-Authored-By: Claude Opus 5.5 * chore(parsers,producer): trim comments added with the media format list Co-Authored-By: Claude Opus 5.5 * test(engine): keep the format-limit probe's argv in the shape the ffprobe contract reads Co-Authored-By: Claude Opus 5.5 * fix(engine): the image markup check reads a doctype without regex backtracking Co-Authored-By: Claude Opus 5.5 * chore(parsers): raise the parsers size budget for the media format list Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- packages/cli/src/commands/snapshot.ts | 19 +++- .../cli/src/commands/snapshotHeldVideos.ts | 2 + packages/core/src/mediaGradeAnalyzer.ts | 3 + packages/engine/src/index.ts | 3 + packages/engine/src/utils/alphaPlaneProbe.ts | 2 + .../engine/src/utils/notMediaPayload.test.ts | 54 +++++++++++ packages/engine/src/utils/notMediaPayload.ts | 97 +++++++++++++++++-- packages/engine/src/utils/urlInput.test.ts | 87 +++++++++++++++-- packages/engine/src/utils/urlInput.ts | 13 +-- packages/lint/src/hevcPreviewLint.ts | 4 +- .../src/hevcPreviewLint.windowsHide.test.ts | 1 + packages/parsers/src/ffBinaries.test.ts | 36 +++++++ packages/parsers/src/ffBinaries.ts | 68 +++++++++++++ .../producer/src/services/animatedGifPrep.ts | 2 + .../src/services/assetMediaType.test.ts | 8 ++ .../producer/src/services/assetMediaType.ts | 5 +- .../src/services/distributed/audioSidecar.ts | 22 +++-- .../producer/src/services/heifImagePrep.ts | 3 +- .../src/services/mediaInputFormats.test.ts | 55 +++++++++++ .../render/stages/captureHdrResources.test.ts | 1 + .../render/stages/captureHdrResources.ts | 13 ++- scripts/package-size-budgets.json | 2 +- 22 files changed, 464 insertions(+), 36 deletions(-) create mode 100644 packages/producer/src/services/mediaInputFormats.test.ts diff --git a/packages/cli/src/commands/snapshot.ts b/packages/cli/src/commands/snapshot.ts index 53c1d11f7a..d107e6fff0 100644 --- a/packages/cli/src/commands/snapshot.ts +++ b/packages/cli/src/commands/snapshot.ts @@ -10,6 +10,7 @@ import { isHdrColorSpace, sdrCanvasColourFilters, } from "@hyperframes/engine"; +import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; import { screenshotOnceMore } from "../utils/screenshotOnceMore.js"; import { parseTimeoutMs } from "../utils/timeoutArg.js"; import type { Page } from "puppeteer-core"; @@ -491,7 +492,8 @@ export async function probeContainingSourceFrameSeek( const { stdout } = await runCancellableProcess( ffprobe, [ - ...["-v", "error", "-select_streams", "v:0", "-read_intervals", readIntervals], + ...["-v", "error", ...mediaInputFormatArgs(ffprobe)], + ...["-select_streams", "v:0", "-read_intervals", readIntervals], ...["-show_entries", entries, "-of", "json", "--", videoPath], ], { timeoutMs: FFMPEG_EXTRACT_TIMEOUT_MS, maxBufferBytes: 32 * 1024 * 1024 }, @@ -615,7 +617,8 @@ async function probeSeekLanding( const { stdout } = await runCancellableProcess( ffmpeg, [ - ...["-v", "error", "-copyts", "-noaccurate_seek", "-ss", String(seek), "-i", videoPath], + ...["-v", "error", "-copyts", "-noaccurate_seek", ...mediaInputFormatArgs(ffmpeg)], + ...["-ss", String(seek), "-i", videoPath], ...["-map", "0:v:0", "-c", "copy", "-frames:v", "1", "-f", "framecrc", "-"], ], { timeoutMs: FFMPEG_EXTRACT_TIMEOUT_MS, maxBufferBytes: 1024 * 1024 }, @@ -672,7 +675,12 @@ async function frameColour( videoPath: string, codec: string, ): Promise { - const read = (args: string[]) => runFfmpegOnce(ffmpegPath, args, FFMPEG_EXTRACT_TIMEOUT_MS); + const read = (args: string[]) => + runFfmpegOnce( + ffmpegPath, + [...mediaInputFormatArgs(ffmpegPath), ...args], + FFMPEG_EXTRACT_TIMEOUT_MS, + ); const probe = await read(firstFrameColourArgs(videoPath)); const first = parseFirstFrameColour(probe.stderr); if (probe.code !== 0 || !readsKeyframeTransfers(first)) return first; @@ -724,7 +732,7 @@ export async function extractVideoFrameToBuffer( const args = ["-hide_banner", "-loglevel", "error"]; if (useVp9AlphaDecoder) args.push("-c:v", "libvpx-vp9"); if (seek.seek !== undefined) args.push("-ss", String(seek.seek)); - args.push("-i", videoPath, "-map", "0:v:0"); + args.push(...mediaInputFormatArgs(ffmpegPath), "-i", videoPath, "-map", "0:v:0"); const filters = frameFilters(seek.select, displayedPixels, metadata, toneMapFilter); if (filters.length > 0) args.push("-vf", filters.join(",")); args.push("-frames:v", "1", "-q:v", "2", "-y", outPath); @@ -785,7 +793,8 @@ export async function decodeFramesInOnePass( const filters = [selectFirstFrameAfterEach(byTime.map(([offset]) => offset)), ...rest]; const args = ["-hide_banner", "-loglevel", "error"]; if (useVp9AlphaDecoder) args.push("-c:v", "libvpx-vp9"); - args.push("-ss", String(start), "-i", videoPath, "-map", "0:v:0", "-vf", filters.join(",")); + args.push(...mediaInputFormatArgs(ffmpegPath), "-ss", String(start), "-i", videoPath); + args.push("-map", "0:v:0", "-vf", filters.join(",")); args.push("-fps_mode", "passthrough", "-frames:v", String(byTime.length)); args.push("-q:v", "2", "-y", join(tmp, "%d.png")); return args; diff --git a/packages/cli/src/commands/snapshotHeldVideos.ts b/packages/cli/src/commands/snapshotHeldVideos.ts index 2007282363..f1f7d2b55a 100644 --- a/packages/cli/src/commands/snapshotHeldVideos.ts +++ b/packages/cli/src/commands/snapshotHeldVideos.ts @@ -3,6 +3,7 @@ import { isAbsolute, relative, resolve } from "node:path"; import { parseHTML } from "linkedom"; import { MEDIA_RENDER_ID_ATTR } from "@hyperframes/core"; 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"; @@ -112,6 +113,7 @@ export function probeHeldVideo( [ "-v", "error", + ...mediaInputFormatArgs(ffprobe), "-select_streams", "v:0", "-show_entries", diff --git a/packages/core/src/mediaGradeAnalyzer.ts b/packages/core/src/mediaGradeAnalyzer.ts index 87545dcab5..e564c2a855 100644 --- a/packages/core/src/mediaGradeAnalyzer.ts +++ b/packages/core/src/mediaGradeAnalyzer.ts @@ -1,5 +1,6 @@ import { execFileSync } from "node:child_process"; import { basename, extname } from "node:path"; +import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; const IMAGE_EXTENSIONS = new Set([ ".jpg", @@ -115,6 +116,7 @@ function probeMedia(mediaPath: string, ffprobePath: string): GradeMediaProbe { [ "-v", "error", + ...mediaInputFormatArgs(ffprobePath), "-select_streams", "v:0", "-show_entries", @@ -320,6 +322,7 @@ function readSignalStats( "-nostdin", "-v", "error", + ...mediaInputFormatArgs(ffmpegPath), ...keyframeSeek, "-i", mediaPath, diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index a884f95af1..0aaca8b949 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -414,6 +414,8 @@ export { type KeyframeAnalysis, } from "./utils/ffprobe.js"; +export { urlInputArgs } from "./utils/urlInput.js"; + export { mediaIdPathSegment } from "./utils/mediaPathSegment.js"; export { @@ -422,6 +424,7 @@ export { assertMediaPayload, playlistLeavesItsFolder, fingerprintElementId, + isNotImagePayload, isNotMediaPayload, } from "./utils/notMediaPayload.js"; diff --git a/packages/engine/src/utils/alphaPlaneProbe.ts b/packages/engine/src/utils/alphaPlaneProbe.ts index b963ca1156..f6bf3828d0 100644 --- a/packages/engine/src/utils/alphaPlaneProbe.ts +++ b/packages/engine/src/utils/alphaPlaneProbe.ts @@ -2,6 +2,7 @@ import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { runFfmpeg } from "./runFfmpeg.js"; +import { urlInputArgs } from "./urlInput.js"; // Up to 3 frames at 8x8 rgba; a 1- or 2-frame clip is still a whole sample. const BYTES_PER_SAMPLE_FRAME = 8 * 8 * 4; @@ -39,6 +40,7 @@ export async function probeInputAlphaPlane( [ "-v", "error", + ...urlInputArgs(videoPath), "-c:v", decoder, "-i", diff --git a/packages/engine/src/utils/notMediaPayload.test.ts b/packages/engine/src/utils/notMediaPayload.test.ts index 66adabe092..0d17055c60 100644 --- a/packages/engine/src/utils/notMediaPayload.test.ts +++ b/packages/engine/src/utils/notMediaPayload.test.ts @@ -7,6 +7,7 @@ import { getFfprobeBinary } from "./ffmpegBinaries.js"; import { assertMediaPayload, fingerprintElementId, + isNotImagePayload, isNotMediaPayload, NotMediaPayloadError, playlistLeavesItsFolder, @@ -190,6 +191,45 @@ describe("isNotMediaPayload", () => { }); }); +const MPD = ''; +// One ID3v2.4 tag with a 4-byte body, as ffmpeg skips it before it probes. +const id3 = Buffer.from([0x49, 0x44, 0x33, 4, 0, 0, 0, 0, 0, 4, 1, 2, 3, 4]); + +describe("a file behind an ID3 tag", () => { + it("is refused as media when markup follows the tag", async () => { + const file = writeFixture("tagged.mp3", Buffer.concat([id3, Buffer.from(MPD)])); + expect(await isNotMediaPayload(file)).toBe(true); + expect(await isNotImagePayload(file)).toBe(true); + }); + + it("is kept as media when an MPEG frame follows the tag", async () => { + const file = writeFixture("song.mp3", Buffer.concat([id3, Buffer.from([0xff, 0xfb, 0x90, 0])])); + expect(await isNotMediaPayload(file)).toBe(false); + }); +}); + +describe("isNotImagePayload", () => { + it.each([ + ["a manifest", MPD, true], + ["json", '{"error": "gone"}', true], + [ + "an svg behind a prolog, comment and doctype", + `\n\n]>\n`, + false, + ], + ["an svg whose comment outruns the sniff window", ``, false], + [ + "a utf-16 svg", + Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from("", "utf16le")]), + false, + ], + ["an svg with a long doctype subset", ``, false], + ["a png", Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), false], + ])("for %s", async (_label, contents, refused) => { + expect(await isNotImagePayload(writeFixture("image", contents))).toBe(refused); + }); +}); + describe("assertMediaPayload", () => { it("throws NotMediaPayloadError carrying routing metadata and a hashed element key", async () => { const filePath = writeFixture("nested.html", ""); @@ -284,6 +324,20 @@ describe("playlistLeavesItsFolder", () => { expect(await isNotMediaPayload(file)).toBe(true); }); + it("refuses a list whose nested entry is a manifest, and keeps one that is not there", async () => { + const list = "ffconcat version 1.0\nfile manifest.mpd\n"; + expect( + await playlistLeavesItsFolder(writeTree({ "clip.mp4": list, "manifest.mpd": MPD })), + ).toBe(true); + expect(await playlistLeavesItsFolder(writeTree({ "clip.mp4": list }))).toBe(false); + const encrypted = hls('#EXT-X-KEY:METHOD=AES-128,URI="key.bin"', "#EXTINF:1,", "seg.ts"); + expect( + await playlistLeavesItsFolder( + writeTree({ "clip.m3u8": encrypted, "key.bin": "<\x01\x02", "seg.ts": "{\x00" }), + ), + ).toBe(false); + }); + it("refuses a nested list naming a private file from its own folder", async () => { const master = writeTree({ "master.m3u8": "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1\nsub/inner.m3u8\n", diff --git a/packages/engine/src/utils/notMediaPayload.ts b/packages/engine/src/utils/notMediaPayload.ts index e3206f4ef9..b9d58b047e 100644 --- a/packages/engine/src/utils/notMediaPayload.ts +++ b/packages/engine/src/utils/notMediaPayload.ts @@ -124,13 +124,17 @@ async function openRegular(path: string | Buffer): Promise { return null; } -/** Read up to {@link SNIFF_BYTES} from the front of the file, with its length; null when it is not a regular file. */ +/** Read up to {@link SNIFF_BYTES} from where ffmpeg starts reading (past ID3v2 tags), with the length from there; null when it is not a regular file or its tags never end. */ async function readHead(filePath: string): Promise<{ head: Buffer; fileSize: number } | null> { const fh = await openRegular(filePath); if (!fh) return null; try { const { size } = await fh.stat(); - return { head: await readAt(fh, SNIFF_BYTES, 0), fileSize: size }; + const first = await readAt(fh, SNIFF_BYTES, 0); + const start = await textStart(fh, first); + if (start < 0) return null; + const head = start === 0 ? first : await readAt(fh, SNIFF_BYTES, start); + return { head, fileSize: size - start }; } finally { await fh.close().catch(() => {}); } @@ -234,6 +238,79 @@ export async function assertMediaPayload(filePath: string, elementId: string): P } } +const IMAGE_SNIFF_BYTES = 64 * 1024; +function withoutLeadingBlanks(text: string): string { + let at = 0; + while (at < text.length && (text[at] === "\0" || /\s/.test(text[at]!))) at++; + return text.slice(at); +} + +/** The head as text: UTF-16 behind its byte order mark is decoded, anything else read byte per character. */ +function headText(head: Buffer): string { + const [first, second] = head; + if (first === 0xff && second === 0xfe) return head.subarray(2).toString("utf16le"); + if (first === 0xfe && second === 0xff) { + return Buffer.from(head.subarray(2, head.length - (head.length % 2))) + .swap16() + .toString("utf16le"); + } + return head.toString("latin1", byteOrderMarkLength(head)); +} + +function afterMarker(text: string, from: number, end: string): string | null { + const at = text.indexOf(end, from); + return at < 0 ? null : text.slice(at + end.length); +} + +/** The text after a doctype, whose internal subset in brackets may itself hold `>`. */ +function afterDoctype(text: string): string | null { + for (let at = 9; at < text.length; at++) { + if (text[at] === ">") return text.slice(at + 1); + if (text[at] === "[") at = text.indexOf("]", at); + if (at < 0) return null; + } + return null; +} + +/** The text after one leading prolog, comment or doctype; null when none opens it or it never ends. */ +function afterProlog(text: string): string | null { + if (text.startsWith(""); + if (text.startsWith(""); + return /^]/i.test(rest) || (!complete && /^<[?!]/.test(rest)); +} + +/** + * True when an `` src opens as JSON or markup other than an SVG image, after the ID3v2 tags, byte order mark + * and blanks ffmpeg and a browser skip. A binary image passes. Like {@link isNotMediaPayload}, never throws. + */ +export async function isNotImagePayload(filePath: string): Promise { + try { + const fh = await openRegular(filePath); + if (!fh) return true; + try { + const start = await textStart(fh, await readAt(fh, SNIFF_BYTES, 0)); + if (start < 0) return true; + const head = await readAt(fh, IMAGE_SNIFF_BYTES, start); + const text = withoutLeadingBlanks(headText(head)); + if (text.startsWith("{") || text.startsWith("[")) return true; + return text.startsWith("<") && !opensWithSvgRoot(text, head.length < IMAGE_SNIFF_BYTES); + } finally { + await fh.close().catch(() => {}); + } + } catch { + return false; + } +} + const MAX_PLAYLIST_BYTES = 8 * 1024 * 1024; const MAX_PLAYLIST_DEPTH = 8; const HTTP_URL = /^https?:\/\//i; @@ -380,6 +457,13 @@ async function textStart(fh: FileHandle, first: Buffer): Promise { return -1; } +// Encrypted segments and keys open with random bytes, so only a real document signature counts. +const MANIFEST_SIGNATURE = /^<(\?xml|MPD\b|!DOCTYPE|html\b|SmoothStreamingMedia\b)/i; + +function opensAsManifest(head: Buffer): boolean { + return MANIFEST_SIGNATURE.test(withoutLeadingBlanks(headText(head))); +} + function startsAsPlaylist(head: Buffer): boolean { const text = head .toString("utf8") @@ -388,15 +472,16 @@ function startsAsPlaylist(head: Buffer): boolean { return text.startsWith("#EXTM3U") || /^ffconcat\s+version/i.test(text); } -/** The list's text as latin1, null when the file is not a list, true when it must be refused unread. One open. */ -async function readList(path: string): Promise { +/** The list's text as latin1, null when the file is not a list, true when it must be refused unread (a document, when `nested`). One open. */ +async function readList(path: string, nested: boolean): Promise { const fh = await openRegular(Buffer.from(path, "latin1")); if (!fh) return true; try { const first = await readAt(fh, SNIFF_BYTES, 0); const start = await textStart(fh, first); if (start < 0) return true; - if (!startsAsPlaylist(start === 0 ? first : await readAt(fh, SNIFF_BYTES, start))) return null; + const head = start === 0 ? first : await readAt(fh, SNIFF_BYTES, start); + if (!startsAsPlaylist(head)) return nested && opensAsManifest(head) ? true : null; const body = await readAt(fh, MAX_PLAYLIST_BYTES + 1, start); return body.length > MAX_PLAYLIST_BYTES ? true : body.toString("latin1"); } finally { @@ -416,7 +501,7 @@ async function listLeaves( walk: Walk, depth: number, ): Promise { - const text = await readList(readPath).catch((error: unknown) => + const text = await readList(readPath, depth > 0).catch((error: unknown) => depth > 0 && !isMissing(error) ? true : null, ); if (text === null) return false; diff --git a/packages/engine/src/utils/urlInput.test.ts b/packages/engine/src/utils/urlInput.test.ts index 472b69e6a3..57c53284ae 100644 --- a/packages/engine/src/utils/urlInput.test.ts +++ b/packages/engine/src/utils/urlInput.test.ts @@ -2,15 +2,30 @@ import { spawnSync } from "node:child_process"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { describe, expect, it, vi } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { VideoSourceExtractionError } from "../services/videoFrameExtractor.js"; import { getFfmpegBinary, getFfprobeBinary } from "./ffmpegBinaries.js"; +import { _setDemuxerListingForTests } from "@hyperframes/parsers/ff-binaries"; import { redactUrlQueriesInError, urlInputArgs } from "./urlInput.js"; const signed = "https://bucket.s3.us-east-2.amazonaws.com/a/b?X-Amz-Credential=AKID&X-Amz-Signature=deadbeef"; +const LISTING = [ + " ..d = Is a device", + " D concat Virtual concatenation script", + " D dash Dynamic Adaptive Streaming over HTTP", + " D hls Apple HTTP Live Streaming", + " D imf IMF (Interoperable Master Format)", + " D d lavfi Libavfilter virtual input device", + " D mov,mp4,m4a QuickTime / MOV", +].join("\n"); + +const formatsOf = (args: string[]) => args[args.indexOf("-format_whitelist") + 1]!.split(","); + describe("urlInputArgs", () => { + afterEach(() => _setDemuxerListingForTests()); + const caBundles = ["/nonexistent.crt", import.meta.filename]; const verifyAndReconnect = [ "-tls_verify", @@ -25,16 +40,22 @@ describe("urlInputArgs", () => { "2", ]; + const anyDemuxers = ["-format_whitelist", expect.any(String)]; + it("makes ffmpeg verify the certificate of an https input and reconnect on network errors", () => { expect(urlInputArgs(signed, { caBundles })).toEqual([ ...verifyAndReconnect, "-reconnect_on_network_error", "1", + ...anyDemuxers, ]); }); it("gives ffprobe only the options its 4.0 build accepts", () => { - expect(urlInputArgs(signed, { tool: "ffprobe", caBundles })).toEqual(verifyAndReconnect); + expect(urlInputArgs(signed, { tool: "ffprobe", caBundles })).toEqual([ + ...verifyAndReconnect, + ...anyDemuxers, + ]); }); it.each(["true", "1", " TRUE\n"])( @@ -49,7 +70,6 @@ describe("urlInputArgs", () => { "-format_whitelist", expect.any(String), ]); - expect(urlInputArgs("/plan/compiled/bg.mp4", { caBundles: [] })).toEqual([]); } finally { vi.unstubAllEnvs(); } @@ -91,11 +111,66 @@ describe("urlInputArgs", () => { }, ); - it("adds nothing for local paths and plain http", () => { - expect(urlInputArgs("/plan/compiled/bg.mp4", { caBundles: [] })).toEqual([]); - expect(urlInputArgs("http://127.0.0.1:8080/bg.mp4", { caBundles: [] })).toEqual([]); + it("lets a local path or plain http open every demuxer except the ones that open other files", () => { + _setDemuxerListingForTests(LISTING); + for (const input of ["/plan/compiled/bg.mp4", "http://127.0.0.1:8080/bg.mp4"]) { + expect(formatsOf(urlInputArgs(input, { caBundles: [] }))).toEqual([ + "concat", + "hls", + "mov", + "mp4", + "m4a", + ]); + } }); + it("falls back to the self-contained list when the demuxers cannot be listed", () => { + _setDemuxerListingForTests("not a listing"); + expect(formatsOf(urlInputArgs("/plan/compiled/bg.mp4"))).toContain("mov"); + expect(formatsOf(urlInputArgs("/plan/compiled/bg.mp4"))).not.toContain("hls"); + }); + + it.skipIf(spawnSync(getFfprobeBinary(), ["-version"]).status !== 0)( + "carries the demuxer limit into every file a concat list or playlist opens", + () => { + const formats = formatsOf(urlInputArgs("/plan/compiled/bg.mp4")); + const dir = mkdtempSync(join(tmpdir(), "hf-url-input-")); + try { + const lavfi = ["-f", "lavfi", "-i", "testsrc2=size=32x32:rate=5:duration=1"]; + spawnSync( + getFfmpegBinary(), + ["-v", "error", "-y", ...lavfi, "-c:v", "mpeg2video"].concat([ + "-f", + "mpegts", + join(dir, "seg.ts"), + ]), + ); + const lists = { + "concat.mp4": "ffconcat version 1.0\nfile seg.ts\n", + "list.m3u8": "#EXTM3U\n#EXT-X-TARGETDURATION:1\n#EXTINF:1,\nseg.ts\n#EXT-X-ENDLIST\n", + }; + for (const [name, body] of Object.entries(lists)) { + const file = join(dir, name); + writeFileSync(file, body); + const probe = (allowed: string[]) => + spawnSync(getFfprobeBinary(), [ + "-v", + "error", + "-format_whitelist", + allowed.join(","), + "-show_format", + "--", + file, + ]).status; + expect(probe(formats)).toBe(0); + expect(probe(formats.filter((format) => format !== "mpegts"))).not.toBe(0); + } + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }, + ); + it("refuses an https input when no CA bundle exists", () => { expect(() => urlInputArgs(signed, { caBundles: ["/nonexistent.crt"] })).toThrow(/no CA bundle/); }); diff --git a/packages/engine/src/utils/urlInput.ts b/packages/engine/src/utils/urlInput.ts index b2c26dde12..e42eab03b4 100644 --- a/packages/engine/src/utils/urlInput.ts +++ b/packages/engine/src/utils/urlInput.ts @@ -1,6 +1,8 @@ import { existsSync } from "node:fs"; import { redactUrlQueryStrings } from "@hyperframes/core"; +import { SELF_CONTAINED_FORMATS, mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; import { resolveRestrictPageNetwork } from "../config.js"; +import { getFfmpegBinary, getFfprobeBinary } from "./ffmpegBinaries.js"; // The Lambda nodejs image (Amazon Linux 2023) ships the first; Debian/Ubuntu the second; macOS the third. const CA_BUNDLES = [ @@ -12,14 +14,11 @@ const CA_BUNDLES = [ // Lambda's ffprobe 4.0 accepts these but not -reconnect_on_network_error; a stalled read fails after 30 s. const RECONNECT_ARGS = ["-rw_timeout", "30000000", "-reconnect", "1", "-reconnect_delay_max", "2"]; -const SELF_CONTAINED_FORMATS = - "mov,matroska,mxf,avi,mpeg,mpegts,mpegvideo,flv,asf,ogg,wav,mp3,aac,flac,aiff,gif,image2,png_pipe,jpeg_pipe,webp_pipe,bmp_pipe,tiff_pipe"; - export function selfContainedFormatArgs(): string[] { return resolveRestrictPageNetwork() ? ["-format_whitelist", SELF_CONTAINED_FORMATS] : []; } -/** ffmpeg and ffprobe skip TLS certificate checks unless told to; an https input must be verified, and on a render service be a self-contained container that names only HTTPS. */ +/** Every input of composition media reads only demuxers that open nothing by themselves. ffmpeg and ffprobe skip TLS certificate checks unless told to; an https input must be verified, and on a render service be a self-contained container that names only HTTPS. */ export function urlInputArgs( input: string, { @@ -27,7 +26,9 @@ export function urlInputArgs( caBundles = CA_BUNDLES, }: { tool?: "ffmpeg" | "ffprobe"; caBundles?: readonly string[] } = {}, ): string[] { - if (!/^https:\/\//i.test(input)) return []; + const mediaFormats = () => + mediaInputFormatArgs(tool === "ffprobe" ? getFfprobeBinary() : getFfmpegBinary()); + if (!/^https:\/\//i.test(input)) return mediaFormats(); const caFile = caBundles.find((path) => existsSync(path)); if (!caFile) { throw new Error( @@ -37,7 +38,7 @@ export function urlInputArgs( const networkRetry = tool === "ffmpeg" ? ["-reconnect_on_network_error", "1"] : []; const renderServiceLimits = resolveRestrictPageNetwork() ? ["-protocol_whitelist", "https,tls,tcp", ...selfContainedFormatArgs()] - : []; + : mediaFormats(); return [ "-tls_verify", "1", diff --git a/packages/lint/src/hevcPreviewLint.ts b/packages/lint/src/hevcPreviewLint.ts index 56a7561e0c..7db79767d4 100644 --- a/packages/lint/src/hevcPreviewLint.ts +++ b/packages/lint/src/hevcPreviewLint.ts @@ -3,7 +3,7 @@ import { existsSync } from "node:fs"; import { join } from "node:path"; import { rewriteAssetPath } from "@hyperframes/parsers/asset-paths"; import { parseNumeric } from "@hyperframes/parsers/composition-contract"; -import { findFfBinary } from "@hyperframes/parsers/ff-binaries"; +import { findFfBinary, mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; import { cleanAssetUrl, isRemoteOrInlineUrl, @@ -88,6 +88,7 @@ async function probeIsHevc(ffprobePath: string, filePath: string): Promise { }); vi.mock("@hyperframes/parsers/ff-binaries", () => ({ findFfBinary: () => "/fake/bin/ffprobe", + mediaInputFormatArgs: () => [], })); import { lintHevcPreviewCodec } from "./hevcPreviewLint.js"; diff --git a/packages/parsers/src/ffBinaries.test.ts b/packages/parsers/src/ffBinaries.test.ts index 0cb9acc213..fea5c3c7dd 100644 --- a/packages/parsers/src/ffBinaries.test.ts +++ b/packages/parsers/src/ffBinaries.test.ts @@ -477,3 +477,39 @@ describe("isX86OnlyMachO", () => { expect(isX86OnlyMachO("/nonexistent/ffprobe")).toBe(false); }); }); + +describe("mediaInputFormatArgs", () => { + const formatsOf = (args: string[]) => args[args.indexOf("-format_whitelist") + 1]!.split(","); + + afterEach(async () => (await importFresh())._setDemuxerListingForTests()); + + it("reads an ffmpeg 4 listing, which has no device column", async () => { + const { _setDemuxerListingForTests, mediaInputFormatArgs } = await importFresh(); + _setDemuxerListingForTests( + [ + "File formats:", + " D. = Demuxing supported", + " .E = Muxing supported", + " --", + " D 3dostr 3DO STR", + " D dash Dynamic Adaptive Streaming over HTTP", + " D mov,mp4,m4a QuickTime / MOV", + ].join("\n"), + ); + expect(formatsOf(mediaInputFormatArgs("/ffprobe"))).toEqual(["3dostr", "mov", "mp4", "m4a"]); + }); + + it.skipIf(process.platform === "win32")("asks a binary that failed once again", async () => { + const { mediaInputFormatArgs, SELF_CONTAINED_FORMATS } = await importFresh(); + const dir = mkdtempSync(join(tmpdir(), "hf-demuxers-")); + onTestFinished(() => rmSync(dir, { recursive: true, force: true })); + const binary = join(dir, "ffprobe"); + writeFileSync( + binary, + `#!/bin/sh\nif [ -e "${dir}/ran" ]; then echo ' D wav WAV'; else touch "${dir}/ran"; exit 1; fi\n`, + ); + chmodSync(binary, 0o755); + expect(mediaInputFormatArgs(binary)).toEqual(["-format_whitelist", SELF_CONTAINED_FORMATS]); + expect(mediaInputFormatArgs(binary)).toEqual(["-format_whitelist", "wav"]); + }); +}); diff --git a/packages/parsers/src/ffBinaries.ts b/packages/parsers/src/ffBinaries.ts index f1e3dc68f1..953c709120 100644 --- a/packages/parsers/src/ffBinaries.ts +++ b/packages/parsers/src/ffBinaries.ts @@ -1,3 +1,4 @@ +import { execFileSync } from "node:child_process"; import { accessSync, closeSync, @@ -245,3 +246,70 @@ export function clearFfBinaryLookupCache(): void { lastMissAt.clear(); skippedX86Binaries.clear(); } + +/** What a render service lets a media input open: containers that name no other file. */ +export const SELF_CONTAINED_FORMATS = + "mov,matroska,mxf,avi,mpeg,mpegts,mpegvideo,flv,asf,ogg,wav,mp3,aac,flac,aiff,gif,image2,png_pipe,jpeg_pipe,webp_pipe,bmp_pipe,tiff_pipe"; + +// Demuxers that open other files or URLs on their own. hls and concat stay allowed: ffmpeg copies the +// format whitelist into every nested open, so a list cannot reach these, and the folder walk confines its paths. +const PATH_OPENING_DEMUXERS = new Set([ + "dash", + "imf", + "mlv", + "vobsub", + "dvdvideo", + "sdp", + "rtsp", + "rtp", + "sap", + "avisynth", + "vapoursynth", + "lavfi", +]); + +/** A `-format_whitelist` value from a `-demuxers` listing; undefined when no row parses. */ +function demuxersFromListing(listing: string): string | undefined { + const row = /Is a device/.test(listing) ? /^ D[E ]([d ]) (\S+)/ : /^ D[E ]() (\S+)/; + const names = new Set(); + for (const line of listing.split("\n")) { + const match = row.exec(line); + if (!match || match[1] === "d") continue; + const aliases = match[2]!.split(","); + if (aliases.some((alias) => PATH_OPENING_DEMUXERS.has(alias))) continue; + for (const alias of aliases) names.add(alias); + } + return names.size > 0 ? [...names].join(",") : undefined; +} + +const demuxerLists = new Map(); +let listingOverride: string | undefined; + +/** Tests feed a `-demuxers` listing so none spawns a binary; no argument forgets it. */ +export function _setDemuxerListingForTests(listing?: string): void { + listingOverride = listing; + demuxerLists.clear(); +} + +/** Every demuxer `listingBinary` reports that opens nothing by itself; one that cannot be asked gets the self-contained list, and is asked again. */ +export function mediaInputFormatArgs(listingBinary: string): string[] { + let formats = demuxerLists.get(listingBinary); + if (formats === undefined) { + try { + formats = demuxersFromListing( + listingOverride ?? + execFileSync(listingBinary, ["-hide_banner", "-demuxers"], { + encoding: "utf8", + timeout: 10_000, + windowsHide: true, + stdio: ["ignore", "pipe", "ignore"], + }), + ); + } catch { + formats = undefined; + } + if (formats === undefined) return ["-format_whitelist", SELF_CONTAINED_FORMATS]; + demuxerLists.set(listingBinary, formats); + } + return ["-format_whitelist", formats]; +} diff --git a/packages/producer/src/services/animatedGifPrep.ts b/packages/producer/src/services/animatedGifPrep.ts index 373f10dee8..feb671468c 100644 --- a/packages/producer/src/services/animatedGifPrep.ts +++ b/packages/producer/src/services/animatedGifPrep.ts @@ -9,6 +9,7 @@ import { isPrivateUnderAssetRoots, runFfmpeg, renameWithRetry, + urlInputArgs, } from "@hyperframes/engine"; import { namesAnotherNetworkRoot } from "@hyperframes/parsers/asset-resolution"; import { isHttpUrl } from "../utils/urlDownloader.js"; @@ -225,6 +226,7 @@ export function buildAnimatedGifTranscodeArgs(input: { args.push( "-ignore_loop", "1", + ...urlInputArgs(input.inputPath), "-i", input.inputPath, "-an", diff --git a/packages/producer/src/services/assetMediaType.test.ts b/packages/producer/src/services/assetMediaType.test.ts index 45894ed35b..315c1b6eff 100644 --- a/packages/producer/src/services/assetMediaType.test.ts +++ b/packages/producer/src/services/assetMediaType.test.ts @@ -270,6 +270,14 @@ describe("preflightCompositionAssetMediaTypes", () => { }, ); + it("refuses a manifest as an image source", async () => { + writeFileSync( + join(projectDir, "manifest.png"), + '', + ); + await expect(run({ imageSrc: "manifest.png" })).rejects.toBeInstanceOf(NotMediaPayloadError); + }); + it("leaves an SVG image source alone", async () => { // ffprobe reads SVG through its svg_pipe demuxer, so an XML body is a // legitimate payload and must not be swept up by the sniff. diff --git a/packages/producer/src/services/assetMediaType.ts b/packages/producer/src/services/assetMediaType.ts index 90bb432f73..edc026e905 100644 --- a/packages/producer/src/services/assetMediaType.ts +++ b/packages/producer/src/services/assetMediaType.ts @@ -4,6 +4,7 @@ import { fingerprintElementId, isHttpUrl, isInsideAssetRoots, + isNotImagePayload, isNotMediaPayload, isPrivateUnderAssetRoots, playlistLeavesItsFolder, @@ -131,13 +132,15 @@ export function dropMediaOutsideProject(input: { } // STUDIO-5433: the one place every local src is seen whatever its timing. Documents are refused under