Skip to content

🐛 fix(sanitize): split CSS tokens where browsers split them - #2137

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/tbmtu9f3
Nov 15, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/tbmtu9f3

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

The scrubber behind style attributes and <style> bodies split CSS into declarations at different places than a browser does for some inputs. Text the scrubber held to be inside one declaration's string or url() could be a separate declaration to a browser. A sanitizer that allows style could therefore let through properties outside its css_properties allow-list. 🔒 After this change the scrubber splits where a browser splits, and drops the few constructs that browsers tokenize differently.

Architecture

The token skipper now treats the CDO token, hash and at-keyword tokens the way CSS Syntax Level 3 does, so url( after them starts a url token only where a browser starts one. The fast path is unchanged: only a name followed by ( or + takes the out-of-line check, which looks back at what precedes the name. The scheme check applies the same CDO rule, so a url() or expression() right after a CDO is checked.

A comment opener inside a url token no longer hides the tokens after it, because a url token's body holds no strings or comments and the scanner now steps over it.

Behavior changes

  • Declarations holding a CDO, hash or at-keyword token right before a function name are split the way a browser splits them, so some values are kept or dropped differently.
  • A declaration with a legacy unicode-range token is dropped, including a real unicode-range descriptor. CSS Syntax Level 3 removed the unicode-range token, and tokenizers that keep it disagree on where it ends, so the scrubber drops a <style> rule whose prelude holds one as well.
  • A url() whose body holds a comment opener is now checked by the scheme check.

Fixes #1768.

The CSS scrubber stepped over `<!--`, `#name` and `@name` one code point
at a time, so it disagreed with a browser about whether a following
`url(` starts a url token. It could then take the inside of a browser
declaration for a string and keep it, letting properties outside
css_properties reach style attributes and <style> bodies.

These tokens only matter when the name after them is followed by `(` or
`+`, so the check runs there, out of line, and looks back at what
precedes the name: a `#` or `@` makes it a hash or at-keyword name, and
an unescaped `<!` before `--` makes the name start after the CDO. Other
names pay one compare. The scheme check steps over url token bodies so a
comment opener inside one hides nothing after it, and a declaration or
rule holding a legacy unicode-range token is dropped, since tokenizers
disagree on where that token ends.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:sec-fix/tbmtu9f3 (b7d7706) with main (f7c67d8)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 398bcb6 into tox-dev:main Nov 15, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSS sanitizer keeps position and inset declarations that the property allowlist removes

1 participant