Sanitizer bypass if the sanitized markup is assigned to srcdoc
Firefox's Sanitizer API parses markup using the fragment parsing algorithm, but when the sanitised output is assigned to a sink that uses the document parsing algorithm such as iframe.srcdoc, the two parsers disagree about how SVG foreign content is handled. An attacker could craft markup that passes sanitisation yet executes script when the result is placed in such a sink.
Originally disclosed on Mozilla
- Fixed in 8 months
- Paid in 2 months
- Disclosed after 8 months
I've found a bug in Firefox sanitizer that can be exploited if the result of sanitizeToString is used in a sink that doesn't parse the HTML using fragment parsing algorithm (examples being: iframe.srcdoc, data:text/html,markup etc.)
Proof of concept
Here's the proof-of-concept:
<iframe id=ifr></iframe>
<script>
const bypass = `<svg><font color><title><u rel="</title><img src onerror=alert(document.domain)>">`;
ifr.srcdoc = new Sanitizer().sanitizeToString(bypass);
</script>
Make sure to test on Firefox Nightly. An alert is shown despite using the sanitizer.
Explanation
There's an interesting difference in HTML parsing in HTML spec between fragment parsing algorithm and document parsing algorithm in foreign content.
Suppose we have the following markup:
<svg><p>Test
If we use document parsing algorithm (via srcdoc for instance), it creates the following DOM tree:
├ <svg svg>
└ <html p>
(I'm using a notation that tag name is prepended with a namespace name)
On the other hand, if we use fragment parsing algorithm (via innerHTML), it creates the following DOM tree:
└ <svg svg>
└ <svg p>
This is correct per HTML spec and rules of parsing tokens in foreign content (however, interestingly, Chrome parses both cases the same way).
According to the specification, if the parses is in document parsing mode, then certain tokens (<p> included) "escape" the foreign content. The same doesn't happen in fragment parsing.
Another interesting tag that "escapes" foreign content is <font> because it does so only if it has any attributes named color, face or size.
So in document parsing mode the markup <svg><font> creates svg font, while <svg><font face> creates html font.
Now consider the following markup (that was the bypass):
<svg>
<font color>
<title>
<u rel="</title>
<img src onerror=alert(document.domain)>">
Internally, Firefox sanitizer makes use of fragment parsing algorithm. This means that the markup is parsed into the following DOM tree:
└ <svg svg>
└ <svg font color="">
└ <svg title>
└ <html u rel="</title><img src onerror=alert(1)>">
All of these elements and attributes are allow-listed by the sanitizer. When authors use sanitizeToString, the markup is serialized to:
<svg>
<font color="">
<title>
<u rel="</title>
<img src onerror=alert(document.domain)>"></u></title></font></svg>
Now, when this result is assigned to srcdoc (or other places with document parsing mode), it is parsed into the following DOM tree:
├ <svg svg>
└ <html font color="">
├ <html title>
│ └ #text: <u rel="
├ <html img src="" onerror="alert(document.domain)"/>
└ #text: ">
So because of the difference in parsing between document and fragment, <font color> "escaped" foreign content and thus title is now parsed in HTML namespace, not in SVG namespace, which means that it is closed on first instance of </title> leading to the bypass.
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
The sanitiser parses input with the fragment parsing algorithm while the sink re-parses the serialised output with the document parsing algorithm, and the two algorithms disagree on when certain tags escape SVG foreign content.
What the attacker needed
Firefox Nightly; app sanitises markup then uses srcdoc
How it was found
- Create an iframe element in a page.
- Build markup that mixes SVG with a font tag carrying a colour attribute, a title tag and a u tag whose attribute value contains a closing title tag and an image with an error handler.
- Pass that markup through the Sanitizer's string-sanitising method and assign the result to the iframe's srcdoc.
- Load the page in Firefox Nightly and observe the alert firing.
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 meant to clean HTML so that dangerous parts like scripts are removed. This report shows that the cleaner and the browser page that later displays the cleaned HTML can read the same markup in different ways, so something the cleaner thought was harmless turns into executable script. The result is that a page using the cleaner in a particular way can still be attacked.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.