fix(anatomy): url() locality skips CSS whitespace only (security audit R3 pass 2)

This commit is contained in:
bchanot committed 2026-10-15 00:40:30 +02:00
1 parent 157c854ed1
commit 23895b9351
6 files changed
+90 -10

No files matched your search

@@ -11,6 +11,7 @@ Migrate the anatomy core to Rust (review R3 scope): SVG analysis, anatomical tre
- `apps/app/tests/unit/canvas.trees.test.ts` is renamed to `.tsx` (it now mounts the canvas): a rename, no test deleted.
- The sanitiser oracle (`oracles/r3/sanitiser.json`) was regenerated as corpus files were added; the TypeScript it runs (`packages/pdf/src`, `packages/anatomy/src`) is unchanged on the branch (criterion 4), so every regeneration is the same TypeScript's verdict. `highlight.json` gained `edgeDocument` only; its cases and sanitised trees are unchanged since d1aa35e (criterion 4).
- Pass 2 (after the security audit, PASS + 1 MEDIUM + 2 LOW): the prescan searches each closer after its whole opener (`<!-->` desync), `href` locality on ASCII-trimmed text only, root `transform`/`filter`/`mask`/`clip-path` removed; 5 corpus files added (41), the wasm corpus has 49 cases; counts below updated.
- Pass 3 (security pass 2 on the fix: PASS + LOW): `url(` skips CSS whitespace only; corpus 42 files, wasm corpus 50 cases.
- Visual screenshots are uncommitted by design (plan O6); their result is recorded in RUST.md §9.3.
## ACCEPTANCE CRITERIA
@@ -40,10 +41,10 @@ Migrate the anatomy core to Rust (review R3 scope): SVG analysis, anatomical tre
EXPECT: gzip under 600000 bytes / exit 0 / app 352, core-wasm 29, pdf 140, anatomy 131, all passed
9. The hostile corpus runs through the shipped wasm under the stated budgets, and the canvas composes within its own.
CHECK: grep -E "R3 hostile corpus|R3 sheet handle" /tmp/r3-verify-check.log
EXPECT: a hostile-corpus line with 49 cases, slowest under 150 ms, largest growth under 32 MiB; a sheet-handle line with a warm head median under 15 ms
EXPECT: a hostile-corpus line with 50 cases, slowest under 150 ms, largest growth under 32 MiB; a sheet-handle line with a warm head median under 15 ms
10. The hostile corpus covers the owner's classes (scripts, events, external and data references, DTD/entities, depth, element count, `<use>` count, cycles and expansion, malformed, namespaces, CSS, size).
CHECK: ls crates/core/tests/fixtures/svg/hostile | wc -l; ls crates/core/tests/fixtures/svg/hostile | tr '\n' ' '
EXPECT: 41 files including comment-desync-depth, comment-desync-attributes, href-unicode-space, root-presentation, and script, events, script-urls, external-use, data-and-images, external-urls, doctype-entities, malformed, namespaces, css, use-self-cycle, use-mutual-cycle, root-class-style, long-title
EXPECT: 42 files including url-unicode-space, comment-desync-depth, comment-desync-attributes, href-unicode-space, root-presentation, and script, events, script-urls, external-use, data-and-images, external-urls, doctype-entities, malformed, namespaces, css, use-self-cycle, use-mutual-cycle, root-class-style, long-title
11. The docs record the phase: RUST.md §9 (proofs, measurements, differences, risks), CHANGELOG, CLAUDE.md map.
CHECK: grep -c "^## 9. R3" docs/development/RUST.md; grep -c "Rust R3, anatomy core" CHANGELOG.md; grep -c "anatomy/{svg/{prescan,tree,sanitize,refs,number,serialize},layer,compose}" CLAUDE.md
EXPECT: 1 / 1 / 1
+11 -3
View File
@@ -5,7 +5,7 @@
//! reference grammar, no text outside `title`/`desc`. A refused element goes with its
//! subtree, a refused attribute alone; both are named in `removed` (names, never values).
use super::number::{is_js_space, js_number_of, js_split_list, js_to_string};
use super::number::{js_number_of, js_split_list, js_to_string};
use super::tree::{Element, Node, SvgRejection, XLINK_NS};
const ALLOWED_ELEMENTS: [&str; 28] = [
@@ -273,6 +273,11 @@ fn value_acceptable(qname: &str, value: &str) -> bool {
}
}
/// CSS whitespace (what the tokenizer skips inside `url(`).
fn is_css_space(c: char) -> bool {
matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0C')
}
/// An `href` as the URL parser reads it: only ASCII whitespace is stripped (the engine
/// trimmed JavaScript spaces, so a no-break space before `#a` passed as local and resolved
/// as a relative URL).
@@ -305,12 +310,15 @@ fn url_openings(value: &str) -> impl Iterator<Item = usize> + '_ {
})
}
/// After `url(`: `\s*`, an optional quote, `\s*`; returns the offset of the next character.
/// After `url(`: whitespace, an optional quote, whitespace; returns the offset of the next
/// character. Only CSS whitespace is skipped, as the browser's CSS tokenizer skips it: the
/// engine skipped JavaScript spaces, so a no-break space before `#a` read as local but the
/// browser resolves it as a relative URL (security audit R3, pass 2).
fn skip_url_prefix(value: &str, at: usize) -> usize {
let rest = &value[at..];
let mut chars = rest.char_indices().peekable();
let skip_space = |chars: &mut std::iter::Peekable<std::str::CharIndices<'_>>| {
while chars.peek().is_some_and(|&(_, c)| is_js_space(c)) {
while chars.peek().is_some_and(|&(_, c)| is_css_space(c)) {
chars.next();
}
};
@@ -0,0 +1,2 @@
<svg xmlns="http://www.w3.org/2000/svg"><filter id="f"/><path d="M0 0h1" fill="url( #f)"/><path d="M0 0h1" style="fill:url( #f)"/><path d="M0 0h1" filter="url( #f)"/><path d="M0 0h1" filter="url(
#f)"/></svg>

After

Width:  |  Height:  |  Size: 211 B

@@ -3960,6 +3960,62 @@
"unbound-prefix.svg": {
"rejected": "malformed"
},
"url-unicode-space.svg": {
"removed": [],
"tree": {
"ns": "http://www.w3.org/2000/svg",
"name": "svg",
"attrs": {
"xmlns": "http://www.w3.org/2000/svg"
},
"children": [
{
"ns": "http://www.w3.org/2000/svg",
"name": "filter",
"attrs": {
"id": "f"
},
"children": []
},
{
"ns": "http://www.w3.org/2000/svg",
"name": "path",
"attrs": {
"d": "M0 0h1",
"fill": "url( #f)"
},
"children": []
},
{
"ns": "http://www.w3.org/2000/svg",
"name": "path",
"attrs": {
"d": "M0 0h1",
"style": "fill:url( #f)"
},
"children": []
},
{
"ns": "http://www.w3.org/2000/svg",
"name": "path",
"attrs": {
"d": "M0 0h1",
"filter": "url( #f)"
},
"children": []
},
{
"ns": "http://www.w3.org/2000/svg",
"name": "path",
"attrs": {
"d": "M0 0h1",
"filter": "url( #f)"
},
"children": []
}
]
}
},
"use-mutual-cycle.svg": {
"removed": [],
"tree": {
+4
View File
@@ -42,6 +42,10 @@ fn declared(file: &str) -> Option<Value> {
// stripped, nothing else); the TS trimmed JavaScript spaces (security audit R3).
"href-unicode-space.svg" => json!({ "removed": ["use@href", "use@xlink:href"],
"strip_unicode_spaced_refs": true }),
// C3 references: `url(` skips CSS whitespace only, as the browser's tokenizer does
// (security audit R3, pass 2): a Unicode space before `#` is not local.
"url-unicode-space.svg" => json!({ "removed": ["path@fill", "path@style"],
"strip": [["path", "fill"], ["path", "style"]] }),
// C3 root: nothing that paints outside the box on the inline root (security audit R3).
"root-presentation.svg" => {
json!({ "removed": ["svg@transform", "svg@filter", "svg@mask", "svg@clip-path"] })
+14 -5
View File
@@ -752,7 +752,7 @@ imports it and restyles `data-state` in place as before.
- O3 (`oracles/r3/highlight.json`): 36 highlighted compositions (every
observation and view of the three reports, an existing nested `<defs>`, a
detail view with its own asset and no viewBox): tree-equal.
- O4/O5 (`oracles/r3/sanitiser.json`): 41 hostile files
- O4/O5 (`oracles/r3/sanitiser.json`): 42 hostile files
(`tests/fixtures/svg/hostile`) and 10 generated cases: equal to the
TypeScript verdict or a declared one (9.4); the 4 product SVGs (horse
drawing, partner logo, 2 public logos) untouched, tree-equal. Mutation
@@ -762,7 +762,11 @@ imports it and restyles `data-state` in place as before.
searched from inside its own opener: `<!-->` let crafted markup hide 5,000
nesting levels or 3,000 attributes, then roxmltree overflowed the stack),
fixed with regression cases in cargo and through the shipped wasm; two LOW
(Unicode-spaced `href`, root painting attributes) fixed the same way.
(Unicode-spaced `href`, root painting attributes) fixed the same way. Pass 2
on the fix: PASS (about 4 million differential documents, prescan against
roxmltree: no undercount, no cap breach, no stack overflow on a 64 KiB stack;
the old desync and its variants answer `too_deep` on the shipped module); its
LOW (the same Unicode-space tolerance inside `url(`) fixed.
- Shipped wasm (`packages/core-wasm/tests/anatomy.test.ts`): the corpus plus
floods (99,000 nesting levels, 20,001 nodes, 150,000 `<` in a comment, 257
attributes, 2 MiB + 1, a 12 × 20 `<use>` chain, 1.9 MiB of `>`), each in a
@@ -792,7 +796,8 @@ imports it and restyles `data-state` in place as before.
canvas box); an `id` outside `[A-Za-z_][\w.:-]*` is removed; an `href` is
local only when it reads `#name` once ASCII whitespace is stripped, as the
URL parser strips it (the TypeScript also trimmed Unicode spaces, so a
no-break space before `#a` passed and resolved as a relative URL).
no-break space before `#a` passed and resolved as a relative URL); inside
`url(` only CSS whitespace is skipped before `#`, for the same reason.
4. A reference cycle, an expansion over 50,000 elements, and the floods of
9.2 step 1 are refused; the TypeScript accepted them (size counted on the
raw text, refused subtrees included).
@@ -833,8 +838,12 @@ imports it and restyles `data-state` in place as before.
- The engine (PDF) still sanitises with the TypeScript allowlist and has no
reference budget: the `<use>` expansion gate of `PDF_IMPORT_HARDENING.md`
§5 is met in the core, closed for the PDF path only when R4 switches it.
- roxmltree's recursion: safe behind the prescan depth cap; pin the release
with the iterative parser when it ships.
- roxmltree's recursion: safe behind the prescan depth cap (differentially
fuzzed, 9.3); pin the release with the iterative parser when it ships.
- References are checked inside one drawing; in the page `#id` resolves against
the whole document (the detail modal imports a second copy of the drawing,
as before R3). Drawings with their own assets (Step 13) should get ids
scoped per view before external uploads are drawn.
- Firefox and WebKit: not run here (no browser build); the owner's LAN run on
the phone covers WebKit.
- The degraded notice inside the canvas (9.4 item 7) and its wording ("le