From 60fb9d2c5e0c213787888300dd98b8cb05fc2b31 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Tue, 10 Nov 2026 20:26:24 +0000 Subject: [PATCH] fix(producer,engine): harden render paths, render slots and local listeners (#7508) * fix(producer): build the render job before taking a render slot A request whose options fail the strict render-request validator now gets a 400 on both the blocking and streaming routes, and its inline project directory is removed. Validate workers up front like the other overrides. Co-Authored-By: Claude Opus 5.5 * fix(producer): write compiled sub-compositions only inside the compiled directory Co-Authored-By: Claude Opus 5.5 * fix(engine): keep media work files inside their directory for any element id Map a media element id to one plain path segment before it names a frame directory or an intermediate audio file. Ids that already are one plain segment map to themselves, so existing paths stay the same; the producer and the distributed plan derive the same segment. Co-Authored-By: Claude Opus 5.5 * fix(engine): listen on loopback by default in the engine file server The file server feeds the co-located capture browser, which loads it from localhost. A caller that wants another interface passes hostname. Co-Authored-By: Claude Opus 5.5 * fix(producer): refuse cross-origin browser posts to the standalone render server A web page on another origin can no longer start a render or lint through the server's POST routes. Requests without an Origin header (CLI tools, server-to-server callers) and same-origin pages are unchanged. Co-Authored-By: Claude Opus 5.5 * chore(engine): trim comments on the media path segment and file server host Co-Authored-By: Claude Opus 5.5 * fix(producer): trust the browser's Sec-Fetch-Site verdict for render server posts A same-origin page behind a reverse proxy that rewrites the Host header sent an Origin that no longer matched Host, so the cross-origin guard refused it. The guard now uses Sec-Fetch-Site when the browser sends it and falls back to the Origin and Host comparison only for browsers that do not. Co-Authored-By: Claude Opus 5.5 * fix(engine): keep generated media path names apart from plain ids and within filename limits A plain media id that already ends like a generated name now takes the hashed branch too, so it can no longer share frame folders or audio work files with the unsafe id that produced that name. The readable part of a generated name is capped at 64 UTF-8 bytes before the digest, so long or multibyte ids that contain separators stay within the 255-byte filename limit. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- packages/engine/src/index.ts | 2 + .../src/services/audioMixer.order.test.ts | 36 ++++++- packages/engine/src/services/audioMixer.ts | 7 +- .../engine/src/services/fileServer.test.ts | 33 +++++- packages/engine/src/services/fileServer.ts | 11 +- .../videoFrameExtractor.format.test.ts | 17 ++- .../src/services/videoFrameExtractor.ts | 11 +- .../engine/src/utils/mediaPathSegment.test.ts | 63 +++++++++++ packages/engine/src/utils/mediaPathSegment.ts | 28 +++++ .../producer/src/server.crossOrigin.test.ts | 69 ++++++++++++ .../producer/src/server.renderSlot.test.ts | 102 ++++++++++++++++++ packages/producer/src/server.ts | 88 ++++++++++++--- .../src/services/distributed/chunkFrames.ts | 3 +- .../src/services/distributed/planV2.test.ts | 9 +- .../src/services/distributed/planV2.ts | 9 +- .../services/render/deferredVideoFrames.ts | 3 +- .../producer/src/services/render/shared.ts | 19 ++-- .../src/services/renderOrchestrator.test.ts | 31 ++++++ 18 files changed, 498 insertions(+), 43 deletions(-) create mode 100644 packages/engine/src/utils/mediaPathSegment.test.ts create mode 100644 packages/engine/src/utils/mediaPathSegment.ts create mode 100644 packages/producer/src/server.crossOrigin.test.ts create mode 100644 packages/producer/src/server.renderSlot.test.ts diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 315c5b3508..ef8396cc7d 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -399,6 +399,8 @@ export { type KeyframeAnalysis, } from "./utils/ffprobe.js"; +export { mediaIdPathSegment } from "./utils/mediaPathSegment.js"; + export { NOT_MEDIA_PAYLOAD, NotMediaPayloadError, diff --git a/packages/engine/src/services/audioMixer.order.test.ts b/packages/engine/src/services/audioMixer.order.test.ts index e02754a504..968c42394e 100644 --- a/packages/engine/src/services/audioMixer.order.test.ts +++ b/packages/engine/src/services/audioMixer.order.test.ts @@ -1,6 +1,6 @@ -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { basename, join } from "node:path"; +import { basename, dirname, join } from "node:path"; import { setImmediate } from "node:timers/promises"; import { afterEach, expect, it, vi } from "vitest"; @@ -108,3 +108,35 @@ it.each(["flat", "one bus", "two buses"])( } }, ); + +it("keeps intermediate files for an id with parent segments inside the work directory", async () => { + const dir = mkdtempSync(join(tmpdir(), "hf-audio-id-path-")); + dirs.push(dir); + writeFileSync(join(dir, "clip.wav"), "RIFF0000WAVE"); + const workDir = join(dir, "project", "work"); + const outputs: string[] = []; + runFfmpegMock.mockImplementation(async (args: string[]) => { + const out = args.at(-1)!; + outputs.push(out); + writeFileSync(out, "RIFF0000WAVE"); + return { success: true, durationMs: 1, stderr: "", exitCode: 0 }; + }); + const element: AudioElement = { + id: "../../escaped", + src: "clip.wav", + type: "audio", + start: 0, + end: 1, + mediaStart: 0, + layer: 0, + volume: 1, + }; + + const result = await processCompositionAudio([element], dir, workDir, join(dir, "out.m4a"), 1); + + expect(result.success).toBe(true); + const trimmed = outputs.filter((out) => out.endsWith("-trimmed.wav")); + expect(trimmed).toHaveLength(1); + expect(dirname(trimmed[0]!)).toBe(workDir); + expect(existsSync(join(dir, "escaped-trimmed.wav"))).toBe(false); +}); diff --git a/packages/engine/src/services/audioMixer.ts b/packages/engine/src/services/audioMixer.ts index bffc0d394d..c765872151 100644 --- a/packages/engine/src/services/audioMixer.ts +++ b/packages/engine/src/services/audioMixer.ts @@ -72,6 +72,7 @@ import { } from "@hyperframes/core"; import { readMasterFxChain, resolveAudioGroups } from "@hyperframes/core/audio-groups"; import { applyAudioFxChain, AudioFxRenderError } from "./audioFxRender.js"; +import { mediaIdPathSegment } from "../utils/mediaPathSegment.js"; import type { AudioVolumeKeyframe } from "./audioMixer.types.js"; import { createStagingDir } from "@hyperframes/core/atomic-file"; @@ -1572,7 +1573,7 @@ export async function processCompositionAudio( let audioSrcPath = srcPath; if (element.type === "video") { - const extractedPath = join(workDir, `${element.id}-extracted.wav`); + const extractedPath = join(workDir, `${mediaIdPathSegment(element.id)}-extracted.wav`); const extractResult = await extractAudioSegment( srcPath, extractedPath, @@ -1602,7 +1603,7 @@ export async function processCompositionAudio( } audioSrcPath = extractedPath; } else { - const trimmedPath = join(workDir, `${element.id}-trimmed.wav`); + const trimmedPath = join(workDir, `${mediaIdPathSegment(element.id)}-trimmed.wav`); const prepResult = await extractAudioSegment( srcPath, trimmedPath, @@ -1671,7 +1672,7 @@ export async function processCompositionAudio( const fxResult = await applyAudioFxChain( audioSrcPath, chain, - join(workDir, `${element.id}-fx.wav`), + join(workDir, `${mediaIdPathSegment(element.id)}-fx.wav`), { trackId: element.id, signal: effectiveSignal, diff --git a/packages/engine/src/services/fileServer.test.ts b/packages/engine/src/services/fileServer.test.ts index ac1f3b99b1..c097228f0f 100644 --- a/packages/engine/src/services/fileServer.test.ts +++ b/packages/engine/src/services/fileServer.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vitest"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; -import { tmpdir } from "node:os"; +import { networkInterfaces, tmpdir } from "node:os"; import { join } from "node:path"; import { createFileServer } from "./fileServer.js"; @@ -30,3 +30,34 @@ describe("file server media responses", () => { } }); }); + +function firstExternalIpv4(): string | undefined { + return Object.values(networkInterfaces()) + .flat() + .find((address) => address?.family === "IPv4" && !address.internal)?.address; +} + +describe("file server listener", () => { + it("answers on loopback and not on another interface", async () => { + const projectDir = mkdtempSync(join(tmpdir(), "hf-engine-bind-")); + try { + writeFileSync(join(projectDir, "index.html"), ""); + const server = await createFileServer({ projectDir }); + try { + expect((await fetch(`${server.url}/index.html`)).status).toBe(200); + expect((await fetch(`http://127.0.0.1:${server.port}/index.html`)).status).toBe(200); + const external = firstExternalIpv4(); + if (external) { + const remote = fetch(`http://${external}:${server.port}/index.html`, { + signal: AbortSignal.timeout(1_000), + }); + await expect(remote).rejects.toThrow(); + } + } finally { + server.close(); + } + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }); +}); diff --git a/packages/engine/src/services/fileServer.ts b/packages/engine/src/services/fileServer.ts index d0133ed305..5ac4149080 100644 --- a/packages/engine/src/services/fileServer.ts +++ b/packages/engine/src/services/fileServer.ts @@ -52,6 +52,7 @@ export interface FileServerOptions { bodyScripts?: string[]; /** Strip embedded runtime scripts from HTML before injection. Default: true. */ stripEmbeddedRuntime?: boolean; + hostname?: string; } export interface FileServerHandle { @@ -104,7 +105,13 @@ function resolveContainedPaths( } export function createFileServer(options: FileServerOptions): Promise { - const { projectDir, compiledDir, port = 0, stripEmbeddedRuntime = true } = options; + const { + projectDir, + compiledDir, + port = 0, + stripEmbeddedRuntime = true, + hostname = "127.0.0.1", + } = options; const headScripts = options.headScripts ?? []; const bodyScripts = options.bodyScripts ?? []; @@ -146,7 +153,7 @@ export function createFileServer(options: FileServerOptions): Promise { - const server = serve({ fetch: app.fetch, port }, (info) => { + const server = serve({ fetch: app.fetch, port, hostname }, (info) => { const actualPort = info.port; const url = `http://localhost:${actualPort}`; resolve({ diff --git a/packages/engine/src/services/videoFrameExtractor.format.test.ts b/packages/engine/src/services/videoFrameExtractor.format.test.ts index a9933b9bc4..7529d7bb3c 100644 --- a/packages/engine/src/services/videoFrameExtractor.format.test.ts +++ b/packages/engine/src/services/videoFrameExtractor.format.test.ts @@ -1,6 +1,6 @@ import { vi, afterAll, beforeAll, describe, expect, it } from "vitest"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs"; -import { basename, join } from "node:path"; +import { basename, dirname, join } from "node:path"; import { tmpdir } from "node:os"; import { spawnSync } from "node:child_process"; import { @@ -148,6 +148,21 @@ describe.skipIf(!HAS_FFMPEG)("video frame extraction format", () => { expect(existsSync(frame)).toBe(true); }); + it("keeps the frames of a video whose id has parent segments inside the output directory", async () => { + const outDir = join(FIXTURE_DIR, "id-path", "work", "frames"); + mkdirSync(outDir, { recursive: true }); + const result = await extractAllVideoFrames( + [{ ...fixtureVideo(), id: "../../escaped" }], + FIXTURE_DIR, + { fps: 1, outputDir: outDir }, + ); + expect(result.errors).toEqual([]); + const extracted = result.extracted[0]!; + expect(dirname(extracted.outputDir)).toBe(outDir); + expect(dirname(extracted.framePaths.get(0)!)).toBe(extracted.outputDir); + expect(existsSync(join(FIXTURE_DIR, "id-path", "escaped"))).toBe(false); + }); + it("keeps the rightmost 8 columns of a 1080-wide (16n + 8) jpg extraction", async () => { const source = join(FIXTURE_DIR, "white-1080.mp4"); const synth = await runFfmpeg([ diff --git a/packages/engine/src/services/videoFrameExtractor.ts b/packages/engine/src/services/videoFrameExtractor.ts index acbbf702e4..2011ba02dc 100644 --- a/packages/engine/src/services/videoFrameExtractor.ts +++ b/packages/engine/src/services/videoFrameExtractor.ts @@ -83,6 +83,7 @@ import { } from "../utils/psnrFilterAvailability.js"; import { DEFAULT_CONFIG, type EngineConfig } from "../config.js"; import { unwrapTemplate } from "../utils/htmlTemplate.js"; +import { mediaIdPathSegment } from "../utils/mediaPathSegment.js"; import { FRAME_FILENAME_PREFIX, gcExtractionCache, @@ -1317,7 +1318,7 @@ async function extractVideoFramesRangeUnredacted( const fps = fpsToNumber(normalizedFps); const ffmpegFps = fpsToFfmpegArg(normalizedFps); - const videoOutputDir = outputDirOverride ?? join(outputDir, videoId); + const videoOutputDir = outputDirOverride ?? join(outputDir, mediaIdPathSegment(videoId)); if (!existsSync(videoOutputDir)) mkdirSync(videoOutputDir, { recursive: true }); let metadata: VideoMetadata; @@ -2571,7 +2572,7 @@ export async function extractAllVideoFrames( ): Promise { const { work, cacheTarget } = miss; if (!cacheTarget) { - const outputDir = join(options.outputDir, work.video.id); + const outputDir = join(options.outputDir, mediaIdPathSegment(work.video.id)); const attempted = await runVideoExtractionWithRetry( () => extractVideoFramesRange( @@ -2650,7 +2651,7 @@ export async function extractAllVideoFrames( const tailFrames = Math.min(expectedFrames, Math.ceil(fps)); if (expectedFrames <= tailFrames) return null; const deferred = (totalFrames: number): ExtractedFrames => { - const outputDir = join(options.outputDir, work.video.id); + const outputDir = join(options.outputDir, mediaIdPathSegment(work.video.id)); mkdirSync(outputDir, { recursive: true }); return { videoId: work.video.id, @@ -2694,7 +2695,7 @@ export async function extractAllVideoFrames( const totalFrames = firstTailFrame + storedTailFrames; return totalFrames <= expectedFrames * 0.6 ? null : deferred(totalFrames); } - const probeDir = join(options.outputDir, `${work.video.id}.tail-probe`); + const probeDir = join(options.outputDir, `${mediaIdPathSegment(work.video.id)}.tail-probe`); try { const tail = await extractVideoFramesRange( work.videoPath, @@ -2742,7 +2743,7 @@ export async function extractAllVideoFrames( return sliceSupersetMember( member, superset, - join(options.outputDir, work.video.id), + join(options.outputDir, mediaIdPathSegment(work.video.id)), fps, configuredFps, ); diff --git a/packages/engine/src/utils/mediaPathSegment.test.ts b/packages/engine/src/utils/mediaPathSegment.test.ts new file mode 100644 index 0000000000..c35e64c71c --- /dev/null +++ b/packages/engine/src/utils/mediaPathSegment.test.ts @@ -0,0 +1,63 @@ +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import { afterAll, describe, expect, it } from "vitest"; +import { mediaIdPathSegment } from "./mediaPathSegment.js"; + +describe("mediaIdPathSegment", () => { + it.each(["clip", "hf-video-0", "clip__hf2", "intro.bg", "my clip", "视频"])( + "keeps a plain id (%s) unchanged", + (id) => { + expect(mediaIdPathSegment(id)).toBe(id); + }, + ); + + it.each(["..", ".", "../../home", "a/b", "a\\b", "/abs", "C:evil", ""])( + "maps %j to one segment inside its root", + (id) => { + const segment = mediaIdPathSegment(id); + expect(segment).not.toMatch(/[/\\:]/); + expect(segment).not.toMatch(/^\.{1,2}$/); + const root = resolve("/work/frames"); + expect(dirname(resolve(root, segment))).toBe(root); + expect(mediaIdPathSegment(id)).toBe(segment); + }, + ); + + it("keeps distinct unsafe ids apart", () => { + expect(mediaIdPathSegment("a/b")).not.toBe(mediaIdPathSegment("a\\b")); + expect(mediaIdPathSegment("a/b")).not.toBe("a_b"); + }); + + it("never gives a plain id the name generated for another id", () => { + const generated = mediaIdPathSegment("a/b"); + expect(mediaIdPathSegment(generated)).not.toBe(generated); + expect(mediaIdPathSegment("clip~0123456789ab")).not.toBe("clip~0123456789ab"); + }); + + describe("on disk", () => { + const root = mkdtempSync(join(tmpdir(), "hf-media-segment-")); + afterAll(() => rmSync(root, { recursive: true, force: true })); + + it("keeps the audio work files of a colliding pair apart", () => { + const first = join(root, `${mediaIdPathSegment("a/b")}-trimmed.wav`); + const second = join(root, `${mediaIdPathSegment(mediaIdPathSegment("a/b"))}-trimmed.wav`); + writeFileSync(first, "first"); + writeFileSync(second, "second"); + expect(readFileSync(first, "utf8")).toBe("first"); + }); + + it.each([ + ["a long multi-component id", ["x", "y", "z"].map((c) => c.repeat(100)).join("/")], + ["a long multibyte id", `${"视频".repeat(30)}/${"视频".repeat(30)}`], + ])("names a frame folder and work file for %s", (_label, id) => { + const segment = mediaIdPathSegment(id); + const workFile = `${segment}-extracted.wav`; + expect(Buffer.byteLength(workFile, "utf8")).toBeLessThanOrEqual(255); + expect(Buffer.from(segment, "utf8").toString("utf8")).toBe(segment); + mkdirSync(join(root, segment), { recursive: true }); + writeFileSync(join(root, workFile), ""); + expect(mediaIdPathSegment(`${id}!`)).not.toBe(segment); + }); + }); +}); diff --git a/packages/engine/src/utils/mediaPathSegment.ts b/packages/engine/src/utils/mediaPathSegment.ts new file mode 100644 index 0000000000..fa793d92fb --- /dev/null +++ b/packages/engine/src/utils/mediaPathSegment.ts @@ -0,0 +1,28 @@ +import { createHash } from "node:crypto"; + +const PLAIN_SEGMENT = /^(?!\.{1,2}$)[^/\\:]+$/; +const GENERATED_SUFFIX = /~[0-9a-f]{12}$/; +const READABLE_MAX_BYTES = 64; + +/** + * The one path segment a media id names on disk. A plain segment maps to itself. Any other id, + * including a plain one that already ends like a generated name, maps to a byte-capped readable + * form plus a digest of the whole id, so the two namespaces never meet. + */ +export function mediaIdPathSegment(id: string): string { + if (PLAIN_SEGMENT.test(id) && !GENERATED_SUFFIX.test(id)) return id; + const readable = truncateUtf8(id.replace(/[/\\:]/g, "_"), READABLE_MAX_BYTES); + const digest = createHash("sha256").update(id).digest("hex").slice(0, 12); + return `${readable}~${digest}`; +} + +function truncateUtf8(text: string, maxBytes: number): string { + let bytes = 0; + let out = ""; + for (const char of text) { + bytes += Buffer.byteLength(char, "utf8"); + if (bytes > maxBytes) break; + out += char; + } + return out; +} diff --git a/packages/producer/src/server.crossOrigin.test.ts b/packages/producer/src/server.crossOrigin.test.ts new file mode 100644 index 0000000000..5dad287e68 --- /dev/null +++ b/packages/producer/src/server.crossOrigin.test.ts @@ -0,0 +1,69 @@ +import { describe, expect, it, vi } from "vitest"; + +const started = vi.hoisted(() => ({ count: 0 })); + +vi.mock("./services/renderOrchestrator.js", () => { + class RenderCancelledError extends Error {} + + return { + RenderCancelledError, + createRenderJob: (config: Record) => ({ + config, + progress: 0, + currentStage: "queued", + framesRendered: 0, + totalFrames: 0, + warnings: [], + }), + executeRenderJob: async (job: Record) => { + started.count += 1; + job.outcome = "completed"; + }, + }; +}); + +import { createProducerApp } from "./server.js"; + +function postRender(headers: Record) { + return createProducerApp().request("http://localhost:9847/render", { + method: "POST", + headers: { host: "localhost:9847", ...headers }, + body: JSON.stringify({ html: "" }), + }); +} + +describe("createProducerApp: cross-origin browser requests", () => { + it("refuses a page on another origin, even as a text/plain simple request", async () => { + const before = started.count; + const res = await postRender({ origin: "https://example.com", "content-type": "text/plain" }); + expect(res.status).toBe(403); + expect(started.count).toBe(before); + }); + + it("refuses a malformed origin", async () => { + expect((await postRender({ origin: "null" })).status).toBe(403); + }); + + it.each(["cross-site", "same-site"])( + "refuses a page the browser marks %s, even when Origin matches Host", + async (fetchSite) => { + const res = await postRender({ + origin: "http://localhost:9847", + "sec-fetch-site": fetchSite, + }); + expect(res.status).toBe(403); + }, + ); + + it.each([ + ["no Origin (curl, server-to-server)", {}], + ["a same-origin page", { origin: "http://localhost:9847" }], + [ + "a same-origin page behind a proxy that rewrites Host", + { origin: "https://render.example.com", "sec-fetch-site": "same-origin" }, + ], + ])("renders for %s", async (_label, headers) => { + const res = await postRender({ "content-type": "application/json", ...headers }); + expect(res.status).toBe(200); + }); +}); diff --git a/packages/producer/src/server.renderSlot.test.ts b/packages/producer/src/server.renderSlot.test.ts new file mode 100644 index 0000000000..e6493223d5 --- /dev/null +++ b/packages/producer/src/server.renderSlot.test.ts @@ -0,0 +1,102 @@ +import { mkdtempSync, readdirSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Hono } from "hono"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const jobFactory = vi.hoisted(() => ({ fail: false })); + +vi.mock("./services/renderOrchestrator.js", () => { + class RenderCancelledError extends Error {} + + return { + RenderCancelledError, + createRenderJob: (config: Record) => { + if (jobFactory.fail) throw new Error("job config rejected"); + return { + config, + progress: 0, + currentStage: "queued", + framesRendered: 0, + totalFrames: 0, + warnings: [], + }; + }, + executeRenderJob: async (job: Record) => { + job.outcome = "completed"; + job.currentStage = "complete"; + }, + }; +}); + +import { createRenderHandlers } from "./server.js"; + +function createApp(rendersDir: string) { + const app = new Hono(); + const handlers = createRenderHandlers({ maxConcurrentRenders: 1, rendersDir }); + app.post("/render", handlers.render); + app.post("/render/stream", handlers.renderStream); + app.get("/render/queue", handlers.queue); + return app; +} + +function post(app: Hono, path: string, body: Record) { + return app.request(path, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ html: "", ...body }), + }); +} + +async function activeRenders(app: Hono): Promise { + const res = await app.request("/render/queue"); + return ((await res.json()) as { activeRenders: number }).activeRenders; +} + +describe("render handlers: rejected job config", () => { + const priorTmp = process.env.PRODUCER_TMP_PROJECT_DIR; + let scratch: string; + const inlineProjects = () => + readdirSync(scratch).filter((name) => name.startsWith("producer-project-")); + + beforeEach(() => { + jobFactory.fail = false; + scratch = mkdtempSync(join(tmpdir(), "producer-slot-test-")); + process.env.PRODUCER_TMP_PROJECT_DIR = scratch; + }); + + afterEach(() => { + if (priorTmp === undefined) delete process.env.PRODUCER_TMP_PROJECT_DIR; + else process.env.PRODUCER_TMP_PROJECT_DIR = priorTmp; + rmSync(scratch, { recursive: true, force: true }); + }); + + it.each([0, 1.5, -2])("answers 400 for workers %s on both routes", async (workers) => { + const app = createApp(join(scratch, "renders")); + const blocking = await post(app, "/render", { workers }); + expect(blocking.status).toBe(400); + expect(await blocking.json()).toMatchObject({ error: "workers must be a positive integer" }); + + const streamed = await (await post(app, "/render/stream", { workers })).text(); + expect(streamed).toContain("workers must be a positive integer"); + expect(inlineProjects()).toEqual([]); + }); + + it.each(["/render", "/render/stream"])( + "frees the slot and the inline project when job construction throws on %s", + async (route) => { + const app = createApp(join(scratch, "renders")); + jobFactory.fail = true; + const failed = await post(app, route, {}); + if (route === "/render") expect(failed.status).toBe(400); + expect(await failed.text()).toContain("job config rejected"); + expect(await activeRenders(app)).toBe(0); + expect(inlineProjects()).toEqual([]); + + jobFactory.fail = false; + const next = await post(app, "/render", {}); + expect(next.status).toBe(200); + expect(await activeRenders(app)).toBe(0); + }, + ); +}); diff --git a/packages/producer/src/server.ts b/packages/producer/src/server.ts index 7defb681cb..24b7fccaab 100644 --- a/packages/producer/src/server.ts +++ b/packages/producer/src/server.ts @@ -351,6 +351,35 @@ function resolvePreparedRenderOutput( return { input, cleanupProjectDir, absoluteOutputPath }; } +type PreparedRenderJob = + | { + job: RenderJob; + input: RenderInput; + cleanupProjectDir?: string; + absoluteOutputPath: string; + } + | { error: string }; + +/** + * Build the render job before a render slot is taken, so a request the strict + * render-request validator rejects never holds a slot. On failure the inline + * project directory is removed here, since no handler cleanup will run. + */ +function prepareRenderJob( + prepared: PreparedRenderInput, + rendersDir: string, + log: ProducerLogger, +): PreparedRenderJob { + try { + const output = resolvePreparedRenderOutput(prepared, rendersDir, log); + const job = createRenderJob(buildRenderJobConfig(output.input, output.absoluteOutputPath, log)); + return { ...output, job }; + } catch (error) { + cleanupTempDir(prepared.cleanupProjectDir, log); + return { error: error instanceof Error ? error.message : String(error) }; + } +} + /** * Validate explicitly-supplied render overrides that can't be sanely coerced. * Returns an error string for a clean 400, or `undefined` when the body is @@ -367,6 +396,9 @@ function validateRenderOverrides(body: Record): string | undefi ) { return "maxRenderMs must be a non-negative integer number of milliseconds"; } + if (typeof body.workers === "number" && (!Number.isInteger(body.workers) || body.workers < 1)) { + return "workers must be a positive integer"; + } if (body.variables !== undefined && !isPlainObject(body.variables)) { return 'variables must be a JSON object keyed by variable id (e.g. {"title":"Hello"})'; } @@ -750,11 +782,11 @@ export function createRenderHandlers(options: HandlerOptions = {}): RenderHandle return c.json({ success: false, requestId, error: preparedResult.error }, 400); } - const { input, cleanupProjectDir, absoluteOutputPath } = resolvePreparedRenderOutput( - preparedResult.prepared, - rendersDir, - log, - ); + const preparedJob = prepareRenderJob(preparedResult.prepared, rendersDir, log); + if ("error" in preparedJob) { + return c.json({ success: false, requestId, error: preparedJob.error }, 400); + } + const { job, input, cleanupProjectDir, absoluteOutputPath } = preparedJob; const release = await renderSemaphore.acquire(); @@ -765,8 +797,6 @@ export function createRenderHandlers(options: HandlerOptions = {}): RenderHandle quality: input.quality, }); - const job = createRenderJob(buildRenderJobConfig(input, absoluteOutputPath, log)); - try { await executeRenderJob( job, @@ -839,15 +869,21 @@ export function createRenderHandlers(options: HandlerOptions = {}): RenderHandle const prepared = await prepareSseRenderRequest(c, stream, requestId); if (!prepared) return; - const { input, cleanupProjectDir, absoluteOutputPath } = resolvePreparedRenderOutput( - prepared, - rendersDir, - log, - ); + const preparedJob = prepareRenderJob(prepared, rendersDir, log); + if ("error" in preparedJob) { + await stream.writeSSE({ + data: JSON.stringify({ + type: "error", + requestId, + error: preparedJob.error, + stage: "validation", + }), + }); + return; + } + const { job, input, cleanupProjectDir, absoluteOutputPath } = preparedJob; log.info("render-stream started", { requestId, projectDir: input.projectDir }); - - const job = createRenderJob(buildRenderJobConfig(input, absoluteOutputPath, log)); const abortController = new AbortController(); const onRequestAbort = () => abortController.abort(new RenderCancelledError("request_aborted")); @@ -948,6 +984,23 @@ export function createRenderHandlers(options: HandlerOptions = {}): RenderHandle // Public app factory // --------------------------------------------------------------------------- +/** + * True when a browser sent the request from a page on another origin. The browser's own + * Sec-Fetch-Site verdict wins when present, so a same-origin page behind a proxy that rewrites + * Host is still served; older browsers fall back to comparing Origin with Host. + */ +function isCrossOriginBrowserRequest(c: Context): boolean { + const fetchSite = c.req.header("sec-fetch-site"); + if (fetchSite) return fetchSite !== "same-origin" && fetchSite !== "none"; + const origin = c.req.header("origin"); + if (!origin) return false; + try { + return new URL(origin).host !== c.req.header("host"); + } catch { + return true; + } +} + /** * Create a Hono app with clean public routes for OSS use. */ @@ -955,6 +1008,13 @@ export function createProducerApp(options: HandlerOptions = {}): Hono { const app = new Hono(); const handlers = createRenderHandlers(options); + app.post("*", async (c, next) => { + if (isCrossOriginBrowserRequest(c)) { + return c.json({ success: false, error: "Cross-origin requests are not allowed" }, 403); + } + await next(); + }); + app.get("/health", handlers.health); app.post("/render", handlers.render); app.post("/render/stream", handlers.renderStream); diff --git a/packages/producer/src/services/distributed/chunkFrames.ts b/packages/producer/src/services/distributed/chunkFrames.ts index 3288409148..a57fbb8d19 100644 --- a/packages/producer/src/services/distributed/chunkFrames.ts +++ b/packages/producer/src/services/distributed/chunkFrames.ts @@ -10,6 +10,7 @@ import { type EngineConfig, type ExtractedFrames, type FrameLookupTable, + mediaIdPathSegment, type ShownBox, } from "@hyperframes/engine"; import { resolveVideoCaptureBeyondViewport } from "../render/captureBeyondViewport.js"; @@ -85,7 +86,7 @@ export function rebuildExtractedFramesFromPlanDir( ): ExtractedFrames[] { const result: ExtractedFrames[] = []; for (const v of videos) { - const outputDir = join(planDir, "video-frames", v.videoId); + const outputDir = join(planDir, "video-frames", mediaIdPathSegment(v.videoId)); if (!existsSync(outputDir)) { throw new Error( `[renderChunk] planDir missing extracted video frames for ${JSON.stringify(v.videoId)}: ` + diff --git a/packages/producer/src/services/distributed/planV2.test.ts b/packages/producer/src/services/distributed/planV2.test.ts index 2775f080d2..e503362469 100644 --- a/packages/producer/src/services/distributed/planV2.test.ts +++ b/packages/producer/src/services/distributed/planV2.test.ts @@ -14,6 +14,7 @@ import { import { tmpdir } from "node:os"; import { pathToFileURL } from "node:url"; import { dirname, join } from "node:path"; +import { mediaIdPathSegment } from "@hyperframes/engine"; import { planFileSha256, recomputePlanHashFromPlanDir } from "../render/stages/freezePlan.js"; import { canonicalJsonStringify, sha256Hex } from "../render/stages/planHash.js"; import { chunkAudioSlicePath } from "./chunkAudio.js"; @@ -776,13 +777,13 @@ describe("Plan v2 manifest", () => { }); } - it("rejects an extracted video identifier that escapes the video-frame root", () => { + it("reads an extracted video identifier with parent segments only inside the video-frame root", () => { const root = tempPath("hf-plan-v2-unsafe-video-id-"); const v1 = createV1Plan(root, { video: true, videoId: "../escape" }); + const frameRoot = join(v1, "video-frames", mediaIdPathSegment("../escape")); - expect(() => createPlanV2FromV1(v1, join(root, "v2"))).toThrow( - 'unsafe extracted video id: "../escape"', - ); + expect(() => createPlanV2FromV1(v1, join(root, "v2"))).toThrow(frameRoot); + expect(existsSync(join(v1, "escape"))).toBe(false); }); it("falls back to the full source frame pack when video metadata is absent", () => { diff --git a/packages/producer/src/services/distributed/planV2.ts b/packages/producer/src/services/distributed/planV2.ts index ee1eb872e9..ba225b1c1d 100644 --- a/packages/producer/src/services/distributed/planV2.ts +++ b/packages/producer/src/services/distributed/planV2.ts @@ -30,7 +30,12 @@ import { import { tmpdir } from "node:os"; import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; import { DISTRIBUTED_FPS, type DistributedFps, isDistributedFps } from "@hyperframes/core"; -import { createFrameLookupTable, type ExtractedFrames, renameWithRetry } from "@hyperframes/engine"; +import { + createFrameLookupTable, + type ExtractedFrames, + mediaIdPathSegment, + renameWithRetry, +} from "@hyperframes/engine"; import { readPlanRemoteFiles, planFileSha256, @@ -276,7 +281,7 @@ function isExtractionCacheCompleteSentinelPath(path: string): boolean { function resolveExtractedVideoOutputDir(planDir: string, videoId: string): string { const videoRoot = resolve(planDir, "video-frames"); - const outputDir = resolve(videoRoot, videoId); + const outputDir = resolve(videoRoot, mediaIdPathSegment(videoId)); if (outputDir === videoRoot || !outputDir.startsWith(`${videoRoot}${sep}`)) { throw new PlanV2IntegrityError(`unsafe extracted video id: ${JSON.stringify(videoId)}`); } diff --git a/packages/producer/src/services/render/deferredVideoFrames.ts b/packages/producer/src/services/render/deferredVideoFrames.ts index 5b19102c98..244af33078 100644 --- a/packages/producer/src/services/render/deferredVideoFrames.ts +++ b/packages/producer/src/services/render/deferredVideoFrames.ts @@ -6,6 +6,7 @@ import { type ExtractedFrames, extractVideoFramesRange, type FrameRange, + mediaIdPathSegment, runVideoExtractionWithRetry, type ShownBox, type VideoElement, @@ -71,7 +72,7 @@ export async function extractDeferredFrames(input: { const shown = shownDeferredFrameIndexes(input, input.frames, input.fps); return Promise.all( deferred.map(async (video) => { - const outputDir = join(input.outputRoot, video.videoId); + const outputDir = join(input.outputRoot, mediaIdPathSegment(video.videoId)); const indexes = [...(shown.get(video.videoId) ?? [])].sort((a, b) => a - b); if (indexes.length === 0) { return { ...video, outputDir, framePaths: new Map(), ownedByLookup: false }; diff --git a/packages/producer/src/services/render/shared.ts b/packages/producer/src/services/render/shared.ts index 73b29c6e70..14adf30809 100644 --- a/packages/producer/src/services/render/shared.ts +++ b/packages/producer/src/services/render/shared.ts @@ -25,12 +25,13 @@ import { checkOutputResolutionCompatibility, type CanvasResolution, } from "@hyperframes/core"; -import type { - AudioElement, - ExtractedFrames, - ImageElement, - ParallelProgress, - VideoElement, +import { + mediaIdPathSegment, + type AudioElement, + type ExtractedFrames, + type ImageElement, + type ParallelProgress, + type VideoElement, } from "@hyperframes/engine"; import type { CompiledComposition } from "../htmlCompiler.js"; import { defaultLogger, type ProducerLogger } from "../../logger.js"; @@ -162,6 +163,10 @@ export function writeCompiledArtifacts( for (const [srcPath, html] of compiled.subCompositions) { const outPath = join(compileDir, srcPath); + if (!isPathInside(outPath, compileDir)) { + console.warn(`[Render] Skipping sub-composition with unsafe path: ${srcPath}`); + continue; + } mkdirSync(dirname(outPath), { recursive: true }); writeFileSync(outPath, html, "utf-8"); } @@ -677,7 +682,7 @@ export function materializeExtractedFramesForCompiledDir( const resolvedOut = pathModule.resolve(ext.outputDir); if (isPathInside(resolvedOut, resolvedCompiledDir, { pathModule })) continue; - const linkPath = pathModule.join(compiledFrameRoot, ext.videoId); + const linkPath = pathModule.join(compiledFrameRoot, mediaIdPathSegment(ext.videoId)); if (!fileSystem.existsSync(linkPath)) { fileSystem.mkdirSync(pathModule.dirname(linkPath), { recursive: true }); stageExtractedFrameDir( diff --git a/packages/producer/src/services/renderOrchestrator.test.ts b/packages/producer/src/services/renderOrchestrator.test.ts index dc22df0d20..73cdb828e1 100644 --- a/packages/producer/src/services/renderOrchestrator.test.ts +++ b/packages/producer/src/services/renderOrchestrator.test.ts @@ -1189,6 +1189,37 @@ describe("writeCompiledArtifacts — external assets on Windows drive-letter pat const escapeTarget = join(workDir, "etc", "passwd"); expect(existsSync(escapeTarget)).toBe(false); }); + + it("writes sub-compositions only inside compileDir", () => { + const sandboxRoot = mkdtempSync(join(tmpdir(), "hf-orch-root-")); + tempDirs.push(sandboxRoot); + const workDir = join(sandboxRoot, "work"); + mkdirSync(workDir, { recursive: true }); + + const compiled = { + html: "", + subCompositions: new Map([ + ["scenes/intro.html", "intro"], + ["outro.html?v=2", "outro"], + ["outro.html?/../../../escaped.html", "escaped"], + ]), + videos: [], + audios: [], + unresolvedCompositions: [], + externalAssets: new Map(), + width: 1920, + height: 1080, + staticDuration: 10, + renderModeHints: { recommendScreenshot: false, reasons: [] }, + hasShaderTransitions: false, + }; + + writeCompiledArtifacts(compiled, workDir, false); + + expect(readFileSync(join(workDir, "compiled", "scenes", "intro.html"), "utf-8")).toBe("intro"); + expect(readFileSync(join(workDir, "compiled", "outro.html?v=2"), "utf-8")).toBe("outro"); + expect(existsSync(join(sandboxRoot, "escaped.html"))).toBe(false); + }); }); function createCompiledComposition(