Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Security: a crafted MP4 can't freeze the server during a price check

Adarshd07ac1d85c10
diff --git a/src/lib/media-probe.ts b/src/lib/media-probe.ts
index 0d94f5bc..434da39a 100644
--- a/src/lib/media-probe.ts
+++ b/src/lib/media-probe.ts
@@ -50,6 +50,11 @@ export type ReadRange = (start: number, end: number) => Promise<Uint8Array>;
const HEAD_BYTES = 256 * 1024;
const MAX_MOOV_BYTES = 64 * 1024 * 1024;
const ISO_TOP = ["ftyp", "moov", "mdat", "free", "wide", "skip"];
+/**
+ * The most top-level boxes walked before the moov box. A real MP4 / MOV has a handful (ftyp, free, wide, mdat, uuid…;
+ * a fragmented one puts its moov first). A crafted file of millions of empty boxes would otherwise cost one read each.
+ */
+const MAX_TOP_BOXES = 64;
/**
* {@link probeMedia} without reading the whole file: an MP4 / MOV is walked box header by box header
@@ -62,7 +67,8 @@ export async function probeMediaRanges(read: ReadRange, size: number): Promise<M
const head = await read(0, Math.min(size, HEAD_BYTES));
if (head.length < 12) return null;
if (!ISO_TOP.includes(fourcc(head, 4))) return size <= head.length ? probeMedia(head) : null;
- for (let o = 0; o + 8 <= size; ) {
+ for (let o = 0, n = 0; o + 8 <= size; n++) {
+ if (n >= MAX_TOP_BOXES) return null;
const h = o + 16 <= head.length ? head.subarray(o, o + 16) : await read(o, Math.min(size, o + 16));
if (h.length < 8) return null;
const d = new DataView(h.buffer, h.byteOffset, h.byteLength);
@@ -99,7 +105,8 @@ export function probeMediaRangesSync(read: ReadRangeSync, size: number, opts: {
const head = read(0, Math.min(size, opts.headBytes ?? HEAD_BYTES));
if (head.length < 12) return null;
if (!ISO_TOP.includes(fourcc(head, 4))) return probeMedia(head, size);
- for (let o = 0; o + 8 <= size; ) {
+ for (let o = 0, n = 0; o + 8 <= size; n++) {
+ if (n >= MAX_TOP_BOXES) return null;
const h = o + 16 <= head.length ? head.subarray(o, o + 16) : read(o, Math.min(size, o + 16));
if (h.length < 8) return null;
const d = new DataView(h.buffer, h.byteOffset, h.byteLength);
diff --git a/tests/media-probe.test.ts b/tests/media-probe.test.ts
index be84a331..a676ba10 100644
--- a/tests/media-probe.test.ts
+++ b/tests/media-probe.test.ts
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
-import { probeBlob, probeMedia, probeMediaRanges, probeUrl } from "@/lib/media-probe";
+import { probeBlob, probeMedia, probeMediaRanges, probeMediaRangesSync, probeUrl } from "@/lib/media-probe";
const u32 = (...v: number[]) =>
Buffer.concat(
@@ -169,3 +169,34 @@ describe("probing without reading the whole file (Trim, the Ad Multiplier)", ()
expect(r.reads).toHaveLength(1);
});
});
+
+describe("a crafted MP4 can't make the box walk read without end", () => {
+ /** "ftyp", then `n` empty 8-byte "free" boxes, then a real moov: without a cap, each box past the first read is one more read. */
+ function freeBoxes(n: number): Buffer {
+ const whole = mp4();
+ const free = Buffer.alloc(8 * n);
+ for (let i = 0; i < n; i++) {
+ free.writeUInt32BE(8, i * 8);
+ free.write("free", i * 8 + 4);
+ }
+ return Buffer.concat([whole.subarray(0, 16), free, whole.subarray(16 + 72)]);
+ }
+
+ it("gives up after a few dozen top-level boxes, with only a few reads (at once and in turns)", async () => {
+ const buf = freeBoxes(400_000); // 3.2 MB of boxes: past the first read, one 16-byte read per box without a cap
+ let reads = 0;
+ const sync = (start: number, end: number) => (reads++, new Uint8Array(buf.subarray(start, end)));
+ expect(probeMediaRangesSync(sync, buf.length, { headBytes: 1024 * 1024 })).toBeNull();
+ expect(reads).toBeLessThan(100);
+ reads = 0;
+ const inTurns = async (start: number, end: number) => (reads++, new Uint8Array(buf.subarray(start, end)));
+ expect(await probeMediaRanges(inTurns, buf.length)).toBeNull();
+ expect(reads).toBeLessThan(100);
+ });
+
+ it("still finds the moov of a real file after a handful of top-level boxes", async () => {
+ const buf = freeBoxes(6);
+ expect(probeMediaRangesSync((s, e) => new Uint8Array(buf.subarray(s, e)), buf.length)).toEqual(probeMedia(mp4()));
+ expect(await probeMediaRanges(async (s, e) => new Uint8Array(buf.subarray(s, e)), buf.length)).toEqual(probeMedia(mp4()));
+ });
+});