diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index 37d956a48..9cf85c3bd 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -212,7 +212,8 @@ pub struct Viewport { } /// Wraps a validated SVG root in a new viewport while preserving the original root as content. -/// The original root must have finite, positive pixel or point dimensions. +/// The original root must have finite, positive pixel or point dimensions, and the wrapped +/// output must still pass [`preflight`] under `limits`. pub fn rewrite_root_viewport(svg: &str, target: Viewport, limits: &Limits, output_limit: usize) -> Result { if !target.width.is_finite() || !target.height.is_finite() || target.width <= 0.0 || target.height <= 0.0 { return Err(Error::Viewport("target dimensions must be finite and positive".into())); @@ -256,6 +257,9 @@ pub fn rewrite_root_viewport(svg: &str, target: Viewport, limits: &Limits, outpu output.push_str(&prefix); output.push_str(inner); output.push_str(""); + // The wrapper adds a level, so a source at the depth limit can come out over it: check the + // output under the limits `parse_text` applies rather than return text it would refuse. + preflight(&output, limits)?; Ok(output) } diff --git a/crates/svg/src/tree/tests.rs b/crates/svg/src/tree/tests.rs index 99b0c1cab..35cb13a2e 100644 --- a/crates/svg/src/tree/tests.rs +++ b/crates/svg/src/tree/tests.rs @@ -666,3 +666,22 @@ fn rewrite_root_viewport_refusals() { assert_eq!(rewrite_root_viewport(sized, one, &limits, fits.len()).unwrap(), fits); assert!(matches!(rewrite_root_viewport(sized, one, &limits, fits.len() - 1), Err(Error::OutputTooLarge(n)) if n == fits.len() - 1)); } + +#[test] +fn rewrite_root_viewport_output_stays_within_the_limits() { + let limits = shallow(); + let one = Viewport { width: 1.0, height: 1.0 }; + let mut refused_at_the_limit = false; + for n in 0..limits.depth { + let source = svg(&groups(n)); + if preflight(&source, &limits).is_err() { + break; + } + match rewrite_root_viewport(&source, one, &limits, 1 << 20) { + Ok(out) => assert!(parse_text(&out, &limits, &options()).is_ok(), "{n} groups"), + Err(Error::TooDeep(_)) => refused_at_the_limit = true, + Err(e) => panic!("{n} groups: {e:?}"), + } + } + assert!(refused_at_the_limit, "a source at the depth limit is refused, not wrapped past it"); +}