diff --git a/packages/cli/src/commands/benchmark.ts b/packages/cli/src/commands/benchmark.ts index 882e365348..dbf2cd65fe 100644 --- a/packages/cli/src/commands/benchmark.ts +++ b/packages/cli/src/commands/benchmark.ts @@ -128,6 +128,7 @@ export default defineCommand({ fps: config.fps, quality: config.quality, workers: config.workers, + allowOutsideProjectAssets: true, }); await producer.executeRenderJob(job, project.dir, outputPath); const elapsedMs = Date.now() - startTime; diff --git a/packages/cli/src/commands/render.ts b/packages/cli/src/commands/render.ts index acc8a5bc46..9e4380998f 100644 --- a/packages/cli/src/commands/render.ts +++ b/packages/cli/src/commands/render.ts @@ -1229,7 +1229,9 @@ async function executeLocalRender( signal: cancellation.signal, }); } - const job = producer.createRenderJob(producer.renderConfigFromRequest(request, { logger })); + const job = producer.createRenderJob( + producer.renderConfigFromRequest(request, { logger, allowOutsideProjectAssets: true }), + ); const onProgress = options.quiet ? undefined @@ -1360,6 +1362,7 @@ async function renderRange(input: { config, input.range, input.outputPath, + { allowOutsideProjectAssets: true }, ); framesEncoded = spanResult.framesEncoded; reference = producer.referenceBackgroundResult(spanResult[producer.REFERENCE_BACKGROUND_FIELD]); diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts index 77b8c8082d..aa10d8b6cd 100644 --- a/packages/cli/src/server/studioServer.ts +++ b/packages/cli/src/server/studioServer.ts @@ -683,6 +683,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { ...encoderJobOptions(opts), ...(opts.useGpu ? { useGpu: true } : {}), ...(opts.pageMotionBlur ? { pageMotionBlur: opts.pageMotionBlur } : {}), + allowOutsideProjectAssets: true, }); renderJob = job; const onProgress = (j: { progress: number; currentStage?: string }) => { diff --git a/packages/producer/scripts/integration-tests.json b/packages/producer/scripts/integration-tests.json index b0d0c01722..b073f6211e 100644 --- a/packages/producer/scripts/integration-tests.json +++ b/packages/producer/scripts/integration-tests.json @@ -27,6 +27,7 @@ "src/services/fileServer.test.ts", "src/services/fileServer.file-race.test.ts", "src/services/fileServer.listenError.test.ts", + "src/services/outsideProjectAssets.test.ts", "src/services/healthWorker.test.ts", "src/services/scriptFailureAttribution.test.ts", "src/services/strictVariablesSession.test.ts", diff --git a/packages/producer/src/regression-harness-distributed.ts b/packages/producer/src/regression-harness-distributed.ts index fb9c043ab4..6ad0c2e679 100644 --- a/packages/producer/src/regression-harness-distributed.ts +++ b/packages/producer/src/regression-harness-distributed.ts @@ -140,6 +140,7 @@ export interface RunDistributedSimulatedInput { maxParallelChunks?: number; /** Forwarded to `plan()` and re-applied by `renderChunk()` at boot. */ variables?: Record; + allowOutsideProjectAssets?: boolean; } /** @@ -190,6 +191,7 @@ export async function runDistributedSimulatedRender( variables: input.variables, }, planDir, + { allowOutsideProjectAssets: input.allowOutsideProjectAssets }, ); // Step B: render every chunk. Sequential to keep the harness predictable — diff --git a/packages/producer/src/regression-harness.ts b/packages/producer/src/regression-harness.ts index a573444a7f..5316a274a4 100644 --- a/packages/producer/src/regression-harness.ts +++ b/packages/producer/src/regression-harness.ts @@ -148,6 +148,8 @@ type TestMetadata = { * `--mode=in-process`. Default is the plan's own default (16). */ maxParallelChunks?: number; + /** Render as a trusted local caller (CLI, Studio) that may import files outside `src/`. */ + allowOutsideProjectAssets?: boolean; }; }; @@ -392,6 +394,12 @@ function validateMetadata(meta: unknown): TestMetadata { if (rc.hdr !== undefined && typeof rc.hdr !== "boolean") { throw new Error("meta.json: 'renderConfig.hdr' must be a boolean (or omit for false)"); } + if ( + rc.allowOutsideProjectAssets !== undefined && + typeof rc.allowOutsideProjectAssets !== "boolean" + ) { + throw new Error("meta.json: 'renderConfig.allowOutsideProjectAssets' must be a boolean"); + } if (rc.experimentalFastCapture !== undefined && typeof rc.experimentalFastCapture !== "boolean") { throw new Error( "meta.json: 'renderConfig.experimentalFastCapture' must be a boolean (or omit for false)", @@ -1132,6 +1140,7 @@ export async function runTestSuite( chunkSize: suite.meta.renderConfig.chunkSize, maxParallelChunks: suite.meta.renderConfig.maxParallelChunks, variables: suite.meta.renderConfig.variables, + allowOutsideProjectAssets: suite.meta.renderConfig.allowOutsideProjectAssets, }; if (options.mode === "lambda-local") { const runLambdaLocalRender = await loadLambdaLocalRender(); @@ -1166,6 +1175,7 @@ export async function runTestSuite( debug: false, hdrMode: suite.meta.renderConfig.hdr ? "force-hdr" : "force-sdr", variables: suite.meta.renderConfig.variables, + allowOutsideProjectAssets: suite.meta.renderConfig.allowOutsideProjectAssets, }); await executeRenderJob(job, tempSrcDir, renderedOutputPath); diff --git a/packages/producer/src/renderRequest.ts b/packages/producer/src/renderRequest.ts index 870ad35bee..8ac52fce94 100644 --- a/packages/producer/src/renderRequest.ts +++ b/packages/producer/src/renderRequest.ts @@ -285,13 +285,14 @@ export function createRenderRequest(input: CreateRenderRequestInput): RenderRequ export function renderConfigFromRequest( request: RenderRequest, - runtime: { logger?: ProducerLogger } = {}, + runtime: { logger?: ProducerLogger; allowOutsideProjectAssets?: boolean } = {}, ): RenderConfig { const { engineConfig, distributed: _distributed, ...options } = request.options; return { ...options, producerConfig: engineConfig, logger: runtime.logger, + allowOutsideProjectAssets: runtime.allowOutsideProjectAssets === true, }; } diff --git a/packages/producer/src/services/animatedGifPrep.ts b/packages/producer/src/services/animatedGifPrep.ts index 5f9a0bdf23..7bce6639a6 100644 --- a/packages/producer/src/services/animatedGifPrep.ts +++ b/packages/producer/src/services/animatedGifPrep.ts @@ -7,6 +7,7 @@ import { parseAnimatedGifMetadata, type AnimatedGifMetadata } from "@hyperframes import { DEFAULT_VP9_CPU_USED, runFfmpeg, renameWithRetry } from "@hyperframes/engine"; import { namesAnotherNetworkRoot } from "@hyperframes/parsers/asset-resolution"; import { isHttpUrl } from "../utils/urlDownloader.js"; +import { escapesAssetRoot } from "../utils/paths.js"; import { encoderFailureError } from "./render/encoderInterruption.js"; const PREPARED_GIF_SUBDIR = "_animated_gif"; @@ -32,6 +33,7 @@ export interface PrepareAnimatedGifInputsOptions { sourceAssets?: Map; timeoutMs?: number; transcode?: (input: AnimatedGifTranscodeRequest) => Promise; + assetRoot?: string; } export interface AnimatedGifTranscodeRequest { @@ -79,7 +81,10 @@ function readLoopOverride(el: Element): boolean | null { export function resolveLocalImageSourcePath( src: string, - options: Pick, + options: Pick< + PrepareAnimatedGifInputsOptions, + "projectDir" | "downloadDir" | "sourceAssets" | "assetRoot" + >, ): string | null { const trimmed = src.trim(); if (!trimmed || trimmed.startsWith("data:")) return null; @@ -103,7 +108,10 @@ export function resolveLocalImageSourcePath( resolve(options.downloadDir, normalizedBase), ]; - return candidates.find((candidate) => existsSync(candidate)) ?? null; + const allowed = (candidate: string) => + !escapesAssetRoot(candidate, options.assetRoot) || + !escapesAssetRoot(candidate, options.downloadDir); + return candidates.find((candidate) => existsSync(candidate) && allowed(candidate)) ?? null; } function isUsableFile(path: string): boolean { diff --git a/packages/producer/src/services/assetMediaType.ts b/packages/producer/src/services/assetMediaType.ts index 98bac61652..7073ff09b1 100644 --- a/packages/producer/src/services/assetMediaType.ts +++ b/packages/producer/src/services/assetMediaType.ts @@ -1,4 +1,5 @@ import { existsSync } from "node:fs"; +import { join } from "node:path"; import { fingerprintElementId, isNotMediaPayload, @@ -11,6 +12,8 @@ import { type VideoElement, } from "@hyperframes/engine"; import { withMediaProbeSlot } from "../utils/mediaProbeConcurrency.js"; +import { escapesAssetRoot } from "../utils/paths.js"; +import type { ProducerLogger } from "../logger.js"; export const ASSET_MEDIA_TYPE_MISMATCH = "ASSET_MEDIA_TYPE_MISMATCH" as const; @@ -88,6 +91,28 @@ function isRemoteOrInlineSource(src: string): boolean { return /^(?:https?:|data:|blob:|about:)/i.test(src.trim()); } +/** Treats local media outside the project, compiled and download dirs as missing: no probe or extraction opens it. */ +export function dropMediaOutsideProject(input: { + projectDir: string; + workDir: string; + composition: { videos: VideoElement[]; audios: AudioElement[]; images: ImageElement[] }; + log: ProducerLogger; +}): void { + const { projectDir, workDir, composition, log } = input; + const compiledDir = join(workDir, "compiled"); + const roots = [projectDir, compiledDir, join(workDir, "downloads")]; + const staysInside = ({ id, src }: { id: string; src: string }) => { + if (!src || isRemoteOrInlineSource(src)) return true; + const path = resolveProjectRelativeSrc(src, projectDir, compiledDir); + if (roots.some((root) => !escapesAssetRoot(path, root))) return true; + log.warn(`[Render] Media "${id}" source is missing: it is outside the project folder`, { src }); + return false; + }; + composition.videos = composition.videos.filter(staysInside); + composition.audios = composition.audios.filter(staysInside); + composition.images = composition.images.filter(staysInside); +} + /** * Fail early when a successfully-probed local asset cannot satisfy the HTML * element that owns it. Missing, corrupt, remote-at-runtime, and unprobeable diff --git a/packages/producer/src/services/distributed/plan.ts b/packages/producer/src/services/distributed/plan.ts index fb2cb1c6ca..75fda52e57 100644 --- a/packages/producer/src/services/distributed/plan.ts +++ b/packages/producer/src/services/distributed/plan.ts @@ -49,6 +49,7 @@ import { fpsToNumber, HF_COLOR_GRADING_ATTR, } from "@hyperframes/core"; +import { isSafePath } from "@hyperframes/core/safe-path"; import { type EngineConfig, type VideoFrameFormat, @@ -353,6 +354,8 @@ export interface BuildLocalExecutionPlanOptions { readonly sliceChunkAudio?: boolean; /** Leave the audio to {@link buildPlanAudio}; chunks render video only. mp4 without `cfr` only. */ readonly deferAudio?: boolean; + /** Trusted local callers only; see CompileForRenderOptions. Never read from config. */ + readonly allowOutsideProjectAssets?: boolean; } export interface RemoteProjectFile { @@ -1190,7 +1193,10 @@ async function compilePlanComposition( projectDir: string, config: DistributedRenderConfig, planDir: string, - options: Pick, + options: Pick< + BuildLocalExecutionPlanOptions, + "executionPlanSizeLimitBytes" | "remoteFiles" | "allowOutsideProjectAssets" + >, ) { // Plan-time validation. Rejections here surface as typed errors with // non-retryable codes so workflow adapters don't waste retry budget on @@ -1236,6 +1242,7 @@ async function compilePlanComposition( variables: config.variables, strictVariables: config.strictVariables, pageMotionBlur: config.pageMotionBlur, + allowOutsideProjectAssets: options.allowOutsideProjectAssets === true, }); const entryFile = config.entryFile ?? "index.html"; const htmlPath = join(projectDir, entryFile); @@ -1270,6 +1277,7 @@ async function compilePlanComposition( // false-positive every descendant. const rel = relative(projectDir, src); if (rel === "" || rel.startsWith("..")) return true; + if (!job.config.allowOutsideProjectAssets && !isSafePath(projectDir, src)) return false; const firstSegment = rel.split(sep, 1)[0]; return firstSegment === undefined || !PLAN_PROJECT_DIR_SKIP_SEGMENTS.has(firstSegment); }, @@ -1889,7 +1897,10 @@ export async function plan( projectDir: string, config: DistributedRenderConfig, planDir: string, - options: Pick = {}, + options: Pick< + BuildLocalExecutionPlanOptions, + "chunksCarryAudio" | "allowOutsideProjectAssets" + > = {}, ): Promise { const executionPlan = await buildLocalExecutionPlan(projectDir, config, planDir, options); return { diff --git a/packages/producer/src/services/distributed/renderSpan.ts b/packages/producer/src/services/distributed/renderSpan.ts index 7d1854c4fa..f501a6b547 100644 --- a/packages/producer/src/services/distributed/renderSpan.ts +++ b/packages/producer/src/services/distributed/renderSpan.ts @@ -55,6 +55,7 @@ export async function renderSpan( config: DistributedRenderConfig, span: { startSeconds: number; endSeconds?: number }, outputPath: string, + options: { allowOutsideProjectAssets?: boolean } = {}, ): Promise { if (config.format !== "mp4") { throw new RenderSpanUnsupportedError( @@ -74,7 +75,7 @@ export async function renderSpan( projectDir, { ...config, rejectOnSystemFonts: false, failClosedFontFetch: false }, planDir, - { chunksCarryAudio: true }, + { chunksCarryAudio: true, allowOutsideProjectAssets: options.allowOutsideProjectAssets }, ); const { startFrame, endFrame } = spanFrameRange( span.startSeconds, diff --git a/packages/producer/src/services/distributed/shared.ts b/packages/producer/src/services/distributed/shared.ts index cc3dd3f867..0768dd4a55 100644 --- a/packages/producer/src/services/distributed/shared.ts +++ b/packages/producer/src/services/distributed/shared.ts @@ -446,6 +446,7 @@ export interface SyntheticRenderJobInput { variables?: RenderConfig["variables"]; strictVariables?: RenderConfig["strictVariables"]; pageMotionBlur?: RenderConfig["pageMotionBlur"]; + allowOutsideProjectAssets?: boolean; } /** @@ -475,6 +476,7 @@ export function buildSyntheticRenderJob(input: SyntheticRenderJobInput): RenderJ variables: input.variables, strictVariables: input.strictVariables, pageMotionBlur: input.pageMotionBlur, + allowOutsideProjectAssets: input.allowOutsideProjectAssets, }; return createRenderJob(renderConfig); } diff --git a/packages/producer/src/services/fileServer.test.ts b/packages/producer/src/services/fileServer.test.ts index f1c030dc93..f6166109bd 100644 --- a/packages/producer/src/services/fileServer.test.ts +++ b/packages/producer/src/services/fileServer.test.ts @@ -62,12 +62,14 @@ describe("closeFileServerSafely", () => { async function withFileServer( projectDir: string, run: (server: Awaited>) => Promise, + allowOutsideProjectAssets?: boolean, ): Promise { const server = await createFileServer({ projectDir, preHeadScripts: [], headScripts: [], bodyScripts: [], + allowOutsideProjectAssets, }); try { await run(server); @@ -492,7 +494,7 @@ describe("createFileServer", () => { ); }); - it("serves asset files through project-root symlinked directories", async () => { + it("serves asset files through project-root symlinked directories for local renders", async () => { const workspaceDir = mkdtempSync(join(tmpdir(), "hf-file-server-symlink-assets-")); const adsDir = join(workspaceDir, "Ads"); const projectDir = join(adsDir, "annual-upsell-2"); @@ -508,11 +510,18 @@ describe("createFileServer", () => { ); symlinkSync("../shared", join(projectDir, "shared")); + await withFileServer( + projectDir, + async (server) => { + await expectTextResponse(`${server.url}/shared/brand.css`, { + contentType: "text/css", + bodyIncludes: ".aisplus-glass", + }); + }, + true, + ); await withFileServer(projectDir, async (server) => { - await expectTextResponse(`${server.url}/shared/brand.css`, { - contentType: "text/css", - bodyIncludes: ".aisplus-glass", - }); + expect((await fetch(`${server.url}/shared/brand.css`)).status).toBe(404); }); } finally { rmSync(workspaceDir, { recursive: true, force: true }); diff --git a/packages/producer/src/services/fileServer.ts b/packages/producer/src/services/fileServer.ts index 5372bb99e0..bac1d66f1f 100644 --- a/packages/producer/src/services/fileServer.ts +++ b/packages/producer/src/services/fileServer.ts @@ -716,6 +716,8 @@ export interface FileServerOptions { fps?: Fps; /** Strip embedded runtime scripts from HTML before injection. Default: true. */ stripEmbeddedRuntime?: boolean; + /** Serve project symlinks that lead outside projectDir, as local preview does. Default: false. */ + allowOutsideProjectAssets?: boolean; } export interface FileServerHandle { @@ -786,7 +788,13 @@ function openRegularFile(filePath: string) { } export function createFileServer(options: FileServerOptions): Promise { - const { projectDir, compiledDir, port = 0, stripEmbeddedRuntime = true } = options; + const { + projectDir, + compiledDir, + port = 0, + stripEmbeddedRuntime = true, + allowOutsideProjectAssets = false, + } = options; // HF_EARLY_STUB must run before *any* page script so libraries that write // to window.__hf during page-script execution (e.g. shader-transitions @@ -826,12 +834,13 @@ export function createFileServer(options: FileServerOptions): Promise = null; for (const root of compiledDir ? [compiledDir, projectDir] : [projectDir]) { const candidate = join(root, relativePath); - if (isPathInside(candidate, root)) file = openRegularFile(candidate); + const resolveSymlinks = !allowOutsideProjectAssets && root !== compiledDir; + if (isPathInside(candidate, root, { resolveSymlinks })) file = openRegularFile(candidate); if (file) break; } diff --git a/packages/producer/src/services/heifImagePrep.ts b/packages/producer/src/services/heifImagePrep.ts index 5e112682ae..b128466cf4 100644 --- a/packages/producer/src/services/heifImagePrep.ts +++ b/packages/producer/src/services/heifImagePrep.ts @@ -81,6 +81,7 @@ export interface PrepareHeifImageInputsOptions { sourceAssets?: Map; timeoutMs?: number; transcode?: (input: HeifTranscodeRequest) => Promise; + assetRoot?: string; /** Leaves an image whose conversion failed as authored and reports it, instead of throwing. */ onFailure?: (error: HeifImageConversionError | HeifToolUnavailableError) => void; } diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index f43f4e3922..c7cc1bd2c2 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -79,7 +79,7 @@ import { } from "@hyperframes/parsers/asset-resolution"; import { isPrivateFileUnder, isVcsOrSecretPath } from "@hyperframes/core/safe-path"; import { extractMediaMetadata, extractAudioMetadata } from "../utils/ffprobe.js"; -import { isPathInside, toExternalAssetKey } from "../utils/paths.js"; +import { escapesAssetRoot, isPathInside, toExternalAssetKey } from "../utils/paths.js"; import { collectHiddenVideoIds, collectRenderMedia } from "./renderMediaCollector.js"; import { type VideoElement, @@ -228,6 +228,15 @@ function normalizeCompositionSrcs(html: string, projectDir: string): string { }); } +function resolveCompositionFile( + projectDir: string, + src: string, + assetRoot: string | undefined, +): string | null { + const file = resolveCompositionSrc(projectDir, src); + return file === null || escapesAssetRoot(file, assetRoot) ? null : file; +} + // Git metadata and env files are never inlined into a render, as the preview bundler refuses them. const isPrivateCompositionFile = (projectDir: string, filePath: string): boolean => isPrivateFileUnder(projectDir, filePath, isVcsOrSecretPath); @@ -250,6 +259,7 @@ const isPrivateCompositionFile = (projectDir: string, filePath: string): boolean function assertSubCompositionsUsable( html: string, projectDir: string, + assetRoot: string | undefined, visited: Set = new Set(), ): void { const { document } = parseHTML(html); @@ -261,7 +271,7 @@ function assertSubCompositionsUsable( if (!srcPath) continue; if (isUnresolvedAssetPlaceholder(srcPath)) continue; // __UPPER__ placeholder or unresolved templating token — not a real reference (shared with lint via @hyperframes/parsers) - const filePath = resolveCompositionSrc(projectDir, srcPath); + const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot); if (filePath === null) { problems.push({ srcPath, detail: "it is outside the project folder" }); continue; @@ -308,7 +318,7 @@ function assertSubCompositionsUsable( const nestedVisited = new Set(visited); nestedVisited.add(filePath); try { - assertSubCompositionsUsable(fileHtml, projectDir, nestedVisited); + assertSubCompositionsUsable(fileHtml, projectDir, assetRoot, nestedVisited); } catch (err) { if (err instanceof EmptyCompositionError) { problems.push(...err.problems); @@ -529,6 +539,7 @@ async function resolveMediaDuration( log?: ProducerLogger, urlByAbsentLocalPath?: ReadonlyMap, probes?: SourceProbes, + assetRoot?: string, ): Promise<{ duration: number | null; resolvedPath: string }> { let filePath = src; @@ -547,6 +558,7 @@ async function resolveMediaDuration( } else { filePath = decodedUrlPath(src); if (!filePath.startsWith("/")) filePath = join(baseDir, filePath); + if (escapesAssetRoot(filePath, assetRoot)) return { duration: null, resolvedPath: filePath }; // ffprobe reads only the header of a remote source, so it is probed in place. filePath = urlByAbsentLocalPath?.get(filePath) ?? filePath; if (!isHttpUrl(filePath) && namesAnotherNetworkRoot(baseDir, filePath)) { @@ -669,6 +681,7 @@ async function compileHtmlFile( log?: ProducerLogger, urlByAbsentLocalPath?: ReadonlyMap, probes?: SourceProbes, + assetRoot?: string, ): Promise<{ html: string; unresolvedCompositions: UnresolvedElement[] }> { const { html: staticCompiled, unresolved } = compileTimingAttrs(html); @@ -692,6 +705,7 @@ async function compileHtmlFile( log, urlByAbsentLocalPath, probes, + assetRoot, ).then(({ duration }) => ({ id: el.id, duration })), ), ); @@ -722,6 +736,7 @@ async function compileHtmlFile( log, undefined, probes, + assetRoot, ); return { id: el.id, tagName: el.tagName, duration: el.duration, maxDuration, src: el.src! }; }), @@ -791,6 +806,7 @@ async function parseSubCompositions( visited: Set = new Set(), urlByAbsentLocalPath?: ReadonlyMap, probes?: SourceProbes, + assetRoot?: string, ): Promise<{ subCompositions: Map }> { const subCompositions = new Map(); @@ -809,7 +825,7 @@ async function parseSubCompositions( const srcPath = el.getAttribute("data-composition-src"); if (!srcPath) continue; - const filePath = resolveCompositionSrc(projectDir, srcPath); + const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot); if (filePath === null || isPrivateCompositionFile(projectDir, filePath)) continue; // Circular reference guard @@ -839,6 +855,7 @@ async function parseSubCompositions( undefined, urlByAbsentLocalPath, probes, + assetRoot, ); const nested = await parseSubCompositions( @@ -848,6 +865,7 @@ async function parseSubCompositions( item.nestedVisited, urlByAbsentLocalPath, probes, + assetRoot, ); return { @@ -1054,7 +1072,8 @@ function inlineSubCompositions( html: string, subCompositions: Map, projectDir: string, - variableOverrides: Record = {}, + variableOverrides: Record, + assetRoot: string | undefined, ): string { const { document } = parseHTML(html); const head = document.querySelector("head"); @@ -1103,7 +1122,7 @@ function inlineSubCompositions( resolveHtml: (srcPath: string) => { let compHtml = subCompositions.get(srcPath) || null; if (!compHtml) { - const filePath = resolveCompositionSrc(projectDir, srcPath); + const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot); const read = filePath && !isPrivateCompositionFile(projectDir, filePath) ? readProjectFile(filePath) @@ -1915,7 +1934,11 @@ async function readLocalFont(absPath: string): Promise { } // fallow-ignore-next-line complexity -async function embedLocalFontFaces(html: string, projectDir: string): Promise { +async function embedLocalFontFaces( + html: string, + projectDir: string, + assetRoot: string | undefined, +): Promise { const { fontToDataUri: toDataUri } = await import("@hyperframes/core/fonts/embed"); const styleBlockRe = /]*>([\s\S]*?)<\/style>/gi; const fontFaceRe = /@font-face\s*\{([^}]*)\}/gi; @@ -1937,7 +1960,7 @@ async function embedLocalFontFaces(html: string, projectDir: string): Promise; pageMotionBlur?: "off"; + /** Trusted local renders only (CLI, Studio): import files markup reaches outside projectDir. */ + allowOutsideProjectAssets?: boolean; } /** Renders follow the same `HYPERFRAMES_GSAP_BASE_URL` mirror as Studio preview. */ @@ -2133,6 +2158,10 @@ export async function compileForRender( downloadDir: string, options: CompileForRenderOptions = {}, ): Promise { + const assetRoot = options.allowOutsideProjectAssets ? undefined : projectDir; + if (isPathInside(htmlPath, projectDir) && escapesAssetRoot(htmlPath, assetRoot)) { + throw new Error(`Entry file ${htmlPath} leads outside the project folder`); + } const entryHtml = rebaseDirectEntryAssetPaths(readTextFile(htmlPath), projectDir, htmlPath); const rawHtml = bindVariableMediaSources( normalizeCompositionSrcs(rewriteUnresolvableGsapToCdn(entryHtml, projectDir), projectDir), @@ -2145,7 +2174,7 @@ export async function compileForRender( // unconditional (not gated behind --strict like lint warnings) — a render // that silently drops a scene is strictly worse than one that refuses to // start. - assertSubCompositionsUsable(rawHtml, projectDir); + assertSubCompositionsUsable(rawHtml, projectDir, assetRoot); const probes: SourceProbes = new Map(); const { html: compiledHtml, unresolvedCompositions } = await compileHtmlFile( @@ -2155,6 +2184,7 @@ export async function compileForRender( options.log, options.urlByAbsentLocalPath, probes, + assetRoot, ); // Compile each referenced sub-composition so the inliner can hoist it. @@ -2165,6 +2195,7 @@ export async function compileForRender( undefined, options.urlByAbsentLocalPath, probes, + assetRoot, ); // Ensure the HTML is a full document before inlining sub-compositions. @@ -2181,6 +2212,7 @@ export async function compileForRender( subCompositions, projectDir, options.variables ?? {}, + assetRoot, ); // Strip preload="none" from media elements — the renderer needs to load all @@ -2282,6 +2314,7 @@ export async function compileForRender( cacheDir: options.animatedGifCacheDir, sourceAssets: remoteImageAssets, timeoutMs: options.ffmpegProcessTimeout, + assetRoot, }); if (preparedHeifImages.length > 0) { defaultLogger.debug(`[Compiler] Converted ${preparedHeifImages.length} HEIC image(s) to JPEG`); @@ -2298,17 +2331,20 @@ export async function compileForRender( cacheDir: options.animatedGifCacheDir, sourceAssets: gifSourceAssets, timeoutMs: options.ffmpegProcessTimeout, + assetRoot, }); if (preparedGifs.length > 0) { defaultLogger.debug(`[Compiler] Prepared ${preparedGifs.length} animated GIF input(s) as WebM`); } - const embeddedHtml = await embedLocalFontFaces(htmlWithPreparedGifs, projectDir); + const embeddedHtml = await embedLocalFontFaces(htmlWithPreparedGifs, projectDir, assetRoot); // Collect assets that resolve outside projectDir (e.g. ../shared-assets/hero.png). // These can't be served by the file server, so we map them to paths the // orchestrator will copy into the compiled output directory. - const { html, externalAssets } = collectExternalAssets(embeddedHtml, projectDir); + const { html, externalAssets } = options.allowOutsideProjectAssets + ? collectExternalAssets(embeddedHtml, projectDir) + : { html: embeddedHtml, externalAssets: new Map() }; for (const [relPath, absPath] of remoteMediaAssets) { externalAssets.set(relPath, absPath); @@ -2345,7 +2381,13 @@ export async function compileForRender( for (const video of videos) { if (isHttpUrl(video.src)) continue; const videoPath = resolve(projectDir, video.src); - if (advised.has(videoPath) || namesAnotherNetworkRoot(projectDir, videoPath)) continue; + if ( + advised.has(videoPath) || + namesAnotherNetworkRoot(projectDir, videoPath) || + !existsSync(videoPath) || + escapesAssetRoot(videoPath, assetRoot) + ) + continue; advised.add(videoPath); const reencode = `ffmpeg -i "${video.src}" -c:v libx264 -r 30 -g 30 -keyint_min 30 -movflags +faststart -c:a copy output.mp4`; (async () => { @@ -2947,6 +2989,7 @@ export async function recompileWithResolutions( resolutions: ResolvedDuration[], projectDir: string, downloadDir: string, + assetRoot?: string, ): Promise { if (resolutions.length === 0) return compiled; @@ -2957,7 +3000,15 @@ export async function recompileWithResolutions( // `data-end` on its host, and re-collecting would newly clamp clips to it — // a retiming, not an identity fix. `compiled.videos` was already collected // from this same inlined document, so it is complete and correctly keyed. - const { subCompositions } = await parseSubCompositions(html, projectDir, downloadDir); + const { subCompositions } = await parseSubCompositions( + html, + projectDir, + downloadDir, + undefined, + undefined, + undefined, + assetRoot, + ); const { videos, audios, images } = compiled; const remaining = compiled.unresolvedCompositions.filter( diff --git a/packages/producer/src/services/outsideProjectAssets.test.ts b/packages/producer/src/services/outsideProjectAssets.test.ts new file mode 100644 index 0000000000..77bb12bf90 --- /dev/null +++ b/packages/producer/src/services/outsideProjectAssets.test.ts @@ -0,0 +1,243 @@ +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { + copyFileSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { pathToFileURL } from "node:url"; +import { resolveConfig } from "@hyperframes/engine"; +import { runCompileStage } from "./render/stages/compileStage.js"; +import { runExtractVideosStage } from "./render/stages/extractVideosStage.js"; +import { createRenderJob } from "./renderOrchestrator.js"; +import { createFileServer } from "./fileServer.js"; +import { plan } from "./distributed/plan.js"; +import { materializePlanV2Target, planV2, readPlanV2Manifest } from "./distributed/planV2.js"; +import { toExternalAssetKey } from "../utils/paths.js"; + +const tool = vi.hoisted(() => ({ calls: [] as string[], clip: "" })); + +vi.mock("node:child_process", async (importOriginal) => { + const actual = await importOriginal(); + const recorded = (run: T): T => + new Proxy(run, { + apply(target, thisArg, args: unknown[]) { + tool.calls.push(JSON.stringify(args.slice(0, 2))); + return Reflect.apply(target as (...a: unknown[]) => unknown, thisArg, args); + }, + }); + return { + ...actual, + spawn: recorded(actual.spawn), + spawnSync: recorded(actual.spawnSync), + execFile: recorded(actual.execFile), + execFileSync: recorded(actual.execFileSync), + }; +}); + +vi.mock("../utils/urlDownloader.js", async (importOriginal) => ({ + ...(await importOriginal()), + downloadToTemp: async (_url: string, destDir: string) => { + const path = join(destDir, "remote-clip.mp4"); + copyFileSync(tool.clip, path); + return path; + }, +})); + +const HAS_FFMPEG = spawnSync("ffmpeg", ["-version"]).status === 0; +const PNG_1X1 = Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", + "base64", +); +const roots: string[] = []; + +beforeAll(() => { + if (!HAS_FFMPEG) return; + tool.clip = join(mkdtempSync(join(tmpdir(), "hf-outside-clip-")), "clip.mp4"); + const made = spawnSync("ffmpeg", [ + ...["-v", "error", "-f", "lavfi", "-i", "testsrc=size=64x48:rate=30", "-t", "1"], + ...["-pix_fmt", "yuv420p", tool.clip], + ]); + expect(made.status).toBe(0); +}); + +afterEach(() => { + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); +}); +afterAll(() => { + if (tool.clip) rmSync(resolve(tool.clip, ".."), { recursive: true, force: true }); +}); + +const scene = `

OUTSIDE-SCENE

`; + +function outsideProject(media: boolean) { + const root = mkdtempSync(join(tmpdir(), "hf-outside-assets-")); + roots.push(root); + const projectDir = join(root, "project"); + const outsideDir = join(root, "outside"); + mkdirSync(outsideDir, { recursive: true }); + mkdirSync(join(projectDir, "scenes"), { recursive: true }); + writeFileSync(join(outsideDir, "secret.png"), PNG_1X1); + copyFileSync(tool.clip, join(outsideDir, "clip.mp4")); + writeFileSync(join(outsideDir, "scene.html"), scene); + symlinkSync(join(outsideDir, "secret.png"), join(projectDir, "link.png")); + const outsideClip = join(outsideDir, "clip.mp4"); + const video = (id: string, src: string) => + ``; + const mediaHtml = [ + video("absolute", outsideClip), + video("fileurl", pathToFileURL(outsideClip).href), + video("dotdot", "../outside/clip.mp4"), + video("encoded", "..%2Foutside%2Fclip.mp4"), + video("remote", "https://media.example.com/clip.mp4"), + ``, + ].join(""); + writeFileSync( + join(projectDir, "index.html"), + ` + +
+
+ ${media ? mediaHtml : ""} +
`, + ); + const secretKey = toExternalAssetKey(resolve(projectDir, "../outside/secret.png")); + return { root, projectDir, outsideDir, secretKey }; +} + +function symlinkedComposition(link: "scene" | "entry") { + const { root, projectDir, outsideDir } = outsideProject(false); + symlinkSync(join(outsideDir, "scene.html"), join(projectDir, "scenes", "a.html")); + const host = ` +
+
+
`; + writeFileSync(join(outsideDir, "index.html"), host); + rmSync(join(projectDir, "index.html")); + if (link === "entry") symlinkSync(join(outsideDir, "index.html"), join(projectDir, "index.html")); + else writeFileSync(join(projectDir, "index.html"), host); + return { root, projectDir }; +} + +async function statusOf( + server: { url: string; close: () => void }, + paths: string[], +): Promise { + try { + return await Promise.all(paths.map(async (path) => (await fetch(server.url + path)).status)); + } finally { + server.close(); + } +} + +async function compileInProcess(projectDir: string, allowOutsideProjectAssets?: boolean) { + const workDir = join(projectDir, "..", "work"); + mkdirSync(workDir, { recursive: true }); + const warnings: string[] = []; + const log = { error() {}, info() {}, debug() {}, warn: (line: string) => warnings.push(line) }; + const job = createRenderJob({ + fps: { num: 30, den: 1 }, + quality: "draft", + allowOutsideProjectAssets, + }); + const cfg = resolveConfig(); + const result = await runCompileStage({ + ...{ projectDir, workDir, entryFile: "index.html", job, cfg, log }, + ...{ needsAlpha: false, assertNotAborted: () => {} }, + }); + const compiledDir = join(workDir, "compiled"); + const extract = () => + runExtractVideosStage({ + ...{ projectDir, compiledDir, job, cfg, log, composition: result.composition }, + ...{ abortSignal: undefined, assertNotAborted: () => {} }, + }); + const serve = () => createFileServer({ projectDir, compiledDir, allowOutsideProjectAssets }); + return { result, compiledDir, extract, serve, warnings }; +} + +const sha256 = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex"); +const config = { fps: 30 as const, width: 320, height: 240, format: "mp4" as const }; + +describe.skipIf(!HAS_FFMPEG)("render servers keep markup inside the project", () => { + it("in-process: copies, serves and opens nothing from outside by default", async () => { + const { projectDir, outsideDir, secretKey } = outsideProject(true); + tool.calls.length = 0; + const { result, compiledDir, extract, serve, warnings } = await compileInProcess(projectDir); + const extracted = await extract(); + + expect( + [...result.compiled.externalAssets.keys()].filter((key) => key.startsWith("hf-ext/")), + ).toEqual([]); + expect(existsSync(join(compiledDir, "hf-ext"))).toBe(false); + expect(result.composition.videos.map((video) => video.id)).toEqual(["remote"]); + expect(result.composition.audios.filter((audio) => audio.id === "tone")).toEqual([]); + expect(result.composition.images).toEqual([]); + for (const id of ["absolute", "fileurl", "dotdot", "encoded", "tone"]) { + expect(warnings.some((line) => line.includes(`"${id}" source is missing`))).toBe(true); + } + expect(extracted.extractionResult?.extracted.map((frames) => frames.videoId)).toEqual([ + "remote", + ]); + expect(tool.calls.filter((call) => call.includes(outsideDir))).toEqual([]); + expect( + await statusOf(await serve(), [`/${secretKey}`, "/outside/secret.png", "/link.png"]), + ).toEqual([404, 404, 404]); + }); + + it("Plan v2: publishes and opens nothing from outside by default", async () => { + const { root, projectDir, outsideDir, secretKey } = outsideProject(true); + const planDir = join(root, "v2"); + tool.calls.length = 0; + await planV2(projectDir, config, planDir); + + const artifacts = readPlanV2Manifest(planDir).artifacts; + expect(artifacts.filter((artifact) => artifact.path.includes("hf-ext"))).toEqual([]); + const digests = artifacts.map((artifact) => artifact.sha256); + expect(digests).not.toContain(sha256(PNG_1X1)); + expect(digests).toContain(sha256(readFileSync(tool.clip))); + expect(tool.calls.filter((call) => call.includes(outsideDir))).toEqual([]); + + const chunkDir = join(root, "chunk"); + materializePlanV2Target(planDir, { role: "chunk", chunkIndex: 0 }, chunkDir); + const compiledDir = join(chunkDir, "compiled"); + const server = await createFileServer({ projectDir: compiledDir, compiledDir }); + expect(await statusOf(server, [`/${secretKey}`, "/outside/secret.png", "/link.png"])).toEqual([ + 404, 404, 404, + ]); + }); + + it.each(["scene", "entry"] as const)( + "refuses a symlinked %s that leads outside", + async (link) => { + const { root, projectDir } = symlinkedComposition(link); + await expect(compileInProcess(projectDir)).rejects.toThrow(/outside the project folder/); + await expect(planV2(projectDir, config, join(root, "v2"))).rejects.toThrow( + /outside the project folder/, + ); + }, + ); + + it("trusted local renders still import outside files", async () => { + const { root, projectDir, secretKey } = outsideProject(false); + const { compiledDir, serve } = await compileInProcess(projectDir, true); + expect(existsSync(join(compiledDir, secretKey))).toBe(true); + expect(await statusOf(await serve(), [`/${secretKey}`, "/link.png"])).toEqual([200, 200]); + + const planDir = join(root, "plan"); + await plan(projectDir, config, planDir, { allowOutsideProjectAssets: true }); + expect(existsSync(join(planDir, "compiled", secretKey))).toBe(true); + expect(existsSync(join(planDir, "compiled", "link.png"))).toBe(true); + + const linked = symlinkedComposition("scene"); + const { result } = await compileInProcess(linked.projectDir, true); + expect(result.compiled.html).toContain("OUTSIDE-SCENE"); + }); +}); diff --git a/packages/producer/src/services/render/stages/compileStage.ts b/packages/producer/src/services/render/stages/compileStage.ts index 14ea105a5c..f34afbfc97 100644 --- a/packages/producer/src/services/render/stages/compileStage.ts +++ b/packages/producer/src/services/render/stages/compileStage.ts @@ -36,7 +36,8 @@ */ import { existsSync, writeFileSync } from "node:fs"; -import { join } from "node:path"; +import { join, relative } from "node:path"; +import { escapesAssetRoot } from "../../../utils/paths.js"; import type { ChromaSubsampling, EngineConfig } from "@hyperframes/engine"; import { suggestMatchingPreset, type CanvasResolution } from "@hyperframes/core"; import { extractStandaloneEntryFromIndex, isTemplateEntry } from "@hyperframes/core/compiler"; @@ -51,7 +52,10 @@ import { type CompositionMetadata, } from "../shared.js"; import type { RenderJob } from "../../renderOrchestrator.js"; -import { preflightCompositionAssetMediaTypes } from "../../assetMediaType.js"; +import { + dropMediaOutsideProject, + preflightCompositionAssetMediaTypes, +} from "../../assetMediaType.js"; export interface CompileStageInput { projectDir: string; @@ -163,10 +167,19 @@ export function resolveEntryHtmlPath(input: { workDir: string; entryFile: string; log: ProducerLogger; + assetRoot?: string; }): string { const { projectDir, workDir, entryFile, log } = input; + const readEntry = (path: string) => { + if (escapesAssetRoot(path, input.assetRoot)) { + throw new Error( + `Entry file "${relative(projectDir, path)}" leads outside the project folder`, + ); + } + return readTextFile(path); + }; const htmlPath = join(projectDir, entryFile); - const rawEntry = readTextFile(htmlPath); + const rawEntry = readEntry(htmlPath); if (entryFile === "index.html" || !isTemplateEntry(rawEntry)) return htmlPath; const projectIndexPath = join(projectDir, "index.html"); @@ -176,7 +189,7 @@ export function resolveEntryHtmlPath(input: { ); } const standaloneHtml = extractStandaloneEntryFromIndex( - readTextFile(projectIndexPath), + readEntry(projectIndexPath), entryFile, rawEntry, ); @@ -225,7 +238,8 @@ export async function runCompileStage(input: CompileStageInput): Promise { state.progress = j.progress; diff --git a/scripts/package-size-budgets.json b/scripts/package-size-budgets.json index 869c1d8e73..443dff4334 100644 --- a/scripts/package-size-budgets.json +++ b/scripts/package-size-budgets.json @@ -37,7 +37,7 @@ }, "@hyperframes/producer": { "packed": 880000, - "unpacked": 3165274 + "unpacked": 3210000 }, "@hyperframes/sdk": { "packed": 90662,