fix(anatomy): url() locality skips CSS whitespace only (security audit R3 pass 2)

This commit is contained in:
bchanot committed 2026-10-15 00:40:30 +02:00
1 parent 157c854ed1
commit 23895b9351
6 files changed
+90 -10

No files matched your search

+11 -3
View File
@@ -5,7 +5,7 @@
//! reference grammar, no text outside `title`/`desc`. A refused element goes with its
//! subtree, a refused attribute alone; both are named in `removed` (names, never values).
use super::number::{is_js_space, js_number_of, js_split_list, js_to_string};
use super::number::{js_number_of, js_split_list, js_to_string};
use super::tree::{Element, Node, SvgRejection, XLINK_NS};
const ALLOWED_ELEMENTS: [&str; 28] = [
@@ -273,6 +273,11 @@ fn value_acceptable(qname: &str, value: &str) -> bool {
}
}
/// CSS whitespace (what the tokenizer skips inside `url(`).
fn is_css_space(c: char) -> bool {
matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0C')
}
/// 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).
@@ -305,12 +310,15 @@ fn url_openings(value: &str) -> impl Iterator<Item = usize> + '_ {
})
}
/// After `url(`: `\s*`, an optional quote, `\s*`; returns the offset of the next character.
/// After `url(`: whitespace, an optional quote, whitespace; returns the offset of the next
/// character. Only CSS whitespace is skipped, as the browser's CSS tokenizer skips it: the
/// engine skipped JavaScript spaces, so a no-break space before `#a` read as local but the
/// browser resolves it as a relative URL (security audit R3, pass 2).
fn skip_url_prefix(value: &str, at: usize) -> usize {
let rest = &value[at..];
let mut chars = rest.char_indices().peekable();
let skip_space = |chars: &mut std::iter::Peekable<std::str::CharIndices<'_>>| {
while chars.peek().is_some_and(|&(_, c)| is_js_space(c)) {
while chars.peek().is_some_and(|&(_, c)| is_css_space(c)) {
chars.next();
}
};