Skip to content

KSES: Keep content that follows SVG and MathML markup. - #16120

Open
mukeshpanchal27 wants to merge 1 commit into
WordPress:trunkfrom
mukeshpanchal27:test/kses-foreign-content-truncation
Open

mukeshpanchal27 wants to merge 1 commit into
WordPress:trunkfrom
mukeshpanchal27:test/kses-foreign-content-truncation

Conversation

@mukeshpanchal27

@mukeshpanchal27 mukeshpanchal27 commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Trac ticket: https://core.trac.wordpress.org/ticket/66208

Description

In 7.2, wp_kses() uses the HTML API parser by default. When it meets some SVG or MathML markup, it stops processing and drops all content after that markup. The legacy parser in 7.1 keeps that content.

This PR adds tests only, to show the bug. A fix will follow in this PR after review.

Input HTML API parser (7.2) Legacy parser (7.1)
<p>a</p><math><mi>x</math><p>after</p> <p>a</p> <p>a</p><math><mi>x</math><p>after</p>
<p>1</p><math><mo>+</mo><mtext><b>x</b></mtext></math><p>2</p> (valid MathML) <p>1</p> all kept
<p>a</p><svg><path d="M0"></svg><p>after</p> <p>a</p> <p>a</p><p>after</p>
<p>a</p><svg><p>x</p></svg><p>after</p> <p>a</p> <p>a</p><p>x</p><p>after</p>

Users without unfiltered_html lose content when they save, such as Authors, Contributors, and multisite admins. Every wp_kses_post() call on output is affected too.

Cause

src/wp-includes/kses.php stops processing in two places inside foreign content:

  • It stops when a closing tag does not match the open SVG or MathML element (break near line 1525).
  • It stops when could_escape_foreign_content() returns true (break 2 near line 1771).

It then returns the output up to where the foreign content started: substr( $output, 0, $foreign_content_starts_at ) near line 2024. Dropping the unsafe foreign content is intended. Dropping everything after it is the bug.

Open PR #14841 fixes the same kind of loss for <noscript> only. These tests do not cover <noscript>.

Tests

tests/phpunit/tests/kses/wpKsesForeignContent.php runs the four inputs above through two tests:

  • test_content_after_foreign_content_is_kept uses the default HTML API parser. It fails on trunk for all four cases.
  • test_content_after_foreign_content_is_kept_by_legacy_parser uses the wp_kses_force_legacy_parser filter. It passes for all four cases, which shows that the bug is new in 7.2.

The tests check only that the content before and after the foreign content survives. They do not set what should happen to the SVG or MathML markup itself, so the fix can choose that.

npm run test:php -- --filter Tests_Kses_wpKsesForeignContent
Tests: 8, Assertions: 16, Failures: 4.

Use of AI tools

I used Claude Code to find the bug, write the tests, and draft this description. I reviewed the result.

🤖 Generated with Claude Code

When the HTML API parser in `wp_kses()` meets some SVG or MathML markup, it stops and drops all content after it. The legacy parser keeps that content.

These tests show the bug. They fail with the HTML API parser and pass with the legacy parser.

Follow-up to [64233].

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mukeshpanchal27 mukeshpanchal27 self-assigned this Nov 20, 2026
@mukeshpanchal27
mukeshpanchal27 marked this pull request as ready for review November 20, 2026 07:56
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@dmsnell dmsnell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukeshpanchal27 please see the Make announcement

Knowing where SVG and MathML ends requires the full complexity of the HTML Processor, which isn’t currently in use and not currently able to be used inside wp_kses().

Therefor, when untrusted inputs are processed by wp_kses(), it will not pass through HTML it can’t properly assess, and thus it will stop processing before SVG and MathML which would require more extensive parsing.

Given the notes in the Cause section of this description I thought this would be clear enough that the behavior is intended, and given the note in the linked #14841, that this is not a defect but rather a security-related enhancement, I thought this would be evident.

For now, this is a wontfix. Do you have examples of code that was working in WordPress 7.1 from Core that is broken in 7.2? Note that SVGs and MathML bring in significant sensitivities in terms of security, and it’s appropriate to allow them only from trusted sources (e.g. those with unfiltered_html capabilities).

Enhancing wp_kses() to allow them will require ensuring that once entering SVG or MathML content, that wp_kses() can properly sanitize those elements and their children, as well as properly detect when they end.

The premise of these proposed tests is that it’s trivial to know where the foreign elements end, but take note that the last test-case hard-codes a mis-parse of the HTML, demonstrating the non-triviality of it. We definitely want to avoid asserting that WordPress maintains defects in its parsing.

<!-- this malformed HTML... -->
<p>a</p><svg><p>x</p></svg><p>after</p>

<!-- ...is equivalent to this well-formed HTML -->
<p>a</p><svg></svg><p>x</p><p>after</p>

This is one of countless examples where a naive parsing can lead to tragic mis-parses. In this particular example, the “after” snippet would have had to have been <p>x</p><p>after</p>, because the SVG ended as soon as the <p> appeared.

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.

2 participants