From 752daed44b447bd9c9b82db8313b2ea135e97b26 Mon Sep 17 00:00:00 2001 From: Don Omondi <282319807+mockydevs@users.noreply.github.com> Date: Thu, 29 Oct 2026 20:08:49 +0300 Subject: [PATCH] fix(io): nested SVG images share one XML depth budget when bounded The bounded parser for embedded SVG checked each nested SVG image against MAX_XML_DEPTH on its own. Nested images are parsed on top of their parent's recursion, so 32 levels of images could reach the XML parser about 16,000 elements deep, past what its stack holds. Pass the depth the enclosing drawings leave down to each nested image, as main now does for the ordinary parser. --- crates/io/src/svg.rs | 29 ++++++++++++++++++++--------- crates/io/src/svg/tests.rs | 12 ++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/crates/io/src/svg.rs b/crates/io/src/svg.rs index a591ba59a7..2c724d5b00 100644 --- a/crates/io/src/svg.rs +++ b/crates/io/src/svg.rs @@ -490,7 +490,14 @@ fn webp_frame_sizes(chunks: &[u8], dimensions: &mut (usize, usize), nested: bool /// budget up to [`MAX_EMBEDDED_SVG_DEPTH`], and PNG/JPEG/GIF/WebP images are charged their /// largest declared frame before decoding. Anything else, or any overrun, records an error and /// resolves to no image so the enclosing parse fails. -fn bounded_data_image(mime: &str, data: Arc>, options: &usvg::Options, budget: &Arc>, depth: usize) -> Option { +fn bounded_data_image( + mime: &str, + data: Arc>, + options: &usvg::Options, + budget: &Arc>, + depth: usize, + xml_left: usize, +) -> Option { if budget.lock().unwrap_or_else(std::sync::PoisonError::into_inner).error.is_some() { return None; } @@ -500,7 +507,7 @@ fn bounded_data_image(mime: &str, data: Arc>, options: &usvg::Options, b image_error(budget, "embedded SVG images exceed the nesting limit"); return None; } - match parse_image_bounded(&data, budget.clone(), depth + 1) { + match parse_image_bounded(&data, budget.clone(), depth + 1, xml_left) { Ok(tree) => { let mut counter = budget.lock().unwrap_or_else(std::sync::PoisonError::into_inner); counter.svg_images = counter.svg_images.saturating_add(1); @@ -552,8 +559,8 @@ fn bounded_data_image(mime: &str, data: Arc>, options: &usvg::Options, b } /// [`parse_image_bounded_with`] using the shared system font database. -fn parse_image_bounded(bytes: &[u8], budget: Arc>, depth: usize) -> Result { - parse_image_bounded_with(bytes, budget, depth, fonts) +fn parse_image_bounded(bytes: &[u8], budget: Arc>, depth: usize, xml_left: usize) -> Result { + parse_image_bounded_with(bytes, budget, depth, xml_left, fonts) } /// Parse one (possibly gzip-compressed) SVG at nesting `depth` against `budget`: the byte, @@ -564,16 +571,20 @@ fn parse_image_bounded_with( bytes: &[u8], budget: Arc>, depth: usize, + xml_left: usize, fonts: fn() -> Arc, ) -> Result { if u64::try_from(bytes.len()).unwrap_or(u64::MAX) > MAX_BOUNDED_SVG_BYTES { return Err(IoError::Svg("embedded SVG source exceeds the 16 MiB byte limit".into())); } let xml = inflate_with_limit(bytes, MAX_BOUNDED_SVG_BYTES)?; - let xml_depth = nesting_depth(&xml, MAX_XML_DEPTH); - if xml_depth > MAX_XML_DEPTH { + // Nested SVG images are parsed on top of their parent's recursion, so all levels share one + // MAX_XML_DEPTH budget: `xml_left` is what the enclosing drawings left of it. + let xml_depth = nesting_depth(&xml, xml_left); + if xml_depth > xml_left { return Err(IoError::Svg(format!("elements are nested more than {MAX_XML_DEPTH} levels deep"))); } + let xml_left = xml_left - xml_depth; { let mut counter = budget.lock().unwrap_or_else(std::sync::PoisonError::into_inner); counter.xml_bytes = counter @@ -586,9 +597,9 @@ fn parse_image_bounded_with( if has_text && fontdb.faces().next().is_none() { budget.lock().unwrap_or_else(std::sync::PoisonError::into_inner).fontless_text = true; } - let mut opt = options(fontdb, MAX_XML_DEPTH - xml_depth); + let mut opt = options(fontdb, xml_left); let shared = budget.clone(); - opt.image_href_resolver.resolve_data = Box::new(move |mime, data, options| bounded_data_image(mime, data, options, &shared, depth)); + opt.image_href_resolver.resolve_data = Box::new(move |mime, data, options| bounded_data_image(mime, data, options, &shared, depth, xml_left)); // The bounded inflate above is the only decompression step. from_data would detect a // nested gzip payload and inflate it again without the source-byte limit. let text = std::str::from_utf8(&xml).map_err(|_| IoError::Svg("the SVG is not valid UTF-8".into()))?; @@ -691,7 +702,7 @@ fn parse_bounded_work_with(bytes: &[u8], max_pixels: u64, fonts: fn() -> Arc".repeat(levels), "".repeat(levels)); + let child = image_svg(&nest("", 300)); + let fits = image_svg(&nest(&embedded_image("image/svg+xml", child.as_bytes()), 100)); + assert!(rasterize_bounded(fits.as_bytes(), 1.0, 1_000_000).is_ok(), "400 levels in all fit the budget"); + let deep = image_svg(&nest(&embedded_image("image/svg+xml", child.as_bytes()), 300)); + assert!(rasterize_bounded(deep.as_bytes(), 1.0, 1_000_000).is_err(), "600 levels in all must not reach the parser"); +}