Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions change_log.md
Original file line number Diff line number Diff line change
@@ -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 `<noscript><svg><noscript></noscript>`, ended it early,
and what followed reached the browser as HTML while the sanitizer
still took it for SVG, so an `image` there became an HTML `img`. An
element of such a name is now dropped inside an open one.
* A `caption`, `col`, `colgroup`, `tbody`, `thead`, `tfoot`, `tr`,
`td` or `th` start tag that comes before any other start tag in a
`template`'s content switches a browser to a table mode there, where `math` and `svg` are ignored or
placed elsewhere. The sanitizer took the content for body, so an
`image` in a nested `template` or after a `desc` there was kept and
built as an HTML `img` in the template's content. Such a tag in a
template now makes the output context unknown, and later `image`
elements in that template are dropped.
* Release 20261107.1
* Percent-encode NUL, DEL, C1 controls, noncharacters and lone surrogates
in event-stream, preprocessor- or policy-supplied URL attribute values,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ class ElementAndAttributePolicyBasedSanitizerPolicy
/** Browser tree-construction context for the tags actually emitted. */
private HtmlSanitizer.ForeignContentContext outputForeignContent
= new HtmlSanitizer.ForeignContentContext();
/** Exact contexts inside retained, emitted HTML template elements. */
/**
* Contexts inside retained, emitted HTML template elements: exact, or
* unknown from the start where the template's parent context was unknown.
*/
private final List<HtmlSanitizer.ForeignContentContext>
outputTemplateForeignContents = new ArrayList<>();
/** Output depth at each corresponding retained HTML template start. */
Expand Down Expand Up @@ -171,12 +174,30 @@ class ElementAndAttributePolicyBasedSanitizerPolicy
private final BitSet inForeignContentBeforeOpen = new BitSet();
/** Whether each emitted, stacked output element is in SVG or MathML. */
private final BitSet outputElementInForeignContent = new BitSet();
/**
* HTML elements whose content a browser reads as text up to the first end
* tag of the same name, in any case: the raw-text and RCDATA elements,
* noembed and noframes, which the sanitizer reads as markup but a browser
* reads as text, and noscript, which a browser reads as text when
* scripting is on.
*/
private static final String[] TEXT_CONTENT_ELEMENT_NAMES = {
"iframe", "noembed", "noframes", "noscript", "plaintext", "script",
"style", "textarea", "title", "xmp",
};
/**
* How many of each of {@link #TEXT_CONTENT_ELEMENT_NAMES} are open in the
* output as HTML elements.
*/
private final int[] openTextContentElements =
new int[TEXT_CONTENT_ELEMENT_NAMES.length];
/** The same for {@link #keptCdataElementName}; entries may be null. */
private final List<String> keptCdataNameBeforeOpen = new ArrayList<>();
/**
* True if {@link #out} is the library's own renderer, possibly behind
* {@link HtmlStreamEventReceiverWrapper} decorators, so that what it escapes
* is known. See {@link #isLiteralContentElement}.
* and the names it writes are known. See {@link #isLiteralContentElement},
* {@link #applyPolicies} and {@link #processOutputStartTag}.
*/
private final boolean outIsLibraryRenderer;

