mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 00:18:27 +00:00
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
This commit is contained in:
@@ -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"),
|
||||
`<!doctype html><html><body><div data-composition-id="intro" data-width="100" data-height="100"><img id="pic" src="img.png"></div></body></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-",
|
||||
|
||||
@@ -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<CompiledComposition> {
|
||||
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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user