From be589d17a51aca23448ba05ebfad41707674f777 Mon Sep 17 00:00:00 2001 From: Sainath Poojary Date: Thu, 12 Nov 2026 18:48:43 +0530 Subject: [PATCH 1/3] KSES: Remove NOSCRIPT elements without discarding the content that follows them. --- src/wp-includes/kses.php | 19 ++++++++++++++++-- tests/phpunit/tests/kses.php | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php index f2195e2f3335a..9ecfbfdcecf05 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,23 @@ 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 NOSCRIPT element along with everything inside of it, + * but continue sanitizing the content which follows it. Since none + * of its contents reach the output, it doesn’t matter how a browser + * would have parsed them. Stray NOSCRIPT closers are also removed. */ - 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( 'y', 'y' ), + 'Stray NOSCRIPT closer' => array( 'ab', 'ab' ), + 'Closer inside an attribute' => array( '

After

', '

After

' ), + 'Closer inside a comment' => array( '

After

', '

After

' ), + 'Unclosed NOSCRIPT truncates' => array( '

Before