From 4ab5cdc3add91af965e2db31707643e0f545fdfb Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 25 Oct 2026 15:06:22 +0100 Subject: [PATCH] Address CodeRabbit review: patterns past the recursion cutoff count as too costly usvg keeps a chain of single-use patterns past 200 levels and resvg draws every one, so a costly filter at the end of the chain went uncharged. A referenced root (pattern, mask, clip path, embedded drawing) the check would skip at the cutoff now makes the drawing too costly; ordinary children there still count as before. Co-Authored-By: Claude Opus 5.5 --- crates/io/src/svg.rs | 18 +++++++++++++++++- crates/io/tests/it/svg.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/crates/io/src/svg.rs b/crates/io/src/svg.rs index 5d040c69d3..e6ae119ef2 100644 --- a/crates/io/src/svg.rs +++ b/crates/io/src/svg.rs @@ -492,7 +492,8 @@ fn count_nodes(root: &usvg::Group) -> usize { /// the whole radius window per pixel, convolution the whole kernel, and turbulence runs every /// octave (usvg doesn't clamp `numOctaves`). Masks, clip paths, patterns and embedded drawings /// count too, at the scale of the element that uses them, as resvg renders them. Past `visits` -/// elements the drawing counts as too costly. +/// elements, or with a referenced root nested past [`MAX_RECURSION`], the drawing counts as too +/// costly. fn filter_cost(root: &usvg::Group, base: tiny_skia::Transform, rec: usize, visits: &mut usize) -> u64 { use usvg::filter::Kind; let mut cost = 0u64; @@ -524,6 +525,21 @@ fn filter_cost(root: &usvg::Group, base: tiny_skia::Transform, rec: usize, visit stack.push(child); } if rec >= MAX_RECURSION { + // usvg keeps referenced roots past this depth and resvg draws them: one the check + // would skip makes the drawing too costly. Ordinary children still count. + let mut skipped = false; + match node { + usvg::Node::Path(p) => { + skipped = [p.fill().map(|f| f.paint()), p.stroke().map(|s| s.paint())] + .into_iter() + .flatten() + .any(|paint| matches!(paint, usvg::Paint::Pattern(_))) + } + _ => node.subroots(|_| skipped = true), + } + if skipped { + return u64::MAX; + } continue; } let world = base.pre_concat(node.abs_transform()); diff --git a/crates/io/tests/it/svg.rs b/crates/io/tests/it/svg.rs index 60f9d6c15f..68a1c09b8a 100644 --- a/crates/io/tests/it/svg.rs +++ b/crates/io/tests/it/svg.rs @@ -476,6 +476,37 @@ fn patterns_nested_too_deep_are_refused_not_walked() { assert!(!open(&chain(4)).document.layers.is_empty()); } +/// usvg keeps a chain of single-use patterns past the check's recursion cutoff and resvg draws +/// every one of them: a costly filter at the end of the chain is refused, not rendered unseen. +#[test] +fn a_costly_filter_past_the_pattern_recursion_cutoff_is_refused() { + let chain = |depth: usize, fe: &str| { + let defs: String = (0..depth) + .map(|i| { + let body = if i + 1 < depth { + format!("", i + 1) + } else { + "".into() + }; + format!("{body}") + }) + .collect(); + format!("{fe}{defs}") + }; + // Just over the budget (about 3.5 billion reads), so a regression fails in seconds, not hours. + let fe = ""; + assert!(import("x.svg", chain(4, fe).as_bytes()).is_err(), "the filter alone is over the budget"); + let deep = chain(210, fe); + let e = import("x.svg", deep.as_bytes()).err().unwrap_or_else(|| panic!("opened a costly filter 210 patterns deep")); + assert!(matches!(&e, IoError::Svg(m) if m.contains("too costly")), "{e}"); + assert!(svg::rasterize(deep.as_bytes(), 1.0).is_err()); + // A short chain with an ordinary filter still opens, and so do groups nested past the cutoff. + assert!(!open(&chain(4, "")).document.layers.is_empty()); + let groups = + format!("{}{}", "".repeat(250), "".repeat(250)); + assert!(!open(&groups).document.layers.is_empty()); +} + #[test] fn an_svg_round_trips_through_pcraft_as_shapes() { let r = open(&format!(