fix(anatomy): prescan closers searched after their opener; ASCII-only href trim; root painting attributes removed (security audit R3)
This commit is contained in:
1 parent
d3d63394dd
commit
157c854ed1
13 files changed
+3231
-21
No files matched your search
@@ -8,7 +8,8 @@
|
||||
//! 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`,
|
||||
//! 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)]
|
||||
|
||||
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.
|
||||
"xlink-prefix-rebound.svg" => json!({ "removed": ["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.
|
||||
"root-class-style.svg" => json!({ "removed": ["svg@class", "svg@style"] }),
|
||||
_ => 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 {
|
||||
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");
|
||||
}
|
||||
if root {
|
||||
attrs.remove("class");
|
||||
attrs.remove("style");
|
||||
for name in ["class", "style", "transform", "filter", "mask", "clip-path"] {
|
||||
attrs.remove(name);
|
||||
}
|
||||
}
|
||||
let text_inside = matches!(name, "title" | "desc");
|
||||
let children: Vec<Value> = node["children"]
|
||||
@@ -127,6 +155,9 @@ fn every_hostile_case_gives_the_typescript_verdict_or_a_declared_one() {
|
||||
Some(expected) => {
|
||||
assert_eq!(rust["removed"], expected["removed"], "{file}");
|
||||
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() {
|
||||
strip(
|
||||
&mut tree,
|
||||
|
||||
Reference in new issue
Block a user