Skip to content

Commit 8c50770

Browse files
authored
fix(engine): preserve bounded ffprobe diagnostics (#2772)
* fix(engine): preserve bounded ffprobe diagnostics * test(engine): harden ffprobe diagnostic redaction
1 parent 37b8868 commit 8c50770

2 files changed

Lines changed: 113 additions & 17 deletions

File tree

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ describe("ffprobe missing-binary fallback", () => {
183183

184184
it("spawns the configured absolute FFprobe path when HYPERFRAMES_FFPROBE_PATH is set", async () => {
185185
process.env.HYPERFRAMES_FFPROBE_PATH = "/tools/ffprobe.exe";
186+
const successfulStderr = "recoverable diagnostic on a successful probe";
186187
const { spawn, calls } = createSpawnSpy([
187188
{
188189
kind: "exit",
@@ -191,6 +192,7 @@ describe("ffprobe missing-binary fallback", () => {
191192
streams: [{ codec_type: "audio", codec_name: "aac", sample_rate: "48000", channels: 2 }],
192193
format: { duration: "1.25", bit_rate: "128000" },
193194
}),
195+
stderr: successfulStderr,
194196
},
195197
]);
196198
vi.resetModules();
@@ -200,7 +202,9 @@ describe("ffprobe missing-binary fallback", () => {
200202
const meta = await extractAudioMetadata("/tmp/uses-configured-ffprobe.wav");
201203

202204
expect(meta.durationSeconds).toBe(1.25);
205+
expect(JSON.stringify(meta)).not.toContain(successfulStderr);
203206
expect(calls[0]?.command).toBe(resolve("/tools/ffprobe.exe"));
207+
expect(calls[0]?.args.slice(0, 2)).toEqual(["-v", "error"]);
204208
});
205209

206210
it.each([
@@ -363,6 +367,65 @@ describe("ffprobe missing-binary fallback", () => {
363367
await expect(extractMediaMetadataMocked("/tmp/no-such-video.mp4")).rejects.toThrow(/ffprobe/);
364368
});
365369

370+
it("surfaces bounded ffprobe stderr for invalid media", async () => {
371+
const leadingNoise = "x".repeat(10_000);
372+
const diagnostic = "Invalid data found when processing input";
373+
const inputPath = "/tmp/render/My Secret Video.mp4";
374+
const { spawn, calls } = createSpawnSpy([
375+
{
376+
kind: "exit",
377+
code: 1,
378+
stderr: `${leadingNoise}${inputPath}: ${diagnostic}`,
379+
},
380+
]);
381+
vi.resetModules();
382+
vi.doMock("child_process", () => ({ spawn }));
383+
384+
const { extractAudioMetadata } = await import("./ffprobe.js");
385+
386+
let thrown: unknown;
387+
try {
388+
await extractAudioMetadata(inputPath);
389+
} catch (error) {
390+
thrown = error;
391+
}
392+
expect(String(thrown)).toContain(diagnostic);
393+
expect(String(thrown)).toContain("[input]");
394+
expect(String(thrown)).not.toContain(inputPath);
395+
expect(String(thrown)).not.toContain("My Secret Video.mp4");
396+
expect(String(thrown).length).toBeLessThan(4_500);
397+
expect(calls[0]?.args.slice(0, 2)).toEqual(["-v", "error"]);
398+
expect(calls[0]?.args.at(-1)).toBe(inputPath);
399+
});
400+
401+
it("redacts an input path fragment when the stderr tail starts inside its basename", async () => {
402+
const diagnostic = "Invalid data found when processing input";
403+
const inputPath = "/tmp/render/Confidential Client Preview.mp4";
404+
const retainedPathFragment = "Client Preview.mp4";
405+
const diagnosticPrefix = `: ${diagnostic} `;
406+
const remainingBytes =
407+
8 * 1024 - Buffer.byteLength(retainedPathFragment) - Buffer.byteLength(diagnosticPrefix);
408+
const multibyteCount = Math.floor(remainingBytes / Buffer.byteLength("€"));
409+
const trailingAscii = "x".repeat(remainingBytes - multibyteCount * Buffer.byteLength("€"));
410+
const stderr = `${inputPath}${diagnosticPrefix}${"€".repeat(multibyteCount)}${trailingAscii}`;
411+
const { spawn } = createSpawnSpy([{ kind: "exit", code: 1, stderr }]);
412+
vi.resetModules();
413+
vi.doMock("child_process", () => ({ spawn }));
414+
415+
const { extractAudioMetadata } = await import("./ffprobe.js");
416+
417+
let thrown: unknown;
418+
try {
419+
await extractAudioMetadata(inputPath);
420+
} catch (error) {
421+
thrown = error;
422+
}
423+
expect(String(thrown)).toContain(diagnostic);
424+
expect(String(thrown)).toContain("[input]");
425+
expect(String(thrown)).not.toContain(retainedPathFragment);
426+
expect(String(thrown)).not.toContain("Client Preview.mp4");
427+
});
428+
366429
it("extractAudioMetadata surfaces a ffprobe-missing error verbatim", async () => {
367430
const { spawn, calls } = createSpawnSpy([{ kind: "missing" }]);
368431
hidePathBinaries();

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

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
11
// fallow-ignore-file code-duplication complexity
22
import { spawn } from "child_process";
33
import { readFileSync } from "fs";
4-
import { extname } from "path";
4+
import { basename, extname } from "path";
5+
import { redactTelemetryString } from "@hyperframes/core";
56
import { FFPROBE_PATH_ENV, getFfprobeBinary } from "./ffmpegBinaries.js";
67
import { ManagedChildProcess } from "./managedChildProcess.js";
78
import { trackChildProcess } from "./processTracker.js";
89

10+
const FFPROBE_STDERR_MAX_BYTES = 8 * 1024;
11+
const FFPROBE_ERROR_MAX_CHARS = 4 * 1024;
12+
13+
function redactFfprobeInput(stderr: string, filePath: string): string {
14+
if (!filePath) return stderr;
15+
16+
let redacted = stderr.split(filePath).join("[input]");
17+
const inputBasename = basename(filePath);
18+
if (inputBasename) redacted = redacted.split(inputBasename).join("[input]");
19+
20+
// ManagedChildProcess retains an 8 KiB byte tail. When that boundary lands
21+
// inside the input path, stderr starts with only a suffix of the path, so
22+
// neither exact-path nor generic absolute-path redaction can recognize it.
23+
if (!redacted.startsWith("[input]")) {
24+
for (let offset = 1; offset < filePath.length; offset += 1) {
25+
const suffix = filePath.slice(offset);
26+
if (suffix.length < 4 || !redacted.startsWith(suffix)) continue;
27+
const boundary = redacted[suffix.length];
28+
if (boundary !== undefined && !/[\s:'")\]}]/.test(boundary)) continue;
29+
redacted = `[input]${redacted.slice(suffix.length)}`;
30+
break;
31+
}
32+
}
33+
34+
return redacted;
35+
}
36+
37+
function sanitizeFfprobeDiagnostic(stderr: string, filePath: string): string {
38+
const stderrWithoutInput = redactFfprobeInput(stderr, filePath);
39+
const redacted = redactTelemetryString(stderrWithoutInput, FFPROBE_STDERR_MAX_BYTES);
40+
if (redacted.length <= FFPROBE_ERROR_MAX_CHARS) return redacted;
41+
return `…${redacted.slice(-(FFPROBE_ERROR_MAX_CHARS - 1))}`;
42+
}
43+
944
/** Spawn ffprobe with given args, return stdout. Throws on non-zero exit or missing binary. */
10-
async function runFfprobe(args: string[], signal?: AbortSignal): Promise<string> {
45+
async function runFfprobe(
46+
filePath: string,
47+
argsWithoutInput: string[],
48+
signal?: AbortSignal,
49+
): Promise<string> {
1150
const command = getFfprobeBinary();
12-
const proc = spawn(command, args);
51+
const proc = spawn(command, ["-v", "error", ...argsWithoutInput, filePath]);
1352
trackChildProcess(proc);
1453
let stdout = "";
1554
proc.stdout.on("data", (data) => {
@@ -18,6 +57,7 @@ async function runFfprobe(args: string[], signal?: AbortSignal): Promise<string>
1857
const managed = new ManagedChildProcess(proc, {
1958
signal,
2059
deadlineAtMs: Date.now() + 30_000,
60+
stderrMaxBytes: FFPROBE_STDERR_MAX_BYTES,
2161
});
2262
const outcome = await managed.wait();
2363
if (outcome.reason === "spawn_error") {
@@ -32,8 +72,9 @@ async function runFfprobe(args: string[], signal?: AbortSignal): Promise<string>
3272
throw outcome.error ?? new Error(outcome.stderr);
3373
}
3474
if (outcome.reason !== "exit" || outcome.exitCode !== 0) {
75+
const diagnostic = sanitizeFfprobeDiagnostic(outcome.stderr, filePath);
3576
throw new Error(
36-
`[FFmpeg] ffprobe ${outcome.reason} with code ${outcome.exitCode}: ${outcome.stderr}`,
77+
`[FFmpeg] ffprobe ${outcome.reason} with code ${outcome.exitCode}: ${diagnostic}`,
3778
);
3879
}
3980
return stdout;
@@ -263,14 +304,11 @@ export async function extractMediaMetadata(filePath: string): Promise<VideoMetad
263304

264305
let output: FFProbeOutput | null = null;
265306
try {
266-
const stdout = await runFfprobe([
267-
"-v",
268-
"quiet",
307+
const stdout = await runFfprobe(filePath, [
269308
"-print_format",
270309
"json",
271310
"-show_format",
272311
"-show_streams",
273-
filePath,
274312
]);
275313
output = parseProbeJson(stdout);
276314
} catch (error) {
@@ -361,7 +399,8 @@ export async function extractAudioMetadata(
361399

362400
const probePromise = (async (): Promise<AudioMetadata> => {
363401
const stdout = await runFfprobe(
364-
["-v", "quiet", "-print_format", "json", "-show_format", "-show_streams", filePath],
402+
filePath,
403+
["-print_format", "json", "-show_format", "-show_streams"],
365404
options?.signal,
366405
);
367406
const output = parseProbeJson(stdout);
@@ -373,17 +412,14 @@ export async function extractAudioMetadata(
373412
const sampleRate = audioStream.sample_rate ? parseInt(audioStream.sample_rate) : 44100;
374413
const audioCodec = audioStream.codec_name || "unknown";
375414
if (audioCodec === "aac" && sampleRate > 0) {
376-
const packetStdout = await runFfprobe([
377-
"-v",
378-
"quiet",
415+
const packetStdout = await runFfprobe(filePath, [
379416
"-select_streams",
380417
"a:0",
381418
"-count_packets",
382419
"-show_entries",
383420
"stream=nb_read_packets",
384421
"-print_format",
385422
"json",
386-
filePath,
387423
]);
388424
const packetOutput = parseProbeJson(packetStdout);
389425
const packetCount = Number(packetOutput.streams[0]?.nb_read_packets);
@@ -441,9 +477,7 @@ export async function analyzeKeyframeIntervals(filePath: string): Promise<Keyfra
441477
}
442478

443479
async function analyzeKeyframeIntervalsUncached(filePath: string): Promise<KeyframeAnalysis> {
444-
const stdout = await runFfprobe([
445-
"-v",
446-
"quiet",
480+
const stdout = await runFfprobe(filePath, [
447481
"-select_streams",
448482
"v:0",
449483
"-skip_frame",
@@ -452,7 +486,6 @@ async function analyzeKeyframeIntervalsUncached(filePath: string): Promise<Keyfr
452486
"frame=pts_time",
453487
"-of",
454488
"csv=p=0",
455-
filePath,
456489
]);
457490

458491
const timestamps = stdout

0 commit comments

Comments
 (0)