From fcafab33d9fedbefb0992d7776db3b6ae8b918f6 Mon Sep 17 00:00:00 2001 From: Terra854 Date: Tue, 17 Nov 2026 20:09:30 +0800 Subject: [PATCH] Preserve the exact embedded SVG depth boundary --- crates/svg/README.md | 6 ++++-- crates/svg/src/tree/validated.rs | 7 +++--- crates/svg/src/tree/validated/xml_tests.rs | 25 +++++++++++++++++++++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/crates/svg/README.md b/crates/svg/README.md index 0810b1217d..519015c1b2 100644 --- a/crates/svg/README.md +++ b/crates/svg/README.md @@ -46,8 +46,10 @@ The caller supplies fonts and a sufficient stack/worker for its depth policy. `parse_preflight` accepts only an opaque preflight made with exactly this context's limits. It consumes options and installs its own data-only resolver, including for all children; unrelated callbacks cannot fetch a linked file or bypass admission. -The full expanded root height and each child's expanded height share one depth -allowance. An overlapping root conversion on the same context is refused. +Expanded root ancestor frames (checked height minus the callback leaf) and each +child's complete expanded height share one depth allowance. The callback leaf is +not counted twice; source nesting alone cannot hide expanded reference chains. +An overlapping root conversion on the same context is refused. Unchanged XML converts from its original checked DOM. A modified data URL requires one checked replacement DOM. Invalid URLs become bounded resolver-visible image diff --git a/crates/svg/src/tree/validated.rs b/crates/svg/src/tree/validated.rs index cb1b6e8c30..658b2641f1 100644 --- a/crates/svg/src/tree/validated.rs +++ b/crates/svg/src/tree/validated.rs @@ -191,9 +191,10 @@ impl ValidatedImageResolver { if checked.limits != self.limits { return Err(Error::Parse("checked SVG limits differ from the validated image resolver".into())); } - // A shallow source can expand through use/paint references. Reserve the - // full checked converted height before any child callback, not source depth. - let _scope = self.root_scope(checked.height)?; + // A shallow source can expand through use/paint references. Reserve its + // expanded ancestor frames, excluding the callback leaf itself. The child + // contributes its complete checked height, as in legacy child admission. + let _scope = self.root_scope(checked.height.saturating_sub(1))?; options.image_href_resolver = self.resolver(); convert_checked(checked, &options) } diff --git a/crates/svg/src/tree/validated/xml_tests.rs b/crates/svg/src/tree/validated/xml_tests.rs index b6b5944336..34e1e604ea 100644 --- a/crates/svg/src/tree/validated/xml_tests.rs +++ b/crates/svg/src/tree/validated/xml_tests.rs @@ -162,7 +162,10 @@ fn expanded_root_references_and_child_share_the_converted_depth_limit() { assert!(preflight(&root, &Limits { tree_depth: 5, ..Default::default() }).is_err()); assert!(preflight(&root, &Limits { tree_depth: 6, ..Default::default() }).is_ok()); assert!(preflight(&child, &Limits { tree_depth: 3, ..Default::default() }).is_ok()); - for (depth, admitted) in [(8, false), (9, true)] { + // Expanded root height6 includes the callback leaf: reserve5 ancestors, + // then admit the child's complete height3. Source depth alone would wrongly + // accept the cap7 case, while reserving the leaf twice would reject cap8. + for (depth, admitted) in [(7, false), (8, true)] { let limits = Limits { tree_depth: depth, ..Default::default() }; let checked = preflight(&root, &limits).unwrap(); assert!(checked.depth() < 6, "source nesting is shallower than expanded references"); @@ -174,3 +177,23 @@ fn expanded_root_references_and_child_share_the_converted_depth_limit() { assert_eq!(usage.invalid_svg_images, 0); } } + +#[test] +fn plain_root_callback_leaf_does_not_spend_the_child_boundary_twice() { + let child = drawing(""); // height3 + let root = drawing(&image(&url(child.as_bytes()))); // height2, one ancestor + for (depth, admitted) in [(3, false), (4, true)] { + let limits = Limits { tree_depth: depth, ..Default::default() }; + assert!(preflight(&root, &limits).is_ok()); + assert!(preflight(&child, &limits).is_ok()); + let context = ValidatedImageResolver::new(limits); + let tree = context.parse_preflight(preflight(&root, &limits).unwrap(), usvg::Options::default()).unwrap(); + assert_eq!(context.usage().embedded_svg_count, usize::from(admitted)); + assert_eq!(context.usage().svg_limit_images, usize::from(!admitted)); + assert_eq!(context.usage().invalid_svg_images, 0); + let pixels = super::super::draw::render(&tree, usvg::Transform::identity(), 4, 4, 0, 1 << 20).unwrap(); + let sample = pixels.pixel(1, 1).unwrap(); + assert_eq!((sample.red(), sample.green(), sample.blue()), (0, 0, 0)); + assert_eq!(sample.alpha(), if admitted { 255 } else { 0 }, "exact-boundary child must actually draw"); + } +}