Skip to content

Commit 9792c32

Browse files
authored
fix(producer): reject asset media type mismatches (#2937)
* fix(producer): reject asset media type mismatches * fix(engine): document read-only AVIF probe * fix(engine): bound read-only AVIF brand probe * fix(producer): make media preflight lifecycle-safe * fix(producer): reconcile runtime media before preflight * fix(engine): avoid writable file-open detection * fix(producer): close runtime media preflight gaps
1 parent 1d0d4d8 commit 9792c32

21 files changed

Lines changed: 1756 additions & 156 deletions

‎.github/workflows/ci.yml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,19 @@ jobs:
262262

263263
test:
264264
name: Test
265-
needs: changes
266-
if: needs.changes.outputs.code == 'true'
265+
needs: [changes, producer-source-tests]
266+
# Keep the existing required `Test` context authoritative for producer
267+
# failures too. The dedicated producer matrix remains parallel and legible,
268+
# while this job fails closed if either lane fails or is cancelled.
269+
if: always() && needs.changes.outputs.code == 'true'
267270
runs-on: ubuntu-latest
268271
timeout-minutes: 10
269272
steps:
273+
- name: Require producer source tests
274+
if: needs.producer-source-tests.result != 'success'
275+
run: |
276+
echo "::error::Producer unit/integration tests did not succeed."
277+
exit 1
270278
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
271279
with:
272280
lfs: true

‎packages/engine/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,11 @@ export {
264264
extractVideoMetadata,
265265
extractFinalVideoFrameTimestamp,
266266
extractAudioMetadata,
267+
probeMediaProfile,
267268
analyzeKeyframeIntervals,
268269
type VideoMetadata,
269270
type AudioMetadata,
271+
type MediaProbeProfile,
270272
type KeyframeAnalysis,
271273
} from "./utils/ffprobe.js";
272274

‎packages/engine/src/utils/ffprobe.test.ts‎

Lines changed: 288 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// fallow-ignore-file code-duplication
22
import { EventEmitter } from "events";
3-
import { readFileSync } from "fs";
3+
import { spawnSync } from "child_process";
4+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs";
5+
import { tmpdir } from "os";
46
import { basename, resolve } from "path";
57
import { afterEach, describe, expect, it, vi } from "vitest";
68
import {
@@ -113,6 +115,272 @@ describe("extractPngMetadataFromBuffer", () => {
113115
);
114116
expect(extractPngMetadataFromBuffer(fixture)?.colorSpace?.colorTransfer).toBe("smpte2084");
115117
});
118+
119+
it("keeps metadata fallback independent from full PNG integrity validation", () => {
120+
const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]);
121+
expect(extractPngMetadataFromBuffer(buildPngWithChunks([ihdr]))).toMatchObject({
122+
width: 1,
123+
height: 1,
124+
});
125+
});
126+
});
127+
128+
describe("probeMediaProfile", () => {
129+
afterEach(() => {
130+
vi.resetModules();
131+
vi.doUnmock("child_process");
132+
});
133+
134+
it("classifies still, moving, audio-only, and mixed streams from probe data", async () => {
135+
const { spawn } = createSpawnSpy([
136+
{
137+
kind: "exit",
138+
code: 0,
139+
stdout: JSON.stringify({
140+
streams: [{ codec_type: "video", codec_name: "png" }],
141+
format: { format_name: "png_pipe" },
142+
}),
143+
},
144+
{
145+
kind: "exit",
146+
code: 0,
147+
stdout: JSON.stringify({
148+
streams: [{ codec_type: "video", codec_name: "h264" }],
149+
format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" },
150+
}),
151+
},
152+
{
153+
kind: "exit",
154+
code: 0,
155+
stdout: JSON.stringify({
156+
streams: [{ codec_type: "audio", codec_name: "mp3" }],
157+
format: { format_name: "mp3" },
158+
}),
159+
},
160+
{
161+
kind: "exit",
162+
code: 0,
163+
stdout: JSON.stringify({
164+
streams: [{ codec_type: "video" }, { codec_type: "audio" }],
165+
format: { format_name: "matroska,webm" },
166+
}),
167+
},
168+
]);
169+
vi.resetModules();
170+
vi.doMock("child_process", () => ({ spawn }));
171+
const { probeMediaProfile } = await import("./ffprobe.js");
172+
173+
const validPngPath = resolve(
174+
__dirname,
175+
"../../../producer/tests/hdr-regression/src/hdr-photo-pq.png",
176+
);
177+
await expect(probeMediaProfile(validPngPath)).resolves.toEqual({
178+
hasVideoStream: true,
179+
hasAudioStream: false,
180+
visualKind: "still",
181+
});
182+
await expect(probeMediaProfile("/tmp/extensionless-video")).resolves.toEqual({
183+
hasVideoStream: true,
184+
hasAudioStream: false,
185+
visualKind: "moving",
186+
});
187+
await expect(probeMediaProfile("/tmp/extensionless-audio")).resolves.toEqual({
188+
hasVideoStream: false,
189+
hasAudioStream: true,
190+
visualKind: "none",
191+
});
192+
await expect(probeMediaProfile("/tmp/mixed-av")).resolves.toEqual({
193+
hasVideoStream: true,
194+
hasAudioStream: true,
195+
visualKind: "moving",
196+
});
197+
});
198+
199+
it("does not treat attached cover art in an audio container as an image asset", async () => {
200+
const { spawn } = createSpawnSpy([
201+
{
202+
kind: "exit",
203+
code: 0,
204+
stdout: JSON.stringify({
205+
streams: [
206+
{ codec_type: "video", disposition: { attached_pic: 1 } },
207+
{ codec_type: "audio" },
208+
],
209+
format: { format_name: "mp3" },
210+
}),
211+
},
212+
]);
213+
vi.resetModules();
214+
vi.doMock("child_process", () => ({ spawn }));
215+
const { probeMediaProfile } = await import("./ffprobe.js");
216+
await expect(probeMediaProfile("/tmp/audio-with-cover")).resolves.toMatchObject({
217+
hasAudioStream: true,
218+
visualKind: "none",
219+
});
220+
});
221+
222+
it("deduplicates probes within one cancellation scope without sharing across scopes", async () => {
223+
const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-media-probe-cache-"));
224+
const fixturePath = resolve(fixtureDir, "asset");
225+
writeFileSync(fixturePath, "cache identity only");
226+
const outcome = {
227+
kind: "exit" as const,
228+
code: 0,
229+
stdout: JSON.stringify({
230+
streams: [{ codec_type: "video", codec_name: "h264" }],
231+
format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" },
232+
}),
233+
};
234+
const { spawn, calls } = createSpawnSpy([outcome, outcome]);
235+
vi.resetModules();
236+
vi.doMock("child_process", () => ({ spawn }));
237+
const { probeMediaProfile } = await import("./ffprobe.js");
238+
const firstSignal = new AbortController().signal;
239+
const secondSignal = new AbortController().signal;
240+
try {
241+
await Promise.all([
242+
probeMediaProfile(fixturePath, { signal: firstSignal }),
243+
probeMediaProfile(fixturePath, { signal: firstSignal }),
244+
]);
245+
expect(calls).toHaveLength(1);
246+
await probeMediaProfile(fixturePath, { signal: secondSignal });
247+
expect(calls).toHaveLength(2);
248+
} finally {
249+
rmSync(fixtureDir, { recursive: true, force: true });
250+
}
251+
});
252+
253+
it("bounds the process-scoped probe cache", async () => {
254+
const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-media-probe-lru-"));
255+
const fixturePaths = Array.from({ length: 129 }, (_, index) =>
256+
resolve(fixtureDir, `asset-${index}`),
257+
);
258+
for (const fixturePath of fixturePaths) writeFileSync(fixturePath, "probe identity");
259+
const outcome = {
260+
kind: "exit" as const,
261+
code: 0,
262+
stdout: JSON.stringify({
263+
streams: [{ codec_type: "audio", codec_name: "aac" }],
264+
format: { format_name: "aac" },
265+
}),
266+
};
267+
const { spawn, calls } = createSpawnSpy([outcome]);
268+
vi.resetModules();
269+
vi.doMock("child_process", () => ({ spawn }));
270+
const { probeMediaProfile } = await import("./ffprobe.js");
271+
try {
272+
for (const fixturePath of fixturePaths) await probeMediaProfile(fixturePath);
273+
await probeMediaProfile(fixturePaths[0]!);
274+
expect(calls).toHaveLength(130);
275+
} finally {
276+
rmSync(fixtureDir, { recursive: true, force: true });
277+
}
278+
});
279+
280+
it("classifies extensionless AVIF from its ISO-BMFF brand instead of the generic mov demuxer", async () => {
281+
const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-avif-profile-"));
282+
const fixturePath = resolve(fixtureDir, "asset");
283+
const ftyp = Buffer.alloc(24);
284+
ftyp.writeUInt32BE(24, 0);
285+
ftyp.write("ftyp", 4, 4, "ascii");
286+
ftyp.write("avif", 8, 4, "ascii");
287+
ftyp.writeUInt32BE(0, 12);
288+
ftyp.write("mif1", 16, 4, "ascii");
289+
ftyp.write("avif", 20, 4, "ascii");
290+
writeFileSync(fixturePath, ftyp);
291+
const { spawn } = createSpawnSpy([
292+
{
293+
kind: "exit",
294+
code: 0,
295+
stdout: JSON.stringify({
296+
streams: [{ codec_type: "video", codec_name: "av1" }],
297+
format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" },
298+
}),
299+
},
300+
]);
301+
vi.resetModules();
302+
vi.doMock("child_process", () => ({ spawn }));
303+
const { probeMediaProfile } = await import("./ffprobe.js");
304+
try {
305+
await expect(probeMediaProfile(fixturePath)).resolves.toMatchObject({
306+
hasVideoStream: true,
307+
hasAudioStream: false,
308+
visualKind: "still",
309+
});
310+
} finally {
311+
rmSync(fixtureDir, { recursive: true, force: true });
312+
}
313+
});
314+
315+
it("uses any non-attached video stream when cover art precedes moving video", async () => {
316+
const { spawn } = createSpawnSpy([
317+
{
318+
kind: "exit",
319+
code: 0,
320+
stdout: JSON.stringify({
321+
streams: [
322+
{ codec_type: "video", disposition: { attached_pic: 1 } },
323+
{ codec_type: "video", codec_name: "h264" },
324+
{ codec_type: "audio" },
325+
],
326+
format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" },
327+
}),
328+
},
329+
]);
330+
vi.resetModules();
331+
vi.doMock("child_process", () => ({ spawn }));
332+
const { probeMediaProfile } = await import("./ffprobe.js");
333+
await expect(probeMediaProfile("/tmp/video-with-cover")).resolves.toMatchObject({
334+
visualKind: "moving",
335+
});
336+
});
337+
338+
it.skipIf(spawnSync("ffprobe", ["-version"]).status !== 0)(
339+
"rejects an IHDR-only truncated PNG even when ffprobe accepts png_pipe",
340+
async () => {
341+
vi.resetModules();
342+
vi.doUnmock("child_process");
343+
const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-truncated-png-profile-"));
344+
const fixturePath = resolve(fixtureDir, "asset");
345+
const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]);
346+
writeFileSync(fixturePath, buildPngWithChunks([ihdr]));
347+
const { probeMediaProfile } = await import("./ffprobe.js");
348+
try {
349+
await expect(probeMediaProfile(fixturePath)).rejects.toThrow();
350+
} finally {
351+
rmSync(fixtureDir, { recursive: true, force: true });
352+
}
353+
},
354+
);
355+
356+
it("does not turn an aborted PNG probe into a successful metadata fallback", async () => {
357+
type KillableFakeProc = FakeProc & { kill: (signal?: NodeJS.Signals) => boolean };
358+
const spawn = () => {
359+
const proc = new EventEmitter() as KillableFakeProc;
360+
proc.stdout = new EventEmitter();
361+
proc.stderr = new EventEmitter();
362+
proc.kill = vi.fn(() => {
363+
process.nextTick(() => proc.emit("close", null, "SIGTERM"));
364+
return true;
365+
});
366+
process.nextTick(() => proc.emit("spawn"));
367+
return proc;
368+
};
369+
const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-aborted-png-profile-"));
370+
const fixturePath = resolve(fixtureDir, "asset");
371+
writeFileSync(fixturePath, buildMinimalPng());
372+
vi.resetModules();
373+
vi.doMock("child_process", () => ({ spawn }));
374+
const { probeMediaProfile } = await import("./ffprobe.js");
375+
const controller = new AbortController();
376+
try {
377+
const pending = probeMediaProfile(fixturePath, { signal: controller.signal });
378+
controller.abort(new Error("render cancelled"));
379+
await expect(pending).rejects.toThrow("render cancelled");
380+
} finally {
381+
rmSync(fixtureDir, { recursive: true, force: true });
382+
}
383+
});
116384
});
117385

