From e5e5eec3f127ed82a84fb3ac7dda5af9f020681e Mon Sep 17 00:00:00 2001 From: Mayank Majeji <2611064+mayankmajeji@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:35:30 +0530 Subject: [PATCH] KSES: Keep the content after a NOSCRIPT element when its end is clear. 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 `next_delimiter(); } + /** + * Moves past the NOSCRIPT element whose opening tag is the current token, if + * a browser would end it in the same place whether scripting is enabled or not. + * + * With scripting enabled, a browser reads the contents of a NOSCRIPT element as + * plain text up to the first ``. + * With scripting disabled, as in the Tag Processor, it parses them as HTML, and + * the NOSCRIPT closing tag is ignored while an element such as P or DIV is still + * open inside it. The two readings are only taken to agree when: + * + * - no token parsed as HTML reaches past that first `get_span(); + if ( null === $opener ) { + return false; + } + + $contents_start = $opener->start + $opener->length; + if ( 1 !== preg_match( '~]~i', $this->html, $matches, PREG_OFFSET_CAPTURE, $contents_start ) ) { + return false; + } + + $raw_end = $matches[0][1]; + $open_elements = array(); + while ( $this->next_token() ) { + $span = $this->get_span(); + if ( null === $span ) { + return false; + } + + $token_name = $this->get_token_name(); + if ( $raw_end === $span->start ) { + return ( + 'NOSCRIPT' === $token_name && + $this->is_tag_closer() && + empty( $open_elements ) && + ! self::contains_a_block_delimiter( substr( $this->html, $contents_start, $raw_end - $contents_start ) ) + ); + } + + if ( $span->start + $span->length > $raw_end || 'NOSCRIPT' === $token_name ) { + return false; + } + + if ( '#tag' !== $this->get_token_type() ) { + continue; + } + + if ( $this->is_tag_closer() ) { + if ( array_pop( $open_elements ) !== $token_name ) { + return false; + } + continue; + } + + if ( in_array( $token_name, array( 'PLAINTEXT', 'SVG', 'MATH' ), true ) ) { + return false; + } + + if ( + ! WP_HTML_Processor::is_void( $token_name ) && + ! in_array( $token_name, $special_atomic_elements, true ) + ) { + $open_elements[] = $token_name; + } + } + + return false; + } + /** * Returns a sanitized copy of the input HTML. * @@ -1430,9 +1517,17 @@ public function sanitize() { * based on whether the scripting flag is enabled in a browser. * Rely on trusted inputs for producing the appropriate NOSCRIPT * content, and prevent untrusted inputs from generating it. + * + * Remove the element and its contents when both parsing modes + * agree on where it ends, so that the content after it is kept. + * Otherwise, stop, as the rest of the document is ambiguous. */ if ( 'NOSCRIPT' === $token_name && ! $is_closer ) { - break; + if ( 'html' !== $namespace || ! $this->skip_noscript_element( $special_atomic_elements ) ) { + break; + } + + continue; } /* diff --git a/tests/phpunit/tests/kses/noscript.php b/tests/phpunit/tests/kses/noscript.php new file mode 100644 index 0000000000000..f38cbf0a1362b --- /dev/null +++ b/tests/phpunit/tests/kses/noscript.php @@ -0,0 +1,183 @@ +assertEqualHTML( $expected, wp_kses_post( $content ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_noscript_elements_that_end_at_the_same_place() { + return array( + 'Image fallback' => array( + '
Before
After
', + 'Before
After
', + ), + 'Text only' => array( + 'Before
After
', + 'Before
After
', + ), + 'Empty' => array( + 'Before
After
', + 'Before
After
', + ), + 'Upper and mixed case tag names' => array( + 'Before
After
', + 'Before
After
', + ), + 'Whitespace before the closing bracket' => array( + "Before
After
", + 'Before
After
', + ), + 'Solidus after the closing tag name' => array( + 'Before
After
', + 'Before
After
', + ), + 'Two elements' => array( + 'One
Two
Three
', + 'One
Two
Three
', + ), + 'Similar closing tag inside' => array( + 'Before
After
', + 'Before
After
', + ), + 'Squarespace lazy-loaded image' => array( + 'Intro paragraph.
Rest of the post.
', + 'Intro paragraph.
Rest of the post.
', + ), + 'Closed elements inside' => array( + 'Before
After
', + 'Before
After
', + ), + 'Disqus embed code' => array( + 'After
', + 'After
', + ), + ); + } + + /** + * Tests that content is not kept after a NOSCRIPT element that could end in different + * places, depending on whether a browser has scripting enabled. + * + * With scripting enabled, a browser reads the contents of a NOSCRIPT element as plain + * text up to the first `assertEqualHTML( $expected, wp_kses_post( $content ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_noscript_elements_that_could_end_in_different_places() { + return array( + 'Closing tag inside a SCRIPT element' => array( + 'aAfter
', + 'Before
', + ), + 'Nested NOSCRIPT element' => array( + 'Before
Inside
After
', + 'Before
', + ), + 'Unclosed NOSCRIPT element' => array( + 'Before
Inside
', + 'Before
', + ), + 'Unclosed P element inside' => array( + 'Before
Inside
After
', + 'Before
', + ), + 'Unclosed DIV element inside' => array( + 'Before
Inside
After
', + 'Before
', + ), + 'Unmatched closing tag inside' => array( + 'Before
After
', + 'Before
', + ), + 'Closing tag for an element outside' => array( + 'Before
Inside
After
', + 'Before
Before
After
', + 'Before
', + ), + 'PLAINTEXT element inside' => array( + 'Before
After
', + 'Before
', + ), + 'Block delimiter inside' => array( + 'Before
After
', + 'Before
', + ), + ); + } + + /** + * Tests that the content after a NOSCRIPT element is kept when a user without + * the unfiltered_html capability saves a post. + * + * @ticket 66674 + * + * @covers ::wp_insert_post + */ + public function test_post_content_after_noscript_is_saved() { + kses_init_filters(); + + try { + $post_id = self::factory()->post->create( + array( + 'post_content' => 'Before

After
', + ) + ); + } finally { + kses_remove_filters(); + } + + $this->assertEqualHTML( 'Before
After
', get_post( $post_id )->post_content ); + } +}