Skip to content

fix(rules): read url(), blocks and escapes like a browser in content/hidden-text #679 - #685

Merged
nc9 merged 1 commit into
mainfrom
fix/hidden-text-css-structure-679
Oct 12, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/hidden-text-css-structure-679

Conversation

@nc9

@nc9 nc9 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

content/hidden-text read <style> sheets and inline styles with a scanner that treated a {, } or ; inside an unquoted url(...), inside a ( or [ block, or after a backslash as structure. A sheet such as .a{background:url(}.b{display:none}.c{)} was read as hiding .b, which Chromium shows: the unquoted url( is one token up to ).

Change

packages/rules/src/content/hidden-text.ts, in parseRules (preludes and rule bodies) and parseDeclarations (rule bodies and inline style):

  • An escaped character is part of a name, never structure.
  • An unquoted url( runs to its ) as one token, escapes included, bad-url remnants too. url("...") stays an ordinary function. url( that continues another token (after a name character, #, @, an escaped character, or the whitespace that ends a hex escape) is a function or a ( block, as in a browser.
  • (, [ and { open blocks that only their own closer ends; any other closer inside is plain text.

This is a port of stepBlocks / skipUnquotedUrl from wip/static-hidden-cascade-664-665 (ee5a5d6), applied always rather than behind a strict mode, plus the token-boundary check codex found missing from that port. Everything stays one forward pass; the only lookback (url token boundaries) is bounded by six hex digits plus one backslash run that no other url( can share.

Rule doc page and CHANGELOG updated.

Acceptance evidence

  • The repro fixture produces no finding. CLI from source, --http -m 1 --offline against a local page with the issue's <style> and paragraph: origin/main reports content/hidden-text, this branch reports nothing. Also row 1 of the Chromium set.
  • } and ; inside an unquoted url(...), inside (/[ blocks, and after a backslash are not structure. Covered by rows for each construct, in selectors, rule bodies and inline styles.
  • A plainly written display:none rule still produces the finding, and existing hidden-text tests pass. Rows a plain display:none rule, an ordinary url(), a quoted url(), a data: url() with ; inside, Tailwind escaped class names, @media with parentheses, nested rules, all hidden. 148 hidden-text tests pass.
  • Linear on hostile input. A flood test (400,000 (, [, {, u or backslashes, 100,000 url(, url( + whitespace runs, and floods aimed at the url-boundary lookback) checks each page in under a second.
  • A Chromium-derived row set covers each construct. 60 rows, each verdict read from headless Chromium 156 (getComputedStyle on the payload under the row's stylesheet or inline style, with the probe asserting the browser received the exact bytes). All 60 match this branch; origin/main disagrees with Chromium on 24 of the original rows. CRLF after a hex escape cannot reach Chromium's CSS parser from HTML (the HTML parser normalizes it) but linkedom keeps it, so it has its own test.

No change on ordinary stylesheets. Old vs new over a local corpus of 16,859 real pages: 312 pages parse some <style> block or inline style differently (in a sample: JavaScript inside <style>, nested rules, an escaped quote in a string, Tailwind sheets where the old scanner lost its place), and 0 pages change their hidden-text result.

Checks

  • bun test packages/rules/tests/hidden-text.test.ts 148 pass (24 of the new rows fail on origin/main)
  • bun test packages/audit-engine/tests/streaming-rules-canonical-golden.test.ts packages/audit-engine/tests/scan-truncated-carry.test.ts packages/audit-engine/tests/item-locator-resolve.test.ts packages/rules/tests/item-cap-remainder.test.ts 52 pass
  • packages/rules tsgo clean; docs tangly check --strict passed
  • codex review: three rounds of url-token-boundary findings (#url(, escaped punctuation, CRLF after a hex escape), each fixed with a Chromium row or test; final pass "no findings"

Not in this PR

A comment before a selector (/* Section */ .b{display:none}) still keeps that rule from applying, on main and here: filed as #684. It errs toward no finding, and fixing it changes results on ordinary stylesheets, so it needs its own corpus check.

Agent skill: no change needed (rule id, options and output shape are unchanged).

Closes #679

@nc9
nc9 marked this pull request as ready for review October 12, 2026 01:12
@claude

claude Bot commented Oct 12, 2026

Copy link
Copy Markdown

Reviewed the diff for content/hidden-text tokenization (#679). I found no blocking issues.

  • (low) packages/rules/tests/hidden-text.test.ts: the linear-time test asserts a hard < 1_000 ms wall-clock limit for each of 14 floods. It could flake on a loaded CI runner. Consider a generous multiplier or a relative comparison against a baseline input size.
  • (nit) hidden-text.ts: isNameChar and urlTokenCanStart build a regex test per call. They sit on the hot path for every u/U character in a sheet. A charCode range check would avoid that, though the flood tests suggest it is still fast enough.
  • (nit) The scanner now has three similar loops (parseDeclarations, parseRules prelude, and rule body). Each repeats the escape, string, comment and stepBlocks sequence. A shared step helper would reduce drift risk.

Positives:

  • The scanner stays O(n).
  • The browser-verified Chromium table gives strong coverage, including the escape and hex-escape edge cases.
  • The CHANGELOG and docs are updated.

@nc9

nc9 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Review triage (head 47eafdc, no code change):

  • Flood test wall-clock bound: kept. Measured on this branch on a loaded laptop (load average about 3.6), the slowest of the 14 floods takes 14 ms (a 400,000-character { flood; most take 1 to 12 ms), so the 1,000 ms bound has about 70x headroom. A quadratic regression on 400,000 characters would take seconds, which is what the bound is for. The issue's criterion is "well under a second".
  • Regex per call on every u/U: not on that path. skipUnquotedUrl first compares the next four characters by char code and returns unless they spell url(; isNameChar and the hex-digit test in urlTokenCanStart only run after that match, and the hex test only when the character before url( is whitespace.
  • Shared step helper for the three loops: left as is. The loops differ in what they treat as structure (; in declarations, ;/{/} and at-rule depth in preludes, the matching } in bodies), and the shared part is already stepBlocks plus the two skip helpers. A wrapper would hide which characters each loop stops on.

…hidden-text #679

The default CSS scanner read a {, } or ; inside an unquoted url(...), a
( or [ block, or after a backslash as structure, so
.a{background:url(}.b{display:none}.c{)} hid .b, which Chromium shows.
parseRules and parseDeclarations now skip escapes, read an unquoted url(
to its ) as one token (only where a url token can start), and track
blocks so only the expected closer ends one. Still a single linear pass.
Verified against 60 rows read from headless Chromium 156; a 16,859-page
corpus run changes no finding.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/hidden-text-css-structure-679 branch from 47eafdc to 848bb1e Compare October 12, 2026 04:24
@claude

claude Bot commented Oct 12, 2026

Copy link
Copy Markdown

Reviewed the diff for content/hidden-text. I found no blocking issues. I did not run the tests, and I could not re-check the Chromium verdicts.

  • (low) packages/rules/tests/hidden-text.test.ts (flood test): the < 1_000 ms wall-clock assertion is a flake risk on slow CI runners. It runs 14 floods in one test. Consider a looser bound, or comparing time at n and 2n input sizes to check linearity.
  • (low) hidden-text.ts parseRules: a prelude with an unterminated ( or [ now swallows the rest of the sheet. That matches a browser, but it means every later rule goes unscanned, so a real display:none rule after it produces no finding. A short comment saying this is deliberate would help.
  • (nit) stepBlocks is called for every u or U character, and only then does it check for url(. A check for css[i+3] === "(" before the call would skip most calls. This is a micro-optimisation.
  • (nit) CSS_WHITESPACE and isNameChar rely on a regex per call. This is fine today because the lookback is bounded, but a char-code check would avoid the regex.

The scanner stays a single forward pass, the url-boundary lookback is bounded, and the 60 Chromium-derived rows plus the CRLF and flood tests are solid coverage. The CHANGELOG and docs are updated.

@nc9
nc9 merged commit dd11c9c into main Oct 12, 2026
15 checks passed
@nc9
nc9 deleted the fix/hidden-text-css-structure-679 branch October 12, 2026 04:36
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.

content/hidden-text: a CSS parser differential reads a stylesheet as hiding what a browser shows

1 participant