Skip to content

fix(html): read a style's CSS comments and escapes before judging it - #309

Merged
aRustyDev merged 1 commit into
mainfrom
fix/48-css-hex-escape
Nov 18, 2026
Merged

aRustyDev merged 1 commit into
mainfrom
fix/48-css-hex-escape

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

The sanitiser's style filter did not read a CSS hex escape, so j\61vascript:, \6a avascript: and \65xpression( passed it. #305's report named the gap and left it. This reads a style value the way a stylesheet does before it is judged. It also fixes a second hole of the same kind, found while writing the tests. #48 stays open: the pages half waits for M3.

What changes

  • css_text replaces the two steps that came before the token check (cut out the comments, drop the backslashes). It reads the comments and the escapes in one pass, in the order a stylesheet does (CSS Syntax 3, §4.3.2 and §4.3.7):
    • a comment runs to the first */, or to the end of the value, and /*/ does not close itself;
    • a backslash and one to six hex digits is the code point they name, taken greedily (\61vascript is a and vascript; \76bscript is U+076B and script);
    • a backslash and any other character is that character, never an opener: \/* opens no comment. The old code cut \/* javascript: */ out as a comment, so whatever sat in it was never seen. That hole is the second one.
  • A control character in a style value is removed before the check, as url_ok already does for URLs. An escape can spell one (java\0 script:), so this rule was needed once escapes were read.
  • SCHEMA_VERSION goes from 1 to 2 (ADR-0012), because the policy changed: a description cleaned under 1 may hold a style that 2 drops. Nothing re-cleans those rows, as with the rows from before 0019; description_schema is what a re-clean would select on.
  • Tests (crates/core/html/tests/policy.rs), generated first (AGENTS.md rule 9):
    • prop_a_dangerous_css_token_is_seen_through_any_escape_of_its_characters writes each character of a dangerous token plain, with a backslash, as a short hex escape closed by any whitespace or by nothing, or as six digits, in either case. Its encoder is written from the spec, apart from the decoder.
    • prop_an_escaped_slash_opens_no_comment and prop_a_token_inside_a_comment_is_not_judged (the second is the control of the first: what a real comment holds is kept).
    • prop_an_escaped_harmless_value_is_kept (\5B57\4F53 is a font name, and stays) is the control that none of this passes by dropping every value with a backslash.
    • regression_a_css_hex_escape_cannot_spell_a_dangerous_token pins the gap and its family, and a_css_escape_that_spells_something_else_is_kept pins what the greedy reading makes something else.
  • thrust-core's update model now starts its rows at policy 1 and expects 2 after a description is named. Before, start and expected were both 1, so it could not have told an update that stores the policy from one that forgets to.
  • D-016 and G-46 say what is read now and name the tests.

Checked

