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();
}
};
@@ -0,0 +1,2 @@
<svg xmlns="http://www.w3.org/2000/svg"><filter id="f"/><path d="M0 0h1" fill="url( #f)"/><path d="M0 0h1" style="fill:url( #f)"/><path d="M0 0h1" filter="url( #f)"/><path d="M0 0h1" filter="url(
#f)"/></svg>

After

Width:  |  Height:  |  Size: 211 B

@@ -3960,6 +3960,62 @@
"unbound-prefix.svg": {
"rejected": "malformed"
},
"url-unicode-space.svg": {
"removed": [],
"tree": {
"ns": "http://www.w3.org/2000/svg",
"name": "svg",
"attrs": {
"xmlns": "http://www.w3.org/2000/svg"
},
"children": [
{
"ns": "http://www.w3.org/2000/svg",
"name": "filter",
"attrs": {
"id": "f"
},
"children": []
},
{
"ns": "http://www.w3.org/2000/svg",
"name": "path",
"attrs": {
"d": "M0 0h1",
"fill": "url( #f)"
},
"children": []
},
{
"ns": "http://www.w3.org/2000/svg",
"name": "path",
"attrs": {
"d": "M0 0h1",
"style": "fill:url( #f)"
},
"children": []
},
{
"ns": "http://www.w3.org/2000/svg",
"name": "path",
"attrs": {
"d": "M0 0h1",
"filter": "url( #f)"
},
"children": []
},
{
"ns": "http://www.w3.org/2000/svg",
"name": "path",
"attrs": {
"d": "M0 0h1",
"filter": "url( #f)"
},
"children": []
}
]
}
},
"use-mutual-cycle.svg": {
"removed": [],
"tree": {
+4
View File
@@ -42,6 +42,10 @@ fn declared(file: &str) -> Option<Value> {
// stripped, nothing else); the TS trimmed JavaScript spaces (security audit R3).
"href-unicode-space.svg" => json!({ "removed": ["use@href", "use@xlink:href"],
"strip_unicode_spaced_refs": true }),
// C3 references: `url(` skips CSS whitespace only, as the browser's tokenizer does
// (security audit R3, pass 2): a Unicode space before `#` is not local.
"url-unicode-space.svg" => json!({ "removed": ["path@fill", "path@style"],
"strip": [["path", "fill"], ["path", "style"]] }),
// C3 root: nothing that paints outside the box on the inline root (security audit R3).
"root-presentation.svg" => {
json!({ "removed": ["svg@transform", "svg@filter", "svg@mask", "svg@clip-path"] })