Skip to content

Commit 1b7a7bc

Browse files
committed
REST API: Harden comment author handling.
Props xknown, jonsurrell, andrewserong. git-svn-id: https://develop.svn.wordpress.org/trunk@65570 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 45c4c45 commit 1b7a7bc

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ public function create_item_permissions_check( $request ) {
586586
}
587587

588588
// Limit who can set comment `author`, `author_ip` or `status` to anything other than the default.
589-
if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) {
589+
if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! $this->check_assign_author_permission( $request['author'] ) ) {
590590
return new WP_Error(
591591
'rest_comment_invalid_author',
592592
/* translators: %s: Request parameter. */
@@ -1168,6 +1168,24 @@ public function update_item_permissions_check( $request ) {
11681168
);
11691169
}
11701170

1171+
/*
1172+
* Reassigning a comment copies the new author's email address onto it,
1173+
* which is then returned in the response.
1174+
*/
1175+
if (
1176+
isset( $request['author'] ) &&
1177+
(int) $request['author'] !== (int) $comment->user_id &&
1178+
get_current_user_id() !== (int) $request['author'] &&
1179+
! $this->check_assign_author_permission( $request['author'] )
1180+
) {
1181+
return new WP_Error(
1182+
'rest_comment_invalid_author',
1183+
/* translators: %s: Request parameter. */
1184+
sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author' ),
1185+
array( 'status' => rest_authorization_required_code() )
1186+
);
1187+
}
1188+
11711189
/*
11721190
* check_edit_permission() above only establishes that the comment may be
11731191
* edited where it currently sits, because 'edit_comment' maps to 'edit_post'
@@ -1215,6 +1233,15 @@ public function update_item( $request ) {
12151233
);
12161234
}
12171235

1236+
/*
1237+
* Re-sending the current author is a no-op. Otherwise the author's details
1238+
* would be refreshed from their user account, exposing any change to their
1239+
* email address to whoever can edit the comment.
1240+
*/
1241+
if ( isset( $request['author'] ) && (int) $request['author'] === (int) $comment->user_id ) {
1242+
unset( $request['author'] );
1243+
}
1244+
12181245
$prepared_args = $this->prepare_item_for_database( $request );
12191246

12201247
if ( is_wp_error( $prepared_args ) ) {
@@ -2459,6 +2486,18 @@ protected function check_edit_permission( $comment ) {
24592486
return current_user_can( 'edit_comment', $comment->comment_ID );
24602487
}
24612488

2489+
/**
2490+
* Checks whether the current user can attribute a comment to another user.
2491+
*
2492+
* @since 7.1.5
2493+
*
2494+
* @param int $author_id ID of the user the comment is being attributed to.
2495+
* @return bool Whether the comment can be attributed to the user.
2496+
*/
2497+
protected function check_assign_author_permission( $author_id ) {
2498+
return current_user_can( 'moderate_comments' ) && is_user_member_of_blog( $author_id );
2499+
}
2500+
24622501
/**
24632502
* Checks a comment author email for validity.
24642503
*

0 commit comments

Comments
 (0)