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 <img> src.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* chore(parsers,producer): trim comments added with the media format list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(engine): keep the format-limit probe's argv in the shape the ffprobe contract reads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(engine): the image markup check reads a doctype without regex backtracking

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(parsers): raise the parsers size budget for the media format list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vance Ingalls
2026-11-16 00:35:56 -08:00
committed by GitHub
co-authored by Claude Opus 5.5
parent b741ecf132
commit ead110aab9
22 changed files with 464 additions and 36 deletions
@@ -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",
@@ -7,6 +7,7 @@ import { getFfprobeBinary } from "./ffmpegBinaries.js";
import {
assertMediaPayload,
fingerprintElementId,
isNotImagePayload,
isNotMediaPayload,
NotMediaPayloadError,
playlistLeavesItsFolder,
@@ -190,6 +191,45 @@ describe("isNotMediaPayload", () => {
});
});
const MPD = '<?xml version="1.0"?><MPD xmlns="urn:mpeg:dash:schema:mpd:2011"><Period/></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",
`<?xml version="1.0"?>\n<!-- made by hand -->\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "x.dtd" [<!ENTITY a "b">]>\n<SVG viewBox="0 0 1 1"/>`,
false,
],
["an svg whose comment outruns the sniff window", `<!--${"x".repeat(70_000)}--><svg/>`, false],
[
"a utf-16 svg",
Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from("<svg/>", "utf16le")]),
false,
],
["an svg with a long doctype subset", `<!DOCTYPE svg [${"][".repeat(30_000)}]><svg/>`, 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", "<!DOCTYPE html><html></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",
+91 -6
View File
@@ -124,13 +124,17 @@ async function openRegular(path: string | Buffer): Promise<FileHandle | null> {
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("<?")) return afterMarker(text, 2, "?>");
if (text.startsWith("<!--")) return afterMarker(text, 4, "-->");
return /^<!doctype\b/i.test(text) ? afterDoctype(text) : null;
}
/** Whether markup opens as an SVG; a prolog that runs past the window of a longer file is given the benefit of the doubt. */
function opensWithSvgRoot(markup: string, complete: boolean): boolean {
let rest = withoutLeadingBlanks(markup);
for (let next = afterProlog(rest); next !== null; next = afterProlog(rest)) {
rest = withoutLeadingBlanks(next);
}
return /^<svg[\s/>]/i.test(rest) || (!complete && /^<[?!]/.test(rest));
}
/**
* True when an `<img>` 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<boolean> {
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<number> {
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<string | null | true> {
/** 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<string | null | true> {
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<boolean> {
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;
+81 -6
View File
@@ -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/);
});
+7 -6
View File
@@ -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",