Skip to content
XSS Disclosed report Beginner CVE-2017-0931

[html-janitor] Passing user-controlled data to clean() leads to XSS

The html-janitor library's clean() function assigns untrusted HTML to a detached div's innerHTML, which executes event handlers such as onerror on image elements. An attacker who can supply HTML to clean() can therefore run arbitrary JavaScript.

bayotop Follow

Originally disclosed on HackerOne

Critical 9.8 View original
  • Triaged in 31 hours
  • Fixed in 17 days
  • Disclosed after 17 days

Module:

Summary:

Passing user-controlled data to the module's clean() function can result in arbitrary JS execution, because of unsafe DOM operations.

The description "Cleans up your markup and allows you to take control of your HTML. HTMLJanitor uses a defined whitelist to limit HTML it is given to a defined subset." implies that "dirty" HTML is expected and therefore I would assume the clean method should never result in arbitrary JS being executed.

Description:

The following will result in JS execution:

JavaScript
var myJanitor = new HTMLJanitor({tags:{p:{}}});
var cleanHtml = myJanitor.clean("<p><img src onerror=alert()><p>")

because of this code:

JavaScript
HTMLJanitor.prototype.clean = function (html) {
    var sandbox = document.createElement('div'); // This is not a safe way to create a sandbox.
    sandbox.innerHTML = html; // At this point, the onerror in the img is triggered.

Recommendation:

The "sandbox" should be created using safe browser APIs such as document.implementation.createHTMLDocument();.

Note that I previously reported this issue at https://github.com/guardian/html-janitor/issues/34

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 module itself has a XSS vulnerability.

Note that the author might have never intended to feed untrusted data into the clean() function. However, in that case this should be at least mentioned in the documentation, because other developers most certainly will use the package in such scenarios.

The original report as published on HackerOne, by bayotop. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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 clean() function uses a detached div and assigns untrusted HTML to its innerHTML, which triggers event handlers before any sanitisation occurs.

Impact

Any application that passes user-controlled HTML to clean() could suffer arbitrary JavaScript execution in the victim's browser.

What the attacker needed

Application passes user HTML to clean()

The fix

The sandbox should be created with a safe API such as document.implementation.createHTMLDocument() instead of assigning to innerHTML on a div.

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.

HTML sanitizers are meant to strip dangerous parts out of HTML before it is shown on a page. This library instead put the untrusted HTML into a hidden page element to inspect it, and the browser ran an image error handler during that step. So the tool meant to stop script execution actually allowed it.

What is XSS?

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.

More by bayotop

View all

7 more disclosed reports, best first.

More from Node.js third-party modules

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.