Skip to content

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

Open
mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:fix/66674-kses-noscript
Open

mayankmajeji wants to merge 1 commit into
WordPress:trunkfrom
mayankmajeji:fix/66674-kses-noscript

Conversation

@mayankmajeji

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:
    • no token reaches past that first </noscript (a comment, an attribute value, SCRIPT, STYLE, TEXTAREA, TITLE and similar);
    • every element opened inside is closed, in order, before it. With scripting disabled, </noscript> is ignored while an element such as P or DIV is still open, and a closing tag for an element outside would close the NOSCRIPT early;
    • no NOSCRIPT, PLAINTEXT, SVG or MATH element opens before it;
    • 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, 25 tests):

  • 12 cases where the content after the element is now kept, including the Squarespace and Disqus examples from comment:7 and a post saved with the KSES filters on. All 12 fail on trunk.
  • 13 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 and DIV elements, a closing tag for an element outside, 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 (585 tests, 1,654 assertions), single site and multisite.
  • --group comment, --group html-api, --filter Tests_Post_Filtering: OK. --group formatting shows the same 5 PHPUnit deprecation warnings as trunk.
  • 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, SVG, MathML and block delimiters. For each one, the output was either the sanitized content with the NOSCRIPT element cut out, or the sanitized content before it. Whenever the content after was kept, 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.

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