Skip to content

Track the output context under the name the renderer writes - #541

Merged
jmanico merged 7 commits into
mainfrom
xmp-breakout-image
Nov 11, 2026
Merged

jmanico merged 7 commits into
mainfrom
xmp-breakout-image

Conversation

@jmanico

@jmanico jmanico commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

HtmlStreamRenderer writes xmp, listing and plaintext as pre. A browser ends SVG and MathML content at pre, but not at xmp or plaintext. The policy's output foreign-content tracker was fed the policy's name, not the name the renderer writes. So after <svg><xmp> it still treated the output as SVG, and the image check from #534 kept a following <image>, which the browser builds as an HTML img.

Policy allows svg, image (with src) and xmp, but not img:

output Chrome DOM
main <svg><pre></pre><image src="https://attacker.example/xss.js"></image></svg> svg pre img, URL fetched
this PR <svg></svg><pre></pre> svg pre, no request

Fix

When the output is the library's renderer, applyPolicies now gives each kept element the name the renderer will write (HtmlStreamRenderer.safeName). The output tracker, the output stack, and the tag balancer's queries to the policy then all see pre, as they already did for a literal <pre>. The rendered text is unchanged apart from where the foreign root closes. Other receivers still get, and are tracked under, the policy's name.

Tests

  • testRenamedRawTextElementEndsForeignContent: covers the report's variants (<xmp/>, <XMP>, inside math, g, mi and desc, plus listing and plaintext), names introduced by element policies, and direct events. Each kept image must parse as SVG or MathML, and no img may appear in the parsed tree. Fails on main.
  • testForeignImageAroundRenamedRawTextElementIsKept: an SVG image before the breakout, or inside a new root after it, is still kept. Fails on main because the output shape changed.
  • testCustomReceiverStillGetsThePolicyName: pins the custom-receiver behaviour.
  • testTemplateUnderUnknownContextIsNotKnownForeignContent: covers every MathML text and HTML integration point, under plain and forwarding sinks. Fails on main.
  • testTextContentElementIsNotNestedInItself and testTableModeInTemplateIsNotKnownForeignContent both fail on main.
  • ./mvnw clean verify passes on JDK 11, 17, 21 and 25 (768 tests).

Review rounds

Reviewer Finding Commit
Sol (gpt-6-sol), Fable A postprocessor or receiver that forwards to the renderer without extending HtmlStreamEventReceiverWrapper hid the renderer, and the bypass came back 0d2e10d: where the policy's name and the renderer's name would put the start tag in different namespaces, the output context becomes unknown, so later image elements are dropped
Fable Change-log and javadoc wording; test gaps (void element renamed to xmp, wrapper postprocessor, change listener) d91243e
Opus A template under an already-unknown output context started a fresh known context. <template></template><math><template><svg><mtext><image src=…> kept the image, which Chrome builds as an HTML img. This affects every sink, including on main, and it reopened 0d2e10d's fail-closed path 0513b7b: such a template's content starts unknown. Cost: SVG images in a template that follows another template are dropped
Fable (final head) Comment wording; no hostile-size test a539989
Fable (final head) Three more image→img bypasses already on main: (1) <noscript><svg><noscript></noscript><image src=…>, where a scripting-on browser ends the outer noscript at the inner end tag, and Chrome fetches the URL; (2) the same for title, textarea, noembed and noframes given children by an element policy or an event caller; (3) a table part first in a template switches the browser to a table mode that the tracker modeled as body a150c46: while an HTML-namespace element whose content the browser reads as text is open, a same-named start tag (ASCII case-insensitive, SVG textArea included) is dropped, using constant-time per-name counts. A template context becomes unknown at a table-part start in body mode. testForeignEndTagDoesNotReachOlderHtmlAncestor had pinned shape (1) as expected output; its expectation now drops the inner noscript, and its comment says why

Not changed, judged acceptable by the reviewers: one hostile-only shape that needs a second pass to settle (<ul><math><select><xmp>); text dropped in two hostile-only shapes; style source that surfaces as double-escaped text inside select/option/math, which also happens on main.

Reported by Irhas Abdul Rosyid (@analisaperlengkapan). The other four bypasses were found in review.

🤖 Generated with Claude Code

jmanico and others added 7 commits November 10, 2026 19:54
HtmlStreamRenderer writes xmp, listing and plaintext as pre.  A browser
ends SVG and MathML content at pre but not at xmp or plaintext, so the
policy's output tracker, fed the policy's name, stayed in foreign
content after <svg><xmp> while the browser had left it.  An image start
that followed was then kept as SVG and built by the browser as an HTML
img, bypassing the image check from #534 in policies that allow svg,
image and xmp but not img.

When the output is the library's renderer, the policy now judges each
kept element under the name the renderer will write.  Other receivers
still get, and are tracked under, the policy's name.

Reported by Irhas Abdul Rosyid (@analisaperlengkapan).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nderer

Sol's review found that a postprocessor passing every event on unchanged
hides the renderer from the policy, which then tracked xmp under its own
name and kept the image after <svg><xmp> again.  A receiver the library
cannot see through still gets the policy's name, since #474 relies on
it, but where that name and the renderer's would put a start tag in
different namespaces the output context now becomes unknown, so later
image elements are dropped as after a retained template.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From Fable's review: the change-log bullet now names math, plaintext
and element-policy names and says the exact tracking holds for the
library's renderer; the outIsLibraryRenderer javadoc says it now also
decides the tracked name; the regression test also runs a void element
renamed to xmp, a wrapper-subclass postprocessor and a change listener.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the Opus review.  After a retained template the output context is
unknown, and an unknown context judges a template start as HTML, so the
template's own context started known.  Inside math a browser makes that
template a MathML one: an svg in it is MathML and its mtext a text
integration point, where image builds img.  The sanitizer took the
image for SVG and kept it, under any sink, as
<template></template><math><template><svg><mtext><image src=...>.
It also reopened the fail-closed path of the previous commit for
receivers that forward to the renderer.  Such a template's content now
starts unknown.  The cost is that SVG images in a template that follows
another template are dropped.  The change-log wording on receivers is
also corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From Fable's final-head review: the template-context field and push
comment now say a context can start unknown and that the image must sit
in a text integration point; a long run of templates and renamed raw
text under a forwarding receiver runs in linear time at -Xmx256m.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fable's final-head review found three ways, all already on main, for an
image the sanitizer kept as SVG to be built as an HTML img:

- An element kept inside a same-named HTML element whose content a
  browser reads as text: <noscript><svg><noscript></noscript><image>
  (scripting on), and title, textarea, noembed and noframes given
  children by an element policy or an event caller.  The inner end tag
  ends the outer element early.  An element of such a name, in any
  case, is now dropped while one is open as HTML in the output, counted
  per name so the check is constant time.
- The first table part in a template's content switches a browser to a
  table mode, where math and svg are ignored or placed elsewhere.  A
  template context now becomes unknown at such a tag.

testForeignEndTagDoesNotReachOlderHtmlAncestor pinned an svg noscript
inside an HTML noscript as expected output, which is the first shape;
its expectations now drop the inner element and say why, and its
browser comparison checks that the trees differ by that element alone.
AGENTS.md lists the class.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the Opus review of a150c46: noembed and noframes are text to a
browser whatever the scripting state; a table part switches a template
only before any other start tag; the mode is table body, not body; the
template test names the shapes Chrome builds as img and says the rest
pin the fail-closed rule; the hostile-size run includes a template
table part.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jmanico
jmanico merged commit 4eea862 into main Nov 11, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant