fix(producer): render servers only load assets from inside the project (#7579)

* fix(producer): render servers only load assets from inside the project

The HTTP render handlers and the distributed planner now compile with
outside-project imports off: markup that reaches a file beyond the project
directory (through ../, an absolute path, a file: URL or a symlink) is no
longer copied into the compiled directory, served to the page, probed or
extracted. Local `hyperframes render`, `--start/--end` range renders,
`hyperframes benchmark` and Studio renders pass allowOutsideProjectAssets
and keep importing outside files exactly as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(producer): render the symlinked-assets fixture as a trusted local render

The render-symlinked-assets fixture links src/shared to a sibling folder
outside the project to keep local renders working like preview. The
regression harness now reads renderConfig.allowOutsideProjectAssets from a
fixture's meta.json and passes it to the in-process and
distributed-simulated renders, and this fixture sets it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(producer): render servers read compositions and media only from inside the project

Without the trusted-local opt-in, a sub-composition or entry file that
leads outside the project once symlinks resolve is refused, the same as a
../ sub-composition already is. Local media that resolves outside the
project, compiled and download folders, including absolute paths, file:
URLs and percent-encoded ../ paths, is reported as a missing source and
never probed or extracted. Remote media fetched during the render keeps
working. A missing file is no longer probed for the compile-time advice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(producer): raise the unpacked size budget for the project-folder checks

The producer tarball grows by about 12.5 KB unpacked: the new checks live
in the compiler and render stages, which the index, public-server and
distributed bundles each include. No test files ship in the package.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
James Russo
2026-11-12 02:31:36 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent e8e1177ad6
commit 03ddfb189f
25 changed files with 454 additions and 38 deletions
+1
View File
@@ -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;
+4 -1
View File
@@ -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]);
+1
View File
@@ -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 }) => {
@@ -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",
@@ -140,6 +140,7 @@ export interface RunDistributedSimulatedInput {
maxParallelChunks?: number;
/** Forwarded to `plan()` and re-applied by `renderChunk()` at boot. */
variables?: Record<string, unknown>;
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 —
@@ -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);
+2 -1
View File
@@ -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,
};
}
@@ -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<string, string>;
timeoutMs?: number;
transcode?: (input: AnimatedGifTranscodeRequest) => Promise<void>;
assetRoot?: string;
}
export interface AnimatedGifTranscodeRequest {
@@ -79,7 +81,10 @@ function readLoopOverride(el: Element): boolean | null {
export function resolveLocalImageSourcePath(
src: string,
options: Pick<PrepareAnimatedGifInputsOptions, "projectDir" | "downloadDir" | "sourceAssets">,
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 {
@@ -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
@@ -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<BuildLocalExecutionPlanOptions, "executionPlanSizeLimitBytes" | "remoteFiles">,
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<BuildLocalExecutionPlanOptions, "chunksCarryAudio"> = {},
options: Pick<
BuildLocalExecutionPlanOptions,
"chunksCarryAudio" | "allowOutsideProjectAssets"
> = {},
): Promise<PlanResult> {
const executionPlan = await buildLocalExecutionPlan(projectDir, config, planDir, options);
return {
@@ -55,6 +55,7 @@ export async function renderSpan(
config: DistributedRenderConfig,
span: { startSeconds: number; endSeconds?: number },
outputPath: string,
options: { allowOutsideProjectAssets?: boolean } = {},
): Promise<RenderSpanResult> {
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,
@@ -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);
}
@@ -62,12 +62,14 @@ describe("closeFileServerSafely", () => {
async function withFileServer(
projectDir: string,
run: (server: Awaited<ReturnType<typeof createFileServer>>) => Promise<void>,
allowOutsideProjectAssets?: boolean,
): Promise<void> {
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 });
+13 -4
View File
@@ -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<FileServerHandle> {
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<FileServer
// Each candidate is rejected if `..` segments push it outside the
// intended root: `path.join` normalizes traversal but does not enforce
// containment, so a request like `GET /../etc/passwd` would otherwise
// be served straight off the filesystem. Keep this lexical so project
// symlinks to sibling asset directories behave like preview mode.
// be served straight off the filesystem. compiledDir stays lexical: it
// links extracted frame directories in from the work directory.
let file: ReturnType<typeof openRegularFile> = 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;
}
@@ -81,6 +81,7 @@ export interface PrepareHeifImageInputsOptions {
sourceAssets?: Map<string, string>;
timeoutMs?: number;
transcode?: (input: HeifTranscodeRequest) => Promise<void>;
assetRoot?: string;
/** Leaves an image whose conversion failed as authored and reports it, instead of throwing. */
onFailure?: (error: HeifImageConversionError | HeifToolUnavailableError) => void;
}
+64 -13
View File
@@ -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<string> = 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<string, string>,
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<string, string>,
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<string> = new Set(),
urlByAbsentLocalPath?: ReadonlyMap<string, string>,
probes?: SourceProbes,
assetRoot?: string,
): Promise<{ subCompositions: Map<string, string> }> {
const subCompositions = new Map<string, string>();
@@ -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<string, string>,
projectDir: string,
variableOverrides: Record<string, unknown> = {},
variableOverrides: Record<string, unknown>,
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<LocalFontRead> {
}
// fallow-ignore-next-line complexity
async function embedLocalFontFaces(html: string, projectDir: string): Promise<string> {
async function embedLocalFontFaces(
html: string,
projectDir: string,
assetRoot: string | undefined,
): Promise<string> {
const { fontToDataUri: toDataUri } = await import("@hyperframes/core/fonts/embed");
const styleBlockRe = /<style\b[^>]*>([\s\S]*?)<\/style>/gi;
const fontFaceRe = /@font-face\s*\{([^}]*)\}/gi;
@@ -1937,7 +1960,7 @@ async function embedLocalFontFaces(html: string, projectDir: string): Promise<st
const localPath = urlMatch[1];
if (!localPath || embeddedPaths.has(localPath)) continue;
const absPath = localPath.startsWith("/") ? localPath : resolve(projectDir, localPath);
if (!isPathInside(absPath, projectDir)) continue;
if (!isPathInside(absPath, projectDir) || escapesAssetRoot(absPath, assetRoot)) continue;
const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf";
try {
if (fileBackedAbsolutePaths.has(absPath)) {
@@ -2030,6 +2053,8 @@ export interface CompileForRenderOptions {
*/
variables?: Record<string, unknown>;
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<CompiledComposition> {
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<string, string>() };
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<CompiledComposition> {
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(
@@ -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<typeof import("node:child_process")>();
const recorded = <T extends object>(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<typeof import("../utils/urlDownloader.js")>()),
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 = `<div data-composition-id="scene" data-width="320" data-height="240" data-duration="1"><p>OUTSIDE-SCENE</p></div>`;
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) =>
`<video id="${id}" src="${src}" data-start="0" data-duration="1" muted></video>`;
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"),
`<audio id="tone" src="../outside/clip.mp4" data-start="0" data-duration="1"></audio>`,
].join("");
writeFileSync(
join(projectDir, "index.html"),
`<!doctype html><html><head><meta charset="utf-8">
<style>.bg { background-image: url("../outside/secret.png"); }</style></head><body>
<div data-composition-id="root" data-width="320" data-height="240" data-duration="1">
<img src="../outside/secret.png"><div style="background: url(../outside/secret.png)"></div>
<img src="link.png">${media ? mediaHtml : ""}
</div></body></html>`,
);
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 = `<!doctype html><html><body>
<div data-composition-id="root" data-width="320" data-height="240" data-duration="1">
<div data-composition-src="scenes/a.html" data-start="0" data-duration="1"></div>
</div></body></html>`;
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<number[]> {
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");
});
});
@@ -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<Compile
abortSignal,
} = input;
const htmlPath = resolveEntryHtmlPath({ projectDir, workDir, entryFile, log });
const assetRoot = job.config.allowOutsideProjectAssets ? undefined : projectDir;
const htmlPath = resolveEntryHtmlPath({ projectDir, workDir, entryFile, log, assetRoot });
const compileStart = Date.now();
const compiled = await compileForRender(projectDir, htmlPath, join(workDir, "downloads"), {
log,
@@ -239,6 +253,7 @@ export async function runCompileStage(input: CompileStageInput): Promise<Compile
? join(cfg.extractCacheDir, "animated-gif")
: undefined,
ffmpegProcessTimeout: cfg.ffmpegProcessTimeout,
allowOutsideProjectAssets: job.config.allowOutsideProjectAssets,
});
assertNotAborted();
const compileOnlyMs = Date.now() - compileStart;
@@ -362,6 +377,9 @@ export async function runCompileStage(input: CompileStageInput): Promise<Compile
width: compiled.width,
height: compiled.height,
};
if (!job.config.allowOutsideProjectAssets) {
dropMediaOutsideProject({ projectDir, workDir, composition, log });
}
await preflightCompositionAssetMediaTypes({
projectDir,
compiledDir: join(workDir, "compiled"),
@@ -266,7 +266,7 @@ function makeProbeInput(overrides: {
};
return {
projectDir: "/tmp/hf-probe-test-project",
projectDir: workDir,
workDir: workDir,
job: {
id: "probe-test",
@@ -84,7 +84,10 @@ import {
} from "../shared.js";
import type { RenderConfig, RenderJob } from "../../renderOrchestrator.js";
import { isActionableProbeFailure } from "./probeFailures.js";
import { preflightCompositionAssetMediaTypes } from "../../assetMediaType.js";
import {
dropMediaOutsideProject,
preflightCompositionAssetMediaTypes,
} from "../../assetMediaType.js";
import { resolveCaptureSurface } from "../captureImageFormat.js";
import { computeCompositionObservabilityHash } from "../observability.js";
@@ -437,6 +440,7 @@ export async function runProbeStage(input: ProbeStageInput): Promise<ProbeStageR
port: 0,
preHeadScripts: [VIRTUAL_TIME_SHIM],
fps: job.config.fps,
allowOutsideProjectAssets: job.config.allowOutsideProjectAssets,
});
assertNotAborted();
@@ -622,6 +626,7 @@ export async function runProbeStage(input: ProbeStageInput): Promise<ProbeStageR
resolutions,
projectDir,
join(workDir, "downloads"),
job.config.allowOutsideProjectAssets ? undefined : projectDir,
);
assertNotAborted();
// Update composition metadata with re-parsed media
@@ -834,6 +839,9 @@ export async function runProbeStage(input: ProbeStageInput): Promise<ProbeStageR
}
}
if (!job.config.allowOutsideProjectAssets) {
dropMediaOutsideProject({ projectDir, workDir, composition, log });
}
await preflightCompositionAssetMediaTypes({
projectDir,
compiledDir: join(workDir, "compiled"),
@@ -57,6 +57,7 @@ import {
toFps,
} from "@hyperframes/core";
import { HF_AUDIO_GROUP_TAG } from "@hyperframes/core/audio-groups";
import type { CompileForRenderOptions } from "./htmlCompiler.js";
import { HTML_BODY_CSS_HEIGHT_FIRST_RE, HTML_BODY_CSS_WIDTH_FIRST_RE } from "@hyperframes/parsers";
import {
type EngineConfig,
@@ -439,6 +440,7 @@ export interface RenderConfig {
strictness?: RenderStrictness;
/** Entry HTML file relative to projectDir. Defaults to "index.html". */
entryFile?: string;
allowOutsideProjectAssets?: CompileForRenderOptions["allowOutsideProjectAssets"];
/** Full producer config. When provided, env vars are not read. */
producerConfig?: EngineConfig;
/** Custom logger. Defaults to console-based defaultLogger. */
@@ -3733,6 +3735,7 @@ async function executeRenderPipeline(input: {
port: 0,
preHeadScripts: [VIRTUAL_TIME_SHIM],
fps: job.config.fps,
allowOutsideProjectAssets: job.config.allowOutsideProjectAssets,
});
assertNotAborted();
observability.stageEnd("file_server", fileServerStart);
+6
View File
@@ -10,6 +10,7 @@ import {
isAbsolute as nodeIsAbsolute,
} from "node:path";
import { fileURLToPath } from "node:url";
import { isSafePath } from "@hyperframes/core/safe-path";
export interface RenderPaths {
absoluteProjectDir: string;
@@ -64,6 +65,11 @@ export function isPathInside(
return rel !== "" && !LEADING_PARENT_SEGMENT_RE.test(rel) && !isPathAbsolute(rel);
}
/** True when `assetRoot` is set and `path`, symlinks resolved, leaves it. */
export function escapesAssetRoot(path: string, assetRoot: string | undefined): boolean {
return assetRoot !== undefined && !isSafePath(assetRoot, path);
}
/**
* Build a safe, cross-platform relative key for an absolute asset path
* that lives outside the project directory.
@@ -8,6 +8,7 @@
"maxAudioLagWindows": 1,
"renderConfig": {
"fps": 24,
"workers": 1
"workers": 1,
"allowOutsideProjectAssets": true
}
}
+1
View File
@@ -362,6 +362,7 @@ export function createViteAdapter(
...encoderJobOptions(opts),
...(opts.useGpu ? { useGpu: true } : {}),
...(opts.pageMotionBlur ? { pageMotionBlur: opts.pageMotionBlur } : {}),
allowOutsideProjectAssets: true,
});
const onProgress = (j: { progress: number; currentStage?: string }) => {
state.progress = j.progress;
+1 -1
View File
@@ -37,7 +37,7 @@
},
"@hyperframes/producer": {
"packed": 880000,
"unpacked": 3165274
"unpacked": 3210000
},
"@hyperframes/sdk": {
"packed": 90662,