Skip to content

Commit

Permalink
Comments: Validate new comments before and after comment data is filt…
Browse files Browse the repository at this point in the history
…ered.

This ensures that a Disallowed Comment Keys match will consistently send the comment to the Trash, by checking both the original unmodified comment data and the final filtered comment data.

If the first check has already resulted in a `trash` or `spam` status, the second check is skipped as redundant.

Follow-up to [2894], [3851], [48121], [48575].

Props cfinke, kbrownkd, thompsonsj, mi5t4n, devspace, chaion07, engahmeds3ed, SergeyBiryukov.
Fixes #61827.

git-svn-id: https://develop.svn.wordpress.org/trunk@59267 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Oct 21, 2024
1 parent 815f0c3 commit 309ecbd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/wp-includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2277,9 +2277,14 @@ function wp_new_comment( $commentdata, $wp_error = false ) {
$commentdata['comment_type'] = 'comment';
}

$commentdata['comment_approved'] = wp_allow_comment( $commentdata, $wp_error );

$commentdata = wp_filter_comment( $commentdata );

$commentdata['comment_approved'] = wp_allow_comment( $commentdata, $wp_error );
if ( ! in_array( $commentdata['comment_approved'], array( 'trash', 'spam' ), true ) ) {
// Validate the comment again after filters are applied to comment data.
$commentdata['comment_approved'] = wp_allow_comment( $commentdata, $wp_error );
}

if ( is_wp_error( $commentdata['comment_approved'] ) ) {
return $commentdata['comment_approved'];
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/tests/comment/wpHandleCommentSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,4 +976,41 @@ public function data_should_only_allow_replying_to_an_existing_parent_comment()
'a non-existent parent comment' => array( 'exists' => false ),
);
}

public function test_disallowed_keys_match_gives_approved_status_of_trash() {
$data = array(
'comment_post_ID' => self::$post->ID,
'comment' => 'Comment',
'author' => 'Comment Author',
'email' => '[email protected]',
);

update_option( 'disallowed_keys', "Comment\nfoo" );

$comment = wp_handle_comment_submission( $data );

$this->assertNotWPError( $comment );
$this->assertInstanceOf( 'WP_Comment', $comment );
$this->assertSame( 'trash', $comment->comment_approved );
}

/**
* @ticket 61827
*/
public function test_disallowed_keys_html_match_gives_approved_status_of_trash() {
$data = array(
'comment_post_ID' => self::$post->ID,
'comment' => '<a href=http://example.com/>example</a>',
'author' => 'Comment Author',
'email' => '[email protected]',
);

update_option( 'disallowed_keys', "href=http\nfoo" );

$comment = wp_handle_comment_submission( $data );

$this->assertNotWPError( $comment );
$this->assertInstanceOf( 'WP_Comment', $comment );
$this->assertSame( 'trash', $comment->comment_approved );
}
}

0 comments on commit 309ecbd

Please sign in to comment.