Skip to content
XSS Write-up Advanced

Bypassing DOMPurify with good old XML

DOMPurify, when sanitising XML documents, failed to account for parsing differences between XML and HTML, allowing an attacker to inject malicious markup that would be executed when the sanitised output was later used in an HTML context. This could lead to cross-site scripting.

ryotak Follow

Published on flatt.tech

Hello, I’m RyotaK ( @ryotkak ), a security engineer at Flatt Security Inc.

RyotaK, on flatt.tech

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the root cause is that DOMPurify did not account for all node types (processing instructions and CDATA sections) that are parsed differently by XML and HTML parsers, allowing malicious content to survive sanitisation.

Impact

An attacker could execute arbitrary JavaScript in the context of a victim's browser if the sanitised XML is later inserted into an HTML document, leading to cross-site scripting.

What the attacker needed

DOMPurify used to sanitise XML; Sanitised output inserted into HTML

The fix

DOMPurify was fixed by adding NodeFilter.SHOW_PROCESSING_INSTRUCTION and NodeFilter.SHOW_CDATA_SECTION to the NodeFilter options, ensuring these nodes are scanned and removed if not allowed.

How it was found

  1. Identify that DOMPurify does not scan processing instructions when sanitising XML.
  2. Craft an XML processing instruction that contains HTML markup which will be parsed as a tag by an HTML parser.
  3. Pass the crafted XML to DOMPurify with PARSER_MEDIA_TYPE set to application/xhtml+xml.
  4. Insert the sanitised output into an HTML document, causing the injected markup to execute.

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 write-up 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 that when it cleans XML, it can be tricked because XML and HTML read some characters differently. An attacker could sneak in a script that runs when the cleaned XML is put on a web page.

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 ryotak

View all

6 more disclosed reports, best first.

More from DOMPurify

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.