Preserve the exact embedded SVG depth boundary

This commit is contained in:
Terra854
2026-11-17 20:09:30 +08:00
parent 8af8f407af
commit fcafab33d9
3 changed files with 32 additions and 6 deletions
+4 -2
View File
@@ -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
+4 -3
View File
@@ -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)
}
+24 -1
View File
@@ -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("<g><rect width='4' height='4' fill='black'/></g>"); // 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");
}
}