118386
interface SpawnCall {
@@ -222,6 +490,23 @@ describe("ffprobe missing-binary fallback", () => {
222490
expect(calls[0]?.args.slice(0, 2)).toEqual(["-v", "error"]);
223491
});
224492

493+
it("does not accept an incomplete PNG through the missing-binary fallback", async () => {
494+
const fixtureDir = mkdtempSync(resolve(tmpdir(), "hf-truncated-png-fallback-"));
495+
const fixturePath = resolve(fixtureDir, "asset");
496+
const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]);
497+
writeFileSync(fixturePath, buildPngWithChunks([ihdr]));
498+
const { spawn } = createSpawnSpy([{ kind: "missing" }]);
499+
hidePathBinaries();
500+
vi.resetModules();
501+
vi.doMock("child_process", () => ({ spawn }));
502+
const { probeMediaProfile } = await import("./ffprobe.js");
503+
try {
504+
await expect(probeMediaProfile(fixturePath)).rejects.toThrow(/ffprobe/i);
505+
} finally {
506+
rmSync(fixtureDir, { recursive: true, force: true });
507+
}
508+
});
509+
225510
// `profile` matters now: the packet refinement is an allowlist on AAC-LC,
226511
// because the 1024-sample formula is wrong for LD/ELD/HE and unverified for
227512
// the rest. An unprofiled "aac" stream deliberately keeps its container
@@ -715,11 +1000,12 @@ describe("extractPngMetadataFromBuffer cICP ordering", () => {
7151000
it("does not emit color space until IHDR provides width and height", () => {
7161001
const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]);
7171002
const cicp = pngChunk("cICP", [9, 16, 0, 1]);
1003+
const idat = pngChunk("IDAT", [0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01]);
7181004
const iend = pngChunk("IEND", []);
7191005

7201006
// cICP before IHDR is invalid PNG ordering; make sure we don't return
7211007
// zero-sized metadata in that case.
722-
const malformed = buildPngWithChunks([cicp, ihdr, iend]);
1008+
const malformed = buildPngWithChunks([cicp, ihdr, idat, iend]);
7231009
expect(extractPngMetadataFromBuffer(malformed)).toEqual({
7241010
width: 1,
7251011
height: 1,

0 commit comments

Comments
 (0)