From 4cf26b5b49e6c4f2b42fb28738a1fb576e7494de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 6 Nov 2026 14:46:11 +0000 Subject: [PATCH] fix(producer): render reads a composition src like the preview, refuses paths outside (#7317) * fix(producer): render reads a composition src like the preview, refuses paths outside * fix(producer): a src with an encoded folder re-points its sibling assets; format --- .../src/services/htmlCompiler.test.ts | 66 +++++++++++++++++++ .../producer/src/services/htmlCompiler.ts | 35 +++++++--- packages/producer/src/utils/paths.ts | 11 ++++ 3 files changed, 104 insertions(+), 8 deletions(-) diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index 4ecaa9ea02..6a1a99b578 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -1027,6 +1027,72 @@ describe("detectRenderModeHints", () => { ).rejects.toThrow(/compositions\/does-not-exist\.html/); }); + it.each([ + ["a %20-encoded name", "compositions/my%20scene.html"], + ["a leading slash for the project root", "/compositions/my scene.html"], + ])("compileForRender reads a data-composition-src written with %s", async (_label, src) => { + const projectDir = makeSubCompProject("hf-spelled-subcomp-", [{ id: "intro", src }], { + "my scene.html": validSubCompHtml("intro", "Spelled"), + }); + try { + const compiled = await compileForRender( + projectDir, + join(projectDir, "index.html"), + projectDir, + ); + expect(compiled.html).toContain("Spelled"); + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }); + + it("compileForRender re-points a sibling asset of a sub-composition whose folder is %20-encoded", async () => { + const projectDir = makeSubCompProject( + "hf-encoded-dir-subcomp-", + [{ id: "intro", src: "my%20dir/scene.html" }], + {}, + ); + try { + mkdirSync(join(projectDir, "my dir"), { recursive: true }); + writeFileSync(join(projectDir, "my dir", "img.png"), "png"); + writeFileSync( + join(projectDir, "my dir", "scene.html"), + `
`, + ); + const compiled = await compileForRender( + projectDir, + join(projectDir, "index.html"), + projectDir, + ); + const src = parseHTML(compiled.html).document.getElementById("pic")?.getAttribute("src"); + expect(decodeURIComponent(src ?? "")).toContain("my dir/img.png"); + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }); + + it.each([ + ["dot-dot segments", "../outside-scene.html"], + ["an encoded ../", "%2e%2e%2foutside-scene.html"], + ["a leading slash and dot-dot", "/../outside-scene.html"], + ["an encoded leading slash and dot-dot", "%2f%2e%2e%2foutside-scene.html"], + ])( + "compileForRender refuses a data-composition-src that leaves the project through %s", + async (_label, src) => { + const projectDir = makeSubCompProject("hf-outside-subcomp-", [{ id: "intro", src }], {}); + const outside = join(dirname(projectDir), "outside-scene.html"); + writeFileSync(outside, validSubCompHtml("intro", "Outside")); + try { + await expect( + compileForRender(projectDir, join(projectDir, "index.html"), projectDir), + ).rejects.toThrow(/outside the project folder/); + } finally { + rmSync(outside, { force: true }); + rmSync(projectDir, { recursive: true, force: true }); + } + }, + ); + it("compileForRender preserves a bare fragment's markup, sibling styles, and script", async () => { const projectDir = makeSubCompProject( "hf-fragment-subcomp-", diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 325e791f33..2665cc13f9 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -13,7 +13,7 @@ import { decodedUrlPath } from "@hyperframes/parsers/asset-paths"; import { createHash } from "node:crypto"; import { createReadStream, existsSync, mkdirSync, readFileSync } from "fs"; -import { join, dirname, resolve, basename, relative } from "path"; +import { join, dirname, resolve, basename, relative, sep } from "path"; import { parseHTML } from "linkedom"; import { COMPOSITION_SOURCE_URL, @@ -74,7 +74,7 @@ import { readTextFile, } from "@hyperframes/parsers/asset-resolution"; import { extractMediaMetadata, extractAudioMetadata } from "../utils/ffprobe.js"; -import { isPathInside, toExternalAssetKey } from "../utils/paths.js"; +import { isPathInside, resolveCompositionSrc, toExternalAssetKey } from "../utils/paths.js"; import { collectHiddenVideoIds, collectRenderMedia } from "./renderMediaCollector.js"; import { type VideoElement, @@ -213,6 +213,16 @@ class EmptyCompositionError extends Error { } } +/** The html with each data-composition-src as the project-relative path it names (%xx decoded, no leading /). */ +function normalizeCompositionSrcs(html: string, projectDir: string): string { + return html.replace(/(\bdata-composition-src=)(["'])([^"']*)\2/g, (whole, name, quote, value) => { + if (!value.includes("%") && !value.startsWith("/")) return whole; + const file = resolveCompositionSrc(projectDir, value); + if (file === null) return whole; + return `${name}${quote}${relative(projectDir, file).split(sep).join("/")}${quote}`; + }); +} + /** * Recursively walk every `data-composition-src` reference reachable from * `html` (including nested sub-compositions) and verify each resolves to a @@ -242,7 +252,11 @@ 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 = resolve(projectDir, srcPath); + const filePath = resolveCompositionSrc(projectDir, srcPath); + if (filePath === null) { + problems.push({ srcPath, detail: "it is outside the project folder" }); + continue; + } // Circular reference guard. parseSubCompositions (below) silently // `continue`s on a repeat visit with no reporting at all — mirror that // silence here rather than pretend it surfaces an error somewhere else. @@ -779,7 +793,8 @@ async function parseSubCompositions( const srcPath = el.getAttribute("data-composition-src"); if (!srcPath) continue; - const filePath = resolve(projectDir, srcPath); + const filePath = resolveCompositionSrc(projectDir, srcPath); + if (filePath === null) continue; // Circular reference guard if (visited.has(filePath)) { @@ -791,7 +806,7 @@ async function parseSubCompositions( continue; } - const rawSubHtml = read.text; + const rawSubHtml = normalizeCompositionSrcs(read.text, projectDir); const nestedVisited = new Set(visited); nestedVisited.add(filePath); @@ -1072,8 +1087,9 @@ function inlineSubCompositions( resolveHtml: (srcPath: string) => { let compHtml = subCompositions.get(srcPath) || null; if (!compHtml) { - const read = readProjectFile(resolve(projectDir, srcPath)); - if (read.kind === "file") { + const filePath = resolveCompositionSrc(projectDir, srcPath); + const read = filePath ? readProjectFile(filePath) : null; + if (read?.kind === "file") { compHtml = rewriteUnresolvableGsapToCdn(read.text, projectDir, srcPath); } } @@ -2067,7 +2083,10 @@ export async function compileForRender( options: CompileForRenderOptions = {}, ): Promise { const entryHtml = rebaseDirectEntryAssetPaths(readTextFile(htmlPath), projectDir, htmlPath); - const rawHtml = rewriteUnresolvableGsapToCdn(entryHtml, projectDir); + const rawHtml = normalizeCompositionSrcs( + rewriteUnresolvableGsapToCdn(entryHtml, projectDir), + projectDir, + ); // Pre-flight: every data-composition-src reference must resolve to a // usable file before we spend any time compiling, launching a browser, or diff --git a/packages/producer/src/utils/paths.ts b/packages/producer/src/utils/paths.ts index 14327a081a..b0495d2db2 100644 --- a/packages/producer/src/utils/paths.ts +++ b/packages/producer/src/utils/paths.ts @@ -9,7 +9,9 @@ import { relative as nodeRelative, isAbsolute as nodeIsAbsolute, } from "node:path"; +import { existsSync } from "node:fs"; import { fileURLToPath } from "node:url"; +import { decodedUrlPath } from "@hyperframes/parsers/asset-paths"; export interface RenderPaths { absoluteProjectDir: string; @@ -134,3 +136,12 @@ export function resolveRenderPaths( return { absoluteProjectDir, absoluteOutputPath }; } + +/** The project file a data-composition-src names, as the preview reads it; null when it leaves the project. */ +export function resolveCompositionSrc(projectDir: string, src: string): string | null { + const rooted = (value: string) => nodeResolve(projectDir, value.replace(/^(?:\.?[\\/])+/, "")); + const decoded = rooted(decodedUrlPath(src)); + if (!isPathInside(decoded, projectDir)) return null; + const raw = rooted(src); + return !existsSync(decoded) && isPathInside(raw, projectDir) && existsSync(raw) ? raw : decoded; +}