fix(anatomy): prescan closers searched after their opener; ASCII-only href trim; root painting attributes removed (security audit R3)

This commit is contained in:
bchanot committed 2026-10-15 00:25:43 +02:00
1 parent d3d63394dd
commit 157c854ed1
13 files changed
+3231 -21

No files matched your search

@@ -10,6 +10,7 @@ Migrate the anatomy core to Rust (review R3 scope): SVG analysis, anatomical tre
## CLARIFICATIONS ## CLARIFICATIONS
- `apps/app/tests/unit/canvas.trees.test.ts` is renamed to `.tsx` (it now mounts the canvas): a rename, no test deleted. - `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). - 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.
- Visual screenshots are uncommitted by design (plan O6); their result is recorded in RUST.md §9.3. - Visual screenshots are uncommitted by design (plan O6); their result is recorded in RUST.md §9.3.
## ACCEPTANCE CRITERIA ## ACCEPTANCE CRITERIA
@@ -39,10 +40,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 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. 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 CHECK: grep -E "R3 hostile corpus|R3 sheet handle" /tmp/r3-verify-check.log
EXPECT: a hostile-corpus line with 44 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 49 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). 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' ' ' CHECK: ls crates/core/tests/fixtures/svg/hostile | wc -l; ls crates/core/tests/fixtures/svg/hostile | tr '\n' ' '
EXPECT: 36 files including 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: 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
11. The docs record the phase: RUST.md §9 (proofs, measurements, differences, risks), CHANGELOG, CLAUDE.md map. 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 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 EXPECT: 1 / 1 / 1
+35 -6
View File
@@ -122,20 +122,22 @@ impl<'a> Scan<'a> {
/// Consumes the markup starting at `self.at` (a `<`). /// Consumes the markup starting at `self.at` (a `<`).
fn markup(&mut self, caps: &SvgCaps) -> Result<(), PrescanError> { fn markup(&mut self, caps: &SvgCaps) -> Result<(), PrescanError> {
let rest = &self.bytes[self.at..]; let rest = &self.bytes[self.at..];
// Each closer is searched after its whole opener, as the XML grammar reads it:
// `<!-->` does not close the comment it opens (security audit R3, MEDIUM).
if rest.starts_with(b"<!--") { if rest.starts_with(b"<!--") {
return self.skip_past(b"-->"); return self.skip_past(4, b"-->");
} }
if rest.starts_with(b"<![CDATA[") { if rest.starts_with(b"<![CDATA[") {
return self.skip_past(b"]]>"); return self.skip_past(9, b"]]>");
} }
if rest.starts_with(b"<?") { if rest.starts_with(b"<?") {
return self.skip_past(b"?>"); return self.skip_past(2, b"?>");
} }
if rest.starts_with(b"<!") { if rest.starts_with(b"<!") {
return Err(PrescanError::Malformed); return Err(PrescanError::Malformed);
} }
if rest.starts_with(b"</") { if rest.starts_with(b"</") {
self.skip_past(b">")?; self.skip_past(2, b">")?;
self.depth = self.depth.checked_sub(1).ok_or(PrescanError::Malformed)?; self.depth = self.depth.checked_sub(1).ok_or(PrescanError::Malformed)?;
return Ok(()); return Ok(());
} }
@@ -172,8 +174,9 @@ impl<'a> Scan<'a> {
Ok(()) Ok(())
} }
fn skip_past(&mut self, end: &[u8]) -> Result<(), PrescanError> { /// Moves past `end`, searched from `opener` bytes after the markup's `<`.
let found = find(self.bytes, end, self.at + 1).ok_or(PrescanError::Malformed)?; fn skip_past(&mut self, opener: usize, end: &[u8]) -> Result<(), PrescanError> {
let found = find(self.bytes, end, self.at + opener).ok_or(PrescanError::Malformed)?;
self.at = found + end.len(); self.at = found + end.len();
Ok(()) Ok(())
} }
@@ -327,6 +330,32 @@ mod tests {
assert_eq!(prescan(&bom, &SVG_CAPS), Err(PrescanError::Malformed)); assert_eq!(prescan(&bom, &SVG_CAPS), Err(PrescanError::Malformed));
} }
#[test]
fn a_closer_overlapping_its_opener_does_not_close_it() {
// `<!-->` keeps the comment open until the next `-->`: what follows is still counted.
let deep = format!(
"{SVG}<!--></svg><![CDATA[-->{}{}</svg><!--]]>-->",
"<g>".repeat(100),
"</g>".repeat(100)
);
assert_eq!(prescan(&deep, &SVG_CAPS), Err(PrescanError::TooDeep));
assert_eq!(
prescan(&format!("{SVG}<!-->"), &SVG_CAPS),
Err(PrescanError::Malformed)
);
assert_eq!(
prescan(&format!("{SVG}<?></svg>"), &SVG_CAPS),
Err(PrescanError::Malformed)
);
assert_eq!(
prescan(&format!("{SVG}<![CDATA[]]></svg>"), &SVG_CAPS),
Ok(Prescan {
elements: 1,
depth: 1
})
);
}
#[test] #[test]
fn refuses_doctype_and_entity_anywhere_and_unclosed_markup() { fn refuses_doctype_and_entity_anywhere_and_unclosed_markup() {
for text in [ for text in [
+2 -2
View File
@@ -9,7 +9,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use super::sanitize::url_targets; use super::sanitize::{local_reference_text, url_targets};
use super::tree::{Element, Node, SvgRejection}; use super::tree::{Element, Node, SvgRejection};
/// Expanded elements a drawing may instantiate (the product's drawing: a few thousand). /// Expanded elements a drawing may instantiate (the product's drawing: a few thousand).
@@ -85,7 +85,7 @@ fn reference_ids(element: &Element) -> Vec<String> {
let mut ids = Vec::new(); let mut ids = Vec::new();
for attr in &element.attrs { for attr in &element.attrs {
if attr.qname == "href" || attr.qname == "xlink:href" { if attr.qname == "href" || attr.qname == "xlink:href" {
if let Some(id) = attr.value.trim().strip_prefix('#') { if let Some(id) = local_reference_text(&attr.value).strip_prefix('#') {
ids.push(id.to_owned()); ids.push(id.to_owned());
} }
continue; continue;
+11 -2
View File
@@ -176,7 +176,9 @@ pub fn sanitize(mut root: Element) -> Result<Sanitized, SvgRejection> {
removed: Vec::new(), removed: Vec::new(),
uses: 0, uses: 0,
}; };
for name in ["class", "style"] { // The inline root lives under the page: no page classes or styles, nothing that paints
// outside its box (security audit R3, LOW).
for name in ["class", "style", "transform", "filter", "mask", "clip-path"] {
if root.attr(name).is_some() { if root.attr(name).is_some() {
root.remove_attr(name); root.remove_attr(name);
walk.removed.push(format!("svg@{name}")); walk.removed.push(format!("svg@{name}"));
@@ -263,7 +265,7 @@ fn value_acceptable(qname: &str, value: &str) -> bool {
return false; return false;
} }
match qname { match qname {
"href" | "xlink:href" => is_local_reference(value.trim_matches(is_js_space)), "href" | "xlink:href" => is_local_reference(local_reference_text(value)),
"id" => is_reference_name(value), "id" => is_reference_name(value),
"style" => !style_forbidden(value) && calls_only_local_url(value), "style" => !style_forbidden(value) && calls_only_local_url(value),
_ if PRESENTATION_ATTRIBUTES.contains(&qname) => calls_only_local_url(value), _ if PRESENTATION_ATTRIBUTES.contains(&qname) => calls_only_local_url(value),
@@ -271,6 +273,13 @@ fn value_acceptable(qname: &str, value: &str) -> bool {
} }
} }
/// 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).
pub fn local_reference_text(value: &str) -> &str {
value.trim_matches(|c: char| matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0C'))
}
/// `^#[A-Za-z_][\w.:-]*$`. /// `^#[A-Za-z_][\w.:-]*$`.
fn is_local_reference(value: &str) -> bool { fn is_local_reference(value: &str) -> bool {
value.strip_prefix('#').is_some_and(is_reference_name) value.strip_prefix('#').is_some_and(is_reference_name)
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><g><![CDATA[]]><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></g></svg>

After

Width:  |  Height:  |  Size: 555 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><!--></svg><![CDATA[--><path a0="0" a1="1" a2="2" a3="3" a4="4" a5="5" a6="6" a7="7" a8="8" a9="9" a10="10" a11="11" a12="12" a13="13" a14="14" a15="15" a16="16" a17="17" a18="18" a19="19" a20="20" a21="21" a22="22" a23="23" a24="24" a25="25" a26="26" a27="27" a28="28" a29="29" a30="30" a31="31" a32="32" a33="33" a34="34" a35="35" a36="36" a37="37" a38="38" a39="39" a40="40" a41="41" a42="42" a43="43" a44="44" a45="45" a46="46" a47="47" a48="48" a49="49" a50="50" a51="51" a52="52" a53="53" a54="54" a55="55" a56="56" a57="57" a58="58" a59="59" a60="60" a61="61" a62="62" a63="63" a64="64" a65="65" a66="66" a67="67" a68="68" a69="69" a70="70" a71="71" a72="72" a73="73" a74="74" a75="75" a76="76" a77="77" a78="78" a79="79" a80="80" a81="81" a82="82" a83="83" a84="84" a85="85" a86="86" a87="87" a88="88" a89="89" a90="90" a91="91" a92="92" a93="93" a94="94" a95="95" a96="96" a97="97" a98="98" a99="99" a100="100" a101="101" a102="102" a103="103" a104="104" a105="105" a106="106" a107="107" a108="108" a109="109" a110="110" a111="111" a112="112" a113="113" a114="114" a115="115" a116="116" a117="117" a118="118" a119="119" a120="120" a121="121" a122="122" a123="123" a124="124" a125="125" a126="126" a127="127" a128="128" a129="129" a130="130" a131="131" a132="132" a133="133" a134="134" a135="135" a136="136" a137="137" a138="138" a139="139" a140="140" a141="141" a142="142" a143="143" a144="144" a145="145" a146="146" a147="147" a148="148" a149="149" a150="150" a151="151" a152="152" a153="153" a154="154" a155="155" a156="156" a157="157" a158="158" a159="159" a160="160" a161="161" a162="162" a163="163" a164="164" a165="165" a166="166" a167="167" a168="168" a169="169" a170="170" a171="171" a172="172" a173="173" a174="174" a175="175" a176="176" a177="177" a178="178" a179="179" a180="180" a181="181" a182="182" a183="183" a184="184" a185="185" a186="186" a187="187" a188="188" a189="189" a190="190" a191="191" a192="192" a193="193" a194="194" a195="195" a196="196" a197="197" a198="198" a199="199" a200="200" a201="201" a202="202" a203="203" a204="204" a205="205" a206="206" a207="207" a208="208" a209="209" a210="210" a211="211" a212="212" a213="213" a214="214" a215="215" a216="216" a217="217" a218="218" a219="219" a220="220" a221="221" a222="222" a223="223" a224="224" a225="225" a226="226" a227="227" a228="228" a229="229" a230="230" a231="231" a232="232" a233="233" a234="234" a235="235" a236="236" a237="237" a238="238" a239="239" a240="240" a241="241" a242="242" a243="243" a244="244" a245="245" a246="246" a247="247" a248="248" a249="249" a250="250" a251="251" a252="252" a253="253" a254="254" a255="255" a256="256" a257="257" a258="258" a259="259" a260="260" a261="261" a262="262" a263="263" a264="264" a265="265" a266="266" a267="267" a268="268" a269="269" a270="270" a271="271" a272="272" a273="273" a274="274" a275="275" a276="276" a277="277" a278="278" a279="279" a280="280" a281="281" a282="282" a283="283" a284="284" a285="285" a286="286" a287="287" a288="288" a289="289" a290="290" a291="291" a292="292" a293="293" a294="294" a295="295" a296="296" a297="297" a298="298" a299="299" a300="300" a301="301" a302="302" a303="303" a304="304" a305="305" a306="306" a307="307" a308="308" a309="309" a310="310" a311="311" a312="312" a313="313" a314="314" a315="315" a316="316" a317="317" a318="318" a319="319" a320="320" a321="321" a322="322" a323="323" a324="324" a325="325" a326="326" a327="327" a328="328" a329="329" a330="330" a331="331" a332="332" a333="333" a334="334" a335="335" a336="336" a337="337" a338="338" a339="339" a340="340" a341="341" a342="342" a343="343" a344="344" a345="345" a346="346" a347="347" a348="348" a349="349" a350="350" a351="351" a352="352" a353="353" a354="354" a355="355" a356="356" a357="357" a358="358" a359="359" a360="360" a361="361" a362="362" a363="363" a364="364" a365="365" a366="366" a367="367" a368="368" a369="369" a370="370" a371="371" a372="372" a373="373" a374="374" a375="375" a376="376" a377="377" a378="378" a379="379" a380="380" a381="381" a382="382" a383="383" a384="384" a385="385" a386="386" a387="387" a388="388" a389="389" a390="390" a391="391" a392="392" a393="393" a394="394" a395="395" a396="396" a397="397" a398="398" a399="399" a400="400" a401="401" a402="402" a403="403" a404="404" a405="405" a406="406" a407="407" a408="408" a409="409" a410="410" a411="411" a412="412" a413="413" a414="414" a415="415" a416="416" a417="417" a418="418" a419="419" a420="420" a421="421" a422="422" a423="423" a424="424" a425="425" a426="426" a427="427" a428="428" a429="429" a430="430" a431="431" a432="432" a433="433" a434="434" a435="435" a436="436" a437="437" a438="438" a439="439" a440="440" a441="441" a442="442" a443="443" a444="444" a445="445" a446="446" a447="447" a448="448" a449="449" a450="450" a451="451" a452="452" a453="453" a454="454" a455="455" a456="456" a457="457" a458="458" a459="459" a460="460" a461="461" a462="462" a463="463" a464="464" a465="465" a466="466" a467="467" a4Line truncated

After

Width:  |  Height:  |  Size: 36 KiB

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><!--></svg><![CDATA[--><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><g><Line truncated

After

Width:  |  Height:  |  Size: 34 KiB

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path id="a" d="M0 0h1"/><use href=" #a"/><use xlink:href="#a"/><use href=" #a "/></svg>

After

Width:  |  Height:  |  Size: 175 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" transform="scale(40)" filter="url(#f)" mask="url(#m)" clip-path="url(#c)"><filter id="f"/><mask id="m"/><clipPath id="c"/><path d="M0 0h1"/></svg>

After

Width:  |  Height:  |  Size: 186 B

File diff suppressed because it is too large. Load diff
+34 -3
View File
@@ -8,7 +8,8 @@
//! children with text; the Rust tree is the reader's output serialised and read again. The //! children with text; the Rust tree is the reader's output serialised and read again. The
//! declared rules that change a tree (comments and PIs dropped, text only in `title`/`desc`, //! declared rules that change a tree (comments and PIs dropped, text only in `title`/`desc`,
//! elements outside the SVG namespace removed, ids outside the reference grammar removed, no //! elements outside the SVG namespace removed, ids outside the reference grammar removed, no
//! `class`/`style` on the root) are applied to the TS tree before comparing. //! `class`/`style`/`transform`/`filter`/`mask`/`clip-path` on the root) are applied to the TS
//! tree before comparing.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] #![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common; mod common;
@@ -37,6 +38,16 @@ fn declared(file: &str) -> Option<Value> {
// C3 namespace: `xlink:href` whose prefix is bound to another URI is removed. // C3 namespace: `xlink:href` whose prefix is bound to another URI is removed.
"xlink-prefix-rebound.svg" => json!({ "removed": ["linearGradient@xlink:href"], "xlink-prefix-rebound.svg" => json!({ "removed": ["linearGradient@xlink:href"],
"strip": [["linearGradient", "xlink:href"]] }), "strip": [["linearGradient", "xlink:href"]] }),
// C3 references: an href is local only as the URL parser reads it (ASCII whitespace
// 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 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"] })
}
// C2: the prescan's per-element attribute cap (TS removes the 3,000 data-like names).
"comment-desync-attributes.svg" => json!({ "rejected": "too_complex" }),
// C3 root: no class or style on the inline root. // C3 root: no class or style on the inline root.
"root-class-style.svg" => json!({ "removed": ["svg@class", "svg@style"] }), "root-class-style.svg" => json!({ "removed": ["svg@class", "svg@style"] }),
_ => return None, _ => return None,
@@ -67,6 +78,22 @@ fn strip(tree: &mut Value, element: &str, attribute: &str) {
} }
} }
/// Removes `href`/`xlink:href` values that start with a non-ASCII character (a declared removal).
fn strip_unicode_spaced_refs(tree: &mut Value) {
if let Some(attrs) = tree["attrs"].as_object_mut() {
attrs.retain(|name, value| {
let spaced = value
.as_str()
.and_then(|v| v.chars().next())
.is_some_and(|c| !c.is_ascii());
!((name == "href" || name == "xlink:href") && spaced)
});
}
if let Some(children) = tree["children"].as_array_mut() {
children.iter_mut().for_each(strip_unicode_spaced_refs);
}
}
fn is_space(text: &str) -> bool { fn is_space(text: &str) -> bool {
text.chars().all(|c| matches!(c, ' ' | '\t' | '\n' | '\r')) text.chars().all(|c| matches!(c, ' ' | '\t' | '\n' | '\r'))
} }
@@ -92,8 +119,9 @@ fn declared_tree(node: &Value, keeps_text: bool, root: bool) -> Option<Value> {
attrs.remove("id"); attrs.remove("id");
} }
if root { if root {
attrs.remove("class"); for name in ["class", "style", "transform", "filter", "mask", "clip-path"] {
attrs.remove("style"); attrs.remove(name);
}
} }
let text_inside = matches!(name, "title" | "desc"); let text_inside = matches!(name, "title" | "desc");
let children: Vec<Value> = node["children"] let children: Vec<Value> = node["children"]
@@ -127,6 +155,9 @@ fn every_hostile_case_gives_the_typescript_verdict_or_a_declared_one() {
Some(expected) => { Some(expected) => {
assert_eq!(rust["removed"], expected["removed"], "{file}"); assert_eq!(rust["removed"], expected["removed"], "{file}");
let mut tree = declared_tree(&ts["tree"], false, true).unwrap(); let mut tree = declared_tree(&ts["tree"], false, true).unwrap();
if expected["strip_unicode_spaced_refs"] == true {
strip_unicode_spaced_refs(&mut tree);
}
for pair in expected["strip"].as_array().into_iter().flatten() { for pair in expected["strip"].as_array().into_iter().flatten() {
strip( strip(
&mut tree, &mut tree,
+16 -6
View File
@@ -713,7 +713,8 @@ imports it and restyles `data-state` in place as before.
### 9.2 The SVG reader (`read_svg`) ### 9.2 The SVG reader (`read_svg`)
1. Bounds before any parser (`prescan.rs`, a markup state machine: comments, 1. Bounds before any parser (`prescan.rs`, a markup state machine: comments,
CDATA, PIs and quoted values skipped by the grammar, no regex): 2 MiB, a CDATA, PIs and quoted values skipped by the grammar, each closer searched
after its whole opener so `<!-->` cannot hide markup, no regex): 2 MiB, a
leading BOM refused, `<!DOCTYPE`/`<!ENTITY` anywhere, depth 64, 20,000 leading BOM refused, `<!DOCTYPE`/`<!ENTITY` anywhere, depth 64, 20,000
elements, 256 attributes per element and 100,000 in all, 16 namespace elements, 256 attributes per element and 100,000 in all, 16 namespace
declarations, 100,000 raw `<` and 200,000 raw `=`. Reason: roxmltree 0.21.1 declarations, 100,000 raw `<` and 200,000 raw `=`. Reason: roxmltree 0.21.1
@@ -751,16 +752,21 @@ imports it and restyles `data-state` in place as before.
- O3 (`oracles/r3/highlight.json`): 36 highlighted compositions (every - O3 (`oracles/r3/highlight.json`): 36 highlighted compositions (every
observation and view of the three reports, an existing nested `<defs>`, a observation and view of the three reports, an existing nested `<defs>`, a
detail view with its own asset and no viewBox): tree-equal. detail view with its own asset and no viewBox): tree-equal.
- O4/O5 (`oracles/r3/sanitiser.json`): 36 hostile files - O4/O5 (`oracles/r3/sanitiser.json`): 41 hostile files
(`tests/fixtures/svg/hostile`) and 10 generated cases: equal to the (`tests/fixtures/svg/hostile`) and 10 generated cases: equal to the
TypeScript verdict or a declared one (9.4); the 4 product SVGs (horse TypeScript verdict or a declared one (9.4); the 4 product SVGs (horse
drawing, partner logo, 2 public logos) untouched, tree-equal. Mutation drawing, partner logo, 2 public logos) untouched, tree-equal. Mutation
checks run on the oracle tests (a cap, the namespace rule, the twin floor, checks run on the oracle tests (a cap, the namespace rule, the twin floor,
the halo placement): each mutant failed the suite. the halo placement): each mutant failed the suite.
- Security audit pass 1: PASS with one MEDIUM in the prescan (a closer
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.
- Shipped wasm (`packages/core-wasm/tests/anatomy.test.ts`): the corpus plus - 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 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 attributes, 2 MiB + 1, a 12 × 20 `<use>` chain, 1.9 MiB of `>`), each in a
fresh instance: a closed outcome, no trap, slowest 24.9 ms, largest linear fresh instance (49 cases): a closed outcome, no trap, slowest 24.2 ms, largest linear
memory growth 10.1 MiB. memory growth 10.1 MiB.
- Runtime: the `SheetHandle` constructor's closed refusal (`<kind>: <detail>` - Runtime: the `SheetHandle` constructor's closed refusal (`<kind>: <detail>`
from a plain `Error`) is `invalid_sheet`; any other throw (RangeError, from a plain `Error`) is `invalid_sheet`; any other throw (RangeError,
@@ -781,8 +787,12 @@ imports it and restyles `data-state` in place as before.
2. Comments and processing instructions are dropped; non-whitespace text is 2. Comments and processing instructions are dropped; non-whitespace text is
kept only in `title`/`desc`, each at most 4 KiB (longer: refused whole). kept only in `title`/`desc`, each at most 4 KiB (longer: refused whole).
3. The root loses `class` and `style` (an inline SVG lives under the page's 3. The root loses `class` and `style` (an inline SVG lives under the page's
CSS: `class="fixed inset-0 …"` would cover the application); an `id` CSS: `class="fixed inset-0 …"` would cover the application) and
outside `[A-Za-z_][\w.:-]*` is removed. `transform`, `filter`, `mask`, `clip-path` (they can paint outside the
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).
4. A reference cycle, an expansion over 50,000 elements, and the floods of 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 9.2 step 1 are refused; the TypeScript accepted them (size counted on the
raw text, refused subtrees included). raw text, refused subtrees included).
@@ -799,7 +809,7 @@ imports it and restyles `data-state` in place as before.
| Measure | Value | | Measure | Value |
|---|---| |---|---|
| shipped module | 1,188,740 B raw, **395,771 B gzip -9** (+55.8 KB over R2; budget 600 KB) | | shipped module | 1,188,573 B raw, **395,701 B gzip -9** (+55.8 KB over R2; budget 600 KB) |
| sheet handle with the drawing read (Node) | 8 to 10 ms | | sheet handle with the drawing read (Node) | 8 to 10 ms |
| `view.canvas` main, first (Node) | 1.2 to 1.4 ms; head warm median 0.85 ms; 143 KiB of SVG | | `view.canvas` main, first (Node) | 1.2 to 1.4 ms; head warm median 0.85 ms; 143 KiB of SVG |
| Chromium, preview build, CPU × 4, medians of 3 | editor to canvas 503 ms (476 ms before R3); detail open 143 ms (125 ms before R3, runs 109 to 157); browser parse + import of the canvas text 21 ms (22 ms) | | Chromium, preview build, CPU × 4, medians of 3 | editor to canvas 503 ms (476 ms before R3); detail open 143 ms (125 ms before R3, runs 109 to 157); browser parse + import of the canvas text 21 ms (22 ms) |
+3
View File
@@ -119,6 +119,9 @@ describe('R3 anatomy on the shipped module', () => {
) )
expect(kinds['deep 99,000 levels']).toBe('svg_rejected:too_deep') expect(kinds['deep 99,000 levels']).toBe('svg_rejected:too_deep')
expect(kinds['use chain 12 x 20']).toBe('svg_rejected:too_complex') expect(kinds['use chain 12 x 20']).toBe('svg_rejected:too_complex')
// A closer overlapping its opener (`<!-->`) must not hide nesting from the prescan.
expect(kinds['comment-desync-depth.svg']).toBe('svg_rejected:too_deep')
expect(kinds['comment-desync-attributes.svg']).toBe('svg_rejected:too_complex')
}) })
it('composes the product drawing within budget', async () => { it('composes the product drawing within budget', async () => {