Skip to content

fix(css): deep or self-multiplying stylesheets ran out of stack or time - #128

Merged
jamesconwaydev merged 3 commits into
mainfrom
css-limits
Nov 20, 2026
Merged

jamesconwaydev merged 3 commits into
mainfrom
css-limits

Conversation

@jamesconwaydev

@jamesconwaydev jamesconwaydev commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The CSS parser doesn't recurse, but plenty of code that reads its output does: the derived Clone, Debug and PartialEq on component values and rules, and the cascade's walk over nested rules. A stylesheet or style attribute nested tens of thousands deep aborted the process with a stack overflow.

Other inputs ran far longer than their size suggests:

  • var() substitution capped the number of values copied, not their size, so a short sheet could copy gigabytes.
  • Nested style rules wrote their parents' selectors in again at every level, and wrapped them in :is(), so both the selector text and the time to match it could double per level.
  • Cascade layer names were found by a linear scan for every part of every name.
  • parse_block_contents on rules with no ; between them scanned to the end of the input once per rule.

Separately, cargo doc -p parsec-html-css failed with warnings denied: two links rustdoc couldn't resolve and one to a private item.

Change

  • Functions, blocks and rules nest at most MAX_NESTING (256) deep, counted together. Anything that would open deeper is skipped whole as it's read, so nothing kept is deeper than that; a skipped rule is reported as InvalidRule. MAX_NESTING is exported.
  • A substituted value may copy in at most 1 MiB of the stylesheet, as well as the existing value count. Past that it is invalid at computed-value time.
  • A nested rule's selectors, with their parents written in, are capped at 64 KiB; past that the rule and what's nested in it are ignored. A single parent with no &, or with & alone at the start, is written in as it is rather than as :is(parent). It matches the same elements with the same specificity.
  • Layers are kept in a map, a layer name may have 64 parts, and rules nested deeper than 128 are left out of the cascade.
  • Inside a block, a would-be declaration stops being read as soon as it can't be one (no colon after the name, or a {} block with something other than !important beside it). Both the lazy parser and parse_stylesheet_deep do this.
  • The rustdoc links are fixed, and CI's lint job now builds every crate's docs with RUSTDOCFLAGS=-D warnings, a crate at a time.

Tests

  • crates/parsec-css/tests/limits.rs: each deep input parsed, cloned, compared, debug-printed and cascaded on a 1 MiB stack (on macOS, a debug build needs under 512 KiB; the caps were 512 and 256 at first, which overflowed 1 MiB in Windows debug builds); doubling custom properties are invalid, not slow; nested selectors under real ancestors match in bounded time; long and deeply nested layer names; a{} repeated 40,000 times through all three block parsers. Shallow nesting, & > and layers still give the same results.
  • The deep-nesting unit test in deep.rs now checks where the cut happens and that nothing past it is kept.
  • The conformance counts are unchanged: css-parsing-tests, the tokenizer tests and the four WPT suites give the same passed, failed and skipped as on main.
  • cargo test -p parsec-html-css, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --check, and cargo doc --no-deps -p <crate> with warnings denied for every crate.

Deep nesting parsed fine, but a derived Clone, Debug or comparison of
the result recursed on it, and so did the cascade, so a few hundred KB
of `{` or `var(--a, ` overflowed the stack. Values, blocks and rules now
nest at most MAX_NESTING (512) deep together; deeper ones are dropped
whole as they're read, and a dropped rule is an InvalidRule error.

The cascade had its own ways to blow up:
- var() substitution counted values, not bytes, so a few doubling
  custom properties over one long string copied gigabytes. A value may
  now copy in at most 1 MiB.
- Each level of nesting wrote its parents' selectors in again, so
  `& &, & &` doubled per level. A nested rule's selectors stop at
  64 KiB, and a lone parent is written in without `:is()`, which
  matched in time that doubled with each level.
- Layer names were looked up by a linear scan per part. They're in a
  map now, and a name may have 64 parts.
- Rules nested deeper than 256 are left out of the cascade.

parse_block_contents on `a{}a{}…` scanned to the end of the input for
each `a`, looking for a `;` that would make it a declaration. It now
stops as soon as what it has seen can't be one.

The parsec-html-css docs linked to items rustdoc couldn't resolve. CI
now builds every crate's docs with warnings denied.

@foothillsdigital foothillsdigital left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked f2aa73f. The parser and cascade changes hold up, and one check is red for a real reason.

The Windows job: test (windows-latest) fails in tests/limits.rs with thread '<unknown>' has overflowed its stack (STATUS_STACK_OVERFLOW). That's after layer_names_are_capped, shallow_enough_still_applies, var_substitution_is_capped_in_bytes and nesting_selectors_are_capped passed.

On Linux, with the test's stack made a variable (debug build, --test-threads=1):

stack deep_values_and_rules_stay_on_the_stack rules_without_semicolons_…
1024 KiB ok ok
896 KiB overflows ok
768 KiB overflows ok

So in debug the deep test needs between 896 KiB and 1 MiB of its 1 MiB thread. Windows' bigger debug frames take it over. In release, the whole file passes at 512 KiB and overflows at 256 KiB.

I'd rather not fix this by raising the test's stack. 1 MiB is Windows' default main-thread stack, so a debug build of an app that styles a hostile sheet on its main thread would still abort, which is the crash this PR is closing. Lowering whichever bound does the deepest recursing would leave real margin: MAX_NESTING (512) for the derived Clone/PartialEq/Debug, or MAX_RULE_DEPTH (256) for Collect::rule, whose frames are much bigger. I didn't narrow it to one case of the five. Running each case on its own thread at 896 KiB would show which.

The rest of my checks passed:

  • A parser differential. 200,000 random stylesheets, built from 38 fragments ({ } ; : ! important --x & @media @layer, functions, strings, comments), up to 40 pieces each. I hashed the output of:

    • parse_stylesheet_deep (Debug and to_string);
    • parse_stylesheet;
    • parse_block_contents;
    • parse_declaration_list.

    Every hash matched upstream/main. So Watch only stops sooner and never changes what's parsed, as you say. That includes {} !important, ! important with a space, and custom properties holding anything.

  • nest(). Writing in a single parent in place of :is(parent) is equivalent where you use it. :is(X) has X's specificity, and with & alone at the start, followed by a combinator or nothing, P > .c matches what :is(P) > .c matches. A compound such as &.c, a second &, or several parents still take :is(). A parent with a pseudo-element is invalid either way.

  • Layers: id(&[]) first still makes the unlayered name 0, and ranks uses the index for the same lookups.

  • The other jobs: the remaining 32 checks pass.

Debug builds on Windows ran out of a 1 MiB stack printing, cloning and
cascading a stylesheet nested to the cap. MAX_NESTING is now 256 and
the cascade looks 128 rules deep: on macOS the deep tests need under
512 KiB in a debug build, so a 1 MiB stack has room on every platform.

@foothillsdigital foothillsdigital left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked 2b27298, with MAX_NESTING at 256 and MAX_RULE_DEPTH at 128. The same Linux debug measurement as before:

stack f2aa73f 2b27298
1024 KiB ok ok
896 KiB overflows ok
512 KiB overflows ok
448 KiB overflows overflows

So the deep test now needs under 512 KiB in debug, about half of Windows' 1 MiB main thread, where it was over 896 KiB. That matches your macOS figure.

The rest still holds:

  • deep.rs's own test follows MAX_NESTING - 1 without changes, and parsec-html-css's tests pass here.
  • My parser differential stayed well under either limit, so it's unaffected.
  • All checks are green, test (windows-latest) included.

Approving.

@jamesconwaydev
jamesconwaydev added this pull request to the merge queue Nov 20, 2026
@jamesconwaydev
jamesconwaydev removed this pull request from the merge queue due to a manual request Nov 20, 2026
@jamesconwaydev
jamesconwaydev added this pull request to the merge queue Nov 20, 2026
Merged via the queue into main with commit bea6b28 Nov 20, 2026
33 checks passed
@jamesconwaydev
jamesconwaydev deleted the css-limits branch November 20, 2026 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants