PortSwigger

HyperText Mischief Language | PortSwigger Research


  • Published: Wednesday, 2 December 2026 at 14:30 UTC

  • Updated: Wednesday, 2 December 2026 at 15:20 UTC

Mischievous HTML character with devil horns breaking through a sanitizer filter

Hacking HTML sanitizers

In this post, I’ll show you how I attacked fifteen popular HTML sanitizers. We’ll start by
clobbering
native DOM functions, then overflow HTML entities until a sanitizer falls over. I’ll then show you how to escape comments and textareas, abuse a Safari parsing quirk, and finish with a mutation XSS that uses a brand new HTML feature.

After exploring SVG sanitizers in
Scalable Vector Grief
, I thought I’d turn my attention to HTML. This proved a much tougher nut to crack, probably because HTML sanitizers get more attention and are far more widely used. Still, they didn’t all come out unscathed.

The targets

I found the most popular HTML sanitizers I could and began testing:

Warming up: clobbering native functions

Libraries affected: symfony/html-sanitizer, nahsra/antisamy, flavorjones/loofah (including loofah-html5)

A sanitizer’s job isn’t just to stop script execution. If the sanitized output lives inside a bigger application, such as a webmail client or a forum, it also needs to stop that content from breaking the page around it. DOM clobbering is a great way to do exactly that.

Certain elements with a name attribute get exposed as properties of the document, and those properties take priority over the document’s own methods. You can do this with embed, form, iframe, image, img and object. Name an image after a native function and the function is gone:



Symfony, AntiSamy, Loofah and Loofah HTML5 all allowed the name attribute on img and form elements, so this went straight through:

In: 
Out:

In: 

Once that output lands on the page, any code that calls document.getElementById() or document.querySelector() gets a form or an image instead and throws an exception. Two tiny tags and the host application’s JavaScript is broken.

Classic two-level clobbering like x.y normally needs two elements that share an id, so that window.x becomes an HTMLCollection you can then index by name. It turns out you only need one if you can get the parser to make the second for you:

First

Second

The anchor is a formatting element. When the

closes the paragraph while the anchor is still open, the parser remembers it, and when it reaches the text “Second” it reconstructs it, cloning every attribute. Here’s the DOM you end up with

First

Second

There are now two elements with an id of x, so x is a collection, x.y returns the anchor named y, and because anchors stringify to their href, the alert shows the URL you supplied. One link, one collection.

Label jacking

Library affected: HTMLSanitizer

This one is simple but effective. The sanitizer allowed the for attribute on labels:

In: 
Out:

A label doesn’t care where its target lives, so it can point at an element outside the sanitized content. Imagine the host page contains this:

Clicking the label in the sanitized content clicks the application’s button for you. Your untrusted HTML can now trigger whatever actions the host page exposes, while looking like anything you want.

Entity overflows

Libraries affected: OWASP/java-html-sanitizer, nahsra/antisamy, mganss/HtmlSanitizer

As I was testing the various libraries, I wondered if it would be possible to overflow HTML entities. It was:

In: 
Out: abc

The hex value is too big for the integer the sanitizer stores it in, so the top bit falls off and what’s left is 0x61, the letter a. A browser, on the other hand, follows the spec and turns any code point above the Unicode range into the replacement character. That means the sanitizer and the browser disagree about what the input says, and anything that inspects the raw input, such as a WAF or a blocklist, sees nothing but harmless-looking entities.

Crashing AntiSamy

Library affected: nahsra/antisamy

If you push the overflow a little further, AntiSamy gives up entirely:

In:  
Out: exit 1: org.owasp.validator.html.ScanException: java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFFF

The value wraps to -1, the code point check throws, and the whole scan is aborted. One entity is enough to stop any document containing it from being sanitized.

AntiSamy’s dangling textarea

Library affected: nahsra/antisamy

AntiSamy serializes an empty textarea as an XML-style self-closing tag:

In:  "/>

The sanitizer sees an image with a perfectly harmless title. The browser sees a textarea that ends halfway through that title, followed by a brand new img tag with an onerror handler.

AntiSamy and the conditional comment

Library affected: nahsra/antisamy (requires preserveComments)

This one needs a non-default setting, but it’s my favourite AntiSamy bug because the sanitizer creates the vulnerability itself. With comments preserved:

This input:

Comes out as:

-->

AntiSamy uses NekoHTML, which parses everything between as a single comment. The -!> sequence, with a single dash, doesn’t close a comment, so the comment data is --[if IE]-!>. The img tag only exists as text inside the comment, so it never goes anywhere near AntiSamy’s attribute validation.

Then AntiSamy tries to be helpful. Its processCommentNode() strips IE conditional directives like [if IE], and removing that from the middle of the data glues the dashes together: --[if IE]-!> becomes ---!>. The serializer then checks the comment for -->, doesn’t find it, and writes everything out raw.

When the browser parses the output, it hits ---!> and follows the HTML spec: -- enters the comment end state, the third dash keeps it there, ! moves to the comment end bang state, and > closes the comment. Everything after it, including the unsanitized img, is live HTML.

The root cause is twofold: the serializer only looks for --> when HTML also recognises --!> as a comment close, and processCommentNode() modifies comment data without checking whether it has just created a new way out.

Loofah HTML5 and Safari’s mystery slash

Library affected: flavorjones/loofah (HTML5 mode)

This vulnerability uses , and to make Loofah parse in XML mode while the browser parses in HTML mode. An annotation-xml element with an encoding of text/html is an HTML integration point, which means its contents are parsed as HTML rather than foreign content. So the whole bypass comes down to making the encoding attribute mean text/html to the browser but not to the sanitizer.

The trick is the character 0x0F, which Safari treats as a forward slash:



Loofah decodes the entity, sees an encoding of textx0Fhtml, which isn’t text/html, and treats everything inside as foreign content. In that world the textarea is just an element and the

Here $CHR stands for the raw 0x0F character. Safari reads the encoding as text/html, switches to HTML parsing, and now the textarea really is a textarea. It closes at the inside the style attribute and the script that follows executes. This only works in Safari.

You can do the same thing with plus too:

 ';">

I found both issues with Shazzer:

Characters that act as forward slash in encoding attribute

Characters that act as plus in encoding attribute

You can see all the vectors used in the Shazzer collection.

Paste-based exfiltration with contenteditable

Libraries affected: loofah, mganss/htmlsanitizer

The sanitizers allowed the contenteditable attribute:

In:  

Paste in here


Out:

Paste in here

That doesn’t sound dangerous, but an editable region inside a trusted page is a great place to land a paste-based attack. In Firefox, you can get a user to paste CSS into the editable element and use it to steal hex-based tokens from the page.

To exploit this issue I created a tool called
Frag
, this tool lets you exfiltrate tokens using the technique described in ”
Alex’s Smashing the token limit
” post.

Example token:

When you paste the CSS into the contenteditable area. The CSS leaks the first three and last three hex characters of the token, then every three-character hex sequence that appears somewhere in it. Frag stitches the token back together by walking overlapping three-character windows from the start to the end. If you want to know more about how this works, check out
CSS: the bomb inside your inbox
and
Smashing the token limit
.

You can try it yourself:

1. Clone
https://github.com/hackvertor/frag

2. Run npm install

3. Run npm start

4. Visit localhost:3000 in Firefox

5. Configure a selector to match a token

6. Click the “Copy as HTML for test” button

7. Paste into the test area provided by Frag.

8. Frag should show your exfiltrated token on paste.

This can be adapted to steal any hex-based token or even alphanumeric ones too. In a real attack, the victim would paste the CSS into the target site and the exfiltration server would sit on an external domain.

Bonus: attr() exfiltration in Safari beta

Back in April 2025
Slonser discovered
you could use attr() function in CSS to steal data in Chrome. While I was playing with CSS, I noticed that Safari beta lets you use attr() inside -webkit-image-set() function:


The attribute value is used as an image URL and Safari makes the request, so CSS can read an attribute and send it over the network without any selector brute force at all. attr() is supposed to be prevented from producing URLs for exactly this reason. This works in Safari technology preview Release 257 (WebKit 22627.1.8.19.1).

Mutation XSS with marker processing instructions

To end this piece of research, here’s a mutation XSS that uses a brand new HTML feature. Template elements can now be streamed into a location in the document that’s been marked with a marker processing instruction: