Skip to content

Commit d1e473a

Browse files
committed
test(engine): harden ffprobe diagnostic redaction
1 parent 5383723 commit d1e473a

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

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

Lines changed: 31 additions & 1 deletion
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,7 +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
}),
194-
stderr: "recoverable diagnostic on a successful probe",
195+
stderr: successfulStderr,
195196
},
196197
]);
197198
vi.resetModules();
@@ -201,6 +202,7 @@ describe("ffprobe missing-binary fallback", () => {
201202
const meta = await extractAudioMetadata("/tmp/uses-configured-ffprobe.wav");
202203

203204
expect(meta.durationSeconds).toBe(1.25);
205+
expect(JSON.stringify(meta)).not.toContain(successfulStderr);
204206
expect(calls[0]?.command).toBe(resolve("/tools/ffprobe.exe"));
205207
expect(calls[0]?.args.slice(0, 2)).toEqual(["-v", "error"]);
206208
});
@@ -396,6 +398,34 @@ describe("ffprobe missing-binary fallback", () => {
396398
expect(calls[0]?.args.at(-1)).toBe(inputPath);
397399
});
398400

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+
399429
it("extractAudioMetadata surfaces a ffprobe-missing error verbatim", async () => {
400430
const { spawn, calls } = createSpawnSpy([{ kind: "missing" }]);
401431
hidePathBinaries();

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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";
55
import { redactTelemetryString } from "@hyperframes/core";
66
import { FFPROBE_PATH_ENV, getFfprobeBinary } from "./ffmpegBinaries.js";
77
import { ManagedChildProcess } from "./managedChildProcess.js";
@@ -10,8 +10,32 @@ import { trackChildProcess } from "./processTracker.js";
1010
const FFPROBE_STDERR_MAX_BYTES = 8 * 1024;
1111
const FFPROBE_ERROR_MAX_CHARS = 4 * 1024;
1212

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+
1337
function sanitizeFfprobeDiagnostic(stderr: string, filePath: string): string {
14-
const stderrWithoutInput = filePath ? stderr.split(filePath).join("[input]") : stderr;
38+
const stderrWithoutInput = redactFfprobeInput(stderr, filePath);
1539
const redacted = redactTelemetryString(stderrWithoutInput, FFPROBE_STDERR_MAX_BYTES);
1640
if (redacted.length <= FFPROBE_ERROR_MAX_CHARS) return redacted;
1741
return `…${redacted.slice(-(FFPROBE_ERROR_MAX_CHARS - 1))}`;

0 commit comments

Comments
 (0)