Repository navigation
Description
What
Check how the sanitiser reads a CSS value against an independent CSS tokenizer, and record the answers as a parity_ fixture, as was done for strip_tags (tools/strip-tags/record.py, crates/core/html/tests/strip_tags.rs).
Why
#309 made css_is_dangerous (crates/core/html/src/lib.rs) read a style value's comments and escapes through css_text before it looks for a dangerous token, following CSS Syntax 3 §4.3.2 and §4.3.7. Its tests (crates/core/html/tests/policy.rs) generate values with an encoder (Spell, spelled) that was written from the same reading of the same spec as the decoder. A misreading that both share passes every test. Nothing has compared the reading with a CSS parser or run it in a browser; #309's description and G-46 ("Not shown" in docs/src/testing/status.md) say so.
AGENTS.md testing rule 9 (c) allows a fixture that was recorded from an independent oracle. This is that fixture.
The oracle
tinycss2 is a pure-Python CSS parser that follows CSS Syntax Level 3. Run it under uv (uv run --python 3.12 --no-project --with tinycss2==<pinned> python -I ...), and write the Python version and the tinycss2 version into the fixture's header, as the strip_tags fixture does for Python.
What to compare
css_text is private and deliberately stricter than a tokenizer, so an exact match of its output is the wrong test:
- a code point that is no
char(zero, a surrogate, past U+10FFFF) is dropped, where CSS reads U+FFFD; - a left-over backslash (
\\), whitespace and control characters are removed before the token search.
The property that matters is one-sided: whenever the oracle's reading of a value contains a dangerous token (javascript:, expression(, url(data:, vbscript:, behavior:, -moz-binding, @import), the filter drops the style. Blocking more than the oracle reads is allowed, but each such case should be named and asserted as an exact list, like KNOWN_GAPS in strip_tags.rs, so a change in either direction is seen. Test through validate_html_content; do not widen the public API for it.
Record, from tinycss2.parse_component_value_list, the text of the ident, function, at-keyword, hash, string and url tokens (an unquoted url(...) is one token with its own escape rules), for:
- the generated spellings of the dangerous tokens used by
policy.rs(any hex width, either case, any closing whitespace or none); - hand-written edge cases: a greedy six-digit number followed by a seventh hex digit;
\at the end of the value;\before a newline, inside a string and outside;\\;\/*;/*/; an unterminated comment; NUL, a surrogate and a code point above U+10FFFF as escapes.
Done when
- the fixture is committed with its header, and a
parity_test replays it; - a control fails: a changed recording, and a reader that does not read escapes (rule 8);
- the "not compared with a CSS parser" sentence in G-46 is replaced by what was compared and the exact list of differences, and D-016 names the test;
- if the oracle disagrees with
css_textanywhere that makes a dangerous token invisible, that is a bug incss_textand a finding, not a fixture to adjust (rule 3).
An optional second oracle is a real browser (the computed style of an element whose style holds the value). It is not needed for this issue.
Not urgent: the filter is designed to block at least what a stylesheet reads, and its tests show that against the encoder's reading of the spec. This issue is what would show it against someone else's.
Follows #48 (the work-item half: #300, #302, #305, #309).
🤖 Generated with Claude Code
Activity