Repository navigation
KSES: Keep content that follows SVG and MathML markup. - #16120
mukeshpanchal27 wants to merge 1 commit into
Conversation
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]>
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
dmsnell
left a comment
There was a problem hiding this comment.
@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.
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.
<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><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_htmllose content when they save, such as Authors, Contributors, and multisite admins. Everywp_kses_post()call on output is affected too.Cause
src/wp-includes/kses.phpstops processing in two places inside foreign content:breaknear line 1525).could_escape_foreign_content()returns true (break 2near 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.phpruns the four inputs above through two tests:test_content_after_foreign_content_is_keptuses the default HTML API parser. It fails on trunk for all four cases.test_content_after_foreign_content_is_kept_by_legacy_parseruses thewp_kses_force_legacy_parserfilter. 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.
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