Skip to content
Closed
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
16 changes: 14 additions & 2 deletions src/wp-includes/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/*
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/tests/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<p>Before</p><noscript><img src="a.jpg"></noscript><p>After</p>', '<p>Before</p><p>After</p>' ),
'Text around NOSCRIPT' => array( 'Hello <noscript>Enable JS</noscript> world.', 'Hello world.' ),
'Multiple NOSCRIPT elements' => array( '<noscript>a</noscript>b<noscript>c</noscript>d', 'bd' ),
'Nested NOSCRIPT' => array( '<noscript><noscript>a</noscript>b</noscript>c', 'c' ),
'Uppercase NOSCRIPT' => array( '<NOSCRIPT>x</NOSCRIPT>y', 'y' ),
'Stray NOSCRIPT closer' => array( 'a</noscript>b', 'ab' ),
'Closer inside an attribute' => array( '<noscript><p title="</noscript><img src=x onerror=alert(1)>"></p></noscript><p>After</p>', '<p>After</p>' ),
'Closer inside a comment' => array( '<noscript><!-- </noscript> --><img src=x onerror=alert(1)></noscript><p>After</p>', '<p>After</p>' ),
'Unclosed NOSCRIPT truncates' => array( '<p>Before</p><noscript><b>x</b>', '<p>Before</p>' ),
'Block comments inside NOSCRIPT' => array( '<!-- wp:paragraph --><p>a</p><!-- /wp:paragraph --><noscript><!-- wp:paragraph --></noscript><p>b</p>', '<!-- wp:paragraph --><p>a</p><!-- /wp:paragraph --><p>b</p>' ),
'Newline after NOSCRIPT in PRE' => array( "<pre><noscript>x</noscript>\nline</pre>", "<pre>\n\nline</pre>" ),
'NOSCRIPT in MathML' => array( '<math><noscript>x</noscript></math><p>After</p>', '<math>x</math><p>After</p>' ),
);
}
}
23 changes: 23 additions & 0 deletions tests/phpunit/tests/post/filtering.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<p>Before</p><noscript><img src="a.jpg"></noscript><p>After</p>',
)
);

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