Skip to content

KSES: Keep the content after a NOSCRIPT element when its end is clear. - #16227

Open
mayankmajeji wants to merge 2 commits into
WordPress:trunkfrom
mayankmajeji:fix/66674-kses-noscript
Open

mayankmajeji wants to merge 2 commits into
WordPress:trunkfrom
mayankmajeji:fix/66674-kses-noscript

Conversation

@mayankmajeji

@mayankmajeji mayankmajeji commented Nov 25, 2026 •

Copy link
Copy Markdown

Since the HTML API reimplementation of KSES ([64233]), sanitizing stops at the first <noscript> opening tag. When a user without unfiltered_html saves a post, or someone leaves a comment, everything after the <noscript> is lost:

wp_kses_post( '<p>Before</p><noscript><img src="a.jpg"></noscript><p>After</p>' );
// trunk: <p>Before</p>

This follows the approach @dmsnell described in https://core.trac.wordpress.org/ticket/66674#comment:8. It does not nest NOSCRIPT elements, which is what made #15927 unsafe.

What changes

At an HTML <noscript> opening tag, a new private method skip_noscript_element():

  1. Finds the first </noscript followed by whitespace, / or > in the raw HTML. This is where a browser with scripting enabled ends the element, because it reads the contents as plain text.
  2. Walks the Tag Processor's tokens up to that point. The Tag Processor parses NOSCRIPT contents as HTML, like a browser with scripting disabled.
  3. Removes the element and its contents, and continues after the closing tag, only when both readings end the element in the same place and the contents are a common embed fallback (comment:14):
    • no token reaches past that first </noscript (a comment, an attribute value, IFRAME, STYLE and similar);
    • the only tokens before it are text, comments, and A, IFRAME, IMG and STYLE tags;
    • every A closing tag closes an A element opened inside, and the last A element opened inside is closed before it. An open A element is reconstructed after the NOSCRIPT when scripting is disabled, and a stray </a> closes an A element from outside;
    • a NOSCRIPT closing tag starts right there;
    • the contents hold no block delimiters.

Otherwise, sanitizing stops at the NOSCRIPT element as it does now. The output never contains a NOSCRIPT element, so a browser never has to choose between the two readings. Inside SVG or MathML, nothing changes.

wp_kses_post( '<p>Before</p><noscript><img src="a.jpg"></noscript><p>After</p>' );
// <p>Before</p><p>After</p>

wp_kses_post( 'a<noscript>b<script>c</noscript>d</script>e<h1>site</h1>' );
// a (unchanged)

The contents are removed rather than sanitized and kept. The ticket says that is fine, and keeping them would show fallback content (for example the duplicate <noscript> images in Squarespace exports) to every visitor.

Testing

New tests in tests/phpunit/tests/kses/noscript.php (--filter Tests_Kses_Noscript, 35 tests):

  • 16 cases where the content after the element is now kept, including the Squarespace, Meta Pixel, Disqus and Crowdsignal examples from comment:7, IFRAME and STYLE fallbacks, and a post saved with the KSES filters on. All 16 fail on trunk.
  • 19 cases that must still stop: @dmsnell's SCRIPT example from comment:6, </noscript inside a comment, an attribute value and a TEXTAREA, a nested NOSCRIPT, unclosed P, DIV, B and A elements, an open last A, a stray </a>, a closing tag for an element outside, elements outside the list, a whole page inside, SVG with CDATA, PLAINTEXT, and block delimiters. These pass on trunk and with this change.

Other checks, with PHP 8.4.28, MySQL 9.7.4 and PHPUnit 9.6.39 in the wordpress-develop Docker environment:

  • --group kses: OK (595 tests, 1,664 assertions), single site and multisite.
  • --group comment, --filter Tests_Post_Filtering: OK.
  • The A cases parsed in Chromium 159, Firefox 159 and WebKit, with and without scripting, as described in comment:15.
  • A local differential fuzz run (not included) of 45,000 random inputs mixing NOSCRIPT with comments, CDATA, quotes, raw text elements, nested and unclosed elements, A tags, SVG, MathML and block delimiters. Whenever the content after was kept (6,833 inputs), WP_HTML_Processor (scripting disabled) also closed the NOSCRIPT element at the first raw </noscript. The output never contained a SCRIPT, NOSCRIPT, IFRAME or PLAINTEXT tag or an on* attribute, and sanitizing it again did not change it.
  • PHPCS and PHPStan report no errors on kses.php.

Props sainathpoojary for the first PR and jamesbregenzer for the suggestion to test a Contributor save.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Investigation, code, local testing and PR wording. I reviewed the reasoning, the code and the test results, and I take responsibility for the contribution.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Since the HTML API reimplementation of KSES, sanitizing stops at the first NOSCRIPT opening tag, so everything after it is lost when a user without the unfiltered_html capability saves a post, or someone leaves a comment.

A browser with scripting enabled reads NOSCRIPT contents as plain text up to the first `</noscript`, while the Tag Processor, like a browser with scripting disabled, parses them as HTML. When both readings end the element at the same place, the element and its contents are now removed and sanitizing continues after it. Otherwise, sanitizing still stops there.

See #66674.
@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 mayankmajeji.

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.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Limit the tokens allowed inside a removed NOSCRIPT element to text, comments, and A, IFRAME, IMG and STYLE tags, as suggested in comment:14. An A closing tag must close an A element opened inside, and the last A element opened inside must be closed before the NOSCRIPT closing tag. Otherwise, sanitizing stops before the NOSCRIPT element.

See #66674.
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