Skip to content

Commit 2895705

Browse files
committed
fix(engine): https video reads reconnect and time out, and redaction covers wrapped causes
Signed-URL reads now pass -rw_timeout, -reconnect and -reconnect_delay_max (both Lambda builds accept them) and ffmpeg also -reconnect_on_network_error, so a transient S3 error or a stalled read fails fast or recovers instead of waiting out the process timeout. Query redaction now descends into error.cause.
1 parent 1c0a6be commit 2895705

3 files changed

Lines changed: 54 additions & 12 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ async function runFfprobe(
6666
}
6767

6868
const command = getFfprobeBinary();
69-
const args = ["-v", "error", ...urlInputArgs(filePath), ...argsWithoutInput, "--", filePath];
69+
const args = [
70+
"-v",
71+
"error",
72+
...urlInputArgs(filePath, { tool: "ffprobe" }),
73+
...argsWithoutInput,
74+
"--",
75+
filePath,
76+
];
7077
const proc = spawn(command, args, {
7178
// Nothing is ever written to the child's stdin; leaving it as a pipe is
7279
// what lets a stdin-reading invocation block indefinitely.

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,39 @@ const signed =
66
"https://bucket.s3.us-east-2.amazonaws.com/a/b?X-Amz-Credential=AKID&X-Amz-Signature=deadbeef";
77

88
describe("urlInputArgs", () => {
9-
it("makes ffmpeg verify the certificate of an https input against a CA bundle", () => {
10-
expect(urlInputArgs(signed, ["/nonexistent.crt", import.meta.filename])).toEqual([
11-
"-tls_verify",
9+
const caBundles = ["/nonexistent.crt", import.meta.filename];
10+
const verifyAndReconnect = [
11+
"-tls_verify",
12+
"1",
13+
"-ca_file",
14+
import.meta.filename,
15+
"-rw_timeout",
16+
"30000000",
17+
"-reconnect",
18+
"1",
19+
"-reconnect_delay_max",
20+
"2",
21+
];
22+
23+
it("makes ffmpeg verify the certificate of an https input and reconnect on network errors", () => {
24+
expect(urlInputArgs(signed, { caBundles })).toEqual([
25+
...verifyAndReconnect,
26+
"-reconnect_on_network_error",
1227
"1",
13-
"-ca_file",
14-
import.meta.filename,
1528
]);
1629
});
1730

31+
it("gives ffprobe only the options its 4.0 build accepts", () => {
32+
expect(urlInputArgs(signed, { tool: "ffprobe", caBundles })).toEqual(verifyAndReconnect);
33+
});
34+
1835
it("adds nothing for local paths and plain http", () => {
19-
expect(urlInputArgs("/plan/compiled/bg.mp4", [])).toEqual([]);
20-
expect(urlInputArgs("http://127.0.0.1:8080/bg.mp4", [])).toEqual([]);
36+
expect(urlInputArgs("/plan/compiled/bg.mp4", { caBundles: [] })).toEqual([]);
37+
expect(urlInputArgs("http://127.0.0.1:8080/bg.mp4", { caBundles: [] })).toEqual([]);
2138
});
2239

2340
it("refuses an https input when no CA bundle exists", () => {
24-
expect(() => urlInputArgs(signed, ["/nonexistent.crt"])).toThrow(/no CA bundle/);
41+
expect(() => urlInputArgs(signed, { caBundles: ["/nonexistent.crt"] })).toThrow(/no CA bundle/);
2542
});
2643
});
2744

@@ -35,4 +52,11 @@ describe("redactUrlQueriesInError", () => {
3552
expect(text).not.toContain("deadbeef");
3653
}
3754
});
55+
56+
it("also strips it from a wrapped cause", () => {
57+
const error = redactUrlQueriesInError(
58+
new Error("render failed", { cause: new Error(`read ${signed}`) }),
59+
);
60+
expect(String((error.cause as Error).message)).not.toContain("deadbeef");
61+
});
3862
});

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,35 @@ const CA_BUNDLES = [
88
"/etc/ssl/cert.pem",
99
];
1010

11+
// Lambda's ffprobe 4.0 accepts these but not -reconnect_on_network_error; a stalled read fails after 30 s.
12+
const RECONNECT_ARGS = ["-rw_timeout", "30000000", "-reconnect", "1", "-reconnect_delay_max", "2"];
13+
1114
/** ffmpeg and ffprobe skip TLS certificate checks unless told to; an https input must be verified. */
12-
export function urlInputArgs(input: string, caBundles: readonly string[] = CA_BUNDLES): string[] {
15+
export function urlInputArgs(
16+
input: string,
17+
{
18+
tool = "ffmpeg",
19+
caBundles = CA_BUNDLES,
20+
}: { tool?: "ffmpeg" | "ffprobe"; caBundles?: readonly string[] } = {},
21+
): string[] {
1322
if (!/^https:\/\//i.test(input)) return [];
1423
const caFile = caBundles.find((path) => existsSync(path));
1524
if (!caFile) {
1625
throw new Error(
1726
`[FFmpeg] cannot verify TLS for an https input: no CA bundle at ${caBundles.join(", ")}`,
1827
);
1928
}
20-
return ["-tls_verify", "1", "-ca_file", caFile];
29+
const networkRetry = tool === "ffmpeg" ? ["-reconnect_on_network_error", "1"] : [];
30+
return ["-tls_verify", "1", "-ca_file", caFile, ...RECONNECT_ARGS, ...networkRetry];
2131
}
2232

23-
/** A signed URL grants access until it expires: strip query strings from every string field of an error. */
33+
/** A signed URL grants access until it expires: strip query strings from every string field of an error and its causes. */
2434
export function redactUrlQueriesInError<T>(error: T): T {
2535
if (!(error instanceof Error)) return error;
2636
const fields = error as unknown as Record<string, unknown>;
2737
for (const key of ["message", "stack", ...Object.keys(error)]) {
2838
if (typeof fields[key] === "string") fields[key] = redactUrlQueryStrings(fields[key]);
2939
}
40+
if (error.cause !== error) redactUrlQueriesInError(error.cause);
3041
return error;
3142
}

0 commit comments

Comments
 (0)