]*><\/div>/, ""),
+ });
+ const error = await compileError(dir);
+ expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE");
+ expect(error).toMatchObject({
+ owner: "user",
+ retryable: false,
+ publicMetadata: { exceeded: "generated" },
+ });
+ expect(String(error)).toContain("Composition variable styles add up to");
+ expect(String(error)).toContain("declare them on fewer elements");
+ });
+
+ it("refuses declarers inside a mounted sub-composition that repeat a long host value", async () => {
+ const declarer = `
`;
+ const host = `data-variable-values='${JSON.stringify({ v: "x".repeat(1024 * 1024) })}'`;
+ const dir = project({
+ "index.html": ROOT.replace(
+ 'data-composition-id="scene"',
+ `data-composition-id="scene" ${host}`,
+ ),
+ "scene.html": SCENE.replace("
Scene
", declarer.repeat(100)),
+ });
+ const error = await compileError(dir);
+ expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE");
+ expect(error).toMatchObject({ owner: "user", publicMetadata: { exceeded: "generated" } });
+ });
+
+ it("refuses many media elements bound to one long value", async () => {
+ const url = `https://example.com/${"a".repeat(MAX_VARIABLE_MEDIA_SRC_BYTES - 100)}.mp4`;
+ const videos = `
`.repeat(4_200);
+ const dir = project({ "index.html": withBody(videos), "scene.html": SCENE });
+ const error = await compileForRender(dir, join(dir, "index.html"), dir, {
+ variables: { clip: url },
+ }).then(
+ () => undefined,
+ (e: unknown) => e,
+ );
+ expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE");
+ expect(error).toMatchObject({ publicMetadata: { exceeded: "generated" } });
+ });
+
+ it.each([
+ ["longer than 8 KiB", `https://example.com/${"a".repeat(MAX_VARIABLE_MEDIA_SRC_BYTES)}.mp4`],
+ ["holding a control character", "https://example.com/a\nb.mp4"],
+ ])("leaves src unchanged for a bound value %s, and binds a normal one", async (_, bad) => {
+ const dir = project({
+ "index.html": withBody(`
`),
+ "scene.html": SCENE,
+ });
+ const srcFor = async (clip: string) => {
+ const compiled = await compileForRender(dir, join(dir, "index.html"), dir, {
+ variables: { clip },
+ });
+ return parseHTML(compiled.html).document.getElementById("v1")?.getAttribute("src");
+ };
+ expect(await srcFor(bad)).toBe("fallback.mp4");
+ expect(await srcFor("https://example.com/my clip.mp4")).toBe("https://example.com/my clip.mp4");
+ expect(await srcFor("https://example.com/x.mp4\n")).toBe("https://example.com/x.mp4");
+ });
+});
diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts
index eb098249ab..ff90566ac1 100644
--- a/packages/producer/src/services/htmlCompiler.ts
+++ b/packages/producer/src/services/htmlCompiler.ts
@@ -1190,6 +1190,7 @@ function inlineSubCompositions(
variableOverrides: Record
,
assetRoot: string | undefined,
outsideFiles: OutsideFilePolicy,
+ budget: CompositionExpansionBudget,
): { html: string; variablesByComp: Record> } {
const { document } = parseHTML(html);
const head = document.querySelector("head");
@@ -1205,11 +1206,11 @@ function inlineSubCompositions(
document as unknown as Document,
{},
variableOverrides,
+ { chargeBytes: (bytes) => budget.chargeGenerated("composition variables", bytes) },
);
return { html: emitted ? document.toString() : html, variablesByComp: {} };
}
- const budget = new CompositionExpansionBudget();
const readOnce = new Map();
const readMountHtml = (srcPath: string): string | null => {
const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot);
@@ -1378,6 +1379,7 @@ function inlineSubCompositions(
document as unknown as Document,
result.variablesByComp,
variableOverrides,
+ { chargeBytes: (bytes) => budget.chargeGenerated("composition variables", bytes) },
);
// Inlining is what makes element ids ambiguous: each composition file is
@@ -2261,11 +2263,20 @@ function rebaseDirectEntryAssetPaths(html: string, projectDir: string, htmlPath:
return document.toString();
}
+export const MAX_VARIABLE_MEDIA_SRC_BYTES = 8 * 1024;
+// oxlint-disable-next-line no-control-regex
+const MEDIA_SRC_CONTROL_CHARS = /[\u0000-\u001f\u007f]/;
+
/**
* Point each `data-var-src` video/audio at the clip its variable names, so the length and sound read below
* are the bound clip's. Media under a per-instance `data-variable-values` host keeps the browser's answer.
*/
-function bindVariableMediaSources(html: string, variables: Record): string {
+function bindVariableMediaSources(
+ html: string,
+ variables: Record,
+ budget: CompositionExpansionBudget,
+ log?: ProducerLogger,
+): string {
if (!html.includes("data-var-src")) return html;
const { document } = parseHTML(html);
const declared: Record = {};
@@ -2276,8 +2287,17 @@ function bindVariableMediaSources(html: string, variables: Record MAX_VARIABLE_MEDIA_SRC_BYTES || MEDIA_SRC_CONTROL_CHARS.test(src)) {
+ log?.warn("[Compiler] Refused a variable media source that cannot be a URL", {
+ length: src.length,
+ });
+ continue;
+ }
+ if (el.getAttribute("src") === src) continue;
+ budget.chargeGenerated("variable media sources", src.length);
el.setAttribute("src", src);
changed = true;
}
@@ -2316,9 +2336,12 @@ export async function compileForRender(
}
assertCompositionFileSize(htmlPath, basename(htmlPath));
const entryHtml = rebaseDirectEntryAssetPaths(readTextFile(htmlPath), projectDir, htmlPath);
+ const budget = new CompositionExpansionBudget();
const rawHtml = bindVariableMediaSources(
normalizeCompositionSrcs(rewriteUnresolvableGsapToCdn(entryHtml, projectDir), projectDir),
options.variables ?? {},
+ budget,
+ options.log,
);
// Pre-flight: every data-composition-src reference must resolve to a
@@ -2369,6 +2392,7 @@ export async function compileForRender(
options.variables ?? {},
assetRoot,
outsideFiles,
+ budget,
);
// Strip preload="none" from media elements — the renderer needs to load all