Merge pull request #1878 from storytold/onee/778-svg-guard-path

craft-svg: bound entity scans and reset smooth controls after Z
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 15:13:24 +01:00
commit c7a872e2f2
2 files changed
+71 -13

No files matched your search

+58 -13
View File
@@ -21,22 +21,56 @@ const MAX_NESTING: u32 = 16;
const MAX_NAME: usize = 256;
/// The internal general entities declared in `svg`: name → every value declared for it. Text that
/// only looks like a declaration (in a comment, or in another entity's value) is kept too: the
/// largest value counts, so it can only make the estimate larger.
/// only looks like a declaration in another entity's value is kept too: the largest value counts,
/// so it can only make the estimate larger. Comments and CDATA are skipped, so markers there are
/// not treated as declarations.
fn declared(svg: &str) -> HashMap<&str, Vec<&str>> {
let mut map: HashMap<&str, Vec<&str>> = HashMap::new();
for (at, _) in svg.match_indices("<!ENTITY") {
let rest = svg.get(at + "<!ENTITY".len()..).unwrap_or("").trim_start();
// A parameter entity (`%`) isn't referenced as `&name;`.
if rest.starts_with('%') {
let mut i = 0;
while let Some(rel) = svg.get(i..).and_then(|s| s.find('<')) {
let at = i + rel;
let after = svg.get(at + 1..).unwrap_or("");
if let Some(body) = after.strip_prefix("!--") {
i = match body.find("-->") {
Some(n) => at + 1 + 3 + n + 3,
None => svg.len(),
};
continue;
}
let Some(end) = rest.find(char::is_whitespace) else { continue };
let (name, rest) = rest.split_at(end);
let rest = rest.trim_start();
let Some(quote) = rest.chars().next().filter(|q| matches!(q, '"' | '\'')) else { continue };
let Some(value) = rest.get(1..).and_then(|r| r.split_once(quote)).map(|(v, _)| v) else { continue };
map.entry(name).or_default().push(value);
if let Some(body) = after.strip_prefix("![CDATA[") {
i = match body.find("]]>") {
Some(n) => at + 1 + 8 + n + 3,
None => svg.len(),
};
continue;
}
if let Some(rest) = after.strip_prefix("!ENTITY") {
let rest = rest.trim_start();
// A parameter entity (`%`) isn't referenced as `&name;`.
if rest.starts_with('%') {
i = at + 1;
continue;
}
// Bound the name scan: a comment full of `<!ENTITYx` with no spaces must not
// search the rest of the file for whitespace after every marker.
let window = rest.get(..rest.len().min(MAX_NAME + 1)).unwrap_or(rest);
if let Some(end) = window.find(char::is_whitespace) {
let (name, rest) = rest.split_at(end);
let rest = rest.trim_start();
if let Some(quote) = rest.chars().next().filter(|q| matches!(q, '"' | '\''))
&& let Some(value) = rest.get(1..).and_then(|r| r.split_once(quote)).map(|(v, _)| v)
{
map.entry(name).or_default().push(value);
}
} else if rest.len() > MAX_NAME {
// A name longer than MAX_NAME cannot be referenced, but it must still be
// recorded so `entities_expand_past` can refuse the document.
map.entry(window).or_default().push("");
}
i = at + 1;
continue;
}
i = at + 1;
}
map
}
@@ -99,7 +133,8 @@ pub fn entities_expand_past(svg: &str, most: usize) -> bool {
/// an entity becomes elements where it is referenced, so what the text shows of the tree is not
/// what a reader builds; drawing apps declare entities for namespace names only. A caller may
/// refuse these outright instead of counting them ([`prescan`] counts them ten times). Text that
/// only looks like a declaration (in a comment) counts too. `&#60;` is text, not markup.
/// only looks like a declaration in another entity's value counts too; comments are skipped.
/// `&#60;` is text, not markup.
pub fn entity_holds_markup(svg: &str) -> bool {
declared(svg).values().flatten().any(|value| value.contains('<'))
}
@@ -396,6 +431,16 @@ mod tests {
assert!(expansion_too_large(&long_name));
}
#[test]
fn entity_markers_in_a_comment_are_scanned_in_linear_time() {
// 100,000 `<!ENTITYx` markers with no whitespace: the old scan searched the rest of
// the file after every marker. Skipping comments and bounding the name makes this
// linear, and none of the markers is a declaration.
let markers = "<!ENTITYx".repeat(100_000);
let svg = format!("<svg><!--{markers}--></svg>");
assert!(!entities_expand_past(&svg, MAX_ENTITY_EXPANSION));
}
fn svg(body: &str) -> String {
format!(r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10" height="10">{body}</svg>"#)
}
+13
View File
@@ -214,6 +214,10 @@ pub fn parse_path(d: &str) -> Result<Vec<Subpath>> {
}
cur = start;
cmd = 0;
// Z is not a cubic or quadratic, so a following S or T starts from the
// current point rather than reflecting the pre-close control.
prev_cubic = None;
prev_quad = None;
continue;
}
cmd = c;
@@ -434,6 +438,15 @@ mod tests {
assert_eq!(paths[0].segs[1], Segment::Cubic(Point::new(8.0, -2.0), Point::new(10.0, -2.0), Point::new(12.0, 0.0)));
}
#[test]
fn close_resets_smooth_control_history() {
// After Z, S must not reflect the preceding C's control (20,20) → (0,0).
let paths = parse_path("M10 10 C10 20 20 20 20 10 Z S30 0 30 10").unwrap();
assert_eq!(paths.len(), 2);
assert!(paths[0].closed);
assert_eq!(paths[1].segs[0], Segment::Cubic(Point::new(10.0, 10.0), Point::new(30.0, 0.0), Point::new(30.0, 10.0)));
}
#[test]
fn arc_centers_keep_radius_correction_and_sweep() {
let (center, delta) = arc_center_circular(Point::new(0.0, 0.0), Point::new(10.0, 0.0), 2.0, false, true).unwrap();