Check Result
The tests that cover the gap, on the unchanged code 3 fail (the pinned regression, the generated escape test, and the control-character separator in prop_a_dangerous_style_value_is_dropped); the controls pass. The generated test shrank to expressio\6e (
Mutation testing of the changed lines (--in-diff, cargo-mutants 27.1.0) 18 mutants, 18 caught
Defects planted by hand (what no operator mutant reaches), one at a time a seventh digit, a fifth digit, U+FFFD for a code point that is no character, escapes not read, an unescaped next character that can open a comment, an opener whose star can close it, a backslash kept: each fails the policy tests; the file was restored byte for byte
cargo nextest run --workspace 4700 passed, 10 skipped (the oracle_ tests that the profile excludes); thrust-html has 27 tests
cargo fmt --check; clippy --workspace --all-targets -D warnings, thrust-worker and thrust-edge on wasm32, thrust-api-v1 with openapi; doctests all exit 0
check-deps, check-layout, check-cargo-lock (+ self-test), check-release-plz --first-release required, check-contract-counts (+ self-test), check-migration-lint, check-placement all exit 0
Contract suite, main pass, native-memory 73 of 73 (the two description tests send the nine payloads through create and update)

Not shown, and what to decide

  • The reading was not compared with a CSS parser or run in a browser. The tests' encoder is mine, written from the spec, so it is a second reading of the same text, not an independent oracle (rule 9 (c)). A recorded run against tinycss2 is the way to get one; say if you want it.
  • A code point that is no char is dropped, where a stylesheet reads U+FFFD, and a left-over backslash is still dropped, so java\\script: stays blocked though a stylesheet reads java\script. Both are deliberate: they can only make a token easier to see, never hide one. Blocking more than a browser reads is the safe side of this filter.
  • Rows already stored under policy 1 keep what policy 1 let through; nothing re-cleans them.
  • The CSS check is still a substring test, not a CSS parser, and style="position:fixed..." and id/name clobbering stay allowed, as in ADR-0141.
  • Not run here: the contract suite's native-pg and worker-cf cells, the Postgres adapter tests, and scripts/oracle-v1.sh. No storage code changed, and the only values that change are styles with a backslash, a comment or a control character; CI's cells cover the contract ones.

Refs #48

🤖 Generated with Claude Code

A `style` value was judged after its comments were cut out and its backslashes
dropped, so a CSS hex escape was never read: `j\61vascript:`, `\6a avascript:`
and `\65xpression(` passed the value filter. PR #305's report named the first.
The comment cut came before any escape was read as well, so `\/* javascript: */`
lost its tail to a "comment" that a stylesheet never opens.

`css_text` reads both in one pass, in the order a stylesheet does (CSS Syntax 3,
4.3.2 and 4.3.7): a comment runs to the first `*/` or the end of the value
(`/*/` does not close itself), a backslash and one to six hex digits is the code
point they name, taken greedily, and a backslash and any other character is that
character and never an opener. A code point that is no `char` is dropped and a
control character is removed, as `url_ok` already does: both can only make a
token easier to see. A left-over backslash is still dropped.

SCHEMA_VERSION goes from 1 to 2, because the policy changed (ADR-0012): a
description cleaned under 1 may hold a `style` that 2 drops. Nothing re-cleans
those rows, as with the rows from before migration 0019. The update model of
thrust-core now starts its rows at policy 1 and expects 2 after a description is
named, which it could not tell apart from "kept" while both were 1.

Tests, in thrust-html/tests/policy.rs: a generated test that writes each
character of a dangerous token plain, with a backslash, as a short hex escape
closed by any whitespace or by nothing, or as six digits, in either case (the
encoder is written from the spec, apart from the decoder); the pinned family of
the gap; what the greedy reading makes something else, kept; a token inside a
comment, kept; an escaped slash that opens no comment; and escaped harmless
values, kept, as the control. Before the change the three tests that cover the
gap failed and the controls passed.

Refs: #48
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@aRustyDev
aRustyDev temporarily deployed to preview-control-plane November 18, 2026 18:17 — with GitHub Actions Inactive
@aRustyDev
aRustyDev merged commit cf9669a into main Nov 18, 2026
80 of 83 checks passed
@aRustyDev
aRustyDev deleted the fix/48-css-hex-escape branch November 18, 2026 18:44
aRustyDev added a commit that referenced this pull request Nov 18, 2026
Main moved four commits while #310 was open (#307, #308, #309 and the lettre fix #312), and two files conflicted:

- crates/adapters/postgres/src/lib.rs: #308 made `Migration` borrow its strings (`Migration<'a>`), and this branch added migration 0020 to
  the list. The list is `[Migration<'static>; 20]`: #308's type, this branch's twentieth entry.
- docs/src/testing/status.md: #309 rewrote the G-46 row and this branch added G-47 after it. G-46 is main's row, byte for byte
  (compared with `diff`), and G-47 follows it.

Merged, not rebased, because a force-push is blocked on this repository and #310 is already published.

The lettre bump of #312 comes in with the merge, which is what turns the `security / deny` job green on this branch (it failed on lettre
0.11.23 alone).

Checked on the merged tree before this commit: no conflict markers left, `scripts/check-contract-counts.sh` (74, 6; stage 2: 68, 4),
`cargo fmt --all --check`, `cargo clippy --workspace --all-targets -- -D warnings` and the wasm32 clippy of the Worker crates. The
test batteries are re-run on the merge commit and listed in the pull request.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

This branch was previously deployed

1 inactive deployment
preview-control-plane — e6e690c9 Deployed Nov 18, 2026 by aRustyDev via reconcile #149
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.

1 participant