[html-janitor] Bypassing sanitization using DOM clobbering
The html-janitor sanitisation library can be bypassed because a DOM clobbering trick makes the sanitizer skip nodes it should clean. An attacker who can supply HTML to the clean function could therefore inject event handlers and achieve cross-site scripting.
- Triaged in 5 hours
- Fixed in 13 days
- Disclosed after 13 days
Module:
Name: html-janitor Version: 2.0.2
Summary:
Arbitrary HTML can pass the sanitization process, which can be unexpected and dangerous (XSS) in case user-controlled input is passed to the clean function.
Description:
Proof of concept:
var myJanitor = new HTMLJanitor({tags:{p:{}}});
var cleanHtml = myJanitor.clean("<form><object onmouseover=alert(document.domain) name=_sanitized></object></form>")
console.log(cleanHtml) // logs: <form><object onmouseover=alert(document.domain) name=_sanitized></object></form>
The following check can be leveraged to bypass the whole sanitization process:
do {
// Ignore nodes that have already been sanitized
if (node._sanitized) {
continue;
}
...
As node is the first child in the created tree walker (i.e. in this case the <form> tag) node._sanitized will point to the inner <object> and the check passes.
To learn more about DOM clobbering see: https://www.youtube.com/watch?v=5W-zGBKvLxk (by Mario Heiderich)
Recommendation:
It should be enough to set node._sanitized to false every time a new node is being processed.
Note that I previously reported this issue at https://github.com/guardian/html-janitor/issues/35
Impact
Given the module's description I would assume it should be used to prevent XSS vulnerabilities. This is currently a very dangerous assumption given that the whole sanitization process can be bypassed.
Note that the author might have never intended to feed untrusted data into the clean() function. In that case this is just a regular issue. Furthermore, the fact that untrusted data is unexpected should be at least mentioned in the documentation, because other developers most certainly will use the package in such scenarios.
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 used a property on the node itself as a 'already cleaned' marker, and DOM clobbering allowed an attacker-controlled child element to satisfy that check for its parent.
Impact
If untrusted input is passed to the clean function, the whole sanitisation process can be bypassed, allowing cross-site scripting; the author may not have intended the function to handle untrusted data.
What the attacker needed
Untrusted HTML reaches the clean function
The fix
The sanitiser should reset the sanitised marker to false on every node it processes so that a clobbered property cannot make it skip cleaning.
How it was found
- Create an HTMLJanitor instance that allows only paragraph tags.
- Pass a form containing an object element with an event handler and a name attribute set to the sanitised marker into the clean function.
- Observe that the output still contains the object element with its event 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.
A sanitiser is a library that is meant to strip dangerous parts out of HTML before it is shown on a page. This one checks a property on each element to decide whether it has already been cleaned, but an attacker can make that property point to a different element, so the check wrongly passes and the dangerous HTML survives. That surviving HTML can run JavaScript in the victim's browser, which is called cross-site scripting.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.