Expand Down Expand Up @@ -259,6 +280,7 @@ public void openDocument() {
keptCdataElementName = null;
inForeignContent = false;
outputForeignContent = new HtmlSanitizer.ForeignContentContext();
Arrays.fill(openTextContentElements, 0);
outputTemplateForeignContents.clear();
outputTemplateForeignContentDepths.clear();
literalTextTail = "";
Expand Down Expand Up @@ -766,9 +788,25 @@ private boolean opensHtmlOutputTemplate(

private void processOutputStartTag(
String elementName, List<String> attrs, boolean selfClosing) {
outputForeignContent.processStartTag(elementName, attrs, selfClosing);
HtmlSanitizer.ForeignContentContext templateContext =
currentOutputTemplateForeignContent();
if (!outIsLibraryRenderer) {
// A receiver the library cannot see through may write the name as it
// is, or pass it on to the library's renderer, which writes xmp and
// plaintext as pre. Inside svg or math a browser keeps the one as
// SVG or MathML and leaves foreign content at the other, so where the
// two differ the output context is not known after it.
String rendered = HtmlStreamRenderer.safeName(elementName);
if (!rendered.equals(elementName)) {
outputForeignContent.forgetIfNamespaceDependsOnName(
elementName, rendered, attrs);
if (templateContext != null) {
templateContext.forgetIfNamespaceDependsOnName(
elementName, rendered, attrs);
}
}
}
outputForeignContent.processStartTag(elementName, attrs, selfClosing);
if (templateContext != null) {
templateContext.processStartTag(elementName, attrs, selfClosing);
}
Expand All @@ -783,9 +821,17 @@ private void processOutputEndTag(String elementName) {
}
}

private void pushOutputTemplateForeignContent() {
outputTemplateForeignContents.add(
new HtmlSanitizer.ForeignContentContext());
private void pushOutputTemplateForeignContent(boolean parentUnknown) {
HtmlSanitizer.ForeignContentContext context =
new HtmlSanitizer.ForeignContentContext(true);
// A template judged HTML only because its parent's context is unknown
// may be an SVG or MathML template in the browser, whose content is not
// parsed from an HTML root. Its content starts unknown too, or an svg
// root in it would make an image there look like SVG, while under a
// MathML template a browser makes that svg MathML, and an image in an
// mtext or other text integration point in it an HTML img.
if (parentUnknown) { context.forget(); }
outputTemplateForeignContents.add(context);
outputTemplateForeignContentDepths.add(outputNestingDepth);
}

Expand Down Expand Up @@ -1456,6 +1502,17 @@ private void openTag(
adjustedElementName = null;
}
}
// Inside an HTML title, noscript or other element whose content a
// browser reads as text, the end tag of an element of the same name, in
// any namespace, ends that text early. What followed would reach the
// browser as markup in a context other than the one modeled here, such
// as HTML where an image inside an svg was kept as SVG.
if (adjustedElementName != null) {
int textContent = textContentElementIndex(adjustedElementName);
if (textContent >= 0 && openTextContentElements[textContent] != 0) {
adjustedElementName = null;
}
}
reopenedTableWasRenamed = mode == OpenTagMode.REOPENED_TABLE
&& adjustedElementName != null
&& !"table".equals(adjustedElementName);
Expand Down Expand Up @@ -1769,8 +1826,15 @@ public boolean skippedLastTagAsAttributeless() {

adjustedElementName = policies.elPolicy.apply(elementName, attrs);
if (adjustedElementName != null) {
adjustedElementName =
HtmlLexer.canonicalElementName(adjustedElementName);
// Judge the element under the name the output will carry. The
// library's renderer writes xmp, listing and plaintext as pre, and
// pre, unlike xmp and plaintext, ends SVG and MathML content in a
// browser. Tracking the policy's name instead left the output
// context in foreign content after the browser had left it, so an
// image start there was kept as SVG and built as an HTML img.
adjustedElementName = outIsLibraryRenderer
? HtmlStreamRenderer.safeName(adjustedElementName)
: HtmlLexer.canonicalElementName(adjustedElementName);
}
} else {
adjustedElementName = null;
Expand Down Expand Up @@ -1922,6 +1986,11 @@ void writeOpenTag(
adjustedElementName);
boolean opensHtmlTemplate = opensHtmlOutputTemplate(
elementName, adjustedElementName, attrs);
HtmlSanitizer.ForeignContentContext templateParent =
currentOutputTemplateForeignContent();
boolean templateParentUnknown = opensHtmlTemplate
&& (templateParent != null
? templateParent : outputForeignContent).isUnknown();
if (opensHtmlTemplate) {
// Preserve the legacy conservative tracker at the template boundary,
// while freezing any parent template context for later restoration.
Expand All @@ -1934,6 +2003,10 @@ void writeOpenTag(
outputElementInForeignContent.set(
openElementStack.size() / 2 - 1,
outputElementForLastOpenTagUsedForeignContentRules);
if (!outputElementForLastOpenTagUsedForeignContentRules) {
int textContent = textContentElementIndex(adjustedElementName);
if (textContent >= 0) { ++openTextContentElements[textContent]; }
}
if (isFormScopeOutputBoundary(
adjustedElementName,
outputElementForLastOpenTagUsedForeignContentRules)) {
Expand All @@ -1943,7 +2016,7 @@ void writeOpenTag(
++outputNestingDepth;
outputContainerElementName = adjustedElementName;
if (opensHtmlTemplate) {
pushOutputTemplateForeignContent();
pushOutputTemplateForeignContent(templateParentUnknown);
}
if ("form".equals(adjustedElementName)
&& !outputElementForLastOpenTagUsedForeignContentRules
Expand All @@ -1952,10 +2025,30 @@ void writeOpenTag(
}
}

/**
* The index in {@link #TEXT_CONTENT_ELEMENT_NAMES} of the name as a browser
* matches an end tag, ignoring ASCII case, or -1.
*/
private static int textContentElementIndex(String elementName) {
int n = elementName.length();
if (n < 3 || n > 9) { return -1; }
String lower = Strings.toLowerCase(elementName);
for (int i = 0; i < TEXT_CONTENT_ELEMENT_NAMES.length; ++i) {
if (TEXT_CONTENT_ELEMENT_NAMES[i].equals(lower)) { return i; }
}
return -1;
}

/** Writes an end tag and keeps the modeled output form pointer in sync. */
private void closeOutputElement(String adjustedElementName, int stackIndex) {
boolean elementIsForeign =
outputElementInForeignContent.get(stackIndex / 2);
if (!elementIsForeign) {
int textContent = textContentElementIndex(adjustedElementName);
if (textContent >= 0 && openTextContentElements[textContent] != 0) {
--openTextContentElements[textContent];
}
}
boolean closesHtmlTemplate = closesActiveHtmlOutputTemplate(
adjustedElementName, elementIsForeign);
if (closesHtmlTemplate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ static final class ForeignContentContext {
/** Match the sanitizer's output nesting limit without growing unchecked. */
private static final int MAX_DEPTH = 256;

/**
* True for the contents of an HTML template, where a browser starts in
* the "in template" insertion mode rather than in body.
*/
private final boolean templateContents;

ForeignContentContext() {
this(false);
}

ForeignContentContext(boolean templateContents) {
this.templateContents = templateContents;
}

/** Elements from the first open foreign root through the current node. */
private final List<OpenElement> openElements = new ArrayList<>();

Expand All @@ -278,6 +292,11 @@ boolean isUnknown() {
return unknown;
}

/** Gives up the context, as reaching an unmodeled one does. */
void forget() {
if (!unknown) { becomeUnknown(); }
}

/** Records a form pointer whose target may be outside the tracked region. */
void markFormElementPointerSet() {
formElementPointerSet = true;
Expand Down Expand Up @@ -474,6 +493,21 @@ boolean hasForeignElementNamed(String elementName) {
return false;
}

/**
* Gives up the context when a start tag that may reach the browser under
* either of two names would be inserted as SVG or MathML under one and as
* HTML under the other, as xmp and the pre a renderer may write for it
* are inside an svg. Call before {@link #processStartTag}.
*/
void forgetIfNamespaceDependsOnName(
String elementName, String otherName, List<String> attrs) {
if (!unknown
&& startTagUsesForeignContentRules(elementName, attrs)
!= startTagUsesForeignContentRules(otherName, attrs)) {
becomeUnknown();
}
}

/** Records an HTML end tag known to be ignored without changing context. */
void ignoreEndTagUnderHtmlRules() {
lastTagUsedForeignContentRules = false;
Expand Down Expand Up @@ -680,6 +714,18 @@ private boolean processHtmlStartTag(
return selfClosing && isForeignContentRoot(elementName);
}

if (templateContents
&& htmlInsertionMode == HtmlInsertionMode.IN_BODY
&& TABLE_STRUCTURE_START_TAG_NAMES.contains(elementName)) {
// In body a browser ignores these, but in template contents one can
// switch the template to a table, column group, table body or row
// mode,
// where tags such as svg or math are ignored or foster-parented
// instead of opening foreign content. That is not modeled here.
becomeUnknown();
return false;
}

Namespace namespace;
if ("svg".equals(elementName)) {
namespace = Namespace.SVG;
Expand Down
Loading
Loading