From 209a40d66c41eafdb07c9a2b7886f786c9087bc6 Mon Sep 17 00:00:00 2001
From: FredApps <153173854+FredApps@users.noreply.github.com>
Date: Mon, 16 Nov 2026 17:45:10 +0100
Subject: [PATCH] craft-svg: floor declared() name window to a character
boundary (#1899)
* svg: floor declared() name window to a character boundary
If the MAX_NAME+1 cut split a UTF-8 character, get(..bound).unwrap_or(rest)
fell back to scanning the entire suffix. Floor the window instead, and
when recording an overlong name extend to the next character boundary so
the key is still longer than MAX_NAME.
* test(svg): assert declared() keys stay bounded after a multibyte cut
The overlong-name refusal alone also passed on the old unwrap_or(rest)
fallback, which recorded the whole suffix. Bound recorded keys to
MAX_NAME+4 so the test fails if the suffix is stored again.
---
crates/svg/src/guard.rs | 57 ++++++++++++++++++++++++++++++++++++++---
1 file changed, 53 insertions(+), 4 deletions(-)
diff --git a/crates/svg/src/guard.rs b/crates/svg/src/guard.rs
index 9601b2a13..0be356820 100644
--- a/crates/svg/src/guard.rs
+++ b/crates/svg/src/guard.rs
@@ -20,6 +20,30 @@ const MAX_NESTING: u32 = 16;
/// The longest entity name a reference is looked for with ([`references`]).
const MAX_NAME: usize = 256;
+/// The longest prefix of `s` of at most `max` bytes that ends on a character boundary.
+/// Unlike `s.get(..s.len().min(max)).unwrap_or(s)`, a mid-character cut does not
+/// fall back to the rest of the string.
+fn bounded_prefix(s: &str, max: usize) -> &str {
+ let mut end = s.len().min(max);
+ while end > 0 && !s.is_char_boundary(end) {
+ end -= 1;
+ }
+ &s[..end]
+}
+
+/// A char-bounded prefix of `rest` longer than [`MAX_NAME`], used when the bounded
+/// window found no whitespace and `rest` itself is overlong.
+fn overlong_name_key<'a>(rest: &'a str, window: &'a str) -> &'a str {
+ if window.len() > MAX_NAME {
+ return window;
+ }
+ let mut end = (MAX_NAME + 1).min(rest.len());
+ while end < rest.len() && !rest.is_char_boundary(end) {
+ end += 1;
+ }
+ rest.get(..end).filter(|key| key.len() > MAX_NAME).unwrap_or(window)
+}
+
/// The internal general entities declared in `svg`: name → every value declared for it. Text that
/// 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
@@ -52,8 +76,9 @@ fn declared(svg: &str) -> HashMap<&str, Vec<&str>> {
continue;
}
// Bound the name scan: a comment full of ` HashMap<&str, Vec<&str>> {
}
} 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("");
+ // recorded so `entities_expand_past` can refuse the document. If the
+ // MAX_NAME+1 cut split a character, extend to the next boundary so the
+ // recorded key is still longer than MAX_NAME.
+ map.entry(overlong_name_key(rest, window)).or_default().push("");
}
i = at + 1;
continue;
@@ -441,6 +468,28 @@ mod tests {
assert!(!entities_expand_past(&svg, MAX_ENTITY_EXPANSION));
}
+ #[test]
+ fn a_multibyte_cut_does_not_scan_the_rest_of_the_suffix() {
+ // The MAX_NAME+1 window ends in the middle of `é`. Falling back to the whole
+ // suffix would walk every following `x` looking for whitespace. Inside an
+ // entity value the marker is still counted (same conservative rule as other
+ // declarations in values), and because the name is overlong the document is
+ // refused — but the scan stays linear.
+ let mid = format!("{}é{}", "x".repeat(MAX_NAME), "x".repeat(200_000));
+ let in_value = format!("]>");
+ assert!(entities_expand_past(&in_value, MAX_ENTITY_EXPANSION));
+ // The recorded overlong key must stay a short prefix, not the whole suffix
+ // the old unwrap_or(rest) fallback would have stored.
+ assert!(declared(&in_value).keys().all(|name| name.len() <= MAX_NAME + 4));
+ // A short multibyte name is still read as a normal declaration.
+ let short = "]>