diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php index f2195e2f3335a..509642a3aafbf 100644 --- a/src/wp-includes/kses.php +++ b/src/wp-includes/kses.php @@ -1393,6 +1393,7 @@ private static function contains_a_block_delimiter( string $text ): bool { */ public function sanitize() { $template_depth = 0; + $noscript_depth = 0; $output = ''; $special_newline_at = PHP_INT_MIN; $foreign_content_starts_at = PHP_INT_MAX; @@ -1430,9 +1431,20 @@ 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, but keep what follows it. */ - if ( 'NOSCRIPT' === $token_name && ! $is_closer ) { - break; + if ( 'NOSCRIPT' === $token_name && 'html' === $namespace ) { + if ( ! $is_closer ) { + ++$noscript_depth; + } elseif ( $noscript_depth > 0 ) { + --$noscript_depth; + } + continue; + } + + if ( $noscript_depth > 0 ) { + continue; } /* diff --git a/tests/phpunit/tests/kses.php b/tests/phpunit/tests/kses.php index 4fe2eb69b197b..b5dec51afcc25 100644 --- a/tests/phpunit/tests/kses.php +++ b/tests/phpunit/tests/kses.php @@ -3022,4 +3022,42 @@ public function test_pre_kses_filter_receives_context_arguments() { $received ); } + + /** + * Ensures that NOSCRIPT elements are removed without losing the content after them. + * + * @ticket 66674 + * + * @covers ::wp_kses + * + * @dataProvider data_noscript_elements + * + * @param string $html Input HTML. + * @param string $expected Expected sanitized HTML. + */ + public function test_wp_kses_removes_noscript_and_keeps_following_content( $html, $expected ) { + $this->assertEqualHTML( $expected, wp_kses_post( $html ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_noscript_elements() { + return array( + 'Paragraphs around NOSCRIPT' => array( '
Before
After
', 'Before
After
' ), + 'Text around NOSCRIPT' => array( 'Hello world.', 'Hello world.' ), + 'Multiple NOSCRIPT elements' => array( 'bd', 'bd' ), + 'Nested NOSCRIPT' => array( 'c', 'c' ), + 'Uppercase NOSCRIPT' => array( 'After
', 'After
' ), + 'Closer inside a comment' => array( 'After
', 'After
' ), + 'Unclosed NOSCRIPT truncates' => array( 'Before
Before
' ), + 'Block comments inside NOSCRIPT' => array( 'a
b
', 'a
b
' ), + 'Newline after NOSCRIPT in PRE' => array( "", "x \nline
\n\nline" ), + 'NOSCRIPT in MathML' => array( '
After
', 'After
' ), + ); + } } diff --git a/tests/phpunit/tests/post/filtering.php b/tests/phpunit/tests/post/filtering.php index a44ad873001fb..4590f89946ec4 100644 --- a/tests/phpunit/tests/post/filtering.php +++ b/tests/phpunit/tests/post/filtering.php @@ -111,4 +111,27 @@ public function test_post_content_nobalance_nextpage_more() { $this->assertEqualHTML( $content, $post->post_content ); } + + /** + * Ensures that a user without `unfiltered_html` keeps the content after a NOSCRIPT element. + * + * @ticket 66674 + */ + public function test_post_content_keeps_content_after_noscript_for_contributor() { + $contributor_id = self::factory()->user->create( array( 'role' => 'contributor' ) ); + wp_set_current_user( $contributor_id ); + $this->assertFalse( current_user_can( 'unfiltered_html' ), 'Contributors should not have unfiltered_html.' ); + + // Attach the save filters the way WordPress does for the current user. + kses_init(); + + $id = wp_insert_post( + array( + 'post_author' => $contributor_id, + 'post_content' => 'Before

After
', + ) + ); + + $this->assertEqualHTML( 'Before
After
', get_post( $id )->post_content ); + } }