Skip to content

feat(html): make the hardened sanitiser the default, keeping the editor's embed and rel - #302

Merged
aRustyDev merged 1 commit into
mainfrom
feat/48-hardened-sanitiser
Nov 18, 2026
Merged

aRustyDev merged 1 commit into
mainfrom
feat/48-hardened-sanitiser

Conversation

@aRustyDev

@aRustyDev aRustyDev commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Part 2 of 3 for #48 (the work-item half). It follows #300, which moved the sanitiser into crates/core/html and is merged. This one changes what the sanitiser does: the default policy is now the hardened one, and it keeps what the editor writes. Part 3 stores description_stripped and a schema version through the use-cases.

What changes

  • thrust_builder() is hardened_builder() plus what Plane's allow-list strips from the editor's own output: issue-embed-component with its four attributes (id is already generic; D-017), and a rel as the editor writes it (D-018). ammonia refuses to run with both rel allowed and its own link_rel, so link_rel(None) goes with it.
  • It is the default. Sanitizer::default() and validate_html_content use it, once built (LazyLock), so /api/v1 and Plane's internal routes both do. plane_builder() stays as exact nh3 parity, and the new Sanitizer::new lets a comparison with nh3 ask for it: the nh3 oracle test, the sanitize_check example and the ammonia-4.0.0 probe now do.
  • SCHEMA_VERSION = 1, which part 3 stores with a description.
  • Compat parity. X01 records the pin adding rel="noopener noreferrer" to a link; Thrust now leaves a link as it was sent. A D-018 projection in tests/common/mod.rs removes exactly that string from <a> tags of recorded responses, refuses a recording that sends a rel of its own, and has three controls. The fixture is not edited.
  • Docs. DEVIATIONS.md D-016 to D-019 name the code that is there now and the tests that pin each (the register's header asks for one); status.md G-46.

For you to decide or know

  1. A rel with any token but noopener, noreferrer and nofollow is dropped. D-018 as written says "preserved as the editor writes it". Preserving any rel would let a stored rel="opener" give a target="_blank" link back the window.opener that browsers take from it by default. The editor never writes such a value (0 of 196 documents change), so this costs nothing, and it is one function (rel_ok) and one line in D-018. It is mine, not yours: say so if you would rather have the literal reading.
  2. The corpus has 196 editor-generated documents, not 195. The issue and research/0008 §7 say 195, with 129 unchanged under Plane's list. The corpus is byte-identical to the one the spike measured and the selection rule is unchanged. The 66 documents that Plane's list changes, and the two causes (5 embeds, 61 rels), agree exactly; only the total is one short, so 130 are unchanged. The tests assert the corpus's own figures and D-016 to D-018 say 196. I did not chase which document the research left out.
  3. A CSS hex escape passes the style filter: j\61vascript: and \6a avascript: are kept (a plain backslash, java\script:, is caught). The comment in css_is_dangerous claimed j\61vascript: was handled; it now says what the code does. This is the class of the nine payloads (legacy browsers, [inferred] not exploitable in evergreen ones), and D-016's claim is the corpus's 125 of 125, so I did not widen this PR. Decoding the escapes would be about twenty lines and a generated test. Say if you want it.

Checked

Check Result
The nine payloads of research/0008 §7, through create and update, on compat and on /api/v1 (contract suite, native-memory, the main pass, which holds them) all read back neutralised; main pass 73 of 73 (was 71), the two new tests among them
Control: the same tests with the default pointed back at plane_builder() exactly the two new contract tests fail; the other 71 pass
Control, same mutation, hermetic 6 of the 11 then-existing thrust-html tests fail, and 2 of the 4 sanitiser tests of thrust-editor-doc (the 196-document test fails with "changes 66 of 196" and the generated test fails too)
Control: X01 with the D-018 projection switched off diverges on description_html: recorded <a rel="noopener noreferrer">l</a>, answered <a>l</a>
The default over the editor corpus changes none of the 196; plane_builder() changes 66 (5 + 61); the hardened builder changes none
cargo nextest run --workspace 4679 passed, 10 skipped (the oracle_ tests the profile excludes)
Mutation testing, crates/core/html/src/lib.rs (cargo-mutants 27.1.0) 127 mutants; 84 missed on the first run, 77 after I added the size-limit boundaries and a whitespace-in-scheme test; every mutant of the policy code is caught (49 caught, 1 timeout); all 77 left are in the strip_tags port, which no hermetic test exercises (even strip_tags -> String::new() survives). Part 3 puts it to use and tests it against recorded Django output
oracle-v1.sh native-memory controls VERDICT: PASS, gate, corpus comparison, self-test and controls all 0
cargo clippy -D warnings --locked: workspace, thrust-worker and thrust-edge on wasm32, thrust-api-v1 with openapi all exit 0
cargo fmt --check, cargo deny --locked check, doctests clean
check-deps, check-layout (+ self-test), check-cargo-lock (+ self-test), check-release-plz --first-release required, check-plane-500, check-contract-counts (+ self-test), check-migration-lint, run again on the tree rebased onto main all exit 0

Not shown

  • The native-pg and worker-cf cells of the contract suite, and the browser journeys, are CI's; nothing here touches Postgres or the Worker's bindings.
  • oracle_v1_write_with_sanitiser_matches_python_plus_js, now pinned to plane_builder() because nh3 is its oracle, was not re-run: it needs the generated spike-0006 output.
  • The nine payloads were not run in a browser, and the CSS check is a substring test over a normalised value, not a CSS parser; style="position:fixed…" and id/name clobbering stay allowed, as in ADR-0141.
  • The spike Worker (crates/spikes/editor-worker) now measures Thrust's policy on /html-to-all and /deep; its CPU numbers in research/0008 §9 were Plane's. A comment says so; nothing re-measured.

Refs #48

🤖 Generated with Claude Code

…or's embed and rel

Work-item descriptions are cleaned with Thrust's own allow-list now (issue #48; D-016 to D-019), not Plane's.
`thrust_builder()` is `hardened_builder()` plus what Plane's list would strip from the editor's own output:

- `issue-embed-component` and its four attributes (D-017; `id` is already generic);
- `a rel` as the editor writes it (D-018), which means turning off ammonia's own `link_rel` (it refuses to
  run with both). A `rel` with any token but noopener, noreferrer and nofollow is dropped, so a stored
  `rel="opener"` cannot give a `target="_blank"` link back its `window.opener` (added here; recorded in D-018).

`Sanitizer::default()` and `validate_html_content` use it, so `/api/v1` and Plane's internal routes both do.
`plane_builder()` stays, exact nh3 parity, and `Sanitizer::new` lets a comparison with nh3 ask for it: the
nh3 oracle test, the sanitize_check example and the ammonia-4.0.0 probe now do. `SCHEMA_VERSION` (1) is
added for the stored description to name its policy (used by the next change).

Tests (a static example is used only where the research names it, rule 9 (b)):

- thrust-html: the nine payloads that Plane's list lets through, read by id from the committed XSS corpus,
  are neutralised by the default and not by plane_builder(); generated tests of the value rules with a
  harmless-value control; the embed and link markup kept; the size limits to the byte.
- thrust-editor-doc: the default changes none of the 196 editor-generated documents; plane_builder() changes
  66 for the two named reasons (5 embeds, 61 rels); the hardened builder changes none; a generated test over
  rendered documents. The research and the issue say 195 and 129: the corpus is byte-identical to the
  spike's and has 196 and 130, so the tests assert the corpus's own and DEVIATIONS says so.
- contract suite: two tests send the nine through create and update on compat and on /api/v1 and read each
  back neutralised (the main pass is 73, stage2's 67).
- compat parity: X01 records the pin adding rel="noopener noreferrer"; a D-018 projection removes exactly
  that, refuses a recording that sends a rel of its own, and has three controls.

Controls shown failing, with the default pointed back at plane_builder(): 6 of 11 thrust-html tests, 2 of 4
thrust-editor-doc tests, exactly the two new contract tests (71 others pass), and X01 with the projection off.
Mutation testing of the sanitiser: every mutant of the policy code is caught; the 77 missed are all in the
strip_tags port, which the next change wires in and tests against recorded Django output.

Measured and not fixed here: a CSS hex escape (`j\61vascript:`) passes the style filter. The comment that
claimed otherwise now says so; it is in G-46.

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

This branch was previously deployed

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