How deleting text can turn an HTML comment into active markup

Started by Anoncto@anoncto••0 views•1 replies
A
Anoncto@anoncto
VIP
25
0
#1

Deleting text can make HTML more dangerous. The interesting AntiSamy example in PortSwigger’s HyperText Mischief Language, published 2 December is a sanitizer rewrite that creates a way out of a comment.

The useful lesson is bigger than one library: check what your transformations create, not just what they remove.

The tag that was not a tag

The reported case requires the non-default preserveComments=true setting. An attacker puts markup inside a comment, with an IE conditional directive separating characters that would otherwise form a closing sequence:

Plain text
Before removing the directive: --[if IE]-!>
After removing the directive:  ---!>

The report describes three steps:

  1. AntiSamy’s parser treats the embedded <img> and its event handler as comment text, so they do not pass through element/attribute validation.
  2. The sanitizer removes [if IE]. That joins the surrounding characters into ---!>.
  3. The browser recognises that sequence as closing the comment. The following markup is now an element, not comment data.

The report also identifies a serializer check for --> that misses the browser-recognised --!> ending. A “removed suspicious content” log would not capture the important result: the output has gained syntax.

The DOM scanner implementation shows the relevant boundary: comments take a separate branch, and preserved comment data is rewritten with conditionalDirectives.matcher(value).replaceAll(""). That method does not recheck the rewritten data.

The report does not pin the complete tested build, policy and browser combination or a fix release, so this is an explanation of the reported mechanism—not an affected-version matrix.

Turn the mechanism into a regression test

You do not need an executing event handler to look for this boundary failure. Here is an inert regression-test candidate adapted from the report:

HTML
<div><!-- --[if IE]-!><span data-probe="comment-boundary">marker</span> --></div>

For a local test fixture:

  1. Record the sanitizer version, loaded parser/serializer dependencies, scan mode and full policy.
  2. Sanitize the probe with comments disabled and enabled. Also test the comment without the surrounding div; do not assume fragment-level and nested comments take identical paths.
  3. Save the exact output, then parse it using the application’s actual HTML insertion context and a recorded browser build.
  4. Inspect the resulting DOM. Did a span emerge from what should have remained comment data or been removed?

A newly created element is the signal here; an alert() is unnecessary. No element appearing is a passing case for that configuration, not proof that the sanitizer is universally safe. Checking only for the literal --> in a string misses the parser disagreement this case is about.

I’d drop comments unless the application has a concrete reason to preserve them. If preservation is required, the safety decision has to survive rewriting, serialization and browser parsing—not stop at the first DOM.

For anyone retaining comments in sanitized content: what consumes them downstream, and would rejecting a comment that becomes unsafe after rewriting break that workflow?

0

Join the conversation!