Skip to content

fix(rules): overlay CSS reads group at-rules, nesting, selectors and specificity #1409 - #1535

Closed
nc9 wants to merge 5 commits into
mainfrom
fix/overlay-css-1409
Closed

nc9 wants to merge 5 commits into
mainfrom
fix/overlay-css-1409

Conversation

@nc9

@nc9 nc9 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1409, refs #784.

Reproduced on main (v0.0.159) before the change. One iframe that is a fixed full-viewport cover only through its CSS, 23 CSS shapes, detectFakeAuthOverlay:

shape main this PR
@media all, @media screen, @layer, @supports, @container, CSS nesting no finding finding
main iframe, main .c, iframe[id], iframe[id="x"] no finding finding
#id{fixed} then .c{static} (the id wins in a browser) no finding finding
comment inside a declaration or selector, } in a comment, escapes, inset-block/inset-inline finding (already handled by #1496) finding
.c{static} only, @media print{...} no finding no finding

Change (packages/rules/src/integrity/overlay-style.ts):

  • Group at-rules (@media, @supports, @layer, @container, @scope) are read in place in all three readings; a @media list whose every query is print-only, speech-only or not all is skipped. Blocks are not searched for their end, so unclosed openers stay linear.
  • Style rules nested in a style rule are read to depth 8 (& is the parent, no & a descendant); deeper is not read and the CSS is reported cssTruncated.
  • A complex selector is read for its subject (last) compound: tag iframe or *, .class, #id, attribute selectors with all six operators and the i/s flag, pseudo-classes taken as matching. Another tag or a pseudo-element cannot match. The ancestors are not checked, which errs toward detection; no per-element tree walk, closest() or whole-document scan.
  • The cascade is !important, then specificity (ids, then classes/attributes/pseudo-classes, then tag), then source order. The memo key now includes the frame's values of the attributes some selector tests.
  • hiddenSelectors is unchanged (it keeps the strict selector reader and does not read at-rules): reading more there would hide real label text.

Nesting bounds (follow-up commit, after the automatic security review): a nested rule's selector is read for its last compound only, so a child without & is itself whatever its parent was, and & is the parent's last compound, never its whole text: nothing multiplies with depth. What remains is capped: 64 parts per list, no produced selector over 2,048 characters (its length is worked out before it is built, so a run of & is not expanded), 50,000 nested rules and 20,000 produced selectors per sheet, depth 8. Whatever a cap drops makes the CSS cssTruncated; so does a style rule whose body is past the 256 KB bound and holds nested rules (it was skipped in silence before, because its own selector could not match an iframe). Asserted by counts, not the clock: 20 parts x 10 levels all using & produce 9,360 selectors over 8 levels, 200 x 200 parts produce 4,096, 40 rules of 64 x 64 stop at exactly 20,000, 120,000 nested rules stop at exactly 50,000, a child without & is produced once for 64 parents; 1 MB of & and 1,000 & against a 2,000-character parent produce none; 50,000 levels of nesting read 8 rules in 33 ms (iterative, no recursion beyond the bound). Mutants of each cap (rule cap, selector budget, parts cap, length bound, per-parent emission of a child without &, cut flag, nested-body flag, rule-cap and depth reporting) each fail a test; one survivor is equivalent (the nested-rule oversize callback is unreachable, since a nested bound is always inside its parent's).

Review follow-up (parity with a headless Chrome matrix of 167 shapes, one push): the first version applied rules it could not evaluate as if they matched, so a decoy that a browser never applies (@media (max-width:1px){.cover{position:static}}, .nothing .cover{...}, .cover:hover{...}, @layer after an unlayered cover) hid the real cover: 26 new false negatives against base. Now:

  • Rules of unknown applicability are tagged: those in an at-rule other than @media all|screen|(none) (@supports, @container, @scope, @layer, a media condition, a prelude past 2,048 characters, which is read as undecided with its block still read), and those whose selector has an ancestor part or a pseudo-class (also in nesting, where & is the parent's last compound and a child without & is a descendant).
  • The cascade is worked out twice, with every rule applied and with only the decided ones, and the frame is a cover when either says so (a union like the three CSS readings). An undecided rule can enable a cover (a lone signal: benign .modal-open .frame{position:fixed...} frames are reported at info, an accepted cost) but cannot disable one.
  • A rule or at-rule left open at the end of the CSS ends there (all three readings; base dropped it).
  • Nested order: declarations before the first nested rule, then the nested rules, then the declarations after them, as a browser orders them; nested blocks are taken out of the declaration text, so .c{&{...} position:fixed} reads position:fixed.
  • An attribute value is compared without case on both sides (docs fixed); the docs no longer claim <!-- --> is not read; CHANGELOG states the measured cost (up to about 1.9 s) instead of "under a second"; the nested-oversize callback, unreachable since a nested bound lies inside its parent's, is removed.
  • Tests (87): every false-negative shape of the review as a decoy that must still fire (at-rules, undecided selectors, @layer, prelude lengths 2,060, 3,000 and 100,000, nested decoys), decided decoys that still disable (@media screen, an exact selector), enablers, the three readings, open rules in each reading, nested order and the declaration splitter, a dedicated & test. 18 mutants of the new guards (no decided pass, decided keeping undecided at-rules or selectors, hasUnknown never set, other at-rules known, any media query known, long prelude unread or known, EOF in the token and the plain reading, nested order, nested blocks kept in declarations, attribute case, nested descendant and parent ancestor unmarked, ancestor part and pseudo-class not unknown) each fail a test; the token-reading EOF mutant only died once the test read that reading alone.

Hostile timing (detectFakeAuthOverlay, whole rule, all three readings; the sheet is read up to 4,000,000 characters and reported cssTruncated past that): at 5 MB / 10 MiB: unclosed @media all{ openers 351 / 345 ms, balanced @media all{.n{border:0}} blocks 423 / 367 ms, unclosed a{ openers 67 / 90 ms, nesting past the depth bound 78 / 87 ms, a selector list of millions of parts 83 / 141 ms, 5 MB+ of iframe[a="v"]{border:0} rules 554 / 754 ms, 4 KB descendant selectors 98 / 103 ms, unclosed [ and :is( 234 / 715 ms, escaped at-rule names 506 / 466 ms. With the real overlay rule inside the amount read, it is found in each case that has one. 3,000 frames with distinct attribute values against 2,000 attribute rules: under the 6 s bound.

Mutants (each new guard, run separately, each makes at least one test fail): no at-rule descent (17), print filter off (1), not all off (1), specificity ignored (3), source order ignored (1, after adding a cross-key equal-specificity case), attribute match always true (3), another tag accepted (2), nesting depth 100 (1), memo key without attributes (1), no universal list (2), & not replaced (1), pseudo-class counts nothing (1), :where() counted (1), hidden-text scan reading at-rules (1). One survivor, equivalent: accepting a pseudo-element after removing its explicit guard is still rejected by the identifier check that follows.

Limits, documented in the rule page and CHANGELOG: @layer precedence is not modelled, pseudo-class arguments are not evaluated, attribute values compare case-insensitively (the selector is lower-cased first), nested conditional group rules with bare declarations are not read, an at-rule whose condition may hold ((min-width:1px), @supports not (...)) is read.

Tests: packages/rules/tests/overlay-css-1409.test.ts (87). Passing with the existing integrity-adversarial-784, integrity-evasions-784, integrity-bounds-1423, integrity-budget-784, integrity-budget-1424, integrity, integrity-text-boundary suites; tsgo; docs make validate; the changelog guard. controls.ts is not touched.

Skill: no change needed.

nik

nc9 added 5 commits November 2, 2026 15:37
… attribute selectors, and the specificity cascade #1409

Signed-off-by: Nik Cubrilovic <[email protected]>
…aps on parts, length, rules and selectors, nested oversize reported #1409

Signed-off-by: Nik Cubrilovic <[email protected]>
…ded and full cascades), open rules end at EOF, nested order, long preludes read, layers undecided #1409

Signed-off-by: Nik Cubrilovic <[email protected]>
… the decided reading; spent nesting budget stops the scan; tests for nested at-rules and long nested children #1409

Signed-off-by: Nik Cubrilovic <[email protected]>
…e decided reading, HTML whitespace, s flag not read, case-insensitive attributes; tests for exact budget exits and selector lowering #1409

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9

nc9 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Shipped in v0.0.160. The commits were cherry-picked onto the release branch and rebase-merged: f56ac8e 3e84326 ced8484 94dab13 1e7e28e.

Closing this PR since the change is on main.

nik

@nc9 nc9 closed this Nov 2, 2026
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.

integrity/fake-auth-overlay: model more of the CSS that styles an overlay iframe

1 participant