diff --git a/crates/svg/src/guard.rs b/crates/svg/src/guard.rs index 073bfe34c..f3573e443 100644 --- a/crates/svg/src/guard.rs +++ b/crates/svg/src/guard.rs @@ -178,6 +178,10 @@ pub fn too_wide(svg: &str, most: usize, most_work: usize) -> bool { pub struct Prescan<'s> { /// Whether the elements nest deeper than the cap. The pass stops there. pub too_deep: bool, + /// Deepest element nesting observed (markup entities count ten times). + pub depth: usize, + /// Deepest stack of unclosed source elements, excluding self-closing leaves. + pub open_depth: usize, /// The text with each data URL in an element's attribute values left out: a `data:` value's /// quotes kept, a `url(…)`'s parentheses and quotes. Borrowed when there is none, and the /// whole text when `too_deep`. An embedded picture is a data URL and most of the text when @@ -250,7 +254,7 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> { (b.len(), false) }; // (now, deepest) of the elements, and of the markup inside the document type. - let (mut elements, mut entities) = ((0usize, 0usize), (0usize, 0usize)); + let (mut elements, mut entities, mut open_depth) = ((0usize, 0usize), (0usize, 0usize), 0usize); let mut doctype_end = 0; let mut i = 0; while let Some(at) = b.get(i..).and_then(|rest| rest.iter().position(|c| *c == b'<')).map(|at| i + at) { @@ -287,8 +291,14 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> { end }; if elements.1.saturating_add(entities.1.saturating_mul(10)) > most { - return Prescan { too_deep: true, markers: Cow::Borrowed(svg) }; + return Prescan { + too_deep: true, + depth: elements.1.saturating_add(entities.1.saturating_mul(10)), + open_depth, + markers: Cow::Borrowed(svg), + }; } + open_depth = open_depth.max(elements.0); } let markers = if data_values.is_empty() { Cow::Borrowed(svg) @@ -303,7 +313,7 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> { text.push_str(svg.get(from..).unwrap_or_default()); Cow::Owned(text) }; - Prescan { too_deep: false, markers } + Prescan { too_deep: false, depth: elements.1.saturating_add(entities.1.saturating_mul(10)), open_depth, markers } } #[cfg(test)] diff --git a/crates/svg/src/preflight.rs b/crates/svg/src/preflight.rs index 3ab381ff3..0c2d3a7b1 100644 --- a/crates/svg/src/preflight.rs +++ b/crates/svg/src/preflight.rs @@ -71,6 +71,12 @@ pub fn text<'a>(bytes: &'a [u8], limits: &Limits) -> Result, Error> /// Parses already-decoded SVG text and checks it against `limits`. pub fn parse_text<'a>(svg: &'a str, limits: &Limits) -> Result, Error> { + parse_text_with_depth(svg, limits).map(|(xml, _, _)| xml) +} + +/// [`parse_text`], with the source nesting: the deepest element nesting (markup entities count +/// ten times) and the deepest stack of open elements (a self-closing leaf opens none). +pub fn parse_text_with_depth<'a>(svg: &'a str, limits: &Limits) -> Result<(roxmltree::Document<'a>, usize, usize), Error> { if svg.len() > limits.bytes { return Err(Error::Text(crate::svgz::Error::TooLarge(limits.bytes))); } @@ -83,9 +89,38 @@ pub fn parse_text<'a>(svg: &'a str, limits: &Limits) -> Result Result<(), Error> { + let limits = Limits { depth: 4, ..Default::default() }; + let (document, depth, open_depth) = parse_text_with_depth("", &limits)?; + assert!(document.root_element().has_tag_name("svg")); + // Every element is a level (svg, g, path); a self-closing leaf opens none (svg, g). + assert_eq!(depth, 3); + assert_eq!(open_depth, 2); + let simple = parse_text("", &limits)?; + assert!(simple.root_element().has_tag_name("svg")); + Ok(()) + } + + #[test] + fn source_depth_and_xml_errors_keep_their_specific_kind() { + let shallow = Limits { depth: 1, ..Default::default() }; + assert!(matches!(parse_text_with_depth("", &shallow), Err(Error::TooDeep(1)))); + + let normal = Limits::default(); + assert!(matches!(parse_text_with_depth("", &normal), Err(Error::Xml(_)))); + } } diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index cb33137b8..9cf85c3bd 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -112,13 +112,19 @@ pub enum Error { /// usvg's own error. #[error("invalid SVG: {0}")] Parse(String), + /// The root viewport is missing or has dimensions this SVG helper cannot preserve. + #[error("invalid SVG root viewport: {0}")] + Viewport(String), + /// The rewritten SVG would exceed the caller's output byte budget. + #[error("rewritten SVG exceeds {0} bytes")] + OutputTooLarge(usize), } /// The SVG text of the bytes of an SVG or SVGZ file, checked against `limits`: what [`parse`] /// hands usvg. For a caller that edits the text before it parses ([`parse_text`]). pub fn text<'a>(bytes: &'a [u8], limits: &Limits) -> Result, Error> { let text = svgz::text(bytes, limits.bytes)?; - checked(&text, limits)?; + preflight(&text, limits)?; Ok(text) } @@ -130,8 +136,7 @@ pub fn parse(bytes: &[u8], limits: &Limits, options: &usvg::Options<'_>) -> Resu /// [`parse`] for SVG text the caller already holds. pub fn parse_text(svg: &str, limits: &Limits, options: &usvg::Options<'_>) -> Result { - let xml = checked(svg, limits)?; - usvg::Tree::from_xmltree(&xml, options).map_err(|e| Error::Parse(e.to_string())) + preflight(svg, limits)?.parse(options) } /// Read the root SVG's unitless width and height without constructing a rendering tree. @@ -139,8 +144,8 @@ pub fn parse_text(svg: &str, limits: &Limits, options: &usvg::Options<'_>) -> Re /// Lengths with units, percentages, missing dimensions, and non-SVG roots return `Ok(None)`; /// malformed or over-limit XML returns the same bounded parsing error as [`parse_text`]. pub fn root_unitless_size(svg: &str, limits: &Limits) -> Result, Error> { - let xml = checked(svg, limits)?; - let root = xml.root_element(); + let checked = preflight(svg, limits)?; + let root = checked.xml.root_element(); if root.tag_name().name() != "svg" { return Ok(None); } @@ -152,8 +157,30 @@ pub fn root_unitless_size(svg: &str, limits: &Limits) -> Result(svg: &'s str, limits: &Limits) -> Result, Error> { +/// SVG text after shared byte, entity, attribute, XML and expansion-depth checks. +/// Keep it opaque so consumers use the same checked tree and don't reparse XML locally. +pub struct Preflight<'s> { + xml: roxmltree::Document<'s>, + depth: usize, + /// [`height`]: the nesting usvg converts, references followed. + height: usize, +} + +impl Preflight<'_> { + /// The deepest stack of open source elements, before references are followed; a + /// self-closing leaf opens no level (`` is 2). + pub fn depth(&self) -> usize { + self.depth + } + + /// Converts this already-checked XML into the configured usvg tree. + pub fn parse(self, options: &usvg::Options<'_>) -> Result { + usvg::Tree::from_xmltree(&self.xml, options).map_err(|e| Error::Parse(e.to_string())) + } +} + +/// Checks already-decoded SVG text once, retaining the parsed XML for both depth policy and tree conversion. +pub fn preflight<'s>(svg: &'s str, limits: &Limits) -> Result, Error> { let preflight_limits = crate::preflight::Limits { bytes: limits.bytes, depth: limits.depth, @@ -163,7 +190,7 @@ fn checked<'s>(svg: &'s str, limits: &Limits) -> Result, attribute_work: limits.attribute_work, xml_nodes: limits.xml_nodes, }; - let xml = crate::preflight::parse_text(svg, &preflight_limits).map_err(|e| match e { + let (xml, _, depth) = crate::preflight::parse_text_with_depth(svg, &preflight_limits).map_err(|e| match e { crate::preflight::Error::Text(e) => Error::Text(e), crate::preflight::Error::Entities => Error::Entities, crate::preflight::Error::EntityMarkup => Error::EntityMarkup, @@ -171,10 +198,69 @@ fn checked<'s>(svg: &'s str, limits: &Limits) -> Result, crate::preflight::Error::TooWide(attributes) => Error::TooWide(attributes), crate::preflight::Error::Xml(msg) => Error::Xml(msg), })?; - if height(&xml, limits.tree_depth)?.is_none() { + let Some(height) = height(&xml, limits.tree_depth)? else { return Err(Error::TooDeep(limits.tree_depth)); + }; + Ok(Preflight { xml, depth, height }) +} + +/// Dimensions of an SVG viewport in SVG user units. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Viewport { + pub width: f64, + pub height: f64, +} + +/// 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, 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())); } - Ok(xml) + let checked = preflight(svg, limits)?; + let root = checked.xml.root_element(); + if root.tag_name().name() != "svg" { + return Err(Error::Viewport("root element is not svg".into())); + } + let root_dimension = |key: &str| { + let value = root.attribute(key)?; + let value = value.trim(); + let (number, scale) = match value.strip_suffix("pt") { + Some(points) => (points, 4.0 / 3.0), + None => (value.strip_suffix("px").unwrap_or(value), 1.0), + }; + number.parse::().ok().map(|n| n * scale).filter(|n| n.is_finite() && *n > 0.0) + }; + let (width, height) = root_dimension("width") + .zip(root_dimension("height")) + .ok_or_else(|| Error::Viewport("source has no finite pixel or point dimensions".into()))?; + let range = root.range(); + let preamble = svg.get(..range.start).ok_or_else(|| Error::Viewport("invalid XML preamble range".into()))?; + let inner = svg.get(range).ok_or_else(|| Error::Viewport("invalid root element range".into()))?; + let prefix = format!( + r#""#, + target.width, target.height, width, height + ); + let len = preamble + .len() + .checked_add(prefix.len()) + .and_then(|n| n.checked_add(inner.len())) + .and_then(|n| n.checked_add(6)) + .ok_or(Error::OutputTooLarge(output_limit))?; + if len > output_limit { + return Err(Error::OutputTooLarge(output_limit)); + } + let mut output = String::new(); + output.try_reserve_exact(len).map_err(|_| Error::OutputTooLarge(output_limit))?; + output.push_str(preamble); + 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) } /// Properties whose value an element's descendants use too: the paint server they name is @@ -376,8 +462,8 @@ impl ImageResolver { /// Nested SVGs are checked automatically. The scan shares the normal XML limits and /// streaming-validates URL encoding without allocating another decoded image buffer. pub fn check_document(&self, svg: &str) -> Result<(), Error> { - let xml = checked(svg, &self.limits)?; - account_invalid_data_urls(&xml, &self.used); + let checked = preflight(svg, &self.limits)?; + account_invalid_data_urls(&checked.xml, &self.used); Ok(()) } @@ -520,8 +606,8 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' return None; } used.bytes.fetch_add(text.len(), Ordering::Relaxed); - let xml = match checked(&text, limits) { - Ok(xml) => xml, + let checked = match preflight(&text, limits) { + Ok(checked) => checked, // Decoding UTF-16 or Latin-1 can exceed the text cap after the // raw/inflated bytes fitted. This is a resource refusal, not invalid XML. Err(Error::Text(svgz::Error::TooLarge(_))) => { @@ -538,10 +624,8 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' return None; } }; - let Some(xml_depth) = height(&xml, limits.tree_depth).ok().flatten() else { - used.svg_limit.fetch_add(1, Ordering::Relaxed); - return None; - }; + // The nesting usvg converts, references followed, not the source depth alone. + let xml_depth = checked.height; let total_depth = used.xml_depth.load(Ordering::Relaxed).checked_add(xml_depth); let Some(total_depth) = total_depth.filter(|depth| *depth <= limits.tree_depth) else { used.svg_limit.fetch_add(1, Ordering::Relaxed); @@ -550,8 +634,8 @@ fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<' let previous_depth = used.xml_depth.swap(total_depth, Ordering::Relaxed); let depth = used.depth.fetch_add(1, Ordering::Relaxed).saturating_add(1); used.svg_depth.fetch_max(depth, Ordering::Relaxed); - account_invalid_data_urls(&xml, used); - let tree = usvg::Tree::from_xmltree(&xml, options); + account_invalid_data_urls(&checked.xml, used); + let tree = checked.parse(options); used.xml_depth.store(previous_depth, Ordering::Relaxed); used.depth.fetch_sub(1, Ordering::Relaxed); match tree { diff --git a/crates/svg/src/tree/tests.rs b/crates/svg/src/tree/tests.rs index 103c0fe7c..35cb13a2e 100644 --- a/crates/svg/src/tree/tests.rs +++ b/crates/svg/src/tree/tests.rs @@ -292,7 +292,7 @@ mod resource_limit_usage { assert_eq!(svgz::inflate(&data, cap).unwrap().len(), cap, "raw bytes fit exactly"); assert_eq!(svgz::text(&data, cap).unwrap().len(), decoded.len(), "decoding itself succeeds"); let limits = Limits { bytes: cap, embedded_bytes: decoded.len(), ..shallow() }; - assert!(matches!(checked(&decoded, &limits), Err(Error::Text(svgz::Error::TooLarge(n))) if n == cap)); + assert!(matches!(preflight(&decoded, &limits), Err(Error::Text(svgz::Error::TooLarge(n))) if n == cap)); let image = data_image(&data); let (found, usage) = resolved(&svg(&image), limits, 1); assert!(found.is_empty(), "{encoding}, gzip {gzip}"); @@ -312,7 +312,7 @@ mod resource_limit_usage { let decoded = svgz::decode(&raw).unwrap(); let limits = Limits { bytes: decoded.len(), embedded_bytes: raw.len(), ..shallow() }; assert!(decoded.len() > limits.embedded_bytes); - checked(&decoded, &limits).unwrap(); // The independent per-image cap allows this complete XML. + preflight(&decoded, &limits).unwrap(); // The independent per-image cap allows this complete XML. for gzip in [false, true] { let data = if gzip { svgz::compress(&raw) } else { raw.clone() }; assert_eq!(svgz::inflate(&data, limits.embedded_bytes).unwrap().len(), raw.len()); @@ -584,3 +584,104 @@ mod invalid_image_accounting { assert_eq!(report.raster_limit_images, 1); } } + +#[test] +fn preflight_keeps_source_depth_and_parses_the_checked_tree() { + let text = svg(""); + let checked = preflight(&text, &Limits::default()).unwrap(); + // svg and g are open; the self-closing rect opens no level. + assert_eq!(checked.depth(), 2); + let tree = checked.parse(&options()).unwrap(); + assert_eq!(tree.size().width(), 10.0); + assert!(matches!(preflight(&svg(&groups(30)), &shallow()), Err(Error::TooDeep(24)))); +} + +#[test] +fn preflight_matches_parse_text_refusals() { + for text in [""] { + assert!(matches!(preflight(text, &Limits::default()), Err(Error::Xml(_)))); + assert!(matches!(parse_text(text, &Limits::default(), &options()), Err(Error::Xml(_)))); + } +} + +#[test] +fn rewrite_root_viewport_wraps_the_root_and_keeps_the_preamble() { + let source = concat!( + "\n]>\n", + "", + "" + ); + let out = rewrite_root_viewport(source, Viewport { width: 40.0, height: 30.0 }, &Limits::default(), 4096).unwrap(); + assert!(out.starts_with("\n]>\n")); + let tree = parse_text(&out, &Limits::default(), &options()).unwrap(); + assert_eq!((tree.size().width(), tree.size().height()), (40.0, 30.0)); +} + +#[test] +fn rewrite_root_viewport_reads_points_and_pixels() { + let points = rewrite_root_viewport( + "", + Viewport { width: 1.0, height: 1.0 }, + &Limits::default(), + 4096, + ) + .unwrap(); + assert!(points.contains("viewBox=\"0 0 96 48\"")); + let pixels = rewrite_root_viewport( + "", + Viewport { width: 1.0, height: 1.0 }, + &Limits::default(), + 4096, + ) + .unwrap(); + assert!(pixels.contains("viewBox=\"0 0 12 8\"")); +} + +#[test] +fn rewrite_root_viewport_refusals() { + let limits = Limits::default(); + let one = Viewport { width: 1.0, height: 1.0 }; + let sized = ""; + for target in [ + Viewport { width: f64::NAN, height: 1.0 }, + Viewport { width: 1.0, height: f64::INFINITY }, + Viewport { width: 0.0, height: 1.0 }, + Viewport { width: 1.0, height: -2.0 }, + ] { + assert!(matches!(rewrite_root_viewport(sized, target, &limits, 4096), Err(Error::Viewport(_)))); + } + assert!(matches!(rewrite_root_viewport("", one, &limits, 4096), Err(Error::Viewport(_)))); + for source in [ + "", + "", + "", + "", + "", + ] { + assert!(matches!(rewrite_root_viewport(source, one, &limits, 4096), Err(Error::Viewport(_))), "{source}"); + } + assert!(matches!(rewrite_root_viewport(" 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"); +}