fix(producer): refuse a render whose mounts copy more than 100,000 elements (#8295)

* test(producer): cap the elements a render's mounts copy

* fix(producer): refuse a render whose mounts copy more than 100,000 elements
This commit is contained in:
Miguel Ángel
2026-11-20 18:49:53 +00:00
committed by GitHub
parent 6cf81c6689
commit 31959db985
5 changed files with 86 additions and 1 deletions
@@ -1259,6 +1259,26 @@ describe("SVG namespacing compatibility on current compiler", () => {
});
});
it("tells the caller each mount's element count, template content included, before copying it", () => {
const { document } = parseHTML(`<!DOCTYPE html><html><body><div data-composition-id="main">
<div data-composition-id="a" data-composition-src="art.html" data-start="0" data-duration="2"></div>
<div data-composition-id="b" data-composition-src="art.html" data-start="0" data-duration="2"></div>
</div></body></html>`);
const charged: Array<[string, number]> = [];
inlineSubCompositions(document, [...document.querySelectorAll("[data-composition-src]")], {
resolveHtml: () =>
`<template><div data-composition-id="art"><p><b></b></p><i></i></div></template>`,
parseHtml: (html) => parseHTML(html).document,
chargeElements: (src, count) => charged.push([src, count]),
});
expect(charged).toEqual([
["art.html", 5],
["art.html", 5],
]);
});
// fails without the fix: renaming a mount's colliding SVG ids charged nothing to the render's budget.
it("charges the bytes SVG id renaming adds to the mount that grew", () => {
const { document } = parseHTML(`<!DOCTYPE html><html><body><div data-composition-id="main">
@@ -320,6 +320,22 @@ export interface InlineSubCompositionsOptions {
* built, and each scoped sheet rule by rule as `scopeCssToComposition` reports it. May throw.
*/
chargeEmitted?: (srcPath: string, bytes: number) => void;
/** Told how many elements each mount copies, before it is copied. May throw. */
chargeElements?: (srcPath: string, count: number) => void;
}
/** Every element under `root`, an HTML template's content included. */
function countElements(root: ParentNode): number {
let count = 0;
const pending: ParentNode[] = [root];
for (let node = pending.pop(); node; node = pending.pop()) {
for (const el of Array.from(node.children)) {
count += 1;
const html = el.localName === "template" && !el.firstElementChild && "content" in el;
pending.push(html ? (el as HTMLTemplateElement).content : el);
}
}
return count;
}
export interface ExternalLink {
@@ -417,6 +433,7 @@ export function inlineSubCompositions(
tagScenes = false,
readProjectScript,
chargeEmitted,
chargeElements,
} = options;
const rootStyles = [...document.querySelectorAll("style")];
@@ -441,6 +458,12 @@ export function inlineSubCompositions(
}
return doc;
};
const elementCounts = new Map<string, number>();
const elementCount = (html: string) => {
let count = elementCounts.get(html);
if (count === undefined) elementCounts.set(html, (count = countElements(parseOnce(html))));
return count;
};
const parseCopy = (html: string): Document => {
const copy = parseOnce(html).cloneNode(true);
return isDocument(copy) ? copy : parseHtml(html);
@@ -485,6 +508,7 @@ export function inlineSubCompositions(
continue;
}
chargeElements?.(src, elementCount(compHtml));
const compDoc = parseCopy(compHtml);
// Determine composition IDs
@@ -17,6 +17,8 @@ const megabytes = (bytes: number) => `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
*/
export const MAX_EXPANDED_COMPOSITION_BYTES = 4 * MAX_COMPOSITION_HTML_BYTES;
const MAX_COMPOSITION_MOUNTS = 10_000;
/** About 4x the largest composition in 7 days of production renders (24,800 elements). */
export const MAX_EXPANDED_ELEMENTS = 100_000;
class CompositionTooLargeError extends Error {
readonly code = COMPOSITION_TOO_LARGE;
@@ -30,7 +32,9 @@ class CompositionTooLargeError extends Error {
bytes: number;
limitBytes: number;
mounts?: number;
exceeded?: "bytes" | "mounts" | "generated";
elements?: number;
limitElements?: number;
exceeded?: "bytes" | "mounts" | "generated" | "elements";
}>,
) {
super(message);
@@ -84,6 +88,23 @@ export class CompositionExpansionBudget {
private bytes = 0;
private mounts = 0;
private generated = 0;
private elements = 0;
chargeElements(file: string, count: number): void {
this.elements += count;
if (this.elements <= MAX_EXPANDED_ELEMENTS) return;
throw new CompositionTooLargeError(
`Sub-composition mounts, ending at "${file}", add up to ${this.elements} elements. ` +
`One render inlines at most ${MAX_EXPANDED_ELEMENTS} elements. Mount large or repeated scenes fewer times.`,
{
bytes: this.bytes,
limitBytes: MAX_EXPANDED_COMPOSITION_BYTES,
elements: this.elements,
limitElements: MAX_EXPANDED_ELEMENTS,
exceeded: "elements",
},
);
}
/** `bytes` may be negative when a reserved upper bound is settled to the real size. */
chargeGenerated(file: string, bytes: number): void {
@@ -9,6 +9,7 @@ import {
CompositionExpansionBudget,
MAX_COMPOSITION_HTML_BYTES,
MAX_EXPANDED_COMPOSITION_BYTES,
MAX_EXPANDED_ELEMENTS,
} from "./compositionSizeLimit.js";
import {
compileForRender,
@@ -141,6 +142,24 @@ describe("composition expansion budget", () => {
expect(() => budget.charge("scene.html", 1)).toThrow(/64\.0 MB of composition HTML/);
});
// fails without the fix: the budget counted bytes, so a small scene mounted many times copied any number of elements.
it("refuses mounts whose copied elements pass the cap, before copying past it", async () => {
const perScene = 1000;
const scene = `<template><div data-composition-id="scene">${"<i></i>".repeat(perScene - 2)}</div></template>`;
const mounts = Math.floor(MAX_EXPANDED_ELEMENTS / perScene) + 1;
const error = await compileError(
project({ "index.html": rootMounting("scene.html", mounts), "scene.html": scene }),
);
expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE");
expect(error).toMatchObject({
publicMetadata: {
elements: mounts * perScene,
limitElements: MAX_EXPANDED_ELEMENTS,
exceeded: "elements",
},
});
});
it("refuses one large file mounted many times", async () => {
const big = Math.ceil(MAX_EXPANDED_COMPOSITION_BYTES / 5) + 1;
const dir = project({
@@ -1341,6 +1341,7 @@ function inlineSubCompositions(
return compHtml;
},
chargeEmitted: (srcPath: string, bytes: number) => budget.chargeGenerated(srcPath, bytes),
chargeElements: (srcPath: string, count: number) => budget.chargeElements(srcPath, count),
parseHtml: (htmlStr: string) => parseHTML(htmlStr).document as unknown as Document,
// Mirrors the preview bundler: a sub-composition's SIBLING assets resolve
// against its own directory, project-root refs stay as authored.