fix(engine): media URL reads reach only public hosts under a restricted page network (#7946)

This commit is contained in:
Miguel Ángel
2026-11-16 17:05:23 +00:00
committed by GitHub
parent dbc9c18262
commit 994fbbde2a
7 changed files with 300 additions and 27 deletions
+1 -1
View File
@@ -414,7 +414,7 @@ export {
type KeyframeAnalysis,
} from "./utils/ffprobe.js";
export { urlInputArgs } from "./utils/urlInput.js";
export { urlInputArgs, withPublicOnlyProxy } from "./utils/urlInput.js";
export { mediaIdPathSegment } from "./utils/mediaPathSegment.js";
+34 -8
View File
@@ -11,7 +11,12 @@ import {
pixelFormatHasAlpha,
videoStreamHasAlpha,
} from "@hyperframes/parsers/media-alpha";
import { urlInputArgs } from "./urlInput.js";
import {
contentAddressedSha256,
declareContentAddressedUrl,
urlInputArgs,
withPublicOnlyProxy,
} from "./urlInput.js";
import { TextArtMediaError } from "./notMediaPayload.js";
import { decodableAudioStream, misstatesPcmByteRate } from "@hyperframes/parsers/media-streams";
import { runFfmpeg } from "./runFfmpeg.js";
@@ -83,7 +88,33 @@ export async function runFfprobe(
"--",
filePath,
];
// With a restricted page network, a URL input not declared by the render reaches only public hosts.
const proxying = withPublicOnlyProxy(args, [args.length - 1]);
const routed = proxying && (await proxying);
try {
return await spawnFfprobe(
command,
routed?.args ?? args,
routed?.env,
filePath,
signal,
stdoutOptions,
);
} finally {
await routed?.close();
}
}
async function spawnFfprobe(
command: string,
args: string[],
env: NodeJS.ProcessEnv | undefined,
filePath: string,
signal?: AbortSignal,
stdoutOptions?: { retainTail?: boolean; maxChars?: number },
): Promise<string> {
const proc = spawn(command, args, {
...(env && { env }),
// Nothing is ever written to the child's stdin; leaving it as a pipe is
// what lets a stdin-reading invocation block indefinitely.
stdio: ["ignore", "pipe", "pipe"],
@@ -371,13 +402,8 @@ function isPngImageProbe(output: FFProbeOutput): boolean {
);
}
const contentAddressedUrls = new Map<string, string>();
const withoutQuery = (url: string) => url.split(/[?#]/, 1)[0] ?? url;
/** A URL whose bytes can never change (its path holds their hash), so probes of it are cacheable. */
export function declareContentAddressedUrl(url: string, sha256: string): void {
contentAddressedUrls.set(withoutQuery(url), sha256);
}
export { declareContentAddressedUrl };
const probeFactsUrls = new Map<string, string>();
@@ -388,7 +414,7 @@ export function declareProbeFactsUrl(url: string, factsUrl: string): void {
export function mediaFileIdentity(filePath: string): string | null {
if (/^https?:\/\//i.test(filePath)) {
const sha256 = contentAddressedUrls.get(withoutQuery(filePath));
const sha256 = contentAddressedSha256(filePath);
return sha256 ? `sha256:${sha256}` : null;
}
try {
+21 -12
View File
@@ -9,6 +9,7 @@
import { AsyncLocalStorage } from "node:async_hooks";
import { spawn, type ChildProcess } from "child_process";
import { getFfmpegBinary } from "./ffmpegBinaries.js";
import { ffmpegInputIndexes, withPublicOnlyProxy } from "./urlInput.js";
import { trackChildProcess } from "./processTracker.js";
import {
ManagedChildProcess,
@@ -160,12 +161,12 @@ export function describeFfmpegFailure(
: error;
}
function spawnFfmpeg(args: string[]): ChildProcess {
function spawnFfmpeg(args: string[], env?: NodeJS.ProcessEnv): ChildProcess {
// windowsHide: ffmpeg/ffprobe are console-subsystem binaries, so without
// this Node opens a visible console window per spawn on Windows. A render
// shells out dozens of times across parallel workers, which flashes a burst
// of windows across the user's desktop. No-op on macOS and Linux.
const ffmpeg = spawn(getFfmpegBinary(), args, { windowsHide: true });
const ffmpeg = spawn(getFfmpegBinary(), args, { windowsHide: true, ...(env && { env }) });
trackChildProcess(ffmpeg);
return ffmpeg;
}
@@ -210,22 +211,30 @@ export function withFfmpegProgress<T>(onProgress: () => void, work: () => Promis
export async function runFfmpeg(args: string[], opts?: RunFfmpegOptions): Promise<RunFfmpegResult> {
if (opts?.signal?.aborted) return { ...ABORTED_BEFORE_START };
const onProgress = progressListener.getStore();
// With a restricted page network, a URL input not declared by the render reaches only public hosts.
const proxying = withPublicOnlyProxy(args, ffmpegInputIndexes(args));
const routed = proxying && (await proxying);
let framesWritten = 0;
let secondsWritten = 0;
let managed: ManagedChildProcess | undefined;
const readStats = ffmpegStatsReader(({ frames = 0, seconds = 0 }) => {
if (frames <= framesWritten && seconds <= secondsWritten) return;
framesWritten = Math.max(frames, framesWritten);
secondsWritten = Math.max(seconds, secondsWritten);
managed.markActivity();
managed?.markActivity();
onProgress?.();
});
const managed = new ManagedChildProcess(spawnFfmpeg(args), {
signal: opts?.signal,
inactivityTimeoutMs: opts?.timeout ?? DEFAULT_TIMEOUT,
onStderr: (chunk) => {
readStats(chunk);
opts?.onStderr?.(chunk);
},
});
return toResult(await managed.wait());
try {
managed = new ManagedChildProcess(spawnFfmpeg(routed?.args ?? args, routed?.env), {
signal: opts?.signal,
inactivityTimeoutMs: opts?.timeout ?? DEFAULT_TIMEOUT,
onStderr: (chunk) => {
readStats(chunk);
opts?.onStderr?.(chunk);
},
});
return toResult(await managed.wait());
} finally {
await routed?.close();
}
}
@@ -0,0 +1,138 @@
import { spawnSync } from "node:child_process";
import { createServer, type Server } from "node:http";
import type { AddressInfo } from "node:net";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { onRenderRefusal } from "../services/publicNetwork/captureProxy.js";
import { getFfmpegBinary } from "./ffmpegBinaries.js";
import { declareContentAddressedUrl, runFfprobe } from "./ffprobe.js";
import { runFfmpeg } from "./runFfmpeg.js";
import { urlInputArgs, withPublicOnlyProxy } from "./urlInput.js";
const HAS_FF = spawnSync(getFfmpegBinary(), ["-version"]).status === 0;
const HAS_CA_BUNDLE = (() => {
try {
return urlInputArgs("https://example.com/clip.mp4").length > 0;
} catch {
return false;
}
})();
/** A local service the render must never reach: it counts every request it gets. */
async function localService(): Promise<{ server: Server; url: string; hits: () => number }> {
let hits = 0;
const server = createServer((_req, res) => {
hits++;
res.writeHead(404).end();
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const { port } = server.address() as AddressInfo;
return { server, url: `http://127.0.0.1:${port}/clip.mp4`, hits: () => hits };
}
function refusals(): { classes: string[]; stop: () => void } {
const classes: string[] = [];
const stop = onRenderRefusal((hostClass) => classes.push(hostClass));
return { classes, stop };
}
describe("media inputs with PRODUCER_RESTRICT_PAGE_NETWORK on", () => {
beforeEach(() => vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "true"));
afterEach(() => vi.unstubAllEnvs());
// fails without the fix: an http input got no protocol limit and no proxy, so ffprobe fetched the local service.
it.skipIf(!HAS_FF)("never lets ffprobe reach a local address", { timeout: 60_000 }, async () => {
const service = await localService();
const seen = refusals();
try {
await expect(runFfprobe(service.url, ["-show_format"])).rejects.toThrow();
expect(service.hits()).toBe(0);
expect(seen.classes).toContain("loopback");
} finally {
seen.stop();
service.server.close();
}
});
// fails without the fix: ffmpeg was handed the URL directly, with no public-only proxy in front of it.
it.skipIf(!HAS_FF)("refuses the cloud metadata address", { timeout: 60_000 }, async () => {
const seen = refusals();
try {
await expect(
runFfprobe("http://169.254.169.254/latest/meta-data/", ["-show_format"]),
).rejects.toThrow();
expect(seen.classes).toContain("link-local");
} finally {
seen.stop();
}
});
// Without a CA bundle (Windows) an https input is refused before ffmpeg starts.
it.skipIf(!HAS_FF || !HAS_CA_BUNDLE)(
"refuses a private https host",
{ timeout: 60_000 },
async () => {
const seen = refusals();
try {
await expect(runFfprobe("https://10.0.0.1/clip.mp4", ["-show_format"])).rejects.toThrow();
expect(seen.classes).toContain("private");
} finally {
seen.stop();
}
},
);
// fails without the fix: a read that skips urlInputArgs handed its URL to ffmpeg as is.
it.skipIf(!HAS_FF)(
"puts the proxy in front of a URL any ffmpeg read names",
{ timeout: 60_000 },
async () => {
const service = await localService();
try {
const result = await runFfmpeg(["-i", service.url, "-f", "null", "-"]);
expect(result.success).toBe(false);
expect(service.hits()).toBe(0);
} finally {
service.server.close();
}
},
);
it("refuses a URL scheme other than http and https", () => {
for (const input of [
"rtsp://10.0.0.1/live",
"ftp://10.0.0.1/a.mp4",
"concat:/a.mp4|/b.mp4",
"data:,x",
"subfile,,start,0,end,0,,:https://10.0.0.1/a.mp4",
])
expect(() => urlInputArgs(input)).toThrow(/not read with a restricted page network/);
});
// fails without the fix: FFmpeg before 4.4 ignores -http_proxy for https and reads only the http_proxy variable.
it("gives the child the proxy in http_proxy too, and no no_proxy", async () => {
vi.stubEnv("no_proxy", "10.0.0.1");
vi.stubEnv("NO_PROXY", "10.0.0.1");
const routed = await withPublicOnlyProxy(["-i", "https://10.0.0.1/clip.mp4"], [1]);
try {
const proxy = routed!.args[routed!.args.indexOf("-http_proxy") + 1];
expect(proxy).toMatch(/^http:\/\/127\.0\.0\.1:\d+$/);
expect(routed!.env.http_proxy).toBe(proxy);
expect(routed!.env).not.toHaveProperty("no_proxy");
expect(routed!.env).not.toHaveProperty("NO_PROXY");
} finally {
await routed?.close();
}
});
it("reads a local file and a URL the render declared as before, with no proxy", () => {
expect(urlInputArgs("/renders/clip.mp4")).not.toContain("-protocol_whitelist");
expect(urlInputArgs("C:\\renders\\clip.mp4")).not.toContain("-protocol_whitelist");
const declared = "https://bucket.s3.amazonaws.com/renders/abc.mp4";
declareContentAddressedUrl(declared, "a".repeat(64));
const args = urlInputArgs(`${declared}?X-Amz-Signature=deadbeef`, {
caBundles: [import.meta.filename],
});
expect(args[args.indexOf("-protocol_whitelist") + 1]).toBe("https,tls,tcp");
expect(args).not.toContain("-http_proxy");
});
});
+4 -2
View File
@@ -59,16 +59,18 @@ describe("urlInputArgs", () => {
});
it.each(["true", "1", " TRUE\n"])(
"limits what an https input may open to HTTPS on a render service (%j)",
"on a render service, reads an https input the render did not declare only through the public-only proxy (%j)",
(flag) => {
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", flag);
try {
expect(urlInputArgs(signed, { tool: "ffprobe", caBundles })).toEqual([
...verifyAndReconnect,
"-protocol_whitelist",
"https,tls,tcp",
"http,https,tls,tcp,httpproxy",
"-format_whitelist",
expect.any(String),
"-http_proxy",
"http://127.0.0.1:9",
]);
} finally {
vi.unstubAllEnvs();
+85 -4
View File
@@ -14,6 +14,37 @@ const CA_BUNDLES = [
// Lambda's ffprobe 4.0 accepts these but not -reconnect_on_network_error; a stalled read fails after 30 s.
const RECONNECT_ARGS = ["-rw_timeout", "30000000", "-reconnect", "1", "-reconnect_delay_max", "2"];
const contentAddressedUrls = new Map<string, string>();
const withoutQuery = (url: string) => url.split(/[?#]/, 1)[0] ?? url;
/** An operator's file whose path holds its hash: probes of it are cacheable, and it is read without the proxy. */
export function declareContentAddressedUrl(url: string, sha256: string): void {
contentAddressedUrls.set(withoutQuery(url), sha256);
}
export function contentAddressedSha256(url: string): string | undefined {
return contentAddressedUrls.get(withoutQuery(url));
}
/** A URL scheme ffmpeg opens itself (2+ letters, so `C:\` stays a path); `subfile,` names the URL it opens. */
const SCHEME = /^([a-z][a-z0-9+.-]+):/i;
const PROXIED_PROTOCOLS = "http,https,tls,tcp,httpproxy";
const ROUTES = new Map<string, "local" | "proxy">([
["file", "local"],
["http", "proxy"],
["https", "proxy"],
]);
function restrictedRoute(input: string): "local" | "declared" | "proxy" {
const scheme = /^subfile,/i.test(input) ? "subfile" : SCHEME.exec(input)?.[1]?.toLowerCase();
const route = scheme === undefined ? "local" : ROUTES.get(scheme);
if (!route)
throw new Error(`[FFmpeg] a ${scheme}: input is not read with a restricted page network`);
return route === "proxy" && /^https:/i.test(input) && contentAddressedSha256(input)
? "declared"
: route;
}
export function selfContainedFormatArgs(): string[] {
return resolveRestrictPageNetwork() ? ["-format_whitelist", SELF_CONTAINED_FORMATS] : [];
}
@@ -28,6 +59,15 @@ export function urlInputArgs(
): string[] {
const mediaFormats = () =>
mediaInputFormatArgs(tool === "ffprobe" ? getFfprobeBinary() : getFfmpegBinary());
const restricted = resolveRestrictPageNetwork();
const route = restricted ? restrictedRoute(input) : undefined;
if (route === "proxy" && !/^https:\/\//i.test(input))
return [
...RECONNECT_ARGS,
...networkRetryFor(tool),
...proxiedLimits(),
...PROXY_THAT_REACHES_NOTHING,
];
if (!/^https:\/\//i.test(input)) return mediaFormats();
const caFile = caBundles.find((path) => existsSync(path));
if (!caFile) {
@@ -35,10 +75,13 @@ export function urlInputArgs(
`[FFmpeg] cannot verify TLS for an https input: no CA bundle at ${caBundles.join(", ")}`,
);
}
const networkRetry = tool === "ffmpeg" ? ["-reconnect_on_network_error", "1"] : [];
const renderServiceLimits = resolveRestrictPageNetwork()
? ["-protocol_whitelist", "https,tls,tcp", ...selfContainedFormatArgs()]
: mediaFormats();
const networkRetry = networkRetryFor(tool);
const renderServiceLimits =
route === "proxy"
? [...proxiedLimits(), ...PROXY_THAT_REACHES_NOTHING]
: restricted
? ["-protocol_whitelist", "https,tls,tcp", ...selfContainedFormatArgs()]
: mediaFormats();
return [
"-tls_verify",
"1",
@@ -50,6 +93,44 @@ export function urlInputArgs(
];
}
const networkRetryFor = (tool: "ffmpeg" | "ffprobe") =>
tool === "ffmpeg" ? ["-reconnect_on_network_error", "1"] : [];
const proxiedLimits = () => [
"-protocol_whitelist",
PROXIED_PROTOCOLS,
...selfContainedFormatArgs(),
];
const PROXY_THAT_REACHES_NOTHING = ["-http_proxy", "http://127.0.0.1:9"];
type Routed = { args: string[]; env: NodeJS.ProcessEnv; close(): Promise<void> };
/** Undefined, with nothing awaited, unless an input at `inputAt` must be read through the public-only proxy. */
export function withPublicOnlyProxy(
args: readonly string[],
inputAt: readonly number[],
): Promise<Routed> | undefined {
if (!resolveRestrictPageNetwork()) return undefined;
const proxied = inputAt.filter((at) => restrictedRoute(args[at]!) === "proxy");
return proxied.length === 0 ? undefined : startProxyFor(args, proxied);
}
async function startProxyFor(args: readonly string[], proxied: readonly number[]): Promise<Routed> {
const { logRefusal, startCaptureProxy } =
await import("../services/publicNetwork/captureProxy.js");
const proxy = await startCaptureProxy(null, logRefusal);
const out = [...args];
for (const at of [...proxied].sort((a, b) => b - a)) {
const flagAt = out[at - 1] === "-i" || out[at - 1] === "--" ? at - 1 : at;
out.splice(flagAt, 0, ...proxiedLimits(), "-http_proxy", proxy.url);
}
// FFmpeg before 4.4 takes an https proxy only from http_proxy, and every version skips it for a no_proxy host.
const { no_proxy: _a, NO_PROXY: _b, ...env } = process.env;
return { args: out, env: { ...env, http_proxy: proxy.url }, close: () => proxy.close() };
}
export const ffmpegInputIndexes = (args: readonly string[]): number[] =>
args.flatMap((arg, at) => (arg === "-i" && at + 1 < args.length ? [at + 1] : []));
/** A signed URL grants access until it expires: strip query strings from every string field of an error and its causes. */
export function redactUrlQueriesInError<T>(error: T): T {
if (!(error instanceof Error)) return error;
@@ -34,6 +34,7 @@ import {
trackChildProcess,
type AudioMetadata,
type RunFfmpegResult,
withPublicOnlyProxy,
} from "@hyperframes/engine";
import { redactKnownPaths, redactTelemetryString } from "@hyperframes/core";
import { measureTruePeakDbfs } from "./audioTruePeak.js";
@@ -686,7 +687,23 @@ export async function runFfprobeJson<T>(args: string[], signal?: AbortSignal): P
if (!args.includes("--")) {
throw new Error('[audioPadTrim] ffprobe args must terminate options with "--".');
}
// With a restricted page network, a URL input not declared by the render reaches only public hosts.
const proxying = withPublicOnlyProxy(args, [args.indexOf("--") + 1]);
const routed = proxying && (await proxying);
try {
return await spawnFfprobeJson<T>(routed?.args ?? args, routed?.env, signal);
} finally {
await routed?.close();
}
}
async function spawnFfprobeJson<T>(
args: string[],
env: NodeJS.ProcessEnv | undefined,
signal?: AbortSignal,
): Promise<T> {
const proc = spawn(getFfprobeBinary(), args, {
...(env && { env }),
stdio: ["ignore", "pipe", "pipe"],
windowsHide: true,
});