From 31959db985269172f521997528a97d2a9e9c61cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 20 Nov 2026 18:49:53 +0000 Subject: [PATCH] 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 --- .../compiler/inlineSubCompositions.test.ts | 20 ++++++++++++++++ .../src/compiler/inlineSubCompositions.ts | 24 +++++++++++++++++++ .../src/services/compositionSizeLimit.ts | 23 +++++++++++++++++- .../services/htmlCompiler.sizeLimit.test.ts | 19 +++++++++++++++ .../producer/src/services/htmlCompiler.ts | 1 + 5 files changed, 86 insertions(+), 1 deletion(-) diff --git a/packages/core/src/compiler/inlineSubCompositions.test.ts b/packages/core/src/compiler/inlineSubCompositions.test.ts index 57475c8373..982ec6dd30 100644 --- a/packages/core/src/compiler/inlineSubCompositions.test.ts +++ b/packages/core/src/compiler/inlineSubCompositions.test.ts @@ -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(`
+
+
+
`); + const charged: Array<[string, number]> = []; + + inlineSubCompositions(document, [...document.querySelectorAll("[data-composition-src]")], { + resolveHtml: () => + ``, + 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(`
diff --git a/packages/core/src/compiler/inlineSubCompositions.ts b/packages/core/src/compiler/inlineSubCompositions.ts index 64b370dae4..beeebaf798 100644 --- a/packages/core/src/compiler/inlineSubCompositions.ts +++ b/packages/core/src/compiler/inlineSubCompositions.ts @@ -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(); + 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 diff --git a/packages/producer/src/services/compositionSizeLimit.ts b/packages/producer/src/services/compositionSizeLimit.ts index e35c4892cc..287db782d5 100644 --- a/packages/producer/src/services/compositionSizeLimit.ts +++ b/packages/producer/src/services/compositionSizeLimit.ts @@ -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 { diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts index ee09b3f2f5..08582982ad 100644 --- a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -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 = ``; + 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({ diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index d64a0313eb..4a50719581 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -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.