diff --git a/AGENTS.md b/AGENTS.md
index c835e887..db10c11b 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -67,6 +67,7 @@ Use these as targets when touching parser, policy, or renderer code:
- Raw-text and RCDATA content handling (`style`, `noscript`, and friends): CVE-2025-66021 (advisory GHSA-g9gq-3pfx-2gw2)
- mXSS via serialize and re-parse differences
- SVG and MathML namespace and case tricks, and names the HTML parser rewrites: outside foreign content an `image` start tag builds `img`, so the element allowlist must not let one name through as the other
+- Gaps between the policy's model of the output context and the browser's, which the `image` check trusts: names the renderer writes in place of the policy's (`xmp` as `pre`), receivers that hide the renderer from the policy, `template` content (an unknown parent context, or a table part switching its insertion mode), and an element kept inside a same-named one whose content a browser reads as text (`title`, `textarea`, `noscript`, `noembed`, `noframes`), whose end tag ends the outer element early
- `javascript:`, `data:`, and scheme-relative URLs in allowed attributes, including namespaced URL attributes such as SVG `xlink:href` in any case (GHSA-643j-x48g-g86p), URL attributes under another name or holding a list of URLs, such as `imagesrcset` and `ping` (GHSA-x759-2xw8-gp4m), `url(...)` in SVG presentation attributes such as `fill` (GHSA-76cc-622x-jwm4), and SVG `set` and `animate`, whose `attributeName` makes them write their values into another attribute (GHSA-ppfr-7xq6-p6c2)
- Entity and encoding edge cases: malformed entities, entities without semicolons, null bytes, mixed case, nested and unbalanced tags
- Denial of service: unbounded recursion over nested CSS functions (GHSA-x6fc-6qh4-g3wr), quadratic passes over one tag carrying many attributes, with and without a change listener (GHSA-47x2-9gq9-jg3m), quadratic tag-balancer paths and unbounded formatting reconstruction (release 20260921.1, #490)
diff --git a/change_log.md b/change_log.md
index 10808dc4..1e4a3bf0 100644
--- a/change_log.md
+++ b/change_log.md
@@ -1,6 +1,47 @@
# OWASP Java HTML Sanitizer Change Log
Most recent at top.
+ * Next release
+ * The renderer writes `xmp`, `listing` and `plaintext` as `pre`, and a
+ browser ends SVG and MathML content at `pre` although not at `xmp` or
+ `plaintext`. The sanitizer tracked the output context under the name
+ the policy gave, so after an `xmp` or `plaintext` inside `svg` or
+ `math`, written so or produced by an element policy, it still took the
+ output for foreign content, and kept a following `image` that the
+ browser then built as an HTML `img`. This bypassed the `image` check
+ added in 20261107.1 in policies that allow `svg` or `math`, `image`,
+ and `xmp` or `plaintext`, but not `img`. With the library's renderer,
+ the output context now follows the name the renderer writes. A
+ postprocessor or receiver that does not extend
+ `HtmlStreamEventReceiverWrapper` still gets the policy's name; since
+ it may pass that on to the renderer, an `xmp` or `plaintext` that
+ would be SVG or MathML under its own name now makes the output
+ context unknown for it, and later `image` elements are dropped.
+ Reported by Irhas Abdul Rosyid (@analisaperlengkapan).
+ * A `template` written where the output context was already unknown,
+ for example after an earlier `template`, started a known context of
+ its own. Inside `math` a browser makes it a MathML template, so an
+ `svg` in it is MathML and its `mtext` a text integration point, where
+ `image` builds an HTML `img`; the sanitizer took that `image` for SVG
+ and kept it. Such a template's content now starts unknown, so
+ `image` elements in it are dropped, including SVG images in a
+ `template` that follows another.
+ * A browser reads the content of an HTML `title`, `textarea`,
+ `noscript` (with scripting on), `noembed` or `noframes` as text up to
+ the first end tag of the same name, in any case and whatever element
+ wrote it. An element of that name kept inside one, such as an SVG
+ `noscript` in `y",
+ new HtmlPolicyBuilder().allowElements("noscript", "p")
+ .allowTextIn("noscript").toFactory()
+ .sanitize("
x
y"));
+ assertEquals("",
+ noscript.sanitize(""));
+ }
+
+ /**
+ * Fails if, in the output, an element a browser reads as text holds a
+ * start tag of its own name, in any case, before its end tag.
+ */
+ private static void assertNoTextContentElementInItself(
+ String out, String message) {
+ String lower = Strings.toLowerCase(out);
+ for (String name : new String[] {
+ "title", "textarea", "noscript", "noembed", "noframes"
+ }) {
+ int start = lower.indexOf("<" + name);
+ while (start >= 0) {
+ int end = lower.indexOf("" + name, start);
+ int nested = lower.indexOf("<" + name, start + 1);
+ assertFalse(nested >= 0 && (end < 0 || nested < end),
+ name + " in " + name + ": " + message + "\n" + out);
+ start = end < 0 ? -1 : lower.indexOf("<" + name, end);
+ }
+ }
+ }
+
+ /**
+ * The first table-structure start tag in HTML template contents switches
+ * the template to a table mode, where math and svg are ignored or put
+ * elsewhere. The sanitizer modeled the contents as in body, so a template
+ * or desc there let an image through that a browser builds as img, as
+ * Chrome does for caption or colgroup followed by svg, desc and colgroup,
+ * and for col followed by math and template. The other shapes here pin the
+ * fail-closed rule: any such tag makes the template's context unknown.
+ */
+ @Test
+ void testTableModeInTemplateIsNotKnownForeignContent() throws Exception {
+ String image = "";
+ PolicyFactory p = new HtmlPolicyBuilder()
+ .allowElements("template", "math", "svg", "desc", "mtext", "image",
+ "caption", "col", "colgroup", "tbody", "thead", "tfoot", "tr",
+ "td", "th")
+ .allowAttributes("src").onElements("image")
+ .allowUrlProtocols("https").toFactory();
+ for (String part : new String[] {
+ "caption", "col", "colgroup", "tbody", "thead", "tfoot", "tr", "td",
+ "th"
+ }) {
+ for (String rest : new String[] {
+ "" + image,
+ "
" + image,
+ "" + image,
+ }) {
+ String input = "<" + part + ">" + rest;
+ String out = p.sanitize(input);
+ assertFalse(out.contains("";
+ assertEquals("" + svgImage + "",
+ p.sanitize("" + svgImage + ""));
+ }
+
+ /** A receiver that passes every event on, and that the library cannot see through. */
+ private static HtmlStreamEventReceiver forwardTo(
+ final HtmlStreamEventReceiver sink) {
+ return new HtmlStreamEventReceiver() {
+ @Override public void openDocument() { sink.openDocument(); }
+ @Override public void closeDocument() { sink.closeDocument(); }
+ @Override public void openTag(String name, List attrs) {
+ sink.openTag(name, attrs);
+ }
+ @Override public void closeTag(String name) { sink.closeTag(name); }
+ @Override public void text(String text) { sink.text(text); }
+ };
+ }
+
+ @Test
+ void testForeignImageAroundRenamedRawTextElementIsKept() throws Exception {
+ PolicyFactory p = imagePolicyBuilder()
+ .allowElements("xmp", "listing", "plaintext", "pre").toFactory();
+ String image = "";
+ // Before the breakout, and inside a new root after it, an image is SVG.
+ String out = p.sanitize("" + image + "x"
+ + "" + image + "");
+ assertEquals(
+ "" + image + "
x
" + image + "", out);
+ assertForeignInOutput(p, "" + image + "",
+ Arrays.asList("image"));
+ // Outside foreign content nothing changes: xmp is still written as pre.
+ assertEquals("
<b>
", p.sanitize(""));
+ }
+
+ @Test
+ void testCustomReceiverStillGetsThePolicyName() {
+ // Only the library's renderer renames xmp; another receiver gets the
+ // name the policy gave. Whether it writes that name, which inside svg a
+ // browser keeps as SVG, or passes it on to the renderer, which writes
+ // pre, is unknown, so no image after it is trusted as SVG, even in a new
+ // svg root, as after a retained template. Before it nothing changes.
+ PolicyFactory p = imagePolicyBuilder().allowElements("xmp").toFactory();
+ List opened = new ArrayList<>();
+ HtmlSanitizer.sanitize(
+ ""
+ + "",
+ p.apply(new HtmlStreamEventReceiver() {
+ @Override public void openDocument() { }
+ @Override public void closeDocument() { }
+ @Override public void openTag(String name, List attrs) {
+ opened.add(name);
+ }
+ @Override public void closeTag(String name) { }
+ @Override public void text(String text) { }
+ }));
+ assertEquals(
+ Arrays.asList("svg", "image", "xmp", "xmp", "svg"), opened);
+ }
+
+ private static int count(String s, String sub) {
+ int n = 0;
+ for (int i = s.indexOf(sub); i >= 0; i = s.indexOf(sub, i + 1)) { ++n; }
+ return n;
+ }
+
private static void emitAnnotationXmlImage(
HtmlStreamEventReceiver events, String encoding) {
events.openTag("math", new ArrayList());
@@ -3919,18 +4303,25 @@ void testForeignEndTagDoesNotReachOlderHtmlAncestor() throws Exception {
.allowElements(names).allowWithoutAttributes(names)
.allowTextIn("noscript").toFactory();
// A browser drops the raw td and tr, which are outside any table, so the
- // output is compared with its own reparse only.
+ // output is compared with its own reparse only. The svg noscript is
+ // dropped, not kept: with scripting on, a browser reads the outer
+ // noscript as text up to the first , which would be the inner
+ // one's (see testTextContentElementIsNotNestedInItself). Its logical
+ // entry is still what the foreign end tag closes.
assertRoundTripAndBalanced(
p, "
",
- "
"
+ "
"
+ "
");
// Content after the foreign close belongs to the outer noscript, and its
// end tag still finds that element.
String input = "xy";
- String expected = "xy";
+ String expected = "xy";
assertRoundTripAndBalanced(p, input, expected);
- assertEquals(parseAsBrowser(input), parseAsBrowser(expected));
+ // The browser tree differs from the input's by the dropped svg noscript
+ // alone, and x stays in the outer noscript.
+ assertEquals(parseAsBrowser(input).replace(" \n", ""),
+ parseAsBrowser(expected));
}
/** A wrapper the policy already closed cannot alias an older wrapper. */