Repository navigation
Make max parsing depth configurable. - #1223
Merged
Merged
Conversation
LaurenzV
reviewed
Nov 4, 2026
LaurenzV
left a comment
Collaborator
There was a problem hiding this comment.
Thanks! LGTM once CI is fixed, but in addition:
We probably should pass the same limit here:
resvg/crates/usvg/src/parser/mod.rs
Line 125 in 4cc963b
Would also be good to add a test in usvg/tests/parser:
fn parse_with_max_depth(svg: &str, max_depth: u32) -> Result<usvg::Tree, usvg::Error> {
let opt = usvg::Options {
max_depth,
..usvg::Options::default()
};
usvg::Tree::from_str(svg, &opt)
}
#[test]
fn max_depth_limits_nesting() {
let svg = "<svg xmlns='http://www.w3.org/2000/svg'>\
<g><g><rect width='1' height='1'/></g></g>\
</svg>";
assert!(parse_with_max_depth(svg, 3).is_ok());
assert!(matches!(
parse_with_max_depth(svg, 2),
Err(usvg::Error::NodesLimitReached)
));
}
#[test]
fn max_depth_counts_use_instances() {
let without_use = "<svg xmlns='http://www.w3.org/2000/svg'>\
<defs><rect id='r' width='1' height='1'/></defs>\
</svg>";
let with_use = "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>\
<defs><rect id='r' width='1' height='1'/></defs>\
<use xlink:href='#r'/>\
</svg>";
assert!(parse_with_max_depth(without_use, 2).is_ok());
assert!(matches!(
parse_with_max_depth(with_use, 2),
Err(usvg::Error::NodesLimitReached)
));
}
#[test]
fn max_depth_applies_to_nested_svg_images() {
let svg = "<svg xmlns='http://www.w3.org/2000/svg'>\
<image width='1' height='1' href=\"data:image/svg+xml,\
%3Csvg xmlns='http://www.w3.org/2000/svg' width='1' height='1'%3E\
%3Cg%3E%3Crect width='1' height='1'/%3E%3C/g%3E%3C/svg%3E\"/>\
</svg>";
assert!(parse_with_max_depth(svg, 2).unwrap().root().has_children());
assert!(!parse_with_max_depth(svg, 1).unwrap().root().has_children());
}
Collaborator
|
I'll probably cut a new release later today or tomorrow, so if you need this and don't have the time to apply the fixes, let me know and I can do it quickly. 😄 |
Contributor
Author
|
Thanks for writing those tests! I fixed the compile errors. |
Collaborator
|
|
Signed-off-by: Josh Matthews <[email protected]>
LaurenzV
approved these changes
Nov 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Servo hits stack overflows with extreme nesting at depths lower than 1024. With the limit lowered, we're able to handle those cases without taking down the browser.