fix(anatomy): prescan closers searched after their opener; ASCII-only href trim; root painting attributes removed (security audit R3)

This commit is contained in:
bchanot committed 2026-10-15 00:25:43 +02:00
1 parent d3d63394dd
commit 157c854ed1
13 files changed
+3231 -21

No files matched your search

+35 -6
View File
@@ -122,20 +122,22 @@ impl<'a> Scan<'a> {
/// Consumes the markup starting at `self.at` (a `<`).
fn markup(&mut self, caps: &SvgCaps) -> Result<(), PrescanError> {
let rest = &self.bytes[self.at..];
// Each closer is searched after its whole opener, as the XML grammar reads it:
// `<!-->` does not close the comment it opens (security audit R3, MEDIUM).
if rest.starts_with(b"<!--") {
return self.skip_past(b"-->");
return self.skip_past(4, b"-->");
}
if rest.starts_with(b"<![CDATA[") {
return self.skip_past(b"]]>");
return self.skip_past(9, b"]]>");
}
if rest.starts_with(b"<?") {
return self.skip_past(b"?>");
return self.skip_past(2, b"?>");
}
if rest.starts_with(b"<!") {
return Err(PrescanError::Malformed);
}
if rest.starts_with(b"</") {
self.skip_past(b">")?;
self.skip_past(2, b">")?;
self.depth = self.depth.checked_sub(1).ok_or(PrescanError::Malformed)?;
return Ok(());
}
@@ -172,8 +174,9 @@ impl<'a> Scan<'a> {
Ok(())
}
fn skip_past(&mut self, end: &[u8]) -> Result<(), PrescanError> {
let found = find(self.bytes, end, self.at + 1).ok_or(PrescanError::Malformed)?;
/// Moves past `end`, searched from `opener` bytes after the markup's `<`.
fn skip_past(&mut self, opener: usize, end: &[u8]) -> Result<(), PrescanError> {
let found = find(self.bytes, end, self.at + opener).ok_or(PrescanError::Malformed)?;
self.at = found + end.len();
Ok(())
}
@@ -327,6 +330,32 @@ mod tests {
assert_eq!(prescan(&bom, &SVG_CAPS), Err(PrescanError::Malformed));
}
#[test]
fn a_closer_overlapping_its_opener_does_not_close_it() {
// `<!-->` keeps the comment open until the next `-->`: what follows is still counted.
let deep = format!(
"{SVG}<!--></svg><![CDATA[-->{}{}</svg><!--]]>-->",
"<g>".repeat(100),
"</g>".repeat(100)
);
assert_eq!(prescan(&deep, &SVG_CAPS), Err(PrescanError::TooDeep));
assert_eq!(
prescan(&format!("{SVG}<!-->"), &SVG_CAPS),
Err(PrescanError::Malformed)
);
assert_eq!(
prescan(&format!("{SVG}<?></svg>"), &SVG_CAPS),
Err(PrescanError::Malformed)
);
assert_eq!(
prescan(&format!("{SVG}<![CDATA[]]></svg>"), &SVG_CAPS),
Ok(Prescan {
elements: 1,
depth: 1
})
);
}
#[test]
fn refuses_doctype_and_entity_anywhere_and_unclosed_markup() {
for text in [
+2 -2
View File
@@ -9,7 +9,7 @@
use std::collections::HashMap;
use super::sanitize::url_targets;
use super::sanitize::{local_reference_text, url_targets};
use super::tree::{Element, Node, SvgRejection};
/// Expanded elements a drawing may instantiate (the product's drawing: a few thousand).
@@ -85,7 +85,7 @@ fn reference_ids(element: &Element) -> Vec<String> {
let mut ids = Vec::new();
for attr in &element.attrs {
if attr.qname == "href" || attr.qname == "xlink:href" {
if let Some(id) = attr.value.trim().strip_prefix('#') {
if let Some(id) = local_reference_text(&attr.value).strip_prefix('#') {
ids.push(id.to_owned());
}
continue;
+11 -2
View File
@@ -176,7 +176,9 @@ pub fn sanitize(mut root: Element) -> Result<Sanitized, SvgRejection> {
removed: Vec::new(),
uses: 0,
};
for name in ["class", "style"] {
// The inline root lives under the page: no page classes or styles, nothing that paints
// outside its box (security audit R3, LOW).
for name in ["class", "style", "transform", "filter", "mask", "clip-path"] {
if root.attr(name).is_some() {
root.remove_attr(name);
walk.removed.push(format!("svg@{name}"));
@@ -263,7 +265,7 @@ fn value_acceptable(qname: &str, value: &str) -> bool {
return false;
}
match qname {
"href" | "xlink:href" => is_local_reference(value.trim_matches(is_js_space)),
"href" | "xlink:href" => is_local_reference(local_reference_text(value)),
"id" => is_reference_name(value),
"style" => !style_forbidden(value) && calls_only_local_url(value),
_ if PRESENTATION_ATTRIBUTES.contains(&qname) => calls_only_local_url(value),
@@ -271,6 +273,13 @@ fn value_acceptable(qname: &str, value: &str) -> bool {
}
}
/// An `href` as the URL parser reads it: only ASCII whitespace is stripped (the engine
/// trimmed JavaScript spaces, so a no-break space before `#a` passed as local and resolved
/// as a relative URL).
pub fn local_reference_text(value: &str) -> &str {
value.trim_matches(|c: char| matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0C'))
}
/// `^#[A-Za-z_][\w.:-]*$`.
fn is_local_reference(value: &str) -> bool {
value.strip_prefix('#').is_some_and(is_reference_name)