feat(anatomy): Rust SVG reader: tree, allowlist sanitiser, reference graph, serialiser (O4/O5 green)

This commit is contained in:
bchanot committed 2026-10-14 23:46:10 +02:00
1 parent a7b3d7a4c1
commit 85c2183791
12 files changed
+10851 -6

No files matched your search

+18 -1
View File
@@ -1,3 +1,20 @@
//! SVG text → bounded, sanitised tree → text.
//! SVG text → bounded, sanitised tree → text (plan R3 C2–C4). One reader for every SVG the
//! product draws: the sheet drawings now, the branding and Step 13 uploads later.
pub mod number;
pub mod prescan;
pub mod refs;
pub mod sanitize;
pub mod serialize;
pub mod tree;
pub use sanitize::Sanitized;
pub use tree::{Element, Node, SvgRejection};
/// Bounds, parses, sanitises and checks the references of an SVG text.
pub fn read_svg(text: &str) -> Result<Sanitized, SvgRejection> {
let root = tree::parse_svg(text)?;
let sanitized = sanitize::sanitize(root)?;
refs::check_references(&sanitized.root)?;
Ok(sanitized)
}
+183
View File
@@ -0,0 +1,183 @@
//! The JavaScript number semantics the engine's `stdDeviation` clamp relies on
//! (`packages/pdf/src/svg/sanitize.ts`): `String.prototype.trim`, `split(/[\s,]+/)`,
//! `Number(string)` and `String(number)`. Written by hand because Rust's `f64::from_str`
//! accepts `inf`, `nan` and `infinity` where `Number` gives NaN, and refuses `0x10` where
//! `Number` gives 16.
use crate::json::js_number;
/// ECMAScript WhiteSpace and LineTerminator (what `trim` removes and `\s` matches).
pub fn is_js_space(c: char) -> bool {
matches!(
c,
'\t' | '\n' | '\u{0B}' | '\u{0C}' | '\r' | ' ' | '\u{A0}' | '\u{1680}' | '\u{2000}'
..='\u{200A}'
| '\u{2028}'
| '\u{2029}'
| '\u{202F}'
| '\u{205F}'
| '\u{3000}'
| '\u{FEFF}'
)
}
/// `value.trim().split(/[\s,]+/)`.
pub fn js_split_list(value: &str) -> Vec<&str> {
let trimmed = value.trim_matches(is_js_space);
let mut parts = Vec::new();
let mut rest = trimmed;
loop {
match rest.find(|c: char| is_js_space(c) || c == ',') {
None => {
parts.push(rest);
return parts;
}
Some(at) => {
parts.push(&rest[..at]);
rest = rest[at..].trim_start_matches(|c: char| is_js_space(c) || c == ',');
}
}
}
}
/// `Number(text)`: StringToNumber of ECMAScript.
pub fn js_number_of(text: &str) -> f64 {
let s = text.trim_matches(is_js_space);
if s.is_empty() {
return 0.0;
}
if let Some(value) = radix_literal(s) {
return value;
}
let (sign, unsigned) = match s.as_bytes()[0] {
b'+' => (1.0, &s[1..]),
b'-' => (-1.0, &s[1..]),
_ => (1.0, s),
};
if unsigned == "Infinity" {
return sign * f64::INFINITY;
}
if !is_decimal_literal(unsigned) {
return f64::NAN;
}
unsigned.parse::<f64>().map_or(f64::NAN, |v| sign * v)
}
/// `0x…`, `0o…`, `0b…` (no sign allowed, as in ECMAScript).
fn radix_literal(s: &str) -> Option<f64> {
let radix = match s.get(..2)? {
"0x" | "0X" => 16,
"0o" | "0O" => 8,
"0b" | "0B" => 2,
_ => return None,
};
let digits = &s[2..];
if digits.is_empty() {
return Some(f64::NAN);
}
let mut value = 0.0_f64;
for c in digits.chars() {
match c.to_digit(radix) {
Some(d) => value = value * f64::from(radix) + f64::from(d),
None => return Some(f64::NAN),
}
}
Some(value)
}
/// StrUnsignedDecimalLiteral without `Infinity`: digits [. digits] | . digits, then an
/// optional exponent.
fn is_decimal_literal(s: &str) -> bool {
let b = s.as_bytes();
let mut i = 0;
let digits = |i: &mut usize| {
let start = *i;
while b.get(*i).is_some_and(u8::is_ascii_digit) {
*i += 1;
}
*i - start
};
let integer = digits(&mut i);
let mut fraction = 0;
if b.get(i) == Some(&b'.') {
i += 1;
fraction = digits(&mut i);
}
if integer + fraction == 0 {
return false;
}
if matches!(b.get(i), Some(b'e' | b'E')) {
i += 1;
if matches!(b.get(i), Some(b'+' | b'-')) {
i += 1;
}
if digits(&mut i) == 0 {
return false;
}
}
i == b.len()
}
/// `String(number)`, non-finite values included.
pub fn js_to_string(value: f64) -> String {
if value.is_nan() {
"NaN".to_owned()
} else if value.is_infinite() {
if value > 0.0 { "Infinity" } else { "-Infinity" }.to_owned()
} else {
js_number(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_numbers_as_javascript_does() {
let cases: [(&str, f64); 14] = [
("", 0.0),
(" ", 0.0),
("2.5", 2.5),
(" +5 ", 5.0),
(".5", 0.5),
("5.", 5.0),
("1e3", 1000.0),
("1e-7", 1e-7),
("0x10", 16.0),
("0b11", 3.0),
("Infinity", f64::INFINITY),
("-Infinity", f64::NEG_INFINITY),
("-0x10", f64::NAN),
("1_0", f64::NAN),
];
for (text, expected) in cases {
let got = js_number_of(text);
assert!(
got == expected || (got.is_nan() && expected.is_nan()),
"{text:?} → {got}"
);
}
for text in [
"inf", "nan", "infinity", "NaN", "e5", "1e", ".", "0x", "1.2.3",
] {
assert!(js_number_of(text).is_nan(), "{text:?}");
}
}
#[test]
fn splits_lists_as_the_engine_does() {
assert_eq!(js_split_list(" 200 , 1_0"), ["200", "1_0"]);
assert_eq!(js_split_list("+5,1e3"), ["+5", "1e3"]);
assert_eq!(js_split_list(""), [""]);
assert_eq!(js_split_list(",5"), ["", "5"]);
assert_eq!(js_split_list("5,"), ["5", ""]);
}
#[test]
fn prints_non_finite_values_as_javascript_does() {
assert_eq!(js_to_string(f64::NEG_INFINITY), "-Infinity");
assert_eq!(js_to_string(1e-7), "1e-7");
assert_eq!(js_to_string(100.0), "100");
}
}
+211
View File
@@ -0,0 +1,211 @@
//! Internal references of a sanitised tree (owner brief R3 §5: internal references and the
//! complexity of `<use>`). The sanitiser already keeps only `#id` targets; here every
//! `href` / `xlink:href` and every `url(#id)` becomes an edge to the first element holding
//! that id (document order, as browsers resolve it). A cycle through tree and reference edges
//! is refused, and so is a document whose expanded size passes the budget: each reference
//! adds the expanded size of its target, so 256 `<use>` of a large group, nested uses or a
//! clip path instantiated many times are all counted. The walk is iterative (reference
//! chains are not bounded by the depth cap) and saturating, with an early exit.
use std::collections::HashMap;
use super::sanitize::url_targets;
use super::tree::{Element, Node, SvgRejection};
/// Expanded elements a drawing may instantiate (the product's drawing: a few thousand).
pub const MAX_EXPANDED_ELEMENTS: u64 = 50_000;
struct Flat {
edges: Vec<usize>,
}
/// Checks the reference graph of `root`; answers the expanded element count.
pub fn check_references(root: &Element) -> Result<u64, SvgRejection> {
let (flat, refs) = flatten(root);
let ids = first_ids(root);
let mut graph: Vec<Flat> = flat;
for (node, targets) in refs {
for id in targets {
if let Some(&target) = ids.get(id.as_str()) {
graph[node].edges.push(target);
}
}
}
expanded_size(&graph)
}
/// Elements in document order with their child edges, and the reference ids of each.
fn flatten(root: &Element) -> (Vec<Flat>, Vec<(usize, Vec<String>)>) {
let mut flat = Vec::new();
let mut refs = Vec::new();
push_element(root, &mut flat, &mut refs);
(flat, refs)
}
fn push_element(
element: &Element,
flat: &mut Vec<Flat>,
refs: &mut Vec<(usize, Vec<String>)>,
) -> usize {
let index = flat.len();
flat.push(Flat { edges: Vec::new() });
let targets = reference_ids(element);
if !targets.is_empty() {
refs.push((index, targets));
}
for child in &element.children {
if let Node::Element(child) = child {
let child_index = push_element(child, flat, refs);
flat[index].edges.push(child_index);
}
}
index
}
/// First element of each id, in document order (the same order `flatten` numbers them).
fn first_ids(root: &Element) -> HashMap<&str, usize> {
let mut ids = HashMap::new();
let mut counter = 0;
collect_ids(root, &mut counter, &mut ids);
ids
}
fn collect_ids<'a>(element: &'a Element, counter: &mut usize, ids: &mut HashMap<&'a str, usize>) {
if let Some(id) = element.attr("id") {
ids.entry(id).or_insert(*counter);
}
*counter += 1;
for child in element.element_children() {
collect_ids(child, counter, ids);
}
}
/// Ids named by `href`, `xlink:href` (`#id`) and every `url(#id)` of the element.
fn reference_ids(element: &Element) -> Vec<String> {
let mut ids = Vec::new();
for attr in &element.attrs {
if attr.qname == "href" || attr.qname == "xlink:href" {
if let Some(id) = attr.value.trim().strip_prefix('#') {
ids.push(id.to_owned());
}
continue;
}
ids.extend(url_targets(&attr.value));
}
ids
}
/// Post-order walk with an explicit stack: grey = on the current path (a cycle when met
/// again), black = size known.
fn expanded_size(graph: &[Flat]) -> Result<u64, SvgRejection> {
#[derive(Clone, Copy, PartialEq)]
enum Mark {
White,
Grey,
Black,
}
if graph.is_empty() {
return Ok(0);
}
let mut mark = vec![Mark::White; graph.len()];
let mut size = vec![0_u64; graph.len()];
let mut stack: Vec<(usize, usize)> = vec![(0, 0)];
mark[0] = Mark::Grey;
while let Some(&mut (node, ref mut next)) = stack.last_mut() {
if let Some(&target) = graph[node].edges.get(*next) {
*next += 1;
match mark[target] {
Mark::Grey => return Err(SvgRejection::ReferenceCycle),
Mark::White => {
mark[target] = Mark::Grey;
stack.push((target, 0));
}
Mark::Black => {}
}
continue;
}
let total = graph[node]
.edges
.iter()
.fold(1_u64, |sum, &t| sum.saturating_add(size[t]));
if total > MAX_EXPANDED_ELEMENTS {
return Err(SvgRejection::TooComplex);
}
size[node] = total;
mark[node] = Mark::Black;
stack.pop();
}
Ok(size[0])
}
#[cfg(test)]
mod tests {
use super::*;
use crate::anatomy::svg::tree::parse_svg;
fn check(body: &str) -> Result<u64, SvgRejection> {
let text = format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">{body}</svg>"
);
let root = parse_svg(&text).unwrap_or_else(|e| panic!("{e:?}"));
check_references(&root)
}
#[test]
fn counts_each_reference_as_its_target_expanded() {
// root + path + 3 uses each expanding the path (1): 1 + 1 + 3 × 2
assert_eq!(
check("<path id=\"p\"/><use href=\"#p\"/><use href=\"#p\"/><use xlink:href=\"#p\"/>"),
Ok(8)
);
// a dangling reference adds nothing; a colour is not a reference
assert_eq!(check("<path fill=\"url(#none)\"/>"), Ok(2));
assert_eq!(
check("<g id=\"ff9500\"><path fill=\"#ff9500\"/></g>"),
Ok(3)
);
}
#[test]
fn refuses_cycles_through_use_paint_and_clip_paths() {
assert_eq!(
check("<g id=\"a\"><use href=\"#a\"/></g>"),
Err(SvgRejection::ReferenceCycle)
);
assert_eq!(
check("<g id=\"a\"><use href=\"#b\"/></g><g id=\"b\"><use href=\"#a\"/></g>"),
Err(SvgRejection::ReferenceCycle)
);
assert_eq!(
check(
"<clipPath id=\"c\"><use href=\"#g\"/></clipPath><g id=\"g\"><path clip-path=\"url(#c)\"/></g>"
),
Err(SvgRejection::ReferenceCycle)
);
assert_eq!(
check(
"<linearGradient id=\"a\" xlink:href=\"#b\"/><linearGradient id=\"b\" xlink:href=\"#a\"/>"
),
Err(SvgRejection::ReferenceCycle)
);
}
#[test]
fn refuses_an_expansion_over_the_budget_without_overflow() {
// 8 levels × 30 uses: 30^8 instances; refused early, no overflow.
let mut body = String::from("<path id=\"l0\"/>");
for level in 1..=8 {
body.push_str(&format!(
"<g id=\"l{level}\">{}</g>",
format!("<use href=\"#l{}\"/>", level - 1).repeat(30)
));
}
assert_eq!(check(&body), Err(SvgRejection::TooComplex));
// 200 uses of a 300-element group: 60,000 > 50,000
let group = format!("<g id=\"big\">{}</g>", "<path/>".repeat(299));
assert_eq!(
check(&format!("{group}{}", "<use href=\"#big\"/>".repeat(200))),
Err(SvgRejection::TooComplex)
);
}
}
+395
View File
@@ -0,0 +1,395 @@
//! The allowlist sanitiser (STEP6 §15, `packages/pdf/src/svg/sanitize.ts`), one for every
//! SVG the product draws. The engine's lists and value rules, matched on qualified names
//! like the engine; plus the declared R3 rules (plan C3): elements in the SVG namespace only,
//! no `class`/`style` on the root (inline SVG lives under the page's CSS), `id` values in the
//! 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::tree::{Element, Node, SvgRejection, XLINK_NS};
const ALLOWED_ELEMENTS: [&str; 28] = [
"svg",
"g",
"path",
"circle",
"ellipse",
"rect",
"line",
"polyline",
"polygon",
"defs",
"linearGradient",
"radialGradient",
"stop",
"filter",
"feGaussianBlur",
"feMerge",
"feMergeNode",
"feOffset",
"feFlood",
"feComposite",
"feBlend",
"feColorMatrix",
"clipPath",
"mask",
"title",
"desc",
"switch",
"use",
];
const ALLOWED_ATTRIBUTES: [&str; 74] = [
"id",
"class",
"d",
"cx",
"cy",
"r",
"rx",
"ry",
"x",
"y",
"x1",
"y1",
"x2",
"y2",
"dx",
"dy",
"width",
"height",
"points",
"transform",
"viewBox",
"version",
"preserveAspectRatio",
"fill",
"fill-opacity",
"fill-rule",
"stroke",
"stroke-width",
"stroke-linecap",
"stroke-linejoin",
"stroke-opacity",
"stroke-dasharray",
"stroke-dashoffset",
"stroke-miterlimit",
"opacity",
"filter",
"clip-path",
"clip-rule",
"mask",
"display",
"visibility",
"color",
"gradientUnits",
"gradientTransform",
"spreadMethod",
"offset",
"stop-color",
"stop-opacity",
"stdDeviation",
"in",
"in2",
"result",
"mode",
"operator",
"k1",
"k2",
"k3",
"k4",
"flood-color",
"flood-opacity",
"type",
"values",
"color-interpolation-filters",
"filterUnits",
"primitiveUnits",
"systemLanguage",
"font-family",
"font-size",
"font-weight",
"xmlns",
"xmlns:xlink",
"xlink:href",
"href",
"style",
];
/// CSS-like values: a function call there is a reference or an exploit.
const PRESENTATION_ATTRIBUTES: [&str; 27] = [
"fill",
"fill-opacity",
"fill-rule",
"stroke",
"stroke-width",
"stroke-linecap",
"stroke-linejoin",
"stroke-opacity",
"stroke-dasharray",
"stroke-dashoffset",
"stroke-miterlimit",
"opacity",
"filter",
"clip-path",
"clip-rule",
"mask",
"display",
"visibility",
"color",
"stop-color",
"stop-opacity",
"flood-color",
"flood-opacity",
"color-interpolation-filters",
"font-family",
"font-size",
"font-weight",
];
/// The engine's cap on `<use>` elements and on each `stdDeviation` number.
pub const MAX_USES: usize = 256;
pub const MAX_STD_DEVIATION: f64 = 100.0;
const STYLE_FORBIDDEN: [&str; 5] = [
"expression(",
"image-set(",
"@import",
"javascript:",
"behavior:",
];
/// A sanitised tree and what was taken out of it.
#[derive(Debug, Clone, PartialEq)]
pub struct Sanitized {
pub root: Element,
/// `tag` and `tag@attribute` in document order (empty on the product's SVGs).
pub removed: Vec<String>,
}
/// Sanitises a parsed root (the caller parsed it with `tree::parse_svg`).
pub fn sanitize(mut root: Element) -> Result<Sanitized, SvgRejection> {
let mut walk = Walk {
removed: Vec::new(),
uses: 0,
};
for name in ["class", "style"] {
if root.attr(name).is_some() {
root.remove_attr(name);
walk.removed.push(format!("svg@{name}"));
}
}
walk.visit(&mut root)?;
Ok(Sanitized {
root,
removed: walk.removed,
})
}
struct Walk {
removed: Vec<String>,
uses: usize,
}
impl Walk {
fn visit(&mut self, element: &mut Element) -> Result<(), SvgRejection> {
if element.qname == "use" {
self.uses += 1;
if self.uses > MAX_USES {
return Err(SvgRejection::TooManyUses);
}
}
self.sanitize_attributes(element);
let keeps_text = matches!(element.qname.as_str(), "title" | "desc");
let children = std::mem::take(&mut element.children);
for child in children {
match child {
Node::Element(mut child) if element_allowed(&child) => {
self.visit(&mut child)?;
element.children.push(Node::Element(child));
}
Node::Element(child) => self.removed.push(child.qname),
Node::Text(text) if keeps_text || is_xml_space(&text) => {
element.children.push(Node::Text(text));
}
Node::Text(_) => {}
}
}
Ok(())
}
fn sanitize_attributes(&mut self, element: &mut Element) {
let tag = element.qname.clone();
element.attrs.retain(|a| {
let keep = ALLOWED_ATTRIBUTES.contains(&a.qname.as_str())
&& namespace_fits(&a.qname, a.namespace.as_deref())
&& value_acceptable(&a.qname, &a.value);
if !keep {
self.removed.push(format!("{tag}@{}", a.qname));
}
keep
});
clamp_std_deviation(element);
}
}
fn element_allowed(element: &Element) -> bool {
ALLOWED_ELEMENTS.contains(&element.qname.as_str()) && element.is_svg_namespace()
}
/// `xlink:href` must be in the xlink namespace; every other kept name is unprefixed.
fn namespace_fits(qname: &str, namespace: Option<&str>) -> bool {
match qname {
"xlink:href" => namespace == Some(XLINK_NS),
"xmlns" | "xmlns:xlink" => true,
_ => namespace.is_none(),
}
}
fn is_xml_space(text: &str) -> bool {
text.chars().all(|c| matches!(c, ' ' | '\t' | '\n' | '\r'))
}
/// The engine's value rules, in its order, then the R3 id grammar.
fn value_acceptable(qname: &str, value: &str) -> bool {
if value.contains('\\') || !urls_are_local(value) {
return false;
}
match qname {
"href" | "xlink:href" => is_local_reference(value.trim_matches(is_js_space)),
"id" => is_reference_name(value),
"style" => !style_forbidden(value) && calls_only_local_url(value),
_ if PRESENTATION_ATTRIBUTES.contains(&qname) => calls_only_local_url(value),
_ => true,
}
}
/// `^#[A-Za-z_][\w.:-]*$`.
fn is_local_reference(value: &str) -> bool {
value.strip_prefix('#').is_some_and(is_reference_name)
}
/// `[A-Za-z_][\w.:-]*`, the whole value.
pub fn is_reference_name(value: &str) -> bool {
let mut chars = value.chars();
chars
.next()
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | ':' | '-'))
}
/// Byte offsets right after each case-insensitive `url(`.
fn url_openings(value: &str) -> impl Iterator<Item = usize> + '_ {
let lower = value.to_ascii_lowercase();
let mut from = 0;
std::iter::from_fn(move || {
let at = lower.get(from..)?.find("url(")? + from;
from = at + 4;
Some(from)
})
}
/// After `url(`: `\s*`, an optional quote, `\s*`; returns the offset of the next character.
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)) {
chars.next();
}
};
skip_space(&mut chars);
if chars.peek().is_some_and(|&(_, c)| c == '\'' || c == '"') {
chars.next();
}
skip_space(&mut chars);
at + chars.peek().map_or(rest.len(), |&(i, _)| i)
}
/// The ids of the value's `url(#id)` references, in order.
pub fn url_targets(value: &str) -> Vec<String> {
url_openings(value)
.filter_map(|at| value[skip_url_prefix(value, at)..].strip_prefix('#'))
.map(|rest| {
rest.chars()
.take_while(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | ':' | '-'))
.collect::<String>()
})
.filter(|id| !id.is_empty())
.collect()
}
/// Every `url(` in the value points inside the document (`url(#…)`).
fn urls_are_local(value: &str) -> bool {
url_openings(value).all(|at| value[skip_url_prefix(value, at)..].starts_with('#'))
}
/// No `(` once the `url(#` openings are taken out.
fn calls_only_local_url(value: &str) -> bool {
let mut cut = String::with_capacity(value.len());
let mut kept_from = 0;
for at in url_openings(value) {
let next = skip_url_prefix(value, at);
if value[next..].starts_with('#') {
cut.push_str(&value[kept_from..at - 4]);
kept_from = next + 1;
}
}
cut.push_str(&value[kept_from..]);
!cut.contains('(')
}
fn style_forbidden(value: &str) -> bool {
let lower = value.to_ascii_lowercase();
STYLE_FORBIDDEN.iter().any(|f| lower.contains(f))
}
/// Clamps every number of a `stdDeviation` list to 100; a list with a NaN loses the
/// attribute; the value is rewritten only when a number was clamped.
fn clamp_std_deviation(element: &mut Element) {
let Some(raw) = element.attr("stdDeviation") else {
return;
};
let numbers: Vec<f64> = js_split_list(raw).into_iter().map(js_number_of).collect();
if numbers.iter().any(|n| n.is_nan()) {
element.remove_attr("stdDeviation");
return;
}
if numbers.iter().all(|&n| n <= MAX_STD_DEVIATION) {
return;
}
let clamped: Vec<String> = numbers
.iter()
.map(|&n| js_to_string(n.min(MAX_STD_DEVIATION)))
.collect();
element.set_attr("stdDeviation", clamped.join(" "));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn url_rules_match_the_engine() {
assert!(
urls_are_local("url(#a)") && urls_are_local("URL( '#a')") && urls_are_local("none")
);
assert!(!urls_are_local("url(http://e)") && !urls_are_local("url("));
// JavaScript's `\s` takes the newline, then `#` follows: local.
assert!(urls_are_local("url(\n#a)"));
assert!(calls_only_local_url("url(#a) url( \"#b\")"));
assert!(!calls_only_local_url("rgb(1,2,3)") && !calls_only_local_url("url(#a) blur(2)"));
}
#[test]
fn reference_grammar() {
assert!(
is_local_reference("#lg_1.a:b-c")
&& !is_local_reference("#1a")
&& !is_local_reference("x")
);
assert!(
is_reference_name("osteo-halo") && !is_reference_name("") && !is_reference_name("a b")
);
}
}
+98
View File
@@ -0,0 +1,98 @@
//! Tree → SVG text (plan R3 C4). XML 1.0 without a declaration; the root carries the SVG
//! namespace declaration (added when the source had none, as xmldom does); every other
//! declaration stays where the source made it. Escapes keep a re-parse identical: in
//! attribute values `& < > "` and the whitespace a parser would normalise (`\t \n \r` as
//! character references), in text `& < >` and `\r`. Empty elements are self-closed.
use super::tree::{Element, Node, SVG_NS};
/// The text of `root` and its subtree.
pub fn serialize(root: &Element) -> String {
let mut out = String::with_capacity(4096);
let declares = root.attrs.iter().any(|a| a.qname == "xmlns");
write_element(&mut out, root, !declares);
out
}
fn write_element(out: &mut String, element: &Element, add_namespace: bool) {
out.push('<');
out.push_str(&element.qname);
if add_namespace {
out.push_str(" xmlns=\"");
out.push_str(SVG_NS);
out.push('"');
}
for attr in &element.attrs {
out.push(' ');
out.push_str(&attr.qname);
out.push_str("=\"");
escape_into(out, &attr.value, true);
out.push('"');
}
if element.children.is_empty() {
out.push_str("/>");
return;
}
out.push('>');
for child in &element.children {
match child {
Node::Element(e) => write_element(out, e, false),
Node::Text(t) => escape_into(out, t, false),
}
}
out.push_str("</");
out.push_str(&element.qname);
out.push('>');
}
fn escape_into(out: &mut String, text: &str, attribute: bool) {
for c in text.chars() {
match c {
'&' => out.push_str("&amp;"),
'<' => out.push_str("&lt;"),
'>' => out.push_str("&gt;"),
'\r' => out.push_str("&#13;"),
'"' if attribute => out.push_str("&quot;"),
'\t' if attribute => out.push_str("&#9;"),
'\n' if attribute => out.push_str("&#10;"),
_ => out.push(c),
}
}
}
/// Whether XML 1.0 can carry `text` at all (Char production): layer inputs come from the
/// document, which allows any string.
pub fn is_xml_text(text: &str) -> bool {
text.chars().all(|c| {
matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..)
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::anatomy::svg::tree::parse_svg;
#[test]
fn a_reparse_gives_the_same_tree() {
let text = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><title>a &amp; b &lt; c\r\nd</title>\n <path d=\"M0 0\" id=\"x&#9;y&#10;z&#13;\" fill=\"&quot;&gt;\"/><linearGradient xlink:href=\"#x\"/></svg>";
let first = parse_svg(text).unwrap_or_else(|e| panic!("{e:?}"));
let again = parse_svg(&serialize(&first)).unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(first, again);
}
#[test]
fn declares_the_svg_namespace_when_the_source_did_not() {
let root = parse_svg("<svg><path/></svg>").unwrap_or_else(|e| panic!("{e:?}"));
assert_eq!(
serialize(&root),
"<svg xmlns=\"http://www.w3.org/2000/svg\"><path/></svg>"
);
}
#[test]
fn tells_text_xml_cannot_carry() {
assert!(is_xml_text("Hanche « gauche » 😀\t\n"));
assert!(!is_xml_text("a\u{1}b") && !is_xml_text("\u{FFFE}") && !is_xml_text("\u{B}"));
}
}
+259
View File
@@ -0,0 +1,259 @@
//! The SVG tree the core owns (roxmltree's borrows its input text): elements with their
//! qualified name as written, the resolved namespace, attributes in source order (namespace
//! declarations first, as attributes named `xmlns` / `xmlns:p`), and text. Depth is capped
//! at 64 by the prescan before any tree exists, so recursion over it is bounded.
use super::prescan::{PrescanError, SVG_CAPS, prescan};
pub const SVG_NS: &str = "http://www.w3.org/2000/svg";
pub const XLINK_NS: &str = "http://www.w3.org/1999/xlink";
pub const XMLNS_NS: &str = "http://www.w3.org/2000/xmlns/";
/// A node kept in the tree. Comments and processing instructions are not read (R3 C3).
#[derive(Debug, Clone, PartialEq)]
pub enum Node {
Element(Element),
Text(String),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Element {
/// The name as written (`path`, `s:path`): the allowlist matches it, like the engine.
pub qname: String,
/// The resolved namespace; `None` when the document declares none (xmldom reads such an
/// element as SVG under `image/svg+xml`).
pub namespace: Option<String>,
pub attrs: Vec<Attribute>,
pub children: Vec<Node>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Attribute {
pub qname: String,
pub namespace: Option<String>,
pub value: String,
}
impl Element {
/// A new element in the SVG namespace (the layer and the halo filter).
pub fn svg(name: &str, attrs: Vec<(&str, String)>, children: Vec<Node>) -> Self {
Self {
qname: name.to_owned(),
namespace: Some(SVG_NS.to_owned()),
attrs: attrs
.into_iter()
.map(|(qname, value)| Attribute {
qname: qname.to_owned(),
namespace: None,
value,
})
.collect(),
children,
}
}
pub fn attr(&self, qname: &str) -> Option<&str> {
self.attrs
.iter()
.find(|a| a.qname == qname)
.map(|a| a.value.as_str())
}
/// Sets an attribute in place (keeps its position), or appends it.
pub fn set_attr(&mut self, qname: &str, value: String) {
match self.attrs.iter_mut().find(|a| a.qname == qname) {
Some(a) => a.value = value,
None => self.attrs.push(Attribute {
qname: qname.to_owned(),
namespace: None,
value,
}),
}
}
pub fn remove_attr(&mut self, qname: &str) {
self.attrs.retain(|a| a.qname != qname);
}
/// The local name (after the prefix, if any).
pub fn local_name(&self) -> &str {
self.qname.rsplit(':').next().unwrap_or(&self.qname)
}
pub fn is_svg_namespace(&self) -> bool {
self.namespace.as_deref().is_none_or(|ns| ns == SVG_NS)
}
pub fn element_children(&self) -> impl Iterator<Item = &Element> {
self.children.iter().filter_map(|n| match n {
Node::Element(e) => Some(e),
Node::Text(_) => None,
})
}
}
/// Why an SVG text is refused (closed codes, `reason()` is the wire form).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SvgRejection {
TooLarge,
Doctype,
Malformed,
NotSvg,
TooManyElements,
TooDeep,
TooManyUses,
TooComplex,
ReferenceCycle,
}
impl SvgRejection {
pub fn reason(self) -> &'static str {
match self {
Self::TooLarge => "too_large",
Self::Doctype => "doctype",
Self::Malformed => "malformed",
Self::NotSvg => "not_svg",
Self::TooManyElements => "too_many_elements",
Self::TooDeep => "too_deep",
Self::TooManyUses => "too_many_uses",
Self::TooComplex => "too_complex",
Self::ReferenceCycle => "reference_cycle",
}
}
}
impl From<PrescanError> for SvgRejection {
fn from(error: PrescanError) -> Self {
match error {
PrescanError::TooLarge => Self::TooLarge,
PrescanError::Doctype => Self::Doctype,
PrescanError::TooDeep => Self::TooDeep,
PrescanError::TooManyElements => Self::TooManyElements,
PrescanError::TooComplex => Self::TooComplex,
PrescanError::Malformed => Self::Malformed,
}
}
}
/// Never reached once the prescan passed (its caps are lower); a backstop on parser memory.
const NODES_LIMIT: u32 = 200_000;
/// Bounds, parses and reads an SVG text into the core's tree; the root must be `<svg>` in the
/// SVG namespace (or none), written unprefixed.
pub fn parse_svg(text: &str) -> Result<Element, SvgRejection> {
prescan(text, &SVG_CAPS)?;
let options = roxmltree::ParsingOptions {
allow_dtd: false,
nodes_limit: NODES_LIMIT,
..roxmltree::ParsingOptions::default()
};
let document = roxmltree::Document::parse_with_options(text, options).map_err(|e| match e {
roxmltree::Error::NodesLimitReached => SvgRejection::TooComplex,
roxmltree::Error::DtdDetected => SvgRejection::Doctype,
_ => SvgRejection::Malformed,
})?;
let root = element_of(document.root_element(), text, None);
if root.qname != "svg" || !root.is_svg_namespace() {
return Err(SvgRejection::NotSvg);
}
Ok(root)
}
/// The qualified name as written: the bytes after `<` up to whitespace, `/` or `>`.
fn written_name(text: &str, start: usize) -> String {
text[start + 1..]
.split(|c: char| c.is_ascii_whitespace() || c == '/' || c == '>')
.next()
.unwrap_or_default()
.to_owned()
}
fn element_of(
node: roxmltree::Node<'_, '_>,
text: &str,
parent: Option<roxmltree::Node<'_, '_>>,
) -> Element {
let mut attrs = declarations_of(node, parent);
attrs.extend(node.attributes().map(|a| Attribute {
qname: text[a.range_qname()].to_owned(),
namespace: a.namespace().map(str::to_owned),
value: a.value().to_owned(),
}));
let children = node
.children()
.filter_map(|child| {
if child.is_element() {
Some(Node::Element(element_of(child, text, Some(node))))
} else if child.is_text() {
Some(Node::Text(child.text().unwrap_or_default().to_owned()))
} else {
None
}
})
.collect();
Element {
qname: written_name(text, node.range().start),
namespace: node.tag_name().namespace().map(str::to_owned),
attrs,
children,
}
}
/// The namespaces declared on this element: in scope here and not in the parent's scope.
fn declarations_of(
node: roxmltree::Node<'_, '_>,
parent: Option<roxmltree::Node<'_, '_>>,
) -> Vec<Attribute> {
let inherited: Vec<(Option<&str>, &str)> = parent
.map(|p| p.namespaces().map(|n| (n.name(), n.uri())).collect())
.unwrap_or_default();
node.namespaces()
.filter(|n| n.name() != Some("xml") && !inherited.contains(&(n.name(), n.uri())))
.map(|n| Attribute {
qname: n
.name()
.map_or_else(|| "xmlns".to_owned(), |p| format!("xmlns:{p}")),
namespace: Some(XMLNS_NS.to_owned()),
value: n.uri().to_owned(),
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_names_as_written_namespaces_and_declarations() {
let text = "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 1 1\"><g id=\"a\">\n <use xlink:href=\"#a\"/></g></svg>";
let root = parse_svg(text).unwrap_or_else(|e| panic!("{e:?}"));
let names: Vec<&str> = root.attrs.iter().map(|a| a.qname.as_str()).collect();
assert_eq!(names, ["xmlns", "xmlns:xlink", "viewBox"]);
let g = root
.element_children()
.next()
.unwrap_or_else(|| panic!("g"));
assert!(g.attrs.iter().all(|a| !a.qname.starts_with("xmlns")));
assert_eq!(g.children.len(), 2);
let Node::Element(use_el) = &g.children[1] else {
panic!("use")
};
assert_eq!(use_el.attrs[0].qname, "xlink:href");
assert_eq!(use_el.attrs[0].namespace.as_deref(), Some(XLINK_NS));
}
#[test]
fn refuses_a_prefixed_or_foreign_root_and_accepts_one_without_namespace() {
let prefixed = "<s:svg xmlns:s=\"http://www.w3.org/2000/svg\"/>";
assert_eq!(parse_svg(prefixed), Err(SvgRejection::NotSvg));
assert_eq!(
parse_svg("<svg xmlns=\"http://e\"/>"),
Err(SvgRejection::NotSvg)
);
assert!(parse_svg("<svg><path/></svg>").is_ok());
assert_eq!(
parse_svg("<svg><path xlink:href=\"#p\"/></svg>"),
Err(SvgRejection::Malformed)
);
}
}
+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><g id="1abc"/><g id="ok_1.a:b-c"/><g id="root"/><g id="a b"/></svg>

After

Width:  |  Height:  |  Size: 107 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" class="fixed inset-0 z-50" style="position:fixed;width:100vw"><g class="kept" style="opacity:0.5"><path d="M0 0h1"/></g></svg>

After

Width:  |  Height:  |  Size: 166 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://evil.test/ns"><linearGradient id="a"/><linearGradient id="b" xlink:href="#a"/></svg>

After

Width:  |  Height:  |  Size: 144 B

File diff suppressed because it is too large. Load diff
+262
View File
@@ -0,0 +1,262 @@
//! R3 O4 + O5: the Rust SVG reader against the TypeScript sanitiser's frozen verdicts
//! (`oracles/r3/sanitiser.json`, written by `packages/pdf/tests/r3.sanitiser-oracles.test.ts`
//! on the corpus `fixtures/svg/hostile` and the product SVGs). Every case gives the TS verdict,
//! except the differences the plan declares (rev 2 C2/C3), each listed below with the Rust
//! verdict; a case where Rust would be looser than the TS and is not listed fails.
//!
//! Trees are compared as structure: namespace, local name, attributes by qualified name,
//! 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.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use std::collections::BTreeMap;
use faunosteo_core::anatomy::svg::sanitize::is_reference_name;
use faunosteo_core::anatomy::svg::serialize::serialize;
use faunosteo_core::anatomy::svg::tree::{SVG_NS, parse_svg};
use faunosteo_core::anatomy::svg::{Element, Node, SvgRejection, read_svg};
use serde_json::{Value, json};
/// Rust outcomes that differ from the TS on purpose: file → expected Rust verdict.
fn declared(file: &str) -> Option<Value> {
Some(match file {
// C3 reference graph: a `<use>` cycle is refused (TS accepts it).
"use-self-cycle.svg" | "use-mutual-cycle.svg" => json!({ "rejected": "reference_cycle" }),
// C3 namespace: `<path xmlns="evil">` is removed too (TS keeps it).
"namespaces.svg" => json!({ "removed": [
"svg@xmlns:s", "svg@xmlns:inkscape", "s:path", "path",
"path@inkscape:label", "path@xml:space", "path@xml:lang"
] }),
// C3 id grammar: an id holding a tab, a space or starting with a digit is removed.
"whitespace-references.svg" => json!({ "removed": ["path@id"] }),
"id-grammar.svg" => json!({ "removed": ["g@id", "g@id"] }),
// 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 root: no class or style on the inline root.
"root-class-style.svg" => json!({ "removed": ["svg@class", "svg@style"] }),
_ => return None,
})
}
/// Generated cases with a declared Rust verdict (C2 bounds, C3 budget).
fn declared_spec(spec: &Value) -> Option<&'static str> {
match (spec["kind"].as_str()?, spec["n"].as_u64()) {
("chain", _) => Some("too_complex"),
("bytes", Some(n)) if n > 2 * 1024 * 1024 => Some("too_large"),
("attributes", _) => Some("too_complex"),
_ => None,
}
}
fn tree_value(element: &Element) -> Value {
let attrs: BTreeMap<&str, &str> = element
.attrs
.iter()
.map(|a| (a.qname.as_str(), a.value.as_str()))
.collect();
let children: Vec<Value> = element
.children
.iter()
.map(|n| match n {
Node::Element(e) => tree_value(e),
Node::Text(t) => json!({ "text": t }),
})
.collect();
json!({
"ns": element.namespace.as_deref().unwrap_or(SVG_NS),
"name": element.local_name(),
"attrs": attrs,
"children": children,
})
}
/// Removes the named attribute from every element of that local name (a declared removal).
fn strip(tree: &mut Value, element: &str, attribute: &str) {
if tree["name"] == element {
if let Some(attrs) = tree["attrs"].as_object_mut() {
attrs.remove(attribute);
}
}
if let Some(children) = tree["children"].as_array_mut() {
children
.iter_mut()
.for_each(|c| strip(c, element, attribute));
}
}
/// The Rust output read again, as a tree.
fn reread(element: &Element) -> Value {
tree_value(&parse_svg(&serialize(element)).unwrap())
}
fn is_space(text: &str) -> bool {
text.chars().all(|c| matches!(c, ' ' | '\t' | '\n' | '\r'))
}
/// The declared rules applied to a TS tree (see the header).
fn declared_tree(node: &Value, keeps_text: bool, root: bool) -> Option<Value> {
if node.get("comment").is_some() || node.get("pi").is_some() {
return None;
}
if let Some(text) = node["text"].as_str() {
return (keeps_text || is_space(text)).then(|| node.clone());
}
if node["ns"] != SVG_NS {
return None;
}
let name = node["name"].as_str().unwrap();
let mut attrs = node["attrs"].as_object().unwrap().clone();
if attrs
.get("id")
.and_then(Value::as_str)
.is_some_and(|id| !is_reference_name(id))
{
attrs.remove("id");
}
if root {
attrs.remove("class");
attrs.remove("style");
}
let text_inside = matches!(name, "title" | "desc");
let children: Vec<Value> = node["children"]
.as_array()
.unwrap()
.iter()
.filter_map(|c| declared_tree(c, text_inside, false))
.collect();
Some(json!({ "ns": node["ns"], "name": name, "attrs": attrs, "children": children }))
}
fn verdict(text: &str) -> Value {
match read_svg(text) {
Ok(s) => json!({ "removed": s.removed, "tree": reread(&s.root) }),
Err(e) => json!({ "rejected": e.reason() }),
}
}
#[test]
fn every_hostile_case_gives_the_typescript_verdict_or_a_declared_one() {
let (_, oracle) = common::oracle_file("r3/sanitiser.json");
let hostile = oracle["hostile"].as_object().unwrap();
assert!(hostile.len() >= 34);
for (file, ts) in hostile {
let text = common::repo_file(&format!("crates/core/tests/fixtures/svg/hostile/{file}"));
let rust = verdict(&text);
match declared(file) {
Some(expected) if expected.get("rejected").is_some() => {
assert_eq!(rust, expected, "{file}")
}
Some(expected) => {
assert_eq!(rust["removed"], expected["removed"], "{file}");
let mut tree = declared_tree(&ts["tree"], false, true).unwrap();
for pair in expected["strip"].as_array().into_iter().flatten() {
strip(
&mut tree,
pair[0].as_str().unwrap(),
pair[1].as_str().unwrap(),
);
}
assert_eq!(rust["tree"], tree, "{file}");
}
None if ts.get("rejected").is_some() => assert_eq!(rust, *ts, "{file}"),
None => {
assert_eq!(rust["removed"], ts["removed"], "{file}");
assert_eq!(
rust["tree"],
declared_tree(&ts["tree"], false, true).unwrap(),
"{file}"
);
}
}
}
}
/// `generated(spec)` of the TS generator, byte for byte.
fn generated(spec: &Value) -> String {
const OPEN: &str = "<svg xmlns=\"http://www.w3.org/2000/svg\">";
let n = spec["n"].as_u64().unwrap_or(0) as usize;
match spec["kind"].as_str().unwrap() {
"uses" => format!(
"{OPEN}<path id=\"p\" d=\"M0 0h1\"/>{}</svg>",
"<use href=\"#p\"/>".repeat(n)
),
"chain" => {
let mut body = String::from("<path id=\"l0\" d=\"M0 0h1\"/>");
let (levels, width) = (
spec["levels"].as_u64().unwrap(),
spec["width"].as_u64().unwrap() as usize,
);
for level in 1..=levels {
body.push_str(&format!(
"<g id=\"l{level}\">{}</g>",
format!("<use href=\"#l{}\"/>", level - 1).repeat(width)
));
}
format!("{OPEN}{body}</svg>")
}
"depth" => format!(
"{OPEN}{}{}</svg>",
"<g>".repeat(n - 1),
"</g>".repeat(n - 1)
),
"elements" => format!("{OPEN}{}</svg>", "<path/>".repeat(n - 1)),
"bytes" => {
let frame = format!("{OPEN}<!----></svg>");
format!("{OPEN}<!--{}--></svg>", "x".repeat(n - frame.len()))
}
"attributes" => {
let attrs: String = (0..n).map(|i| format!(" data-a{i}=\"{i}\"")).collect();
format!("{OPEN}<path d=\"M0 0h1\"{attrs}/></svg>")
}
other => panic!("unknown spec {other}"),
}
}
#[test]
fn every_generated_case_gives_the_typescript_verdict_or_a_declared_one() {
let (_, oracle) = common::oracle_file("r3/sanitiser.json");
for case in oracle["specs"].as_array().unwrap() {
let (spec, ts) = (&case["spec"], &case["verdict"]);
let rust = read_svg(&generated(spec));
match (declared_spec(spec), rust) {
(Some(reason), Err(e)) => assert_eq!(e.reason(), reason, "{spec}"),
(Some(reason), Ok(_)) => panic!("{spec}: expected {reason}"),
(None, Err(e)) => assert_eq!(ts["rejected"], e.reason(), "{spec}"),
(None, Ok(s)) => {
let mut removed = s.removed.clone();
removed.sort();
removed.dedup();
assert_eq!(json!(removed), ts["removed"], "{spec}");
}
}
}
}
#[test]
fn the_product_svgs_come_out_untouched() {
let (_, oracle) = common::oracle_file("r3/sanitiser.json");
for entry in oracle["legitimate"].as_array().unwrap() {
let path = entry["path"].as_str().unwrap();
let read = read_svg(&common::repo_file(path)).unwrap_or_else(|e| panic!("{path}: {e:?}"));
assert!(read.removed.is_empty(), "{path}: {:?}", read.removed);
assert_eq!(reread(&read.root), entry["tree"], "{path}");
}
}
#[test]
fn a_rejection_names_a_closed_reason() {
for (text, reason) in [
(
"<svg xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M0 0\"></svg>",
SvgRejection::Malformed,
),
("<html/>", SvgRejection::NotSvg),
] {
assert_eq!(read_svg(text).err(), Some(reason));
}
}