craft-svg: comment markers in a declaration's literal hide no later declaration

declared() skipped a comment or CDATA section wherever `<!--` or
`<![CDATA[` appeared, including inside an entity's quoted value, another
declaration's literal or a processing instruction. With
`<!ENTITY c "<!--">` before a large entity, the scan skipped to the end
of the file, the large entity was never recorded, and
entities_expand_past let its references through to the XML reader.

- declared() reads a declaration's quoted literals and a processing
  instruction as text: a comment or CDATA marker there starts nothing,
  and the declarations after it are still read.
- prescan() counts the markup in an entity's value the same way, and a
  comment, CDATA section or instruction that closes inside the value
  still opens and closes no element.
- too_wide() and prescan() find the end of the internal subset past
  quoted literals, comments and instructions, so a `]>` in a value no
  longer ends the document type early.
This commit is contained in:
dotCooCoo
2026-11-16 11:23:53 -08:00
parent 3af718585c
commit 01fcf22cb0
2 changed files with 152 additions and 27 deletions
+148 -27
View File
@@ -44,29 +44,96 @@ fn overlong_name_key<'a>(rest: &'a str, window: &'a str) -> &'a str {
rest.get(..end).filter(|key| key.len() > MAX_NAME).unwrap_or(window)
}
/// Past the first `close` in `b` at or after `from`; the end of `b` when there is none.
fn past(b: &[u8], from: usize, close: &[u8]) -> usize {
b.get(from..).and_then(|rest| rest.windows(close.len()).position(|w| w == close)).map_or(b.len(), |n| from + n + close.len())
}
/// The end of the markup declaration that starts at `at` (`<!ENTITY`, `<!ATTLIST`, the start of
/// a `<!DOCTYPE`): past its `>`, or past the `[` that opens a document type's internal subset.
/// Quoted literals are skipped, so a `<!--`, `]>` or `>` inside one ends nothing.
fn declaration_end(b: &[u8], at: usize) -> usize {
let mut i = at + 2;
while let Some(&c) = b.get(i) {
i = match c {
b'"' | b'\'' => past(b, i + 1, &[c]),
b'>' | b'[' => return i + 1,
_ => i + 1,
};
}
b.len()
}
/// The end of the internal subset of a document type that starts at `from` (past its `[`): past
/// the `]` that closes it. Quoted literals, comments and processing instructions are skipped, so
/// a `]` inside one closes nothing.
fn subset_end(b: &[u8], from: usize) -> usize {
let mut i = from;
while let Some(&c) = b.get(i) {
let rest = b.get(i..).unwrap_or_default();
i = match c {
b'"' | b'\'' => past(b, i + 1, &[c]),
b']' => return i + 1,
_ if rest.starts_with(b"<!--") => past(b, i + 4, b"-->"),
_ if rest.starts_with(b"<?") => past(b, i + 2, b"?>"),
_ => i + 1,
};
}
b.len()
}
/// Past the comment, character data or instruction that starts at `at` inside a declaration's
/// literal ending at `end`, when it closes before `end`. `missed` holds, for each kind, the literal
/// end at which its close was last not found, so a literal full of openers is searched once.
fn closed_in_literal(b: &[u8], at: usize, end: usize, missed: &mut [usize; 3]) -> Option<usize> {
const KINDS: [(&[u8], &[u8]); 3] = [(b"<!--", b"-->"), (b"<![CDATA[", b"]]>"), (b"<?", b"?>")];
let rest = b.get(at..end)?;
let (kind, (open, close)) = KINDS.iter().enumerate().find(|(_, (open, _))| rest.starts_with(open))?;
let miss = missed.get_mut(kind)?;
if *miss == end {
return None;
}
let found = rest.get(open.len()..).and_then(|r| r.windows(close.len()).position(|w| w == *close)).map(|n| at + open.len() + n + close.len());
if found.is_none() {
*miss = end;
}
found
}
/// 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
/// not treated as declarations.
/// not treated as declarations. A `<!--` or `<![CDATA[` inside a declaration's quoted literal or
/// a processing instruction starts neither, so the declarations after it are still read.
fn declared(svg: &str) -> HashMap<&str, Vec<&str>> {
let b = svg.as_bytes();
let mut map: HashMap<&str, Vec<&str>> = HashMap::new();
let mut i = 0;
// The end of the declaration or processing instruction the scan is in.
let mut literal_end = 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;
}
if let Some(body) = after.strip_prefix("![CDATA[") {
i = match body.find("]]>") {
Some(n) => at + 1 + 8 + n + 3,
None => svg.len(),
};
continue;
if at >= literal_end {
if let Some(body) = after.strip_prefix("!--") {
i = match body.find("-->") {
Some(n) => at + 1 + 3 + n + 3,
None => svg.len(),
};
continue;
}
if let Some(body) = after.strip_prefix("![CDATA[") {
i = match body.find("]]>") {
Some(n) => at + 1 + 8 + n + 3,
None => svg.len(),
};
continue;
}
if after.starts_with('?') {
literal_end = past(b, at + 2, b"?>");
} else if after.starts_with('!') {
literal_end = declaration_end(b, at);
}
}
if let Some(rest) = after.strip_prefix("!ENTITY") {
let rest = rest.trim_start();
@@ -199,11 +266,10 @@ pub fn too_wide(svg: &str, most: usize, most_work: usize) -> bool {
} else if rest.starts_with(b"<![CDATA[") {
i = find(i + 9, b"]]>");
} else if rest.starts_with(b"<!DOCTYPE") {
// To the `]>` closing its subset (the first `>` when it has none).
let end = match (rest.iter().position(|b| *b == b'['), rest.iter().position(|b| *b == b'>')) {
(Some(o), Some(g)) if o < g => find(i, b"]>"),
_ => find(i, b">"),
};
// To the `>` after the `]` closing its subset (its first `>` when it has none),
// quoted literals, comments and instructions skipped.
let head = declaration_end(data, i);
let end = if data.get(head.wrapping_sub(1)) == Some(&b'[') { find(subset_end(data, head), b">") } else { head };
let quotes = data.get(i..end).unwrap_or(&[]).iter().filter(|b| matches!(**b, b'"' | b'\'')).count();
if over(quotes / 2) {
return true;
@@ -318,24 +384,40 @@ pub fn prescan(svg: &str, most: usize) -> Prescan<'_> {
// (now, deepest) of the elements, and of the markup inside the document type.
let (mut elements, mut entities, mut open_depth) = ((0usize, 0usize), (0usize, 0usize), 0usize);
let mut doctype_end = 0;
// The end of the declaration the scan is in: a `<!--`, `<?` or `<![CDATA[` in its quoted
// literals starts nothing, and the markup there is counted.
let mut literal_end = 0;
let mut missed = [0usize; 3];
let mut i = 0;
while let Some(at) = b.get(i..).and_then(|rest| rest.iter().position(|c| *c == b'<')).map(|at| i + at) {
let rest = b.get(at..).unwrap_or_default();
let depth = if at < doctype_end { &mut entities } else { &mut elements };
i = if rest.starts_with(b"<!--") {
let open = at >= literal_end;
i = if open && rest.starts_with(b"<!--") {
find(at, b"-->").unwrap_or(b.len())
} else if rest.starts_with(b"<![CDATA[") {
} else if open && rest.starts_with(b"<![CDATA[") {
find(at, b"]]>").unwrap_or(b.len())
} else if rest.starts_with(b"<?") {
} else if open && rest.starts_with(b"<?") {
find(at, b"?>").unwrap_or(b.len())
} else if rest.starts_with(b"<!DOCTYPE") {
// Up to the `>` that ends it: after its internal subset (`[` … `]`) when it has one.
let plain = tag_end(at, false).0;
let subset = b.get(at..plain).is_some_and(|head| head.contains(&b'['));
doctype_end = if subset { find(at, b"]>").unwrap_or(b.len()) } else { plain };
} else if let Some(end) = (!open).then(|| closed_in_literal(b, at, literal_end, &mut missed)).flatten() {
// A comment, character data or instruction that closes inside the literal holds no
// element of the entity's markup.
end
} else if open && rest.starts_with(b"<!DOCTYPE") {
// Up to the end of its internal subset (`[` … `]`) when it has one, otherwise its `>`.
let head = declaration_end(b, at);
literal_end = head;
doctype_end = if b.get(head.wrapping_sub(1)) == Some(&b'[') { subset_end(b, head) } else { head };
at + 2
} else if rest.starts_with(b"<!") {
// A declaration of the document type: the markup in its value is counted as it comes.
if open {
literal_end = declaration_end(b, at);
}
at + 2
} else if rest.starts_with(b"<?") {
// An instruction in a declaration's literal opens no element; the markup after it is
// counted as it comes.
at + 2
} else if rest.starts_with(b"</") {
depth.0 = depth.0.saturating_sub(1);
@@ -458,6 +540,45 @@ mod tests {
assert!(expansion_too_large(&long_name));
}
#[test]
fn a_comment_or_cdata_marker_in_a_literal_or_an_instruction_hides_no_declaration() {
let big = "x".repeat(1 << 20);
let refs = "&a;".repeat(20);
let large = format!("<!ENTITY a \"{big}\">");
for hidden in [
format!("<!DOCTYPE svg [<!ENTITY c \"<!--\">{large}]>"),
format!("<!DOCTYPE svg [<!ENTITY c '<![CDATA['>{large}]>"),
format!("<!DOCTYPE svg SYSTEM \"<!--\" [{large}]>"),
format!("<!DOCTYPE svg [<!NOTATION n SYSTEM '<!--'>{large}]>"),
format!("<?pi <!-- ?><!DOCTYPE svg [{large}]>"),
format!("<!DOCTYPE svg [<?pi <![CDATA[ ?>{large}]>"),
] {
assert!(expansion_too_large(&format!("{hidden}<svg id=\"{refs}\"/>")));
}
assert!(entity_holds_markup("<?pi <!-- ?><!DOCTYPE svg [<!ENTITY m '<g/>'>]><svg/>"));
// A real comment still hides a declaration-like text.
assert!(!entity_holds_markup("<!DOCTYPE svg [<!-- <!ENTITY m '<g/>'> -->]><svg/>"));
}
#[test]
fn markers_in_a_literal_hide_no_markup_from_the_depth_or_attribute_counts() {
let tenth = DEPTH / 10 + 1;
let nested = format!("{}{}", "<g>".repeat(tenth), "</g>".repeat(tenth));
for bait in ["<!ENTITY c \"<!--\">", "<!ENTITY c \"<?\">", "<!ENTITY c \"]>\">"] {
assert!(too_deep(&format!("<!DOCTYPE svg [{bait}<!ENTITY a \"{nested}\">]><svg>&a;</svg>")), "{bait}");
}
// Comments and instructions in a value open and close no element.
let quiet = format!("<!ENTITY c \"{}\">", "<!-- x --><?pi x?>".repeat(DEPTH));
assert!(!too_deep(&format!("<!DOCTYPE svg [{quiet}]>{}", svg(&groups(8)))));
let closers = format!("<!ENTITY a \"{}\">", "<g><!-- </g> --><?pi </g>?><![CDATA[</g>]]>".repeat(tenth));
assert!(too_deep(&format!("<!DOCTYPE svg [{closers}]><svg>&a;</svg>")));
// Openers that never close in the value are read once each.
let openers = format!("<!ENTITY c \"{}\">", "<!--<?<![CDATA[".repeat(100_000));
assert!(!too_deep(&format!("<!DOCTYPE svg [{openers}]>{}", svg(&groups(8)))));
let many = (0..5000).map(|i| format!(" a{i}=\"1\"")).collect::<String>();
assert!(wide(&format!("<!DOCTYPE svg [<!ENTITY c \"]>\"><!ENTITY e '<g{many}/>'>]><svg>&e;</svg>")));
}
#[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
+4
View File
@@ -92,6 +92,10 @@ fn every_limit_refuses_the_file_past_it() {
let bomb = format!("<!DOCTYPE svg [<!ENTITY a \"{}\">]>{}", "x".repeat(1000), svg(&format!("<title>{}</title>", "&a;".repeat(10))));
assert_eq!(try_parse(&bomb, &Limits { entity_bytes: 9_999, ..shallow() }).err(), Some(Error::Entities));
assert!(try_parse(&bomb, &Limits { entity_bytes: 10_000, ..shallow() }).is_ok());
// A comment marker in an earlier entity's value hides no declaration.
let bait = bomb.replacen("<!ENTITY a", "<!ENTITY c \"<!--\"><!ENTITY a", 1);
assert_eq!(try_parse(&bait, &Limits { entity_bytes: 9_999, ..shallow() }).err(), Some(Error::Entities));
assert!(try_parse(&bait, &Limits { entity_bytes: 10_000, ..shallow() }).is_ok());
// XML nodes.
assert!(matches!(try_parse(&svg(&"<g/>".repeat(100)), &Limits { xml_nodes: 50, ..shallow() }), Err(Error::Xml(_))));
// Not XML, and XML that is not an SVG.