Fix #3564: report TooManyImages error when nested SVG image cap is reached

This commit is contained in:
el-j
2026-10-29 09:57:33 +07:00
committed by bflatastic
parent b65fb23338
commit 39341ef4c8
3 changed files with 50 additions and 1 deletions
+45 -1
View File
@@ -664,7 +664,7 @@ impl Importer {
return None;
}
if self.nested_svg_images >= crate::MAX_NESTED_SVG_IMAGES {
self.error = Some(SvgError::TooLarge(self.max_nodes));
self.error = Some(SvgError::TooManyImages(crate::MAX_NESTED_SVG_IMAGES));
return None;
}
self.nested_svg_images += 1;
@@ -682,3 +682,47 @@ impl Importer {
art
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::import::files::Files;
use crate::import::text::TextSlots;
use std::collections::{HashMap, HashSet};
use vectorcraft_doc::Document;
#[test]
fn svg_image_cap_sets_too_many_images_error() {
let mut im = Importer {
doc: Document::new(100.0, 100.0),
warnings: vec![],
mask_flags: HashMap::new(),
links: HashMap::new(),
slots: TextSlots::default(),
patterns: HashMap::new(),
midpoints: HashMap::new(),
labels: HashMap::new(),
hidden: HashSet::new(),
uses: HashMap::new(),
symbols: HashMap::new(),
blends: HashMap::new(),
files: Files::default(),
non_scaling: HashSet::new(),
data: HashMap::new(),
events: HashMap::new(),
foreign: HashMap::new(),
sublayers: HashSet::new(),
sublayer_groups: HashSet::new(),
screen: 1.0,
converted_nodes: 0,
max_nodes: 1000,
error: None,
nested_svg_images: crate::MAX_NESTED_SVG_IMAGES,
};
let tree = usvg::Tree::from_str(r#"<svg xmlns="http://www.w3.org/2000/svg"/>"#, &usvg::Options::default()).unwrap();
let res = im.svg_image(&tree, Affine::IDENTITY);
assert!(res.is_none());
assert!(matches!(im.error, Some(SvgError::TooManyImages(crate::MAX_NESTED_SVG_IMAGES))));
assert_eq!(im.error.unwrap().to_string(), "the SVG expands to more than 256 nested SVG images");
}
}
+1
View File
@@ -162,6 +162,7 @@ pub(super) struct PendingText {
}
/// The texts [`prepare`] replaced, by placeholder index.
#[derive(Default)]
pub(super) struct TextSlots {
prefix: String,
pub texts: Vec<PendingText>,
+4
View File
@@ -347,6 +347,10 @@ pub enum SvgError {
/// nested-image expansion bombs, #3564).
#[error("the SVG expands to more than {0} objects")]
TooLarge(usize),
/// The SVG expands to more nested SVG images than the reader takes (a guard against
/// nested-image expansion bombs, #3564).
#[error("the SVG expands to more than {0} nested SVG images")]
TooManyImages(usize),
}
/// Export a document as an SVG string (linked images are not returned: see [`export_full`]).