Repository navigation
fix(compiler): bound composition variable styles and variable media sources - #8144
Conversation
…ources Charge every compile-time composition-variable rule against a size budget and stop emitting once it is exceeded. The render compiler shares one CompositionExpansionBudget between mounts, variable styles and variable media sources; other callers get a 32 MiB core default. Host data-variable-values are now parsed once per host instead of once per declarer. Bound media sources over 8 KiB or holding control characters are refused, and every src written is charged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… in errors Bind the trimmed value, as a browser does, so a trailing newline no longer makes compile-time probing disagree with the runtime; refuse only interior control characters or values over 8 KiB, and log the refusal as a warning. The generated-bytes error now says which of variable styles or variable media sources overran. Adds a with-mount test for the after-inline budget. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…t share down Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Edit accuracy: accurate 271 (base branch 271), smooth 77 of thoseThe gate passes. Quarantined, measured but not gated (0) Unstable (1)
|
jrusso1020
left a comment
There was a problem hiding this comment.
Reviewed at 07e6c4e2b3e62c9b3cc994659fb578fe24b3f0d3.
The producer now carries one generated-output budget through media binding and both variable-style paths (packages/producer/src/services/htmlCompiler.ts:2339,1205,1378). Core charges host, root and declarer rules before retaining them, and a thrown charge prevents the variable stylesheet from being appended (packages/core/src/compiler/htmlBundler.ts:1607–1619). Caching parsed host values by element preserves per-instance ownership.
Validation:
- Full workspace
bun run build: passed. - Core htmlBundler + applyVariableBindings: 237 passed.
- Producer htmlCompiler.sizeLimit: 20 passed; htmlCompiler with its Bun runner: 196 passed.
- Replacing the core charge function with a no-op makes both new size-budget tests fail (“expected function to throw”). Source restored.
- Independent adversarial inspection found no blocker. Preview’s disk fallback was inspected, but no oversized live preview or end-to-end render was run.
Nit: rule.length and src.length count UTF-16 units, consistent with existing generated-output accounting, rather than UTF-8 bytes. The limits bound amplification but should not be described as exact serialized-byte ceilings; clarify that convention or align the entire generated counter in a follow-up.
Need: yes, bound variable-driven output growth · Size: ok · Reuse: CompositionExpansionBudget and existing safe media resolver · Simpler: shared charge callback · Structure: fine (checked existing mount accounting and all variable-rule emitters; no extra budget abstraction needed)
Risk: medium — compilation limits can reject unusually large authored inputs.
Verdict: APPROVE
Reasoning: Both previously uncounted output paths now fail within the shared budget, normal compiler coverage passes, and the new tests discriminate disabled charging.
— Rames / hf-review
What
Bounds two places where a composition variable's value could be copied without limit while a render compiles:
emitRootCompositionVariableStyles(core) now charges every rule it builds against a size budget and stops as soon as the budget is exceeded, before the<style>is appended. It takes an optional last parameter{ chargeBytes }. With none, a core default throws aCOMPOSITION_TOO_LARGEerror pastMAX_COMPOSITION_VARIABLE_STYLE_BYTES(32 MiB). Hostdata-variable-valuesare now parsed once per host, not once per declarer.bindVariableMediaSourcesrefuses a bound value over 8 KiB or holding an ASCII control character (a plain space still binds), leavingsrcunchanged and logging at debug. Everysrcit writes is charged.Why
Many
[data-composition-variables]elements under one host with a longdata-variable-valueseach got a rule holding the full value, and manyvideo[data-var-src]elements bound to one value each got a copy of it. Neither was counted, so memory use grew with elements times value length.Related work
Refs SECOPS-2864. Builds on #8045 and #8094: reuses
CompositionExpansionBudgetand its typed, user-owned, non-retryableCompositionTooLargeError. No second error type in producer.How
compileForRendercreates oneCompositionExpansionBudgetand passes it tobindVariableMediaSourcesandinlineSubCompositions(which no longer creates its own). BothemitRootCompositionVariableStylescalls in the producer passchargeBytesthat callbudget.chargeGenerated("composition variables", n), including the no-mount path that had no budget before.assertSubCompositionsUsable's preflight budget andcompileHtmlFileare unchanged.boundMediaSrclives in core and is shared with the browser runtime, so it is not edited. The guard sits in the producer around its result.Test plan
Core: about 100 declarers under one host holding a 1 MiB value throws
COMPOSITION_TOO_LARGEand appends no<style>; a throwingchargeBytesstops emission at the first call.Producer: the no-mount entry with the same markup fails with
CompositionTooLargeError(owner: "user",retryable: false,exceeded: "generated", codeCOMPOSITION_TOO_LARGEthroughextractSafeRenderErrorCode); about 4,200video[data-var-src]bound to one value just under 8 KiB fail the same way; an over-8 KiB value or one with a newline leavessrcunchanged and a normal URL with a space binds, and one with a trailing newline binds trimmed; a mounted sub-composition whose declarers overrun the budget fails the same way (covers the after-inline wiring).All of the new tests fail on origin/main by a wrong result (no throw,
srcset) at these sizes, and pass here. RanhtmlBundler.test.tsandsrc/compiler,src/runtime/applyVariableBindingsin core (710 passed),htmlCompiler.sizeLimit.test.ts(19 passed) andhtmlCompiler.test.ts(196 passed) in producer;tsc --noEmitclean for core and producer; oxlint and oxfmt clean.Unit tests added/updated
Manual testing performed (no end-to-end render and no studio preview run)
Documentation updated (if applicable): no docs change, no CLI flag or command changed
Comments follow CONTRIBUTING.md "Comments"
Not exercised: the browser-side runtime injection in
getVariables.ts(out of scope, it runs in the render's Chrome page), and a real render of an oversized composition through the producer server's error envelope beyond the unit-level code and fields.🤖 Generated with Claude Code