Commit d2e30d3bc5 for wordpress.org
commit d2e30d3bc5fd28edc8c276e2d2c934c9bfd22635
Author: adamsilverstein <adamsilverstein@git.wordpress.org>
Date: Tue Nov 24 06:20:04 2026 +0000
Comments: Tighten note and reaction rules in all handlers.
Ensure all routes enforce that a reaction belongs to its author, carries one of the curated emoji keys and sits under a note on the same post, including Ajax and XML-RPC update routes.
Also, a note reply must have a note on the same post as its parent, and `reaction_summary` only counts reactions that are on their note's post and use a curated key.
Follow-up to [64488], [64999], [65222].
Reviewed by westonruter.
Props westonruter, wildworks, mamaduka.
Fixes #66635.
Built from https://develop.svn.wordpress.org/trunk@65750
git-svn-id: http://core.svn.wordpress.org/trunk@64780 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php
index 076549b218..ba3b9bc285 100644
--- a/wp-admin/includes/ajax-actions.php
+++ b/wp-admin/includes/ajax-actions.php
@@ -1310,12 +1310,33 @@ function wp_ajax_replyto_comment( $action ) {
$comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : 'comment';
+ /*
+ * Notes and reactions are only created through the REST API. The lowercase
+ * ASCII limit also catches spellings the case- and accent-insensitive
+ * collation would match.
+ */
+ if (
+ ! preg_match( '/^[a-z0-9_-]+\z/', $comment_type ) ||
+ in_array( $comment_type, array( 'note', 'reaction' ), true )
+ ) {
+ wp_die( -1 );
+ }
+
$comment_parent = 0;
if ( isset( $_POST['comment_ID'] ) ) {
$comment_parent = absint( $_POST['comment_ID'] );
}
+ // A comment on another post cannot be the parent.
+ if ( $comment_parent ) {
+ $parent = get_comment( $comment_parent );
+
+ if ( $parent && (int) $parent->comment_post_ID !== $comment_post_id ) {
+ $comment_parent = 0;
+ }
+ }
+
$comment_auto_approved = false;
$commentdata = array(
diff --git a/wp-admin/includes/comment.php b/wp-admin/includes/comment.php
index 5f46be6a5c..49c2b3d32c 100644
--- a/wp-admin/includes/comment.php
+++ b/wp-admin/includes/comment.php
@@ -80,6 +80,11 @@ function edit_comment() {
$comment_id = (int) $_POST['comment_ID'];
$comment = get_comment( $comment_id );
+ // Reactions are only validated on creation. Removing one is a delete.
+ if ( $comment && 'reaction' === $comment->comment_type ) {
+ wp_die( __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ), 403 );
+ }
+
/*
* The comment type is not editable from the Edit Comment screen. Changing it
* would turn a private note into a public comment or vice versa, and would
@@ -135,7 +140,7 @@ function edit_comment() {
$comment_status = $_POST['comment_approved'] ?? $comment->comment_approved;
$comment_is_approved = in_array( $comment_status, array( 1, '1', 'approve' ), true );
- // The parent must be a comment of the same type, on the same post, and publicly visible if the comment is approved.
+ // The parent must be a comment of the same type, on the post the comment will be on, and publicly visible if the comment is approved.
if (
! $parent
|| (int) $parent->comment_post_ID !== $comment_post_id
@@ -196,6 +201,19 @@ function edit_comment() {
}
}
+ // The checks above only run when the parent changes. A kept parent must be on the destination post too.
+ if ( $comment && $comment_post_id !== (int) $comment->comment_post_ID ) {
+ $kept_parent = (int) ( isset( $_POST['comment_parent'] ) && is_numeric( $_POST['comment_parent'] ) ? $_POST['comment_parent'] : $comment->comment_parent );
+
+ if ( $kept_parent ) {
+ $parent = get_comment( $kept_parent );
+
+ if ( ! $parent || (int) $parent->comment_post_ID !== $comment_post_id ) {
+ return new WP_Error( 'comment_parent_invalid', __( 'Invalid parent comment.' ) );
+ }
+ }
+ }
+
foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
if ( ! empty( $_POST[ 'hidden_' . $timeunit ] ) && $_POST[ 'hidden_' . $timeunit ] !== $_POST[ $timeunit ] ) {
$_POST['edit_date'] = '1';
@@ -283,6 +301,20 @@ function _wp_check_comment_target_post( WP_Comment $comment, int $post_id ) {
return new WP_Error( 'comment_post_invalid', __( 'Sorry, you are not allowed to move this comment to that post.' ) );
}
+ // Replies and reactions stay on the note's post, so a move would separate them.
+ $children = get_comments(
+ array(
+ 'parent' => $comment->comment_ID,
+ 'type' => array( 'note', 'reaction' ),
+ 'status' => 'any',
+ 'count' => true,
+ )
+ );
+
+ if ( $children ) {
+ return new WP_Error( 'comment_post_invalid', __( 'A note with replies or reactions cannot be moved to another post.' ) );
+ }
+
return true;
}
diff --git a/wp-includes/class-wp-xmlrpc-server.php b/wp-includes/class-wp-xmlrpc-server.php
index 515a2458f9..56448aa6e2 100644
--- a/wp-includes/class-wp-xmlrpc-server.php
+++ b/wp-includes/class-wp-xmlrpc-server.php
@@ -3971,7 +3971,9 @@ class wp_xmlrpc_server extends IXR_Server {
return $this->error;
}
- if ( ! get_comment( $comment_id ) ) {
+ $existing_comment = get_comment( $comment_id );
+
+ if ( ! $existing_comment ) {
return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
}
@@ -3979,6 +3981,11 @@ class wp_xmlrpc_server extends IXR_Server {
return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
}
+ // Reactions are only validated on creation. Removing one is a delete.
+ if ( 'reaction' === $existing_comment->comment_type ) {
+ return new IXR_Error( 403, __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ) );
+ }
+
/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
do_action( 'xmlrpc_call', 'wp.editComment', $args, $this );
$comment = array(
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 490bd7659e..3bb4405905 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
@@ -731,6 +731,14 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
);
}
+ if ( 'note' === $request['type'] && $request['parent'] ) {
+ $parent_check = $this->check_note_parent( absint( $request['parent'] ), absint( $request['post'] ) );
+
+ if ( is_wp_error( $parent_check ) ) {
+ return $parent_check;
+ }
+ }
+
/*
* The canonical reaction key, populated once validated below so the
* stored content matches what was validated (not the raw input).
@@ -841,10 +849,12 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
*
* Scope to active (approved) reactions only — trashed reactions
* are invisible to the user and must not block re-adding the
- * same emoji.
+ * same emoji. The same goes for a reaction stored on another
+ * post, which the reaction summary leaves out.
*/
$existing = get_comments(
array(
+ 'post_id' => absint( $request['post'] ),
'parent' => $request['parent'],
'user_id' => get_current_user_id(),
'type' => 'reaction',
@@ -1035,6 +1045,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
if ( null !== $reaction_key ) {
$matching = get_comments(
array(
+ 'post_id' => absint( $request['post'] ),
'parent' => $request['parent'],
'user_id' => get_current_user_id(),
'type' => 'reaction',
@@ -1233,6 +1244,40 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
);
}
+ if ( 'note' === $comment->comment_type ) {
+ $post_id = isset( $request['post'] ) ? absint( $request['post'] ) : (int) $comment->comment_post_ID;
+ $parent_id = isset( $request['parent'] ) ? absint( $request['parent'] ) : (int) $comment->comment_parent;
+ $is_moving = $post_id !== (int) $comment->comment_post_ID;
+
+ if ( $parent_id && ( $is_moving || $parent_id !== (int) $comment->comment_parent ) ) {
+ $parent_check = $this->check_note_parent( $parent_id, $post_id );
+
+ if ( is_wp_error( $parent_check ) ) {
+ return $parent_check;
+ }
+ }
+
+ // Replies and reactions stay on the note's post, so a move would separate them.
+ if ( $is_moving ) {
+ $children = get_comments(
+ array(
+ 'parent' => $id,
+ 'type' => array( 'note', 'reaction' ),
+ 'status' => 'any',
+ 'count' => true,
+ )
+ );
+
+ if ( $children ) {
+ return new WP_Error(
+ 'rest_comment_has_children',
+ __( 'A note with replies or reactions cannot be moved to another post.' ),
+ array( 'status' => 400 )
+ );
+ }
+ }
+ }
+
/*
* 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
@@ -2010,12 +2055,14 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
'parent' => array(
'description' => __( 'The ID for the parent of the comment.' ),
'type' => 'integer',
+ 'minimum' => 0,
'context' => array( 'view', 'edit', 'embed' ),
'default' => 0,
),
'post' => array(
'description' => __( 'The ID of the associated post object.' ),
'type' => 'integer',
+ 'minimum' => 0,
'context' => array( 'view', 'edit' ),
'default' => 0,
),
@@ -2189,7 +2236,8 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
'description' => __( 'Limit result set to comments of specific parent IDs.' ),
'type' => 'array',
'items' => array(
- 'type' => 'integer',
+ 'type' => 'integer',
+ 'minimum' => 0,
),
);
@@ -2374,26 +2422,27 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
$current_user_id = get_current_user_id();
- $parent_in = $wpdb->prepare(
- 'comment_parent IN (' . implode( ',', array_fill( 0, count( $note_ids ), '%d' ) ) . ')',
+ // Only reactions stored on their note's post count, which is what create_item() writes.
+ $from = "FROM $wpdb->comments AS r INNER JOIN $wpdb->comments AS n";
+ $from .= " ON n.comment_ID = r.comment_parent AND n.comment_post_ID = r.comment_post_ID AND n.comment_type = 'note'";
+ $where = "WHERE r.comment_type = 'reaction' AND r.comment_approved = '1' AND ";
+ $where .= $wpdb->prepare(
+ 'r.comment_parent IN (' . implode( ',', array_fill( 0, count( $note_ids ), '%d' ) ) . ')',
...$note_ids
);
// Query 1: Get aggregated counts per emoji per note.
- $query = "SELECT comment_parent, comment_content, COUNT(*) AS reaction_count FROM $wpdb->comments WHERE comment_type = 'reaction' AND comment_approved = '1' AND ";
- $query .= $parent_in;
- $query .= ' GROUP BY comment_parent, comment_content';
- // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
- $counts = $wpdb->get_results( $query );
+ $query = "SELECT r.comment_parent, r.comment_content, COUNT(*) AS reaction_count $from $where GROUP BY r.comment_parent, r.comment_content";
+ /** @var list<object{ comment_parent: decimal-int-string, comment_content: string, reaction_count: decimal-int-string }&stdClass>|null $counts */
+ $counts = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is prepared above.
// Query 2: Get the current user's own reaction IDs (if logged in).
$my_reactions = array();
if ( $current_user_id ) {
- $query = "SELECT comment_ID, comment_parent, comment_content FROM $wpdb->comments WHERE comment_type = 'reaction' AND comment_approved = '1' AND ";
- $query .= $parent_in;
- $query .= $wpdb->prepare( ' AND user_id = %d', $current_user_id );
- // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
- $user_rows = $wpdb->get_results( $query );
+ $query = "SELECT r.comment_ID, r.comment_parent, r.comment_content $from $where";
+ $query .= $wpdb->prepare( ' AND r.user_id = %d', $current_user_id );
+ /** @var list<object{ comment_ID: decimal-int-string, comment_parent: decimal-int-string, comment_content: string }&stdClass>|null $user_rows */
+ $user_rows = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is prepared above.
if ( $user_rows ) {
foreach ( $user_rows as $row ) {
@@ -2412,15 +2461,27 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
return;
}
+ $allowed_keys = self::get_note_reaction_keys();
+
foreach ( $counts as $row ) {
$note_id = (int) $row->comment_parent;
$emoji_key = wp_strip_all_tags( $row->comment_content );
$key = $note_id . ':' . $emoji_key;
- $this->reaction_summaries[ $note_id ][ $emoji_key ] = array(
- 'count' => (int) $row->reaction_count,
- 'current_user_reaction' => $my_reactions[ $key ] ?? 0,
- );
+ // Only curated emoji keys are summarized.
+ if ( ! in_array( $emoji_key, $allowed_keys, true ) ) {
+ continue;
+ }
+
+ // Rows are grouped on the raw content, so several groups can strip to one key.
+ if ( ! isset( $this->reaction_summaries[ $note_id ][ $emoji_key ] ) ) {
+ $this->reaction_summaries[ $note_id ][ $emoji_key ] = array(
+ 'count' => 0,
+ 'current_user_reaction' => $my_reactions[ $key ] ?? 0,
+ );
+ }
+
+ $this->reaction_summaries[ $note_id ][ $emoji_key ]['count'] += (int) $row->reaction_count;
}
}
@@ -2699,6 +2760,33 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
return true;
}
+ /**
+ * Checks that a note's parent is a note on the same post.
+ *
+ * @since 7.2.0
+ *
+ * @param int $parent_id ID of the parent comment.
+ * @param int $post_id ID of the post the note is, or will be, on.
+ * @return true|WP_Error True if the parent is valid, error object otherwise.
+ */
+ private function check_note_parent( int $parent_id, int $post_id ) {
+ $parent = get_comment( $parent_id );
+
+ if (
+ ! $parent ||
+ 'note' !== $parent->comment_type ||
+ ( $post_id && $post_id !== (int) $parent->comment_post_ID )
+ ) {
+ return new WP_Error(
+ 'rest_comment_invalid_parent',
+ __( 'A note reply must be attached to a note on the same post.' ),
+ array( 'status' => 400 )
+ );
+ }
+
+ return true;
+ }
+
/**
* Checks whether a note belongs to a resolved thread.
*
diff --git a/wp-includes/version.php b/wp-includes/version.php
index a0feae7735..e258978ed0 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-RC2-65749';
+$wp_version = '7.2-RC2-65750';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.