From 01fcf22cb019c5f1be92a6d970af038ef84d9afb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 11:23:53 -0800 Subject: [PATCH] craft-svg: comment markers in a declaration's literal hide no later declaration declared() skipped a comment or CDATA section wherever `"), + _ 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 { + const KINDS: [(&[u8], &[u8]); 3] = [(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 `") { - 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""); } else if rest.starts_with(b"` 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 `").unwrap_or(b.len()) - } else if rest.starts_with(b"").unwrap_or(b.len()) - } else if rest.starts_with(b"").unwrap_or(b.len()) - } else if rest.starts_with(b"` 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"`. + 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""); + for hidden in [ + format!("{large}]>"), + format!("{large}]>"), + format!(""), + format!("{large}]>"), + format!(""), + format!("{large}]>"), + ] { + assert!(expansion_too_large(&format!("{hidden}"))); + } + assert!(entity_holds_markup("'>]>")); + // A real comment still hides a declaration-like text. + assert!(!entity_holds_markup("'> -->]>")); + } + + #[test] + fn markers_in_a_literal_hide_no_markup_from_the_depth_or_attribute_counts() { + let tenth = DEPTH / 10 + 1; + let nested = format!("{}{}", "".repeat(tenth), "".repeat(tenth)); + for bait in ["", "", "\">"] { + assert!(too_deep(&format!("]>&a;")), "{bait}"); + } + // Comments and instructions in a value open and close no element. + let quiet = format!("", "".repeat(DEPTH)); + assert!(!too_deep(&format!("{}", svg(&groups(8))))); + let closers = format!("", "?></g>".repeat(tenth)); + assert!(too_deep(&format!("&a;"))); + // Openers that never close in the value are read once each. + let openers = format!("", "