Sanitizer fragment-only check for <svg:use href=...> bypassed by xlink:href
The Sanitizer API's fragment-only restriction on SVG <use href> was not applied to the equivalent xlink:href attribute, so a sanitised document could still contain a <use> element pointing at an external URL. An attacker could use this to import nodes from another document into a sanitised page.
Originally disclosed on Mozilla
- Fixed in 16 hours
- Paid in 8 days
- Disclosed after 16 hours
The Sanitizer API restricts the value of an SVG <use href="..."> attribute to fragment URLs by default, so that nodes can't be imported from different documents.
However this check doesn't currently consider xlink:href, although that's a valid fallback used by SVGUseElement::Href().
Testcase
new Sanitizer().sanitize(new DOMParser().parseFromString(`
<svg>
<use href="#foo"/>
<use href="http://example/#bar"/>
<use xlink:href="http://example/#baz"/>
</svg>
`, 'text/html')).firstChild.outerHTML
Expected result:
<svg>
<use href="#foo"></use>
<use></use>
<use></use>
</svg>
Actual result:
<svg>
<use href="#foo"></use>
<use></use>
<use xlink:href="http://example/#baz"></use>
</svg>
AFAIU, this happens because the fragment-only check in nsTreeSanitizer::SanitizeAttributes() is only done for attributes without a set namespace. So a patch might be just something like:
diff --git a/dom/base/nsTreeSanitizer.cpp b/dom/base/nsTreeSanitizer.cpp
--- a/dom/base/nsTreeSanitizer.cpp
+++ b/dom/base/nsTreeSanitizer.cpp
@@ -1262,7 +1262,8 @@ void nsTreeSanitizer::SanitizeAttributes
// else not allowed
} else if (aAllowed.mXLink && kNameSpaceID_XLink == attrNs) {
if (nsGkAtoms::href == attrLocal) {
- if (SanitizeURL(aElement, attrNs, attrLocal)) {
+ bool fragmentOnly = aElement->IsSVGElement(nsGkAtoms::use);
+ if (SanitizeURL(aElement, attrNs, attrLocal, fragmentOnly)) {
// in case the attribute removal shuffled the attribute order, start
// the loop again.
--ac;
The fragment-only check was introduced in https://hg.mozilla.org/mozilla-central/rev/37819efa513e53663bad1ebbb47035a8eff16e24 which references bug 1757210, so I'm linking to that one (it's view-restricted, so just guessing).
NB: This has probably already been discussed, but I was just wondering if, given that xlink: attributes are considered deprecated in the docs, the sanitizer could just convert xlink:href into a native href, and drop support for XLink otherwise, so that it doesn't need to be handled as a special case at every turn.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the fragment-only URL check in nsTreeSanitizer::SanitizeAttributes() only runs for attributes without a namespace, so the namespaced xlink:href attribute skips it.
Impact
A sanitised document could retain an SVG <use> element that references an external document, allowing nodes to be imported from a different document despite the fragment-only policy.
What the attacker needed
Use of the Sanitizer API; attacker-controlled HTML passed to the sanitiser
The fix
The fragment-only check should also be applied to the namespaced xlink:href attribute on SVG <use> elements, as shown in the suggested patch.
How it was found
- Create a Sanitizer and parse an HTML string containing SVG <use> elements with href and xlink:href values.
- Run sanitize() on the parsed document.
- Inspect the output: the xlink:href pointing at an external URL is still present.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A sanitiser is a tool that cleans untrusted HTML before it is shown, removing dangerous parts. Here it was meant to stop an SVG <use> element from loading content from another website, but it only checked one spelling of the link attribute and missed the older xlink:href spelling. That meant a crafted SVG could still pull in outside content after sanitising.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.