Commit e01acb8d1c for wordpress.org
commit e01acb8d1c976a51fcb11f2d20612902e5c08cdc
Author: johnbillion <johnbillion@git.wordpress.org>
Date: Wed Nov 18 13:13:56 2026 +0000
REST API: Harden comment author handling.
Props xknown, jonsurrell, andrewserong.
Built from https://develop.svn.wordpress.org/trunk@65570
git-svn-id: http://core.svn.wordpress.org/trunk@64643 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
index f5739e6b3a..490bd7659e 100644
--- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
+++ b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
@@ -586,7 +586,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
// Limit who can set comment `author`, `author_ip` or `status` to anything other than the default.
- if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) {
+ if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! $this->check_assign_author_permission( $request['author'] ) ) {
return new WP_Error(
'rest_comment_invalid_author',
/* translators: %s: Request parameter. */
@@ -1168,6 +1168,24 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
);
}
+ /*
+ * Reassigning a comment copies the new author's email address onto it,
+ * which is then returned in the response.
+ */
+ if (
+ isset( $request['author'] ) &&
+ (int) $request['author'] !== (int) $comment->user_id &&
+ get_current_user_id() !== (int) $request['author'] &&
+ ! $this->check_assign_author_permission( $request['author'] )
+ ) {
+ return new WP_Error(
+ 'rest_comment_invalid_author',
+ /* translators: %s: Request parameter. */
+ sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author' ),
+ array( 'status' => rest_authorization_required_code() )
+ );
+ }
+
/*
* check_edit_permission() above only establishes that the comment may be
* edited where it currently sits, because 'edit_comment' maps to 'edit_post'
@@ -1215,6 +1233,15 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
);
}
+ /*
+ * Re-sending the current author is a no-op. Otherwise the author's details
+ * would be refreshed from their user account, exposing any change to their
+ * email address to whoever can edit the comment.
+ */
+ if ( isset( $request['author'] ) && (int) $request['author'] === (int) $comment->user_id ) {
+ unset( $request['author'] );
+ }
+
$prepared_args = $this->prepare_item_for_database( $request );
if ( is_wp_error( $prepared_args ) ) {
@@ -2459,6 +2486,18 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
return current_user_can( 'edit_comment', $comment->comment_ID );
}
+ /**
+ * Checks whether the current user can attribute a comment to another user.
+ *
+ * @since 7.1.5
+ *
+ * @param int $author_id ID of the user the comment is being attributed to.
+ * @return bool Whether the comment can be attributed to the user.
+ */
+ protected function check_assign_author_permission( $author_id ) {
+ return current_user_can( 'moderate_comments' ) && is_user_member_of_blog( $author_id );
+ }
+
/**
* Checks a comment author email for validity.
*
diff --git a/wp-includes/version.php b/wp-includes/version.php
index e17fcc791c..cd24148660 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-RC1-65569';
+$wp_version = '7.2-RC1-65570';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.