diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php index f2195e2f3335a..406496b511afd 100644 --- a/src/wp-includes/kses.php +++ b/src/wp-includes/kses.php @@ -1383,6 +1383,93 @@ private static function contains_a_block_delimiter( string $text ): bool { return $processor->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