Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 96 additions & 1 deletion src/wp-includes/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 `</noscript` followed by whitespace, `/` or `>`.
* 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 `</noscript`;
* - every element opened inside is closed, in order, before it;
* - no NOSCRIPT, PLAINTEXT, SVG or MATH element opens before it;
* - a NOSCRIPT closing tag starts right there; and
* - the contents hold no block delimiters.
*
* When they agree, the processor is left on the closing tag, so that nothing
* from the element reaches the output. Otherwise, the processor may have moved
* forward and the rest of the document should not be used.
*
* @since 7.2.0
* @access private
*
* @param string[] $special_atomic_elements Elements whose contents the Tag Processor
* returns with the opening tag, without a
* separate closing tag token.
* @return bool Whether the processor moved past the NOSCRIPT element.
*/
private function skip_noscript_element( array $special_atomic_elements ): bool {
$opener = $this->get_span();
if ( null === $opener ) {
return false;
}

$contents_start = $opener->start + $opener->length;
if ( 1 !== preg_match( '~</noscript[ \t\n\f\r/>]~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.
*
Expand Down Expand Up @@ -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;
}

/*
Expand Down
183 changes: 183 additions & 0 deletions tests/phpunit/tests/kses/noscript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php
/**
* Tests how wp_kses() handles NOSCRIPT elements in untrusted content.
*
* @group kses
*
* @covers ::wp_kses
* @covers ::wp_sanitize_html_kses
*/
class Tests_Kses_Noscript extends WP_UnitTestCase {

/**
* Tests that a NOSCRIPT element is removed with its contents and the content after it is kept.
*
* @ticket 66674
*
* @dataProvider data_noscript_elements_that_end_at_the_same_place
*
* @param string $content Content to sanitize.
* @param string $expected Expected sanitized content.
*/
public function test_removes_noscript_and_keeps_following_content( $content, $expected ) {
$this->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(
'<p>Before</p><noscript><img src="a.jpg"></noscript><p>After</p>',
'<p>Before</p><p>After</p>',
),
'Text only' => array(
'<p>Before</p><noscript>Enable JavaScript.</noscript><p>After</p>',
'<p>Before</p><p>After</p>',
),
'Empty' => array(
'<p>Before</p><noscript></noscript><p>After</p>',
'<p>Before</p><p>After</p>',
),
'Upper and mixed case tag names' => array(
'<p>Before</p><NOSCRIPT><img src="a.jpg"></NoScript><p>After</p>',
'<p>Before</p><p>After</p>',
),
'Whitespace before the closing bracket' => array(
"<p>Before</p><noscript><img src=\"a.jpg\"></noscript\n><p>After</p>",
'<p>Before</p><p>After</p>',
),
'Solidus after the closing tag name' => array(
'<p>Before</p><noscript><img src="a.jpg"></noscript/><p>After</p>',
'<p>Before</p><p>After</p>',
),
'Two elements' => array(
'<p>One</p><noscript>a</noscript><p>Two</p><noscript>b</noscript><p>Three</p>',
'<p>One</p><p>Two</p><p>Three</p>',
),
'Similar closing tag inside' => array(
'<p>Before</p><noscript><!-- </noscriptx> --></noscript><p>After</p>',
'<p>Before</p><p>After</p>',
),
'Squarespace lazy-loaded image' => array(
'<p>Intro paragraph.</p><noscript><img src="https://images.squarespace-cdn.com/content/v1/abc/photo.jpg" alt="" /></noscript><img class="thumb-image" src="https://images.squarespace-cdn.com/content/v1/abc/photo.jpg" alt="" /><p>Rest of the post.</p>',
'<p>Intro paragraph.</p><img class="thumb-image" src="https://images.squarespace-cdn.com/content/v1/abc/photo.jpg" alt=""><p>Rest of the post.</p>',
),
'Closed elements inside' => array(
'<p>Before</p><noscript><p>Enable <b>JavaScript</b>.</p><div><span>Thanks</span></div></noscript><p>After</p>',
'<p>Before</p><p>After</p>',
),
'Disqus embed code' => array(
'<div id="disqus_thread"></div><noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a></noscript><p>After</p>',
'<div id="disqus_thread"></div><p>After</p>',
),
);
}

/**
* 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 `</noscript`. With scripting disabled, it parses them as HTML.
*
* @ticket 66674
*
* @dataProvider data_noscript_elements_that_could_end_in_different_places
*
* @param string $content Content to sanitize.
* @param string $expected Expected sanitized content.
*/
public function test_stops_at_ambiguous_noscript( $content, $expected ) {
$this->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(
'a<noscript>b<script>c</noscript>d</script>e<h1>site</h1>',
'a',
),
'Closing tag inside a comment' => array(
'<p>Before</p><noscript><!--</noscript><p>Inside</p>--></noscript><p>After</p>',
'<p>Before</p>',
),
'Closing tag inside an attribute value' => array(
'<p>Before</p><noscript><img alt="</noscript><p>Inside</p>"></noscript><p>After</p>',
'<p>Before</p>',
),
'Closing tag inside a TEXTAREA element' => array(
'<p>Before</p><noscript><textarea></noscript><p>Inside</p></textarea></noscript><p>After</p>',
'<p>Before</p>',
),
'Nested NOSCRIPT element' => array(
'<p>Before</p><noscript><noscript></noscript><p>Inside</p></noscript><p>After</p>',
'<p>Before</p>',
),
'Unclosed NOSCRIPT element' => array(
'<p>Before</p><noscript><p>Inside</p>',
'<p>Before</p>',
),
'Unclosed P element inside' => array(
'<p>Before</p><noscript><p>Inside</noscript><p>After</p>',
'<p>Before</p>',
),
'Unclosed DIV element inside' => array(
'<p>Before</p><noscript><div></noscript><p>Inside</p></div></noscript><p>After</p>',
'<p>Before</p>',
),
'Unmatched closing tag inside' => array(
'<p>Before</p><noscript></span></noscript><p>After</p>',
'<p>Before</p>',
),
'Closing tag for an element outside' => array(
'<div><p>Before</p><noscript></div><p>Inside</p></noscript><p>After</p></div>',
'<div><p>Before</p></div>',
),
'SVG element inside' => array(
'<p>Before</p><noscript><svg><![CDATA[ > </noscript> ]]></svg></noscript><p>After</p>',
'<p>Before</p>',
),
'PLAINTEXT element inside' => array(
'<p>Before</p><noscript><plaintext></noscript><p>After</p>',
'<p>Before</p>',
),
'Block delimiter inside' => array(
'<p>Before</p><noscript><!-- wp:paragraph --></noscript><p>After</p>',
'<p>Before</p>',
),
);
}

/**
* 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' => '<p>Before</p><noscript><img src="a.jpg"></noscript><p>After</p>',
)
);
} finally {
kses_remove_filters();
}

$this->assertEqualHTML( '<p>Before</p><p>After</p>', get_post( $post_id )->post_content );
}
}
Loading