Skip to content

Commit bbdfee1

Browse files
authored
fix(engine): validate remote download integrity (#2938)
1 parent 7bf425b commit bbdfee1

11 files changed

Lines changed: 2219 additions & 133 deletions

File tree

‎packages/cli/src/server/studioServer.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
399399
await import("../../../producer/src/services/deterministicFonts.js");
400400
const { prepareAnimatedGifInputs } =
401401
await import("../../../producer/src/services/animatedGifPrep.js");
402-
const { downloadToTemp } = await import("../../../producer/src/utils/urlDownloader.js");
402+
const { downloadToTemp, writeUrlDownloadTelemetry } =
403+
await import("../../../producer/src/utils/urlDownloader.js");
403404
const gifOutputDir = join(project.dir, ".hyperframes", "prepared-assets", "gif");
404405
const gifDownloadDir = join(project.dir, ".hyperframes", "prepared-assets", "downloads");
405406
const prepared = await prepareAnimatedGifInputs(html, {
@@ -408,7 +409,11 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
408409
outputDir: gifOutputDir,
409410
outputSrcPrefix: ".hyperframes/prepared-assets/gif",
410411
cacheDir: gifOutputDir,
411-
sourceAssets: await downloadRemoteGifImageSources(html, gifDownloadDir, downloadToTemp),
412+
sourceAssets: await downloadRemoteGifImageSources(html, gifDownloadDir, (url, destDir) =>
413+
downloadToTemp(url, destDir, undefined, undefined, undefined, {
414+
onTelemetry: writeUrlDownloadTelemetry,
415+
}),
416+
),
412417
});
413418
return injectDeterministicFontFaces(prepared.html);
414419
},

‎packages/engine/src/index.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,18 @@ export {
273273
type KeyframeAnalysis,
274274
} from "./utils/ffprobe.js";
275275

276-
export { assertPublicHttpsUrl, downloadToTemp, isHttpUrl } from "./utils/urlDownloader.js";
276+
export {
277+
assertPublicHttpsUrl,
278+
downloadToTemp,
279+
fetchPublicHttpsText,
280+
isHttpUrl,
281+
safeDownloadUrlIdentity,
282+
writeUrlDownloadTelemetry,
283+
type SafeDownloadUrlIdentity,
284+
type UrlDownloadOptions,
285+
type UrlDownloadTelemetry,
286+
type PublicHttpsTextOptions,
287+
} from "./utils/urlDownloader.js";
277288
export {
278289
runFfmpeg,
279290
formatFfmpegError,

‎packages/engine/src/services/audioMixer.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe("processCompositionAudio", () => {
5252
const tempDirs: string[] = [];
5353

5454
afterEach(() => {
55+
vi.unstubAllGlobals();
5556
runFfmpegMock.mockClear();
5657
extractAudioMetadataMock.mockReset();
5758
extractAudioMetadataMock.mockResolvedValue({
@@ -66,6 +67,44 @@ describe("processCompositionAudio", () => {
6667
}
6768
});
6869

70+
it("classifies an HTML-as-200 audio source as deterministic user input", async () => {
71+
const baseDir = mkdtempSync(join(tmpdir(), "hf-audio-base-"));
72+
const workDir = mkdtempSync(join(tmpdir(), "hf-audio-work-"));
73+
tempDirs.push(baseDir, workDir);
74+
const fetchMock = vi
75+
.fn()
76+
.mockResolvedValue(new Response("<!doctype html><html><body>denied</body></html>"));
77+
vi.stubGlobal("fetch", fetchMock);
78+
79+
const result = await processCompositionAudio(
80+
[
81+
{
82+
id: "remote-voice",
83+
src: "https://cdn.example/voice",
84+
start: 0,
85+
end: 2,
86+
mediaStart: 0,
87+
layer: 0,
88+
volume: 1,
89+
type: "audio",
90+
},
91+
],
92+
baseDir,
93+
workDir,
94+
join(baseDir, "out.m4a"),
95+
2,
96+
);
97+
98+
expect(fetchMock).toHaveBeenCalledTimes(1);
99+
expect(result.failures).toEqual([
100+
expect.objectContaining({
101+
stage: "download",
102+
owner: "user",
103+
retryable: false,
104+
}),
105+
]);
106+
});
107+
69108
it.each([
70109
{
71110
message: "AbortError: ffprobe operation aborted",

‎packages/engine/src/services/audioMixer.ts‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { closeSync, existsSync, mkdirSync, mkdtempSync, openSync, rmSync, writeF
99
import { join, dirname } from "path";
1010
import { parseHTML } from "linkedom";
1111
import { extractAudioMetadata } from "../utils/ffprobe.js";
12-
import { downloadToTemp, isHttpUrl } from "../utils/urlDownloader.js";
12+
import {
13+
downloadToTemp,
14+
isHttpUrl,
15+
UrlDownloadError,
16+
writeUrlDownloadTelemetry,
17+
} from "../utils/urlDownloader.js";
1318
import { DEFAULT_CONFIG, type EngineConfig } from "../config.js";
1419
import { formatFfmpegError, runFfmpeg, type RunFfmpegResult } from "../utils/runFfmpeg.js";
1520
import { unwrapTemplate } from "../utils/htmlTemplate.js";
@@ -241,16 +246,23 @@ function probeFailure(message: string, elementId: string): AudioProcessingFailur
241246
};
242247
}
243248

244-
function downloadFailure(message: string, elementId: string): AudioProcessingFailure {
249+
function downloadFailure(error: unknown, elementId: string): AudioProcessingFailure {
250+
const message = error instanceof Error ? error.message : String(error);
245251
const invalidSource =
246-
/(?:invalid URL|only HTTPS|private\/reserved|HTTP (?:400|401|403|404|405|410|422)\b)/i.test(
247-
message,
248-
);
252+
error instanceof UrlDownloadError
253+
? error.kind === "http_not_found" ||
254+
error.kind === "http_rejected" ||
255+
error.kind === "invalid_payload" ||
256+
error.kind === "cancelled"
257+
: /(?:invalid URL|only HTTPS|private\/reserved|HTTP (?:400|401|403|404|405|410|422)\b)/i.test(
258+
message,
259+
);
260+
const retryable = error instanceof UrlDownloadError ? error.retryable : !invalidSource;
249261
return {
250262
stage: "download",
251263
reason: "download_failed",
252264
owner: invalidSource ? "user" : "system",
253-
retryable: !invalidSource,
265+
retryable,
254266
elementId,
255267
detail: boundedDetail(`Download failed for audio element ${elementId}: ${message}`),
256268
};
@@ -712,11 +724,11 @@ export async function processCompositionAudio(
712724

713725
if (isHttpUrl(srcPath)) {
714726
try {
715-
srcPath = await downloadToTemp(srcPath, workDir);
727+
srcPath = await downloadToTemp(srcPath, workDir, undefined, signal, undefined, {
728+
onTelemetry: writeUrlDownloadTelemetry,
729+
});
716730
} catch (err: unknown) {
717-
failures.push(
718-
downloadFailure(err instanceof Error ? err.message : String(err), element.id),
719-
);
731+
failures.push(downloadFailure(err, element.id));
720732
return;
721733
}
722734
}

‎packages/engine/src/services/videoFrameExtractor.errorClassification.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vitest";
2-
import { classifyFfmpegSpawnError } from "./videoFrameExtractor.js";
2+
import { UrlDownloadError } from "../utils/urlDownloader.js";
3+
import { classifyFfmpegSpawnError, classifyVideoExtractionError } from "./videoFrameExtractor.js";
34

45
describe("classifyFfmpegSpawnError", () => {
56
it.each(["ENOENT", "EACCES", "ENOEXEC", "UNKNOWN"])(
@@ -18,3 +19,21 @@ describe("classifyFfmpegSpawnError", () => {
1819
});
1920
});
2021
});
22+
23+
describe("classifyVideoExtractionError download integrity", () => {
24+
it("keeps deterministic HTML payloads non-retryable and user-owned as invalid media", () => {
25+
const classified = classifyVideoExtractionError(
26+
new UrlDownloadError("invalid_payload", false, "HTML payload"),
27+
);
28+
expect(classified).toMatchObject({ kind: "invalid_media", retryable: false });
29+
});
30+
31+
it.each(["range_protocol", "length_mismatch", "hash_mismatch"] as const)(
32+
"keeps %s retryable after the downloader's one clean refetch is exhausted",
33+
(kind) => {
34+
expect(
35+
classifyVideoExtractionError(new UrlDownloadError(kind, true, "integrity failure")),
36+
).toMatchObject({ kind: "download_transient", retryable: true });
37+
},
38+
);
39+
});

‎packages/engine/src/services/videoFrameExtractor.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ import {
2828
isHdrColorSpace as isHdrColorSpaceUtil,
2929
type HdrTransfer,
3030
} from "../utils/hdr.js";
31-
import { downloadToTemp, isHttpUrl, UrlDownloadError } from "../utils/urlDownloader.js";
31+
import {
32+
downloadToTemp,
33+
isHttpUrl,
34+
UrlDownloadError,
35+
writeUrlDownloadTelemetry,
36+
} from "../utils/urlDownloader.js";
3237
import { runFfmpeg } from "../utils/runFfmpeg.js";
3338
import { DEFAULT_CONFIG, type EngineConfig } from "../config.js";
3439
import { unwrapTemplate } from "../utils/htmlTemplate.js";
@@ -342,6 +347,14 @@ export function classifyVideoExtractionError(error: unknown): VideoSourceExtract
342347
diagnostic,
343348
);
344349
}
350+
if (error.kind === "invalid_payload") {
351+
return new VideoSourceExtractionError(
352+
"invalid_media",
353+
false,
354+
"Video source download returned a non-media payload",
355+
diagnostic,
356+
);
357+
}
345358
if (error.retryable) {
346359
return new VideoSourceExtractionError(
347360
"download_transient",
@@ -1424,8 +1437,13 @@ export async function extractAllVideoFrames(
14241437
if (isHttpUrl(videoPath)) {
14251438
const downloadDir = join(options.outputDir, "_downloads");
14261439
mkdirSync(downloadDir, { recursive: true });
1427-
videoPath = await downloadToTemp(videoPath, downloadDir, undefined, signal, () =>
1428-
recordTransientRetries(1),
1440+
videoPath = await downloadToTemp(
1441+
videoPath,
1442+
downloadDir,
1443+
undefined,
1444+
signal,
1445+
() => recordTransientRetries(1),
1446+
{ onTelemetry: writeUrlDownloadTelemetry },
14291447
);
14301448
}
14311449

0 commit comments

Comments
 (0)