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
7 changes: 6 additions & 1 deletion src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3004,7 +3004,12 @@ function wp_update_comment( $commentarr, $wp_error = false ) {

$filter_comment = false;
if ( ! has_filter( 'pre_comment_content', 'wp_filter_kses' ) ) {
$filter_comment = ! user_can( $comment['user_id'] ?? 0, 'unfiltered_html' );
/*
* Filter the comment content based on the capability of the user performing
* the update rather than the comment author, for consistency with the post
* editor. Programmatic updates without a logged-in user are filtered by default.
*/
$filter_comment = ! current_user_can( 'unfiltered_html' );
}

if ( $filter_comment ) {
Expand Down
30 changes: 24 additions & 6 deletions tests/phpunit/tests/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ public function test_update_comment_from_privileged_user_by_privileged_user() {
$this->assertSame( $expected_content, $comment->comment_content );
}

/**
* A privileged user editing an unprivileged user's comment may save HTML
* beyond the comment author's allowed markup, consistent with the post
* editor. On multisite, where no user has the unfiltered_html capability,
* the content is still filtered.
*
* @ticket 57343
*/
public function test_update_comment_from_unprivileged_user_by_privileged_user() {
wp_set_current_user( self::$user_id );

Expand Down Expand Up @@ -168,12 +176,22 @@ public function test_update_comment_from_unprivileged_user_by_privileged_user()
wp_set_current_user( 0 );

$comment = get_comment( $comment_id );
$this->assertEqualHTML(
'<a href="http://example.localhost/something.html" rel="nofollow ugc">click</a>',
$comment->comment_content,
'<body>',
'Comment: ' . $comment->comment_content
);

if ( is_multisite() ) {
$this->assertEqualHTML(
'<a href="http://example.localhost/something.html" rel="nofollow ugc">click</a>',
$comment->comment_content,
'<body>',
'Comment: ' . $comment->comment_content
);
} else {
$this->assertEqualHTML(
'<a href="http://example.localhost/something.html" disallowed=attribute rel="nofollow ugc">click</a>',
$comment->comment_content,
'<body>',
'Comment: ' . $comment->comment_content
);
}
}

/**
Expand Down
112 changes: 112 additions & 0 deletions tests/phpunit/tests/comment/wpUpdateComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* Tests for wp_update_comment().
*
* @group comment
*
* @covers ::wp_update_comment
*/
class Tests_Comment_wpUpdateComment extends WP_UnitTestCase {

protected static $post_id;
protected static $admin_id;
protected static $author_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create();
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::$author_id = $factory->user->create( array( 'role' => 'author' ) );
}

public function tear_down() {
wp_set_current_user( 0 );
parent::tear_down();
}

/**
* Updating a comment with a user lacking the unfiltered_html capability
* should filter the comment content.
*
* @ticket 57343
*/
public function test_wp_update_comment_filters_content_without_unfiltered_html() {
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'user_id' => self::$author_id,
)
);

wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_content' => 'A comment with a <script>alert( 1 );</script> tag.',
)
);

$this->assertSame( 'A comment with a tag.', get_comment( $comment_id )->comment_content );
}

/**
* A user with the unfiltered_html capability editing another user's comment
* should be able to save HTML, consistent with the post editor.
*
* @ticket 57343
*/
public function test_wp_update_comment_preserves_html_for_editing_user_with_unfiltered_html() {
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'user_id' => self::$author_id,
)
);

if ( is_multisite() ) {
// In multisite, only Super Admins have the unfiltered_html capability.
grant_super_admin( self::$admin_id );
}

wp_set_current_user( self::$admin_id );

$this->assertTrue( current_user_can( 'unfiltered_html' ) );

wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_content' => 'A comment with a <script>alert( 1 );</script> tag.',
)
);

$this->assertSame(
'A comment with a <script>alert( 1 );</script> tag.',
get_comment( $comment_id )->comment_content
);
}

/**
* Filtering is based on the editing user, not the comment author: content in
* a comment authored by a privileged user is still filtered when the update
* is performed without the unfiltered_html capability.
*
* @ticket 57343
*/
public function test_wp_update_comment_filters_content_despite_privileged_comment_author() {
$comment_id = self::factory()->comment->create(
array(
'comment_post_ID' => self::$post_id,
'user_id' => self::$admin_id,
)
);

// No logged-in user: a programmatic update keeps filtering by default.
wp_update_comment(
array(
'comment_ID' => $comment_id,
'comment_content' => 'A comment with a <script>alert( 1 );</script> tag.',
)
);

$this->assertSame( 'A comment with a tag.', get_comment( $comment_id )->comment_content );
}
}
Loading