From fab4a1d1e61ef3d3b60b789198cffbb751b091e3 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 11:58:44 -0500 Subject: [PATCH 01/31] Comments: Enforce reaction invariants in every comment writer. The reaction comment type's invariants were only enforced in the REST comments controller. The admin-ajax reply handler accepted `comment_type` and an unbound `comment_parent` from the request, so a user with `edit_post` on any post could store an approved reaction under a note on a post they cannot read. The admin-ajax, Edit Comment screen and XML-RPC editors could then rewrite, reattribute and move reactions through `wp_update_comment()`, which has no type guard. Refuse note and reaction types in `wp_ajax_replyto_comment()`, drop a parent on another post in `wp_new_comment()` the way an invalid-status parent is already dropped, make `wp_update_comment()` refuse reactions, and require edit access to the destination post in `edit_comment()`. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-admin/includes/ajax-actions.php | 9 ++ src/wp-admin/includes/comment.php | 13 ++ src/wp-includes/comment.php | 23 +++ .../phpunit/tests/ajax/wpAjaxEditComment.php | 134 ++++++++++++++++++ .../tests/ajax/wpAjaxReplytoComment.php | 89 ++++++++++++ tests/phpunit/tests/comment.php | 122 ++++++++++++++++ tests/phpunit/tests/xmlrpc/wp/editComment.php | 56 ++++++++ 7 files changed, 446 insertions(+) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 076549b218b82..db60a5f6f1154 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1310,6 +1310,15 @@ function wp_ajax_replyto_comment( $action ) { $comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : 'comment'; + /* + * Notes and reactions are created through the REST API, which validates + * their author, parent, post and content together. Refuse them here so this + * handler cannot store rows that validation would have rejected. + */ + if ( in_array( strtolower( $comment_type ), array( 'note', 'reaction' ), true ) ) { + wp_die( -1 ); + } + $comment_parent = 0; if ( isset( $_POST['comment_ID'] ) ) { diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index 1cfc93144e978..402c285e3470d 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -56,6 +56,19 @@ function edit_comment() { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); } + /* + * 'edit_comment' only covers the post the comment is on. Moving it to + * another post needs edit access to that post as well. + */ + if ( ! empty( $_POST['comment_post_ID'] ) ) { + $comment = get_comment( absint( $_POST['comment_ID'] ) ); + $post_id = absint( $_POST['comment_post_ID'] ); + + if ( $comment && $post_id !== (int) $comment->comment_post_ID && ! current_user_can( 'edit_post', $post_id ) ) { + wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); + } + } + if ( isset( $_POST['newcomment_author'] ) ) { $_POST['comment_author'] = $_POST['newcomment_author']; } diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index b6847266eb162..a9ecee60e300d 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -2532,6 +2532,15 @@ function wp_new_comment( $commentdata, $wp_error = false ) { $commentdata['comment_parent'] = ( 'approved' === $parent_status || 'unapproved' === $parent_status ) ? $commentdata['comment_parent'] : 0; + // A comment on another post cannot be the parent. + if ( $commentdata['comment_parent'] > 0 ) { + $parent = get_comment( $commentdata['comment_parent'] ); + + if ( ! $parent || (int) $parent->comment_post_ID !== $commentdata['comment_post_ID'] ) { + $commentdata['comment_parent'] = 0; + } + } + $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] ); $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 ); @@ -3000,6 +3009,20 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } } + /* + * Reactions are immutable. The REST API validates a reaction's author, + * parent note, post and emoji key together when creating it, and nothing + * re-checks that set on update, so an update could reattribute, move or + * re-key the reaction. Removing a reaction is a delete. + */ + if ( is_string( $comment['comment_type'] ) && 'reaction' === strtolower( $comment['comment_type'] ) ) { + if ( $wp_error ) { + return new WP_Error( 'comment_reaction_immutable', __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ) ); + } else { + return false; + } + } + // Make sure that the comment post ID is valid (if specified). if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { if ( $wp_error ) { diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index fb39bd4dae41b..5c0367b5d15cc 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -242,4 +242,138 @@ public function test_wp_update_comment_data_is_wp_error() { public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) { return new WP_Error( 'comment_wrong', 'wp_update_comment_data filter fails for this comment.', 500 ); } + + /** + * Creates an administrator's open note and approved reaction on a post. + * + * @param int $post_id Post to attach the note to. + * @param int $admin_id User who owns the note and reaction. + * @return int The reaction ID. + */ + private function create_admin_reaction( int $post_id, int $admin_id ): int { + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $admin_id, + 'comment_content' => 'Editorial note', + ) + ); + + return self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => $admin_id, + 'comment_content' => '2764', + ) + ); + } + + /** + * Reactions are immutable. The REST API refuses to update them, and the + * admin-ajax writer has to refuse too, or anyone who can edit the post + * rewrites and reattributes other users' reactions. + * + * @ticket security-2719 + */ + public function test_cannot_edit_reaction(): void { + $this->_setRole( 'contributor' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $draft_id = self::factory()->post->create( + array( + 'post_author' => get_current_user_id(), + 'post_status' => 'draft', + ) + ); + $reaction_id = $this->create_admin_reaction( $draft_id, $admin_id ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $reaction_id; + $_POST['content'] = 'rewritten'; + $_POST['newcomment_author'] = 'Forged'; + $_POST['newcomment_author_email'] = 'forged@example.org'; + $_POST['user_id'] = get_current_user_id(); + + try { + $this->_handleAjax( 'edit-comment' ); + $this->fail( 'The handler should have refused to edit a reaction.' ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $this->assertSame( '2764', $reaction->comment_content ); + $this->assertSame( (string) $admin_id, $reaction->user_id ); + $this->assertSame( '', $reaction->comment_author_email ); + } + + /** + * Moving a comment requires edit access to the destination post, not only + * to the post it is on. + * + * @ticket security-2719 + */ + public function test_cannot_move_comment_to_post_user_cannot_edit(): void { + $this->_setRole( 'contributor' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $draft_id = self::factory()->post->create( + array( + 'post_author' => get_current_user_id(), + 'post_status' => 'draft', + ) + ); + $admin_post_id = self::factory()->post->create( array( 'post_author' => $admin_id ) ); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $draft_id ) ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $comment_id; + $_POST['content'] = 'Moved'; + $_POST['comment_post_ID'] = $admin_post_id; + + try { + $this->_handleAjax( 'edit-comment' ); + $this->fail( 'The handler should have refused to move the comment.' ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( (string) $draft_id, $comment->comment_post_ID ); + } + + /** + * A user who can edit both posts can still move a comment between them. + * + * @ticket security-2719 + */ + public function test_can_move_comment_to_post_user_can_edit(): void { + $this->_setRole( 'editor' ); + + $source_post_id = self::factory()->post->create(); + $target_post_id = self::factory()->post->create(); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $source_post_id ) ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $comment_id; + $_POST['content'] = 'Moved'; + $_POST['comment_post_ID'] = $target_post_id; + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( (string) $target_post_id, $comment->comment_post_ID ); + } } diff --git a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php index b67a7ba828439..dce694214579d 100644 --- a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php @@ -270,4 +270,93 @@ public function test_pre_comments_approved() { public function _pre_comment_approved_filter( $approved, $commentdata ) { return new WP_Error( 'comment_wrong', 'pre_comment_approved filter fails for new comment.', 403 ); } + + /** + * Notes and reactions are created through the REST API, which enforces + * their invariants. The reply handler must not mint them. + * + * @ticket security-2719 + * + * @dataProvider data_restricted_comment_types + * + * @param string $type The comment type to request. + */ + public function test_cannot_reply_with_restricted_comment_type( string $type ): void { + $this->_setRole( 'administrator' ); + + $post_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $parent_id; + $_POST['content'] = '2764'; + $_POST['comment_post_ID'] = $post_id; + $_POST['comment_type'] = $type; + + try { + $this->_handleAjax( 'replyto-comment' ); + $this->fail( 'The handler should have refused the comment type.' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '-1', $e->getMessage() ); + } + + $this->assertSame( + array(), + get_comments( + array( + 'type' => $type, + 'status' => 'any', + 'fields' => 'ids', + ) + ), + 'No comment of the restricted type should have been created.' + ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_restricted_comment_types(): array { + return array( + 'reaction' => array( 'reaction' ), + 'reaction, capitalized' => array( 'Reaction' ), + 'note' => array( 'note' ), + 'note, capitalized' => array( 'Note' ), + ); + } + + /** + * A reply whose parent is on a different post is stored unthreaded. + * + * @ticket security-2719 + */ + public function test_reply_to_comment_on_another_post_is_not_threaded(): void { + $this->_setRole( 'administrator' ); + + $post_id = self::factory()->post->create(); + $other_post = self::factory()->post->create(); + $other_comment = self::factory()->comment->create( array( 'comment_post_ID' => $other_post ) ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $other_comment; + $_POST['content'] = 'Reply across posts'; + $_POST['comment_post_ID'] = $post_id; + + try { + $this->_handleAjax( 'replyto-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA ); + $this->assertInstanceOf( SimpleXMLElement::class, $xml ); + + $comment = get_comment( (int) $xml->response[0]->comment['id'] ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + + $this->assertSame( (string) $post_id, $comment->comment_post_ID ); + $this->assertSame( '0', $comment->comment_parent, 'A parent on another post should be dropped.' ); + } } diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index c5c40b9b34da6..1efcf2fe1ad87 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2654,4 +2654,126 @@ public function test_meta_query_invalidation_preserves_absent_callback() { add_action( 'added_comment_meta', 'wp_cache_set_comments_last_changed' ); } } + + /** + * Reactions are validated as a set when created and are immutable after + * that, for every writer that reaches wp_update_comment(). + * + * @ticket security-2719 + * + * @dataProvider data_reaction_comment_types + * + * @param string $type The stored comment type. + */ + public function test_wp_update_comment_refuses_to_update_a_reaction( string $type ): void { + $user_id = self::factory()->user->create(); + $post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $user_id, + 'comment_content' => 'Editorial note', + ) + ); + $reaction_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => $type, + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => $user_id, + 'comment_content' => '2764', + ) + ); + + $update = array( + 'comment_ID' => $reaction_id, + 'comment_content' => '1f680', + 'user_id' => $user_id + 1, + ); + + $result = wp_update_comment( $update, true ); + $this->assertWPError( $result ); + $this->assertSame( 'comment_reaction_immutable', $result->get_error_code() ); + + $this->assertFalse( wp_update_comment( $update ) ); + + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $this->assertSame( '2764', $reaction->comment_content ); + $this->assertSame( (string) $user_id, $reaction->user_id ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_reaction_comment_types(): array { + return array( + 'canonical type' => array( 'reaction' ), + 'capitalized type' => array( 'Reaction' ), + ); + } + + /** + * A parent on another post is dropped, like a parent in an invalid status. + * + * @ticket security-2719 + */ + public function test_wp_new_comment_drops_parent_from_another_post(): void { + $post_id = self::factory()->post->create(); + $other_post_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $other_post_id ) ); + + $comment_id = wp_new_comment( + array( + 'comment_post_ID' => $post_id, + 'comment_parent' => $parent_id, + 'comment_author' => 'Comment Author', + 'comment_author_email' => 'comment@example.org', + 'comment_author_url' => '', + 'comment_content' => 'Reply across posts', + 'comment_type' => 'comment', + 'user_id' => 0, + ) + ); + + $this->assertIsInt( $comment_id ); + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( '0', $comment->comment_parent ); + } + + /** + * A parent on the same post is kept. + * + * @ticket security-2719 + */ + public function test_wp_new_comment_keeps_parent_from_same_post(): void { + $post_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); + + $comment_id = wp_new_comment( + array( + 'comment_post_ID' => $post_id, + 'comment_parent' => $parent_id, + 'comment_author' => 'Comment Author', + 'comment_author_email' => 'comment@example.org', + 'comment_author_url' => '', + 'comment_content' => 'Threaded reply', + 'comment_type' => 'comment', + 'user_id' => 0, + ) + ); + + $this->assertIsInt( $comment_id ); + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( (string) $parent_id, $comment->comment_parent ); + } } diff --git a/tests/phpunit/tests/xmlrpc/wp/editComment.php b/tests/phpunit/tests/xmlrpc/wp/editComment.php index 0f998df96707a..3b2e6d4948f62 100644 --- a/tests/phpunit/tests/xmlrpc/wp/editComment.php +++ b/tests/phpunit/tests/xmlrpc/wp/editComment.php @@ -93,4 +93,60 @@ public function test_trash_comment() { $this->assertSame( 'trash', get_comment( $comment_id )->comment_approved ); } + + /** + * Reactions are immutable, whoever asks: XML-RPC needs no nonce, so it + * must not be the one writer that lets a reaction be rewritten. + * + * @ticket security-2719 + */ + public function test_cannot_edit_reaction(): void { + $contributor_id = $this->make_user_by_role( 'contributor' ); + $admin_id = $this->make_user_by_role( 'administrator' ); + $draft_id = self::factory()->post->create( + array( + 'post_author' => $contributor_id, + 'post_status' => 'draft', + ) + ); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $draft_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $admin_id, + 'comment_content' => 'Editorial note', + ) + ); + $reaction_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $draft_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => $admin_id, + 'comment_content' => '2764', + ) + ); + + $result = $this->myxmlrpcserver->wp_editComment( + array( + 1, + 'contributor', + 'contributor', + $reaction_id, + array( + 'content' => 'rewritten', + 'author' => 'Forged', + ), + ) + ); + + $this->assertIXRError( $result ); + + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $this->assertSame( '2764', $reaction->comment_content ); + $this->assertSame( $admin_id, (int) $reaction->user_id ); + } } From 1d531469a72d7154806a0e8a5545ff27fcfa3b95 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 11:58:45 -0500 Subject: [PATCH 02/31] REST API: Bind reaction summaries to the note's post and curated keys. `prefetch_reaction_summaries()` grouped on `comment_parent` alone and used the raw content as the key, so any approved row typed `reaction` that pointed at a note was counted under it, and groups that stripped to the same key overwrote one another. Join the parent note and require the reaction to be stored on the note's post, skip keys that are not in `get_note_reaction_keys()`, and accumulate counts. Compare the comment type case-insensitively in the update guard, as the database does, so a row typed `Reaction` cannot be counted as a reaction and edited as a comment. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- .../class-wp-rest-comments-controller.php | 45 +++-- .../rest-api/rest-comments-controller.php | 162 ++++++++++++++++++ 2 files changed, 195 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index f5739e6b3a3f3..4f7406c566ce5 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -1151,8 +1151,11 @@ public function update_item_permissions_check( $request ) { * the note's post reattribute a reaction to another user, move it to a * note on a post they cannot edit, or store a duplicate or invalid * key. Removing a reaction is a delete. + * + * The database compares comment types case-insensitively, so match + * the way it does: a row typed `Reaction` is still a reaction. */ - if ( 'reaction' === $comment->comment_type ) { + if ( 'reaction' === strtolower( $comment->comment_type ) ) { return new WP_Error( 'rest_comment_update_not_allowed', __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ), @@ -2348,23 +2351,29 @@ protected function prefetch_reaction_summaries( $note_ids ) { $current_user_id = get_current_user_id(); $parent_in = $wpdb->prepare( - 'comment_parent IN (' . implode( ',', array_fill( 0, count( $note_ids ), '%d' ) ) . ')', + 'r.comment_parent IN (' . implode( ',', array_fill( 0, count( $note_ids ), '%d' ) ) . ')', ...$note_ids ); + /* + * Join the parent note and require the reaction to be stored on the + * note's post. create_item() guarantees that, so a row where the two + * differ was written by something else and is not this note's reaction. + */ + $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 $parent_in"; + // 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'; + $query = "SELECT r.comment_parent, r.comment_content, COUNT(*) AS reaction_count $from $where GROUP BY r.comment_parent, r.comment_content"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $counts = $wpdb->get_results( $query ); // 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 ); + $query = "SELECT r.comment_ID, r.comment_parent, r.comment_content $from $where"; + $query .= $wpdb->prepare( ' AND r.user_id = %d', $current_user_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $user_rows = $wpdb->get_results( $query ); @@ -2385,15 +2394,27 @@ protected function prefetch_reaction_summaries( $note_ids ) { 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 the curated emoji are reactions; anything else is not 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; } } diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 898b344842f9a..2d816b6809237 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6635,4 +6635,166 @@ public function test_reaction_summary_excludes_trashed_reactions() { $this->assertSame( array(), $response->get_data()['reaction_summary'] ); } + + /** + * Creates an open note by the editor on a new post. + * + * @return array{int, int} The post ID and the note ID. + */ + private function create_editor_note(): array { + $post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => self::$editor_id, + 'comment_content' => 'Test note', + ) + ); + + return array( $post_id, $note_id ); + } + + /** + * Reads a note's reaction summary as the editor. + * + * @param int $note_id The note ID. + * @return array + */ + private function get_reaction_summary( int $note_id ): array { + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'context', 'edit' ); + + $data = rest_get_server()->dispatch( $request )->get_data(); + $this->assertIsArray( $data ); + $this->assertArrayHasKey( 'reaction_summary', $data ); + $this->assertIsArray( $data['reaction_summary'] ); + + /** @var array $summary */ + $summary = $data['reaction_summary']; + + return $summary; + } + + /** + * A reaction stored against another post is not a reaction to this note, + * whatever its parent says. + * + * @ticket security-2719 + */ + public function test_reaction_summary_ignores_reactions_stored_on_another_post(): void { + list( , $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $other_post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$subscriber_id, + 'comment_content' => '2764', + ) + ); + + $this->assertSame( array(), $this->get_reaction_summary( $note_id ) ); + } + + /** + * Only the curated emoji keys are summarized. + * + * @ticket security-2719 + */ + public function test_reaction_summary_ignores_uncurated_keys(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$subscriber_id, + 'comment_content' => 'not-an-emoji-key', + ) + ); + + $this->assertSame( array(), $this->get_reaction_summary( $note_id ) ); + } + + /** + * Rows that strip to the same key are counted together rather than the + * last one overwriting the others. + * + * @ticket security-2719 + */ + public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_key(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + + $heart_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$editor_id, + 'comment_content' => '2764', + ) + ); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$subscriber_id, + 'comment_content' => '2764', + ) + ); + + $summary = $this->get_reaction_summary( $note_id ); + + $this->assertCount( 1, $summary ); + $this->assertArrayHasKey( '2764', $summary ); + $this->assertSame( 2, $summary['2764']['count'] ); + $this->assertSame( $heart_id, $summary['2764']['current_user_reaction'] ); + } + + /** + * The database matches comment types case-insensitively, so the update + * guard has to as well, or a row typed `Reaction` is counted as a + * reaction but edited as a comment. + * + * @ticket security-2719 + */ + public function test_cannot_update_reaction_regardless_of_type_case(): void { + wp_set_current_user( self::$editor_id ); + + list( $post_id, $note_id ) = $this->create_editor_note(); + + $reaction_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'Reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$editor_id, + 'comment_content' => '2764', + ) + ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $reaction_id ); + $request->set_param( 'content', '1f680' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_update_not_allowed', $response, 403 ); + + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $this->assertSame( '2764', $reaction->comment_content ); + } } From 738b1ab980d57e7da0ef11a05640d252e06e820b Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 12:04:01 -0500 Subject: [PATCH 03/31] Comments: Limit reply comment types to ASCII keys. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment type column uses a case- and accent-insensitive collation, so a type such as `reactión` is queried as `reaction` while a `strtolower()` comparison in PHP does not see it. Reject non-ASCII types in `wp_ajax_replyto_comment()` so the note and reaction refusal covers every spelling the database would match. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-admin/includes/ajax-actions.php | 10 +++++++++- tests/phpunit/tests/ajax/wpAjaxReplytoComment.php | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index db60a5f6f1154..aaab264c0f0e3 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1314,8 +1314,16 @@ function wp_ajax_replyto_comment( $action ) { * Notes and reactions are created through the REST API, which validates * their author, parent, post and content together. Refuse them here so this * handler cannot store rows that validation would have rejected. + * + * The database compares comment types with a case- and accent-insensitive + * collation, so a type that only differs from `reaction` by case or accents + * would still be queried as one. Limit the type to ASCII key characters so + * the check below sees every spelling the database would match. */ - if ( in_array( strtolower( $comment_type ), array( 'note', 'reaction' ), true ) ) { + if ( + ! preg_match( '/^[a-z0-9_-]+$/i', $comment_type ) || + in_array( strtolower( $comment_type ), array( 'note', 'reaction' ), true ) + ) { wp_die( -1 ); } diff --git a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php index dce694214579d..39c912a9de720 100644 --- a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php @@ -324,6 +324,7 @@ public function data_restricted_comment_types(): array { 'reaction, capitalized' => array( 'Reaction' ), 'note' => array( 'note' ), 'note, capitalized' => array( 'Note' ), + 'reaction, accented' => array( 'reactión' ), ); } From 98e9c0fb81431f08d9f2f78cc9bf1da8a15c1517 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 12:54:40 -0500 Subject: [PATCH 04/31] Comments: Refuse type changes into or out of notes and reactions. The reaction guard in wp_update_comment() only looked at the stored type. edit_comment() passes the whole request to wp_update_comment(), which merges comment_type and user_id, so an author could reply beneath a note on their own post with an ordinary comment and then edit that reply into a reaction attributed to another user. The forged row passed the summary query because its parent is a note on the same post. Refuse an update whose type crosses into or out of note/reaction, the way the REST API already refuses any type change on update. Plain comment type changes such as comment to pingback keep working. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-includes/comment.php | 23 +++++++ .../phpunit/tests/ajax/wpAjaxEditComment.php | 66 +++++++++++++++++++ tests/phpunit/tests/comment.php | 63 ++++++++++++++++++ 3 files changed, 152 insertions(+) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index a9ecee60e300d..e3d64739f53d4 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3023,6 +3023,29 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } } + /* + * Notes and reactions are validated as a set when the REST API creates + * them, and an update does not repeat that validation. Changing a + * comment into one of those types, or one of them into a plain comment, + * would produce a row that creation would have refused. + */ + $old_type = is_string( $comment['comment_type'] ) ? strtolower( $comment['comment_type'] ) : ''; + $new_type = isset( $commentarr['comment_type'] ) && is_string( $commentarr['comment_type'] ) ? strtolower( $commentarr['comment_type'] ) : $old_type; + + if ( + $old_type !== $new_type && + ( + in_array( $old_type, array( 'note', 'reaction' ), true ) || + in_array( $new_type, array( 'note', 'reaction' ), true ) + ) + ) { + if ( $wp_error ) { + return new WP_Error( 'comment_type_immutable', __( 'Comments cannot be converted to or from notes and reactions.' ) ); + } else { + return false; + } + } + // Make sure that the comment post ID is valid (if specified). if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { if ( $wp_error ) { diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index 5c0367b5d15cc..6d55926eef8e3 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -376,4 +376,70 @@ public function test_can_move_comment_to_post_user_can_edit(): void { $this->assertInstanceOf( WP_Comment::class, $comment ); $this->assertSame( (string) $target_post_id, $comment->comment_post_ID ); } + + /** + * An author who can reply beneath a note on their own post must not be + * able to turn that reply into a reaction attributed to someone else. + * + * @ticket security-2719 + */ + public function test_cannot_convert_comment_into_reaction(): void { + $this->_setRole( 'author' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $post_id = self::factory()->post->create( array( 'post_author' => get_current_user_id() ) ); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $admin_id, + 'comment_content' => 'Editorial note', + ) + ); + + // Step 1: reply beneath the note with an ordinary comment. + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_post_ID'] = $post_id; + $_POST['comment_ID'] = $note_id; + $_POST['content'] = 'Ordinary reply'; + + try { + $this->_handleAjax( 'replyto-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $replies = get_comments( + array( + 'post_id' => $post_id, + 'parent' => $note_id, + 'type' => 'comment', + 'status' => 'all', + 'fields' => 'ids', + ) + ); + $this->assertCount( 1, $replies ); + $reply_id = (int) $replies[0]; + + // Step 2: edit the reply into a reaction owned by the administrator. + $_POST = array(); + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $reply_id; + $_POST['content'] = '2764'; + $_POST['comment_type'] = 'reaction'; + $_POST['user_id'] = $admin_id; + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $reply = get_comment( $reply_id ); + $this->assertInstanceOf( WP_Comment::class, $reply ); + $this->assertSame( 'comment', $reply->comment_type ); + $this->assertSame( 'Ordinary reply', $reply->comment_content ); + $this->assertSame( (string) get_current_user_id(), $reply->user_id ); + } } diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 1efcf2fe1ad87..c669ad1700e2c 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2776,4 +2776,67 @@ public function test_wp_new_comment_keeps_parent_from_same_post(): void { $this->assertInstanceOf( WP_Comment::class, $comment ); $this->assertSame( (string) $parent_id, $comment->comment_parent ); } + + /** + * @ticket security-2719 + * + * @dataProvider data_restricted_comment_types + * + * @param string $type Type the comment must not be converted into. + */ + public function test_wp_update_comment_refuses_to_convert_a_comment_into_a_note_or_reaction( string $type ): void { + $post_id = self::factory()->post->create(); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); + + $result = wp_update_comment( + array( + 'comment_ID' => $comment_id, + 'comment_type' => $type, + ), + true + ); + + $this->assertWPError( $result ); + $this->assertSame( 'comment_type_immutable', $result->get_error_code() ); + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( 'comment', $comment->comment_type ); + } + + /** + * @ticket security-2719 + */ + public function test_wp_update_comment_refuses_to_convert_a_note_into_a_comment(): void { + $post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + + $result = wp_update_comment( + array( + 'comment_ID' => $note_id, + 'comment_type' => 'comment', + ), + true + ); + + $this->assertWPError( $result ); + $this->assertSame( 'comment_type_immutable', $result->get_error_code() ); + } + + /** + * @return array + */ + public function data_restricted_comment_types(): array { + return array( + 'reaction' => array( 'reaction' ), + 'reaction uppercase' => array( 'Reaction' ), + 'note' => array( 'note' ), + ); + } } From 281923ba69db278aa3c8ef6b480c89882df15d61 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 12:57:40 -0500 Subject: [PATCH 05/31] Comments: Limit new comment types to ASCII keys on update. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment_type column uses a case- and accent-insensitive collation, so a type such as "reactión" is matched by the reaction summary query while PHP's strtolower() comparison does not see it as a reaction. Refuse a type change whose new value contains anything outside ASCII key characters, the same limit wp_ajax_replyto_comment() applies. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-includes/comment.php | 6 ++++++ tests/phpunit/tests/comment.php | 1 + 2 files changed, 7 insertions(+) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index e3d64739f53d4..a8f9a651f8f61 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3028,6 +3028,11 @@ function wp_update_comment( $commentarr, $wp_error = false ) { * them, and an update does not repeat that validation. Changing a * comment into one of those types, or one of them into a plain comment, * would produce a row that creation would have refused. + * + * The database compares comment types with a case- and accent-insensitive + * collation, so a new type is also limited to ASCII key characters. That + * way the check below sees every spelling the database would treat as a + * note or reaction. */ $old_type = is_string( $comment['comment_type'] ) ? strtolower( $comment['comment_type'] ) : ''; $new_type = isset( $commentarr['comment_type'] ) && is_string( $commentarr['comment_type'] ) ? strtolower( $commentarr['comment_type'] ) : $old_type; @@ -3035,6 +3040,7 @@ function wp_update_comment( $commentarr, $wp_error = false ) { if ( $old_type !== $new_type && ( + ! preg_match( '/^[a-z0-9_-]*$/', $new_type ) || in_array( $old_type, array( 'note', 'reaction' ), true ) || in_array( $new_type, array( 'note', 'reaction' ), true ) ) diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index c669ad1700e2c..6f2cd73bdefa5 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2836,6 +2836,7 @@ public function data_restricted_comment_types(): array { return array( 'reaction' => array( 'reaction' ), 'reaction uppercase' => array( 'Reaction' ), + 'reaction accented' => array( 'reactión' ), 'note' => array( 'note' ), ); } From 4b266f869e63760e06f807abbbea990e233fbd29 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 12:59:35 -0500 Subject: [PATCH 06/31] Comments: Anchor the comment type pattern to the end of the string. "$" matches before a trailing newline, so "reaction\n" passed the pattern while failing the strtolower() comparison. Use "\z" so the whole value has to be ASCII key characters. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-includes/comment.php | 2 +- tests/phpunit/tests/comment.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index a8f9a651f8f61..80292cf30a49f 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3040,7 +3040,7 @@ function wp_update_comment( $commentarr, $wp_error = false ) { if ( $old_type !== $new_type && ( - ! preg_match( '/^[a-z0-9_-]*$/', $new_type ) || + ! preg_match( '/^[a-z0-9_-]*\z/', $new_type ) || in_array( $old_type, array( 'note', 'reaction' ), true ) || in_array( $new_type, array( 'note', 'reaction' ), true ) ) diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 6f2cd73bdefa5..4f2ae7eb38313 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2837,6 +2837,7 @@ public function data_restricted_comment_types(): array { 'reaction' => array( 'reaction' ), 'reaction uppercase' => array( 'Reaction' ), 'reaction accented' => array( 'reactión' ), + 'reaction newline' => array( "reaction\n" ), 'note' => array( 'note' ), ); } From 4b873e104c9b039788e896d9e3d52e3327655ccb Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 13:18:29 -0500 Subject: [PATCH 07/31] Comments: Refuse case-only changes to note and reaction types. The type guard compared lowercased values, so changing a note's type to "NOTE" was written through. The database still matches that row as a note, but most of core compares comment types strictly, so the row would lose the note-specific permission and summary handling. Compare the raw values instead and accept uppercase only for other types. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-includes/comment.php | 17 +++++++++-------- tests/phpunit/tests/comment.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 80292cf30a49f..46275b5540ae2 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3030,19 +3030,20 @@ function wp_update_comment( $commentarr, $wp_error = false ) { * would produce a row that creation would have refused. * * The database compares comment types with a case- and accent-insensitive - * collation, so a new type is also limited to ASCII key characters. That - * way the check below sees every spelling the database would treat as a - * note or reaction. + * collation while most of core compares them strictly, so the raw values + * are compared here and a new type is limited to ASCII key characters. + * That way a spelling the database would treat as a note or reaction, or + * a case-only change to one, is refused as well. */ - $old_type = is_string( $comment['comment_type'] ) ? strtolower( $comment['comment_type'] ) : ''; - $new_type = isset( $commentarr['comment_type'] ) && is_string( $commentarr['comment_type'] ) ? strtolower( $commentarr['comment_type'] ) : $old_type; + $old_type = is_string( $comment['comment_type'] ) ? $comment['comment_type'] : ''; + $new_type = isset( $commentarr['comment_type'] ) && is_string( $commentarr['comment_type'] ) ? $commentarr['comment_type'] : $old_type; if ( $old_type !== $new_type && ( - ! preg_match( '/^[a-z0-9_-]*\z/', $new_type ) || - in_array( $old_type, array( 'note', 'reaction' ), true ) || - in_array( $new_type, array( 'note', 'reaction' ), true ) + ! preg_match( '/^[a-z0-9_-]*\z/i', $new_type ) || + in_array( strtolower( $old_type ), array( 'note', 'reaction' ), true ) || + in_array( strtolower( $new_type ), array( 'note', 'reaction' ), true ) ) ) { if ( $wp_error ) { diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 4f2ae7eb38313..3dbb5a3c69cdb 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2841,4 +2841,35 @@ public function data_restricted_comment_types(): array { 'note' => array( 'note' ), ); } + + /** + * A case-only change would store a type most of core compares strictly. + * + * @ticket security-2719 + */ + public function test_wp_update_comment_refuses_a_case_only_change_to_a_note_type(): void { + $post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + + $result = wp_update_comment( + array( + 'comment_ID' => $note_id, + 'comment_type' => 'NOTE', + ), + true + ); + + $this->assertWPError( $result ); + $this->assertSame( 'comment_type_immutable', $result->get_error_code() ); + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( 'note', $note->comment_type ); + } } From 946c74d1432f9eb05ed0e14cb561c0ea5a1f62aa Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 13:18:29 -0500 Subject: [PATCH 08/31] Comments: Move a note's reactions with it. The reaction summary now only counts reactions stored on the note's post, and reactions cannot be updated on their own, so moving a note to another post would strand its reactions: hidden from the summary and still blocking the same user from reacting again. Move them with the note in wp_update_comment(), which both the REST API and the admin writers go through. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-includes/comment.php | 21 +++++++++++++++++ tests/phpunit/tests/comment.php | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 46275b5540ae2..1f37b3c7f89d9 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3170,6 +3170,27 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } } + /* + * A reaction only counts on its note's post and cannot be updated on its + * own, so a note's reactions move with it. + */ + if ( 'note' === $old_type && absint( $comment['comment_post_ID'] ) !== absint( $comment_post_id ) ) { + $reaction_ids = get_comments( + array( + 'parent' => $comment_id, + 'type' => 'reaction', + 'status' => 'any', + 'fields' => 'ids', + 'orderby' => 'none', + ) + ); + + foreach ( $reaction_ids as $reaction_id ) { + $wpdb->update( $wpdb->comments, array( 'comment_post_ID' => $comment_post_id ), array( 'comment_ID' => $reaction_id ) ); + clean_comment_cache( $reaction_id ); + } + } + clean_comment_cache( $comment_id ); wp_update_comment_count( $comment_post_id ); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 3dbb5a3c69cdb..9b4598e921ef5 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2872,4 +2872,45 @@ public function test_wp_update_comment_refuses_a_case_only_change_to_a_note_type $this->assertInstanceOf( WP_Comment::class, $note ); $this->assertSame( 'note', $note->comment_type ); } + + /** + * Reactions are immutable and only count on their note's post, so they + * have to travel with the note when it moves. + * + * @ticket security-2719 + */ + public function test_wp_update_comment_moves_a_notes_reactions_with_it(): void { + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_a_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + $reaction_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_a_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'comment_content' => '2764', + ) + ); + + $result = wp_update_comment( + array( + 'comment_ID' => $note_id, + 'comment_post_ID' => $post_b_id, + ), + true + ); + + $this->assertSame( 1, $result ); + + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $this->assertSame( (string) $post_b_id, $reaction->comment_post_ID ); + } } From 144ec65452e930dbb4d545209143e71b8452e6e8 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 13:18:29 -0500 Subject: [PATCH 09/31] Comments: Validate the destination post and parent when moving a comment. edit_comment() skipped the destination check for a post ID of zero, which detached the comment from its post and from the post's capability mapping. It also validated a new parent against the post the comment was leaving, and never checked the parent a moved comment kept, so a comment could end up threaded under a comment on another post. Fetch the comment once, refuse a zero or missing destination post, and require any parent, new or kept, to be on the post the comment will be on. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-admin/includes/comment.php | 42 +++++-- .../phpunit/tests/ajax/wpAjaxEditComment.php | 111 ++++++++++++++++++ 2 files changed, 144 insertions(+), 9 deletions(-) diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index 402c285e3470d..00f400c4a12c0 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -56,15 +56,24 @@ function edit_comment() { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); } + $comment = get_comment( absint( $_POST['comment_ID'] ) ); + + if ( ! $comment ) { + wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); + } + + $post_id = (int) $comment->comment_post_ID; + /* * 'edit_comment' only covers the post the comment is on. Moving it to - * another post needs edit access to that post as well. + * another post needs edit access to that post as well, and a comment + * cannot be detached from its post: with no post, 'edit_comment' would + * no longer map to that post's capabilities. */ - if ( ! empty( $_POST['comment_post_ID'] ) ) { - $comment = get_comment( absint( $_POST['comment_ID'] ) ); + if ( isset( $_POST['comment_post_ID'] ) && absint( $_POST['comment_post_ID'] ) !== $post_id ) { $post_id = absint( $_POST['comment_post_ID'] ); - if ( $comment && $post_id !== (int) $comment->comment_post_ID && ! current_user_can( 'edit_post', $post_id ) ) { + if ( ! $post_id || ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); } } @@ -94,9 +103,7 @@ function edit_comment() { $_POST['comment_parent'] = $comment_parent; - $comment = get_comment( $comment_id ); - - if ( $comment && $comment_parent && $comment_parent !== (int) $comment->comment_parent ) { + if ( $comment_parent && $comment_parent !== (int) $comment->comment_parent ) { if ( ! get_option( 'thread_comments' ) ) { return new WP_Error( 'comment_parent_invalid', __( 'The comment parent cannot be changed because threaded comments are disabled.' ) ); } @@ -110,10 +117,10 @@ 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 !== (int) $comment->comment_post_ID + || (int) $parent->comment_post_ID !== $post_id || $parent->comment_type !== $comment->comment_type || in_array( $parent_status, array( 'spam', 'trash' ), true ) || ( $comment_is_approved && 'approved' !== $parent_status ) @@ -171,6 +178,23 @@ function edit_comment() { } } + /* + * A comment moving to another post cannot stay threaded under a comment + * on the post it leaves. The new-parent checks above only run when the + * parent changes, so cover the parent it keeps here. + */ + if ( $post_id !== (int) $comment->comment_post_ID ) { + $kept_parent = isset( $_POST['comment_parent'] ) ? absint( $_POST['comment_parent'] ) : (int) $comment->comment_parent; + + if ( $kept_parent ) { + $parent = get_comment( $kept_parent ); + + if ( ! $parent || (int) $parent->comment_post_ID !== $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'; diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index 6d55926eef8e3..f558f66e69f95 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -442,4 +442,115 @@ public function test_cannot_convert_comment_into_reaction(): void { $this->assertSame( 'Ordinary reply', $reply->comment_content ); $this->assertSame( (string) get_current_user_id(), $reply->user_id ); } + + /** + * A post ID of zero skips the destination check and would orphan the + * comment, which drops its capability mapping to the post. + * + * @ticket security-2719 + */ + public function test_cannot_detach_comment_from_its_post(): void { + $this->_setRole( 'contributor' ); + + $admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + $draft_id = self::factory()->post->create( + array( + 'post_author' => get_current_user_id(), + 'post_status' => 'draft', + ) + ); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $draft_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => $admin_id, + 'comment_content' => 'Editorial note', + ) + ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $note_id; + $_POST['content'] = 'Editorial note'; + $_POST['comment_post_ID'] = 0; + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $draft_id, $note->comment_post_ID ); + } + + /** + * The new parent has to be on the post the comment is moving to, not the + * one it is leaving. + * + * @ticket security-2719 + */ + public function test_cannot_move_comment_under_parent_on_the_post_it_leaves(): void { + $this->_setRole( 'editor' ); + update_option( 'thread_comments', 1 ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $comment_id; + $_POST['content'] = 'Moved'; + $_POST['comment_post_ID'] = $post_b_id; + $_POST['comment_parent'] = $parent_id; + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $comment = get_comment( $comment_id ); + $this->assertInstanceOf( WP_Comment::class, $comment ); + $this->assertSame( (string) $post_a_id, $comment->comment_post_ID ); + $this->assertSame( '0', $comment->comment_parent ); + } + + /** + * A reply cannot move to another post while still threaded under a + * comment on the post it leaves. + * + * @ticket security-2719 + */ + public function test_cannot_move_reply_away_from_its_parent(): void { + $this->_setRole( 'editor' ); + update_option( 'thread_comments', 1 ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); + $reply_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_a_id, + 'comment_parent' => $parent_id, + ) + ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $reply_id; + $_POST['content'] = 'Moved'; + $_POST['comment_post_ID'] = $post_b_id; + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $reply = get_comment( $reply_id ); + $this->assertInstanceOf( WP_Comment::class, $reply ); + $this->assertSame( (string) $post_a_id, $reply->comment_post_ID ); + } } From 83c8c404383dade6568f5cb9a2590e822f736edc Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 13:18:29 -0500 Subject: [PATCH 10/31] Comments: Anchor the reply type pattern the same way as on update. The value is trimmed first, so "$" was safe here, but use "\z" like wp_update_comment() so the two copies of the rule stay identical. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-admin/includes/ajax-actions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index aaab264c0f0e3..e064617b193cd 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1321,7 +1321,7 @@ function wp_ajax_replyto_comment( $action ) { * the check below sees every spelling the database would match. */ if ( - ! preg_match( '/^[a-z0-9_-]+$/i', $comment_type ) || + ! preg_match( '/^[a-z0-9_-]+\z/i', $comment_type ) || in_array( strtolower( $comment_type ), array( 'note', 'reaction' ), true ) ) { wp_die( -1 ); From 96e7e1f4cf94580775f6422de999dc2e2fef6da0 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 13:18:29 -0500 Subject: [PATCH 11/31] REST API: Require a note reply's parent to be a note on the same post. create_item() only validated the parent of reactions, so a user could thread a note of their own under a note on a post they cannot read by naming it as the parent while targeting a post they can edit. Notes are inserted through wp_insert_comment(), so the wp_new_comment() guard does not cover them. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- .../class-wp-rest-comments-controller.php | 17 ++++++++++++++ .../rest-api/rest-comments-controller.php | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 4f7406c566ce5..f73082c0d793f 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -731,6 +731,23 @@ public function create_item( $request ) { ); } + // A note reply must answer a note on the same post. + if ( ! empty( $request['type'] ) && 'note' === $request['type'] && ! empty( $request['parent'] ) ) { + $parent_note = get_comment( absint( $request['parent'] ) ); + + if ( + ! $parent_note || + 'note' !== $parent_note->comment_type || + ( ! empty( $request['post'] ) && (int) $parent_note->comment_post_ID !== absint( $request['post'] ) ) + ) { + return new WP_Error( + 'rest_comment_invalid_parent', + __( 'A note reply must be attached to a note on the same post.' ), + array( 'status' => 400 ) + ); + } + } + /* * The canonical reaction key, populated once validated below so the * stored content matches what was validated (not the raw input). diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 2d816b6809237..6784f5301304c 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6797,4 +6797,27 @@ public function test_cannot_update_reaction_regardless_of_type_case(): void { $this->assertInstanceOf( WP_Comment::class, $reaction ); $this->assertSame( '2764', $reaction->comment_content ); } + + /** + * A note reply has to sit on the same post as the note it answers, or a + * user could thread their own note under one on a post they cannot read. + * + * @ticket security-2719 + */ + public function test_cannot_create_note_reply_under_note_on_another_post(): void { + list( , $note_id ) = $this->create_editor_note(); + $post_b_id = self::factory()->post->create(); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); + $request->set_param( 'type', 'note' ); + $request->set_param( 'post', $post_b_id ); + $request->set_param( 'parent', $note_id ); + $request->set_param( 'content', 'Reply from another post' ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); + } } From b7ac133e3aef9ec9eca9f5b8d1b3423aaaf1d0c1 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 13:35:52 -0500 Subject: [PATCH 12/31] REST API: Use a Yoda condition for the note parent post check. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- .../rest-api/endpoints/class-wp-rest-comments-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index f73082c0d793f..b771377372eb1 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -738,7 +738,7 @@ public function create_item( $request ) { if ( ! $parent_note || 'note' !== $parent_note->comment_type || - ( ! empty( $request['post'] ) && (int) $parent_note->comment_post_ID !== absint( $request['post'] ) ) + ( ! empty( $request['post'] ) && absint( $request['post'] ) !== (int) $parent_note->comment_post_ID ) ) { return new WP_Error( 'rest_comment_invalid_parent', From ba248067866b192937b60d900770d91ad9438517 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 13:38:58 -0500 Subject: [PATCH 13/31] Comments: Shorten the inline comments added for the reaction fixes. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- src/wp-admin/includes/ajax-actions.php | 10 ++------ src/wp-admin/includes/comment.php | 12 +++------ src/wp-includes/comment.php | 25 ++++++------------- .../class-wp-rest-comments-controller.php | 14 +++-------- 4 files changed, 16 insertions(+), 45 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index e064617b193cd..a2bab7fcd2ba1 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1311,14 +1311,8 @@ function wp_ajax_replyto_comment( $action ) { $comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : 'comment'; /* - * Notes and reactions are created through the REST API, which validates - * their author, parent, post and content together. Refuse them here so this - * handler cannot store rows that validation would have rejected. - * - * The database compares comment types with a case- and accent-insensitive - * collation, so a type that only differs from `reaction` by case or accents - * would still be queried as one. Limit the type to ASCII key characters so - * the check below sees every spelling the database would match. + * Notes and reactions are only created through the REST API. The ASCII + * limit also catches spellings the accent-insensitive collation would match. */ if ( ! preg_match( '/^[a-z0-9_-]+\z/i', $comment_type ) || diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index 00f400c4a12c0..d0be0a6fb2715 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -65,10 +65,8 @@ function edit_comment() { $post_id = (int) $comment->comment_post_ID; /* - * 'edit_comment' only covers the post the comment is on. Moving it to - * another post needs edit access to that post as well, and a comment - * cannot be detached from its post: with no post, 'edit_comment' would - * no longer map to that post's capabilities. + * Moving to another post needs edit access there. Without a post, + * 'edit_comment' no longer maps to any post's capabilities. */ if ( isset( $_POST['comment_post_ID'] ) && absint( $_POST['comment_post_ID'] ) !== $post_id ) { $post_id = absint( $_POST['comment_post_ID'] ); @@ -178,11 +176,7 @@ function edit_comment() { } } - /* - * A comment moving to another post cannot stay threaded under a comment - * on the post it leaves. The new-parent checks above only run when the - * parent changes, so cover the parent it keeps here. - */ + // The checks above only run when the parent changes. A kept parent must be on the destination post too. if ( $post_id !== (int) $comment->comment_post_ID ) { $kept_parent = isset( $_POST['comment_parent'] ) ? absint( $_POST['comment_parent'] ) : (int) $comment->comment_parent; diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 1f37b3c7f89d9..4636f97794f7d 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3010,10 +3010,8 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } /* - * Reactions are immutable. The REST API validates a reaction's author, - * parent note, post and emoji key together when creating it, and nothing - * re-checks that set on update, so an update could reattribute, move or - * re-key the reaction. Removing a reaction is a delete. + * Reactions are immutable. Their author, parent note, post and key are + * only validated together on creation. Removing one is a delete. */ if ( is_string( $comment['comment_type'] ) && 'reaction' === strtolower( $comment['comment_type'] ) ) { if ( $wp_error ) { @@ -3024,16 +3022,10 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } /* - * Notes and reactions are validated as a set when the REST API creates - * them, and an update does not repeat that validation. Changing a - * comment into one of those types, or one of them into a plain comment, - * would produce a row that creation would have refused. - * - * The database compares comment types with a case- and accent-insensitive - * collation while most of core compares them strictly, so the raw values - * are compared here and a new type is limited to ASCII key characters. - * That way a spelling the database would treat as a note or reaction, or - * a case-only change to one, is refused as well. + * A comment cannot become a note or reaction, or stop being one, since + * only creation validates those types. Raw values are compared and the + * new type limited to ASCII because the collation is case- and + * accent-insensitive while core compares types strictly. */ $old_type = is_string( $comment['comment_type'] ) ? $comment['comment_type'] : ''; $new_type = isset( $commentarr['comment_type'] ) && is_string( $commentarr['comment_type'] ) ? $commentarr['comment_type'] : $old_type; @@ -3170,10 +3162,7 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } } - /* - * A reaction only counts on its note's post and cannot be updated on its - * own, so a note's reactions move with it. - */ + // Reactions only count on their note's post and cannot be updated alone, so they move with the note. if ( 'note' === $old_type && absint( $comment['comment_post_ID'] ) !== absint( $comment_post_id ) ) { $reaction_ids = get_comments( array( diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index b771377372eb1..66c20149ba9e3 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -1167,10 +1167,8 @@ public function update_item_permissions_check( $request ) { * is re-checked here. Allowing an update would let anyone who can edit * the note's post reattribute a reaction to another user, move it to a * note on a post they cannot edit, or store a duplicate or invalid - * key. Removing a reaction is a delete. - * - * The database compares comment types case-insensitively, so match - * the way it does: a row typed `Reaction` is still a reaction. + * key. Removing a reaction is a delete. strtolower() matches the + * database's case-insensitive type comparison. */ if ( 'reaction' === strtolower( $comment->comment_type ) ) { return new WP_Error( @@ -2372,11 +2370,7 @@ protected function prefetch_reaction_summaries( $note_ids ) { ...$note_ids ); - /* - * Join the parent note and require the reaction to be stored on the - * note's post. create_item() guarantees that, so a row where the two - * differ was written by something else and is not this note's reaction. - */ + // 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 $parent_in"; @@ -2418,7 +2412,7 @@ protected function prefetch_reaction_summaries( $note_ids ) { $emoji_key = wp_strip_all_tags( $row->comment_content ); $key = $note_id . ':' . $emoji_key; - // Only the curated emoji are reactions; anything else is not summarized. + // Only curated emoji keys are summarized. if ( ! in_array( $emoji_key, $allowed_keys, true ) ) { continue; } From 22c761e2edf1515599042f5c8b458e06bcb9bb88 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 9 Nov 2026 13:48:51 -0500 Subject: [PATCH 14/31] Tests: Reference the Trac ticket for the note and reaction hardening tests. See #66635. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX --- tests/phpunit/tests/ajax/wpAjaxEditComment.php | 14 +++++++------- tests/phpunit/tests/ajax/wpAjaxReplytoComment.php | 4 ++-- tests/phpunit/tests/comment.php | 14 +++++++------- .../tests/rest-api/rest-comments-controller.php | 10 +++++----- tests/phpunit/tests/xmlrpc/wp/editComment.php | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index f558f66e69f95..029c6447ffb72 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -278,7 +278,7 @@ private function create_admin_reaction( int $post_id, int $admin_id ): int { * admin-ajax writer has to refuse too, or anyone who can edit the post * rewrites and reattributes other users' reactions. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_edit_reaction(): void { $this->_setRole( 'contributor' ); @@ -317,7 +317,7 @@ public function test_cannot_edit_reaction(): void { * Moving a comment requires edit access to the destination post, not only * to the post it is on. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_move_comment_to_post_user_cannot_edit(): void { $this->_setRole( 'contributor' ); @@ -352,7 +352,7 @@ public function test_cannot_move_comment_to_post_user_cannot_edit(): void { /** * A user who can edit both posts can still move a comment between them. * - * @ticket security-2719 + * @ticket 66635 */ public function test_can_move_comment_to_post_user_can_edit(): void { $this->_setRole( 'editor' ); @@ -381,7 +381,7 @@ public function test_can_move_comment_to_post_user_can_edit(): void { * An author who can reply beneath a note on their own post must not be * able to turn that reply into a reaction attributed to someone else. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_convert_comment_into_reaction(): void { $this->_setRole( 'author' ); @@ -447,7 +447,7 @@ public function test_cannot_convert_comment_into_reaction(): void { * A post ID of zero skips the destination check and would orphan the * comment, which drops its capability mapping to the post. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_detach_comment_from_its_post(): void { $this->_setRole( 'contributor' ); @@ -489,7 +489,7 @@ public function test_cannot_detach_comment_from_its_post(): void { * The new parent has to be on the post the comment is moving to, not the * one it is leaving. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_move_comment_under_parent_on_the_post_it_leaves(): void { $this->_setRole( 'editor' ); @@ -522,7 +522,7 @@ public function test_cannot_move_comment_under_parent_on_the_post_it_leaves(): v * A reply cannot move to another post while still threaded under a * comment on the post it leaves. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_move_reply_away_from_its_parent(): void { $this->_setRole( 'editor' ); diff --git a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php index 39c912a9de720..beb1820aefca1 100644 --- a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php @@ -275,7 +275,7 @@ public function _pre_comment_approved_filter( $approved, $commentdata ) { * Notes and reactions are created through the REST API, which enforces * their invariants. The reply handler must not mint them. * - * @ticket security-2719 + * @ticket 66635 * * @dataProvider data_restricted_comment_types * @@ -331,7 +331,7 @@ public function data_restricted_comment_types(): array { /** * A reply whose parent is on a different post is stored unthreaded. * - * @ticket security-2719 + * @ticket 66635 */ public function test_reply_to_comment_on_another_post_is_not_threaded(): void { $this->_setRole( 'administrator' ); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 9b4598e921ef5..9dc611d4e9f1c 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2659,7 +2659,7 @@ public function test_meta_query_invalidation_preserves_absent_callback() { * Reactions are validated as a set when created and are immutable after * that, for every writer that reaches wp_update_comment(). * - * @ticket security-2719 + * @ticket 66635 * * @dataProvider data_reaction_comment_types * @@ -2721,7 +2721,7 @@ public function data_reaction_comment_types(): array { /** * A parent on another post is dropped, like a parent in an invalid status. * - * @ticket security-2719 + * @ticket 66635 */ public function test_wp_new_comment_drops_parent_from_another_post(): void { $post_id = self::factory()->post->create(); @@ -2751,7 +2751,7 @@ public function test_wp_new_comment_drops_parent_from_another_post(): void { /** * A parent on the same post is kept. * - * @ticket security-2719 + * @ticket 66635 */ public function test_wp_new_comment_keeps_parent_from_same_post(): void { $post_id = self::factory()->post->create(); @@ -2778,7 +2778,7 @@ public function test_wp_new_comment_keeps_parent_from_same_post(): void { } /** - * @ticket security-2719 + * @ticket 66635 * * @dataProvider data_restricted_comment_types * @@ -2805,7 +2805,7 @@ public function test_wp_update_comment_refuses_to_convert_a_comment_into_a_note_ } /** - * @ticket security-2719 + * @ticket 66635 */ public function test_wp_update_comment_refuses_to_convert_a_note_into_a_comment(): void { $post_id = self::factory()->post->create(); @@ -2845,7 +2845,7 @@ public function data_restricted_comment_types(): array { /** * A case-only change would store a type most of core compares strictly. * - * @ticket security-2719 + * @ticket 66635 */ public function test_wp_update_comment_refuses_a_case_only_change_to_a_note_type(): void { $post_id = self::factory()->post->create(); @@ -2877,7 +2877,7 @@ public function test_wp_update_comment_refuses_a_case_only_change_to_a_note_type * Reactions are immutable and only count on their note's post, so they * have to travel with the note when it moves. * - * @ticket security-2719 + * @ticket 66635 */ public function test_wp_update_comment_moves_a_notes_reactions_with_it(): void { $post_a_id = self::factory()->post->create(); diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 6784f5301304c..0c31b6c732809 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6683,7 +6683,7 @@ private function get_reaction_summary( int $note_id ): array { * A reaction stored against another post is not a reaction to this note, * whatever its parent says. * - * @ticket security-2719 + * @ticket 66635 */ public function test_reaction_summary_ignores_reactions_stored_on_another_post(): void { list( , $note_id ) = $this->create_editor_note(); @@ -6706,7 +6706,7 @@ public function test_reaction_summary_ignores_reactions_stored_on_another_post() /** * Only the curated emoji keys are summarized. * - * @ticket security-2719 + * @ticket 66635 */ public function test_reaction_summary_ignores_uncurated_keys(): void { list( $post_id, $note_id ) = $this->create_editor_note(); @@ -6729,7 +6729,7 @@ public function test_reaction_summary_ignores_uncurated_keys(): void { * Rows that strip to the same key are counted together rather than the * last one overwriting the others. * - * @ticket security-2719 + * @ticket 66635 */ public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_key(): void { list( $post_id, $note_id ) = $this->create_editor_note(); @@ -6769,7 +6769,7 @@ public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_ke * guard has to as well, or a row typed `Reaction` is counted as a * reaction but edited as a comment. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_update_reaction_regardless_of_type_case(): void { wp_set_current_user( self::$editor_id ); @@ -6802,7 +6802,7 @@ public function test_cannot_update_reaction_regardless_of_type_case(): void { * A note reply has to sit on the same post as the note it answers, or a * user could thread their own note under one on a post they cannot read. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_create_note_reply_under_note_on_another_post(): void { list( , $note_id ) = $this->create_editor_note(); diff --git a/tests/phpunit/tests/xmlrpc/wp/editComment.php b/tests/phpunit/tests/xmlrpc/wp/editComment.php index 3b2e6d4948f62..860591eab1f85 100644 --- a/tests/phpunit/tests/xmlrpc/wp/editComment.php +++ b/tests/phpunit/tests/xmlrpc/wp/editComment.php @@ -98,7 +98,7 @@ public function test_trash_comment() { * Reactions are immutable, whoever asks: XML-RPC needs no nonce, so it * must not be the one writer that lets a reaction be rewritten. * - * @ticket security-2719 + * @ticket 66635 */ public function test_cannot_edit_reaction(): void { $contributor_id = $this->make_user_by_role( 'contributor' ); From 3544bc9d436dca8dc149b041639849df38aebfc9 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Tue, 10 Nov 2026 07:59:25 -0500 Subject: [PATCH 15/31] Comments: Move note and reaction rules out of wp_update_comment(). Low-level writers stay unrestricted, as elsewhere in core. The request handlers that take user input, edit_comment(), wp.editComment and the REST controller, refuse reaction edits and type changes themselves. Notes can no longer be moved to another post through those handlers, which replaces moving their reactions along with them. --- src/wp-admin/includes/comment.php | 30 ++- src/wp-includes/class-wp-xmlrpc-server.php | 9 +- src/wp-includes/comment.php | 54 ------ .../class-wp-rest-comments-controller.php | 9 + .../phpunit/tests/ajax/wpAjaxEditComment.php | 85 +++++++++ tests/phpunit/tests/comment.php | 178 +----------------- .../rest-api/rest-comments-controller.php | 23 +++ 7 files changed, 161 insertions(+), 227 deletions(-) diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index d0be0a6fb2715..8109f9e4d5b9a 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -62,16 +62,40 @@ function edit_comment() { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); } - $post_id = (int) $comment->comment_post_ID; + $post_id = (int) $comment->comment_post_ID; + $old_type = strtolower( $comment->comment_type ); + + // Reactions are only validated on creation. Removing one is a delete. + if ( 'reaction' === $old_type ) { + wp_die( __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ) ); + } + + /* + * A comment cannot become a note or reaction, or stop being one. The new + * type is limited to ASCII because the collation is case- and + * accent-insensitive while core compares types strictly. + */ + if ( isset( $_POST['comment_type'] ) && $_POST['comment_type'] !== $comment->comment_type ) { + $new_type = is_string( $_POST['comment_type'] ) ? $_POST['comment_type'] : ''; + + if ( + ! preg_match( '/^[a-z0-9_-]*\z/i', $new_type ) || + 'note' === $old_type || + in_array( strtolower( $new_type ), array( 'note', 'reaction' ), true ) + ) { + wp_die( __( 'Sorry, you are not allowed to change the comment type.' ) ); + } + } /* * Moving to another post needs edit access there. Without a post, - * 'edit_comment' no longer maps to any post's capabilities. + * 'edit_comment' no longer maps to any post's capabilities. A note + * stays on its post, since its reactions would not follow it. */ if ( isset( $_POST['comment_post_ID'] ) && absint( $_POST['comment_post_ID'] ) !== $post_id ) { $post_id = absint( $_POST['comment_post_ID'] ); - if ( ! $post_id || ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { + if ( 'note' === $old_type || ! $post_id || ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); } } diff --git a/src/wp-includes/class-wp-xmlrpc-server.php b/src/wp-includes/class-wp-xmlrpc-server.php index 4ac680065f036..4b6b52ae80683 100644 --- a/src/wp-includes/class-wp-xmlrpc-server.php +++ b/src/wp-includes/class-wp-xmlrpc-server.php @@ -3930,7 +3930,9 @@ public function wp_editComment( $args ) { 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.' ) ); } @@ -3938,6 +3940,11 @@ public function wp_editComment( $args ) { 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' === strtolower( $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/src/wp-includes/comment.php b/src/wp-includes/comment.php index 4636f97794f7d..42b99b4a47005 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -3009,42 +3009,6 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } } - /* - * Reactions are immutable. Their author, parent note, post and key are - * only validated together on creation. Removing one is a delete. - */ - if ( is_string( $comment['comment_type'] ) && 'reaction' === strtolower( $comment['comment_type'] ) ) { - if ( $wp_error ) { - return new WP_Error( 'comment_reaction_immutable', __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ) ); - } else { - return false; - } - } - - /* - * A comment cannot become a note or reaction, or stop being one, since - * only creation validates those types. Raw values are compared and the - * new type limited to ASCII because the collation is case- and - * accent-insensitive while core compares types strictly. - */ - $old_type = is_string( $comment['comment_type'] ) ? $comment['comment_type'] : ''; - $new_type = isset( $commentarr['comment_type'] ) && is_string( $commentarr['comment_type'] ) ? $commentarr['comment_type'] : $old_type; - - if ( - $old_type !== $new_type && - ( - ! preg_match( '/^[a-z0-9_-]*\z/i', $new_type ) || - in_array( strtolower( $old_type ), array( 'note', 'reaction' ), true ) || - in_array( strtolower( $new_type ), array( 'note', 'reaction' ), true ) - ) - ) { - if ( $wp_error ) { - return new WP_Error( 'comment_type_immutable', __( 'Comments cannot be converted to or from notes and reactions.' ) ); - } else { - return false; - } - } - // Make sure that the comment post ID is valid (if specified). if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { if ( $wp_error ) { @@ -3162,24 +3126,6 @@ function wp_update_comment( $commentarr, $wp_error = false ) { } } - // Reactions only count on their note's post and cannot be updated alone, so they move with the note. - if ( 'note' === $old_type && absint( $comment['comment_post_ID'] ) !== absint( $comment_post_id ) ) { - $reaction_ids = get_comments( - array( - 'parent' => $comment_id, - 'type' => 'reaction', - 'status' => 'any', - 'fields' => 'ids', - 'orderby' => 'none', - ) - ); - - foreach ( $reaction_ids as $reaction_id ) { - $wpdb->update( $wpdb->comments, array( 'comment_post_ID' => $comment_post_id ), array( 'comment_ID' => $reaction_id ) ); - clean_comment_cache( $reaction_id ); - } - } - clean_comment_cache( $comment_id ); wp_update_comment_count( $comment_post_id ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 66c20149ba9e3..7df7fd1917f63 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -1249,6 +1249,15 @@ public function update_item( $request ) { array( 'status' => 403 ) ); } + + // A note stays on its post, since its reactions would not follow it. + if ( 'note' === $comment->comment_type && $post->ID !== (int) $comment->comment_post_ID ) { + return new WP_Error( + 'rest_comment_note_post_immutable', + __( 'Notes cannot be moved to another post.' ), + array( 'status' => 400 ) + ); + } } if ( empty( $prepared_args ) && isset( $request['status'] ) ) { diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index 029c6447ffb72..8f2a097375e5b 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -553,4 +553,89 @@ public function test_cannot_move_reply_away_from_its_parent(): void { $this->assertInstanceOf( WP_Comment::class, $reply ); $this->assertSame( (string) $post_a_id, $reply->comment_post_ID ); } + + /** + * Moving a note would leave its reactions on the post it left. + * + * @ticket 66635 + */ + public function test_cannot_move_note_to_another_post(): void { + $this->_setRole( 'editor' ); + + $source_post_id = self::factory()->post->create(); + $target_post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $source_post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $note_id; + $_POST['content'] = 'Moved'; + $_POST['comment_post_ID'] = $target_post_id; + + try { + $this->_handleAjax( 'edit-comment' ); + $this->fail( 'The handler should have refused to move the note.' ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $source_post_id, $note->comment_post_ID ); + } + + /** + * A note cannot stop being one, including by a change of case. + * + * @ticket 66635 + * + * @dataProvider data_types_a_note_cannot_become + * + * @param string $type The requested comment type. + */ + public function test_cannot_convert_note_into_another_type( string $type ): void { + $this->_setRole( 'editor' ); + + $post_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $note_id; + $_POST['content'] = 'Converted'; + $_POST['comment_type'] = $type; + + try { + $this->_handleAjax( 'edit-comment' ); + $this->fail( 'The handler should have refused to change the note type.' ); + } catch ( WPAjaxDieStopException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( 'note', $note->comment_type ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_types_a_note_cannot_become(): array { + return array( + 'comment' => array( 'comment' ), + 'uppercase note' => array( 'NOTE' ), + ); + } } diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index 9dc611d4e9f1c..a1edb8320b8ed 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2655,69 +2655,6 @@ public function test_meta_query_invalidation_preserves_absent_callback() { } } - /** - * Reactions are validated as a set when created and are immutable after - * that, for every writer that reaches wp_update_comment(). - * - * @ticket 66635 - * - * @dataProvider data_reaction_comment_types - * - * @param string $type The stored comment type. - */ - public function test_wp_update_comment_refuses_to_update_a_reaction( string $type ): void { - $user_id = self::factory()->user->create(); - $post_id = self::factory()->post->create(); - $note_id = self::factory()->comment->create( - array( - 'comment_post_ID' => $post_id, - 'comment_type' => 'note', - 'comment_approved' => 0, - 'user_id' => $user_id, - 'comment_content' => 'Editorial note', - ) - ); - $reaction_id = self::factory()->comment->create( - array( - 'comment_post_ID' => $post_id, - 'comment_type' => $type, - 'comment_parent' => $note_id, - 'comment_approved' => 1, - 'user_id' => $user_id, - 'comment_content' => '2764', - ) - ); - - $update = array( - 'comment_ID' => $reaction_id, - 'comment_content' => '1f680', - 'user_id' => $user_id + 1, - ); - - $result = wp_update_comment( $update, true ); - $this->assertWPError( $result ); - $this->assertSame( 'comment_reaction_immutable', $result->get_error_code() ); - - $this->assertFalse( wp_update_comment( $update ) ); - - $reaction = get_comment( $reaction_id ); - $this->assertInstanceOf( WP_Comment::class, $reaction ); - $this->assertSame( '2764', $reaction->comment_content ); - $this->assertSame( (string) $user_id, $reaction->user_id ); - } - - /** - * Data provider. - * - * @return array - */ - public function data_reaction_comment_types(): array { - return array( - 'canonical type' => array( 'reaction' ), - 'capitalized type' => array( 'Reaction' ), - ); - } - /** * A parent on another post is dropped, like a parent in an invalid status. * @@ -2778,120 +2715,23 @@ public function test_wp_new_comment_keeps_parent_from_same_post(): void { } /** - * @ticket 66635 - * - * @dataProvider data_restricted_comment_types - * - * @param string $type Type the comment must not be converted into. - */ - public function test_wp_update_comment_refuses_to_convert_a_comment_into_a_note_or_reaction( string $type ): void { - $post_id = self::factory()->post->create(); - $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); - - $result = wp_update_comment( - array( - 'comment_ID' => $comment_id, - 'comment_type' => $type, - ), - true - ); - - $this->assertWPError( $result ); - $this->assertSame( 'comment_type_immutable', $result->get_error_code() ); - - $comment = get_comment( $comment_id ); - $this->assertInstanceOf( WP_Comment::class, $comment ); - $this->assertSame( 'comment', $comment->comment_type ); - } - - /** - * @ticket 66635 - */ - public function test_wp_update_comment_refuses_to_convert_a_note_into_a_comment(): void { - $post_id = self::factory()->post->create(); - $note_id = self::factory()->comment->create( - array( - 'comment_post_ID' => $post_id, - 'comment_type' => 'note', - 'comment_approved' => 0, - ) - ); - - $result = wp_update_comment( - array( - 'comment_ID' => $note_id, - 'comment_type' => 'comment', - ), - true - ); - - $this->assertWPError( $result ); - $this->assertSame( 'comment_type_immutable', $result->get_error_code() ); - } - - /** - * @return array - */ - public function data_restricted_comment_types(): array { - return array( - 'reaction' => array( 'reaction' ), - 'reaction uppercase' => array( 'Reaction' ), - 'reaction accented' => array( 'reactión' ), - 'reaction newline' => array( "reaction\n" ), - 'note' => array( 'note' ), - ); - } - - /** - * A case-only change would store a type most of core compares strictly. - * - * @ticket 66635 - */ - public function test_wp_update_comment_refuses_a_case_only_change_to_a_note_type(): void { - $post_id = self::factory()->post->create(); - $note_id = self::factory()->comment->create( - array( - 'comment_post_ID' => $post_id, - 'comment_type' => 'note', - 'comment_approved' => 0, - ) - ); - - $result = wp_update_comment( - array( - 'comment_ID' => $note_id, - 'comment_type' => 'NOTE', - ), - true - ); - - $this->assertWPError( $result ); - $this->assertSame( 'comment_type_immutable', $result->get_error_code() ); - - $note = get_comment( $note_id ); - $this->assertInstanceOf( WP_Comment::class, $note ); - $this->assertSame( 'note', $note->comment_type ); - } - - /** - * Reactions are immutable and only count on their note's post, so they - * have to travel with the note when it moves. + * The rules for notes and reactions belong to the request handlers. + * wp_update_comment() stays unrestricted, like other low-level writers. * * @ticket 66635 */ - public function test_wp_update_comment_moves_a_notes_reactions_with_it(): void { - $post_a_id = self::factory()->post->create(); - $post_b_id = self::factory()->post->create(); + public function test_wp_update_comment_does_not_restrict_reactions(): void { + $post_id = self::factory()->post->create(); $note_id = self::factory()->comment->create( array( - 'comment_post_ID' => $post_a_id, + 'comment_post_ID' => $post_id, 'comment_type' => 'note', 'comment_approved' => 0, ) ); $reaction_id = self::factory()->comment->create( array( - 'comment_post_ID' => $post_a_id, + 'comment_post_ID' => $post_id, 'comment_type' => 'reaction', 'comment_parent' => $note_id, 'comment_approved' => 1, @@ -2901,8 +2741,8 @@ public function test_wp_update_comment_moves_a_notes_reactions_with_it(): void { $result = wp_update_comment( array( - 'comment_ID' => $note_id, - 'comment_post_ID' => $post_b_id, + 'comment_ID' => $reaction_id, + 'comment_content' => '1f680', ), true ); @@ -2911,6 +2751,6 @@ public function test_wp_update_comment_moves_a_notes_reactions_with_it(): void { $reaction = get_comment( $reaction_id ); $this->assertInstanceOf( WP_Comment::class, $reaction ); - $this->assertSame( (string) $post_b_id, $reaction->comment_post_ID ); + $this->assertSame( '1f680', $reaction->comment_content ); } } diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 0c31b6c732809..3e5a93b3b50b4 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6798,6 +6798,29 @@ public function test_cannot_update_reaction_regardless_of_type_case(): void { $this->assertSame( '2764', $reaction->comment_content ); } + /** + * Moving a note would leave its reactions on the post it left. + * + * @ticket 66635 + */ + public function test_cannot_move_note_to_another_post(): void { + wp_set_current_user( self::$editor_id ); + + list( $post_id, $note_id ) = $this->create_editor_note(); + + $other_post_id = self::factory()->post->create(); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'post', $other_post_id ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_note_post_immutable', $response, 400 ); + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $post_id, $note->comment_post_ID ); + } + /** * A note reply has to sit on the same post as the note it answers, or a * user could thread their own note under one on a post they cannot read. From f0fbb5bdabb50cc6868c0ce5af45fbacc969bbb4 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Tue, 10 Nov 2026 08:15:33 -0500 Subject: [PATCH 16/31] Comments: Drop a cross-post reply parent in the ajax handler. wp_new_comment() goes back to its trunk behavior, in keeping with leaving the core comment functions unrestricted. The reply handler is the writer that takes the parent from the request. --- src/wp-admin/includes/ajax-actions.php | 9 ++++ src/wp-includes/comment.php | 9 ---- tests/phpunit/tests/comment.php | 59 -------------------------- 3 files changed, 9 insertions(+), 68 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index a2bab7fcd2ba1..40f71e3c82976 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1327,6 +1327,15 @@ function wp_ajax_replyto_comment( $action ) { $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/src/wp-includes/comment.php b/src/wp-includes/comment.php index 42b99b4a47005..b6847266eb162 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -2532,15 +2532,6 @@ function wp_new_comment( $commentdata, $wp_error = false ) { $commentdata['comment_parent'] = ( 'approved' === $parent_status || 'unapproved' === $parent_status ) ? $commentdata['comment_parent'] : 0; - // A comment on another post cannot be the parent. - if ( $commentdata['comment_parent'] > 0 ) { - $parent = get_comment( $commentdata['comment_parent'] ); - - if ( ! $parent || (int) $parent->comment_post_ID !== $commentdata['comment_post_ID'] ) { - $commentdata['comment_parent'] = 0; - } - } - $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] ); $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 ); diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php index a1edb8320b8ed..d66dc69308f62 100644 --- a/tests/phpunit/tests/comment.php +++ b/tests/phpunit/tests/comment.php @@ -2655,65 +2655,6 @@ public function test_meta_query_invalidation_preserves_absent_callback() { } } - /** - * A parent on another post is dropped, like a parent in an invalid status. - * - * @ticket 66635 - */ - public function test_wp_new_comment_drops_parent_from_another_post(): void { - $post_id = self::factory()->post->create(); - $other_post_id = self::factory()->post->create(); - $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $other_post_id ) ); - - $comment_id = wp_new_comment( - array( - 'comment_post_ID' => $post_id, - 'comment_parent' => $parent_id, - 'comment_author' => 'Comment Author', - 'comment_author_email' => 'comment@example.org', - 'comment_author_url' => '', - 'comment_content' => 'Reply across posts', - 'comment_type' => 'comment', - 'user_id' => 0, - ) - ); - - $this->assertIsInt( $comment_id ); - - $comment = get_comment( $comment_id ); - $this->assertInstanceOf( WP_Comment::class, $comment ); - $this->assertSame( '0', $comment->comment_parent ); - } - - /** - * A parent on the same post is kept. - * - * @ticket 66635 - */ - public function test_wp_new_comment_keeps_parent_from_same_post(): void { - $post_id = self::factory()->post->create(); - $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); - - $comment_id = wp_new_comment( - array( - 'comment_post_ID' => $post_id, - 'comment_parent' => $parent_id, - 'comment_author' => 'Comment Author', - 'comment_author_email' => 'comment@example.org', - 'comment_author_url' => '', - 'comment_content' => 'Threaded reply', - 'comment_type' => 'comment', - 'user_id' => 0, - ) - ); - - $this->assertIsInt( $comment_id ); - - $comment = get_comment( $comment_id ); - $this->assertInstanceOf( WP_Comment::class, $comment ); - $this->assertSame( (string) $parent_id, $comment->comment_parent ); - } - /** * The rules for notes and reactions belong to the request handlers. * wp_update_comment() stays unrestricted, like other low-level writers. From 4db6f951d7324be89a1c1d4dc3fad27b298aebc1 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Thu, 12 Nov 2026 14:23:11 -0800 Subject: [PATCH 17/31] Apply batched suggestions from code review Co-authored-by: Weston Ruter --- src/wp-admin/includes/ajax-actions.php | 2 +- src/wp-admin/includes/comment.php | 6 +++--- .../endpoints/class-wp-rest-comments-controller.php | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 40f71e3c82976..1a2295b7613d5 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1316,7 +1316,7 @@ function wp_ajax_replyto_comment( $action ) { */ if ( ! preg_match( '/^[a-z0-9_-]+\z/i', $comment_type ) || - in_array( strtolower( $comment_type ), array( 'note', 'reaction' ), true ) + in_array( $comment_type, array( 'note', 'reaction' ), true ) ) { wp_die( -1 ); } diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index 8109f9e4d5b9a..a1ee2aacf5212 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -56,7 +56,7 @@ function edit_comment() { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); } - $comment = get_comment( absint( $_POST['comment_ID'] ) ); + $comment = get_comment( (int) $_POST['comment_ID'] ); if ( ! $comment ) { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); @@ -93,7 +93,7 @@ function edit_comment() { * stays on its post, since its reactions would not follow it. */ if ( isset( $_POST['comment_post_ID'] ) && absint( $_POST['comment_post_ID'] ) !== $post_id ) { - $post_id = absint( $_POST['comment_post_ID'] ); + $post_id = (int) $_POST['comment_post_ID']; if ( 'note' === $old_type || ! $post_id || ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); @@ -202,7 +202,7 @@ function edit_comment() { // The checks above only run when the parent changes. A kept parent must be on the destination post too. if ( $post_id !== (int) $comment->comment_post_ID ) { - $kept_parent = isset( $_POST['comment_parent'] ) ? absint( $_POST['comment_parent'] ) : (int) $comment->comment_parent; + $kept_parent = (int) ( $_POST['comment_parent'] ?? $comment->comment_parent ); if ( $kept_parent ) { $parent = get_comment( $kept_parent ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 7df7fd1917f63..e32c930271618 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -733,12 +733,12 @@ public function create_item( $request ) { // A note reply must answer a note on the same post. if ( ! empty( $request['type'] ) && 'note' === $request['type'] && ! empty( $request['parent'] ) ) { - $parent_note = get_comment( absint( $request['parent'] ) ); + $parent_note = get_comment( $request['parent'] ); if ( ! $parent_note || 'note' !== $parent_note->comment_type || - ( ! empty( $request['post'] ) && absint( $request['post'] ) !== (int) $parent_note->comment_post_ID ) + ( ! empty( $request['post'] ) && $request['post'] !== (int) $parent_note->comment_post_ID ) ) { return new WP_Error( 'rest_comment_invalid_parent', @@ -2381,7 +2381,7 @@ protected function prefetch_reaction_summaries( $note_ids ) { // 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\''; + $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 $parent_in"; // Query 1: Get aggregated counts per emoji per note. From 5383e31e3b5b64749ee1367eb7a781db6c0d324b Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Fri, 13 Nov 2026 10:42:25 -0800 Subject: [PATCH 18/31] Comments: Limit reply comment types to lowercase ASCII keys. Dropping strtolower() from the note and reaction check let 'Note' and 'Reaction' through, because the type pattern was case-insensitive. The collation treats those as the built-in types, so the handler created the rows it is meant to refuse. The pattern is now case-sensitive, which keeps the check free of strtolower(). A reply with an uppercase custom type is refused too. See #66635. Claude-Session: https://claude.ai/code/session_018vwYjSZR5rD8K6SgpLNQFg --- src/wp-admin/includes/ajax-actions.php | 7 ++++--- tests/phpunit/tests/ajax/wpAjaxReplytoComment.php | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 1a2295b7613d5..ba3b9bc2851a1 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -1311,11 +1311,12 @@ 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 ASCII - * limit also catches spellings the accent-insensitive collation would match. + * 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/i', $comment_type ) || + ! preg_match( '/^[a-z0-9_-]+\z/', $comment_type ) || in_array( $comment_type, array( 'note', 'reaction' ), true ) ) { wp_die( -1 ); diff --git a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php index beb1820aefca1..b97967669a3ba 100644 --- a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php @@ -325,6 +325,7 @@ public function data_restricted_comment_types(): array { 'note' => array( 'note' ), 'note, capitalized' => array( 'Note' ), 'reaction, accented' => array( 'reactión' ), + 'uppercase custom type' => array( 'Custom' ), ); } From 9c697bb3a3448e2ac41da51d510b22ca630c5724 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Fri, 13 Nov 2026 10:42:25 -0800 Subject: [PATCH 19/31] Comments: Send a 403 when edit_comment() refuses a request. The refusals added for notes and reactions died with the default 500, which reads as a server fault rather than a denied request. The post ID comparison also casts with (int) like the assignment below it, so a negative ID is no longer compared as its absolute value. See #66635. Claude-Session: https://claude.ai/code/session_018vwYjSZR5rD8K6SgpLNQFg --- src/wp-admin/includes/comment.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index a1ee2aacf5212..6105685743396 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -59,7 +59,7 @@ function edit_comment() { $comment = get_comment( (int) $_POST['comment_ID'] ); if ( ! $comment ) { - wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); + wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ), 403 ); } $post_id = (int) $comment->comment_post_ID; @@ -67,7 +67,7 @@ function edit_comment() { // Reactions are only validated on creation. Removing one is a delete. if ( 'reaction' === $old_type ) { - wp_die( __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ) ); + wp_die( __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ), 403 ); } /* @@ -83,7 +83,7 @@ function edit_comment() { 'note' === $old_type || in_array( strtolower( $new_type ), array( 'note', 'reaction' ), true ) ) { - wp_die( __( 'Sorry, you are not allowed to change the comment type.' ) ); + wp_die( __( 'Sorry, you are not allowed to change the comment type.' ), 403 ); } } @@ -92,11 +92,11 @@ function edit_comment() { * 'edit_comment' no longer maps to any post's capabilities. A note * stays on its post, since its reactions would not follow it. */ - if ( isset( $_POST['comment_post_ID'] ) && absint( $_POST['comment_post_ID'] ) !== $post_id ) { + if ( isset( $_POST['comment_post_ID'] ) && (int) $_POST['comment_post_ID'] !== $post_id ) { $post_id = (int) $_POST['comment_post_ID']; if ( 'note' === $old_type || ! $post_id || ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { - wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); + wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ), 403 ); } } From 2e27bfd579c3a05b9e5e70b717cfc330de562160 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Fri, 13 Nov 2026 10:42:25 -0800 Subject: [PATCH 20/31] REST API: Quote the note type as a string in the reaction summary query. A double-quoted literal is read as an identifier under ANSI_QUOTES and in SQLite, so the join would fail there. The PHP string is double-quoted instead, which still avoids escaping. The parent ID list is appended straight from $wpdb->prepare(), so no interpolated variable sits between the prepare call and the query. See #66635. Claude-Session: https://claude.ai/code/session_018vwYjSZR5rD8K6SgpLNQFg --- .../endpoints/class-wp-rest-comments-controller.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index e32c930271618..3b8a6c4268038 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -2374,16 +2374,15 @@ protected function prefetch_reaction_summaries( $note_ids ) { $current_user_id = get_current_user_id(); - $parent_in = $wpdb->prepare( + // 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 ); - // 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 $parent_in"; - // Query 1: Get aggregated counts per emoji per note. $query = "SELECT r.comment_parent, r.comment_content, COUNT(*) AS reaction_count $from $where GROUP BY r.comment_parent, r.comment_content"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared From eb12398774ed26a512965064cc6e32db2b65b3ec Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Sat, 21 Nov 2026 20:51:55 -0800 Subject: [PATCH 21/31] Comments: Follow trunk's policy for moving notes and comments. Trunk now checks the destination post and refuses type changes in edit_comment(), and lets a note move to a post that supports notes. Drop the overlapping checks and the note-move refusals in edit_comment() and the REST API, keeping the reaction and kept-parent checks trunk lacks. --- src/wp-admin/includes/comment.php | 18 ++++------ .../class-wp-rest-comments-controller.php | 15 +++----- .../phpunit/tests/ajax/wpAjaxEditComment.php | 35 ------------------- .../rest-api/rest-comments-controller.php | 23 ------------ 4 files changed, 10 insertions(+), 81 deletions(-) diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index 66d384f5584d0..c63bb212053f0 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -58,17 +58,6 @@ function edit_comment() { wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ) ); } - $comment = get_comment( (int) $_POST['comment_ID'] ); - - if ( ! $comment ) { - wp_die( __( 'Sorry, you are not allowed to edit comments on this post.' ), 403 ); - } - - // Reactions are only validated on creation. Removing one is a delete. - if ( 'reaction' === strtolower( $comment->comment_type ) ) { - wp_die( __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ), 403 ); - } - if ( isset( $_POST['newcomment_author'] ) ) { $_POST['comment_author'] = $_POST['newcomment_author']; } @@ -91,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' === strtolower( $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 @@ -209,7 +203,7 @@ 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) ( $_POST['comment_parent'] ?? $comment->comment_parent ); + $kept_parent = isset( $_POST['comment_parent'] ) && is_int( $_POST['comment_parent'] ) ? $_POST['comment_parent'] : (int) $comment->comment_parent; if ( $kept_parent ) { $parent = get_comment( $kept_parent ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 39672e898a047..312f3f1ebc4a9 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -732,8 +732,10 @@ public function create_item( $request ) { } // A note reply must answer a note on the same post. - if ( ! empty( $request['type'] ) && 'note' === $request['type'] && ! empty( $request['parent'] ) ) { - $parent_note = get_comment( $request['parent'] ); + $parent_id = $request['parent']; + + if ( 'note' === $request['type'] && is_int( $parent_id ) && $parent_id > 0 ) { + $parent_note = get_comment( $parent_id ); if ( ! $parent_note || @@ -1276,15 +1278,6 @@ public function update_item( $request ) { array( 'status' => 403 ) ); } - - // A note stays on its post, since its reactions would not follow it. - if ( 'note' === $comment->comment_type && $post->ID !== (int) $comment->comment_post_ID ) { - return new WP_Error( - 'rest_comment_note_post_immutable', - __( 'Notes cannot be moved to another post.' ), - array( 'status' => 400 ) - ); - } } if ( empty( $prepared_args ) && isset( $request['status'] ) ) { diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index 8f2a097375e5b..4207c7ca4b6eb 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -554,41 +554,6 @@ public function test_cannot_move_reply_away_from_its_parent(): void { $this->assertSame( (string) $post_a_id, $reply->comment_post_ID ); } - /** - * Moving a note would leave its reactions on the post it left. - * - * @ticket 66635 - */ - public function test_cannot_move_note_to_another_post(): void { - $this->_setRole( 'editor' ); - - $source_post_id = self::factory()->post->create(); - $target_post_id = self::factory()->post->create(); - $note_id = self::factory()->comment->create( - array( - 'comment_post_ID' => $source_post_id, - 'comment_type' => 'note', - 'comment_approved' => 0, - ) - ); - - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $note_id; - $_POST['content'] = 'Moved'; - $_POST['comment_post_ID'] = $target_post_id; - - try { - $this->_handleAjax( 'edit-comment' ); - $this->fail( 'The handler should have refused to move the note.' ); - } catch ( WPAjaxDieStopException $e ) { - unset( $e ); - } - - $note = get_comment( $note_id ); - $this->assertInstanceOf( WP_Comment::class, $note ); - $this->assertSame( (string) $source_post_id, $note->comment_post_ID ); - } - /** * A note cannot stop being one, including by a change of case. * diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index a603cae56f25d..81eb1d306365d 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6798,29 +6798,6 @@ public function test_cannot_update_reaction_regardless_of_type_case(): void { $this->assertSame( '2764', $reaction->comment_content ); } - /** - * Moving a note would leave its reactions on the post it left. - * - * @ticket 66635 - */ - public function test_cannot_move_note_to_another_post(): void { - wp_set_current_user( self::$editor_id ); - - list( $post_id, $note_id ) = $this->create_editor_note(); - - $other_post_id = self::factory()->post->create(); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); - $request->set_param( 'post', $other_post_id ); - $response = rest_get_server()->dispatch( $request ); - - $this->assertErrorResponse( 'rest_comment_note_post_immutable', $response, 400 ); - - $note = get_comment( $note_id ); - $this->assertInstanceOf( WP_Comment::class, $note ); - $this->assertSame( (string) $post_id, $note->comment_post_ID ); - } - /** * A note reply has to sit on the same post as the note it answers, or a * user could thread their own note under one on a post they cannot read. From 290f63877b64087361b20fed64578612b21d3bce Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 23 Nov 2026 09:14:47 -0800 Subject: [PATCH 22/31] Add types for wpdb::get_results() return values --- .../endpoints/class-wp-rest-comments-controller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 312f3f1ebc4a9..8e5d7edafbc1a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -2405,16 +2405,16 @@ protected function prefetch_reaction_summaries( $note_ids ) { // Query 1: Get aggregated counts per emoji per note. $query = "SELECT r.comment_parent, r.comment_content, COUNT(*) AS reaction_count $from $where GROUP BY r.comment_parent, r.comment_content"; - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - $counts = $wpdb->get_results( $query ); + /** @var list|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 r.comment_ID, r.comment_parent, r.comment_content $from $where"; $query .= $wpdb->prepare( ' AND r.user_id = %d', $current_user_id ); - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared - $user_rows = $wpdb->get_results( $query ); + /** @var list|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 ) { From 789bc4a9871faad40c8e31cfc3608ac7d3b120cf Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 23 Nov 2026 09:23:46 -0800 Subject: [PATCH 23/31] Enforce minimum int for comment parent ID --- .../rest-api/endpoints/class-wp-rest-comments-controller.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 8e5d7edafbc1a..4228b0feae6cc 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -2209,7 +2209,8 @@ public function get_collection_params() { 'description' => __( 'Limit result set to comments of specific parent IDs.' ), 'type' => 'array', 'items' => array( - 'type' => 'integer', + 'type' => 'integer', + 'minimum' => 1, ), ); From 290628ff770936d6522c58e1808c42bfd3d8bd84 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 23 Nov 2026 09:44:24 -0800 Subject: [PATCH 24/31] Apply batched suggestions from code review Co-authored-by: Weston Ruter --- src/wp-admin/includes/comment.php | 4 ++-- .../endpoints/class-wp-rest-comments-controller.php | 4 ++-- tests/phpunit/tests/ajax/wpAjaxEditComment.php | 10 +++++----- tests/phpunit/tests/ajax/wpAjaxReplytoComment.php | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index c63bb212053f0..7d554922ed9ec 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -81,7 +81,7 @@ function edit_comment() { $comment = get_comment( $comment_id ); // Reactions are only validated on creation. Removing one is a delete. - if ( $comment && 'reaction' === strtolower( $comment->comment_type ) ) { + if ( $comment && 'reaction' === $comment->comment_type ) { wp_die( __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ), 403 ); } @@ -203,7 +203,7 @@ 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 = isset( $_POST['comment_parent'] ) && is_int( $_POST['comment_parent'] ) ? $_POST['comment_parent'] : (int) $comment->comment_parent; + $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 ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 4228b0feae6cc..9400e8c327f0c 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -734,13 +734,13 @@ public function create_item( $request ) { // A note reply must answer a note on the same post. $parent_id = $request['parent']; - if ( 'note' === $request['type'] && is_int( $parent_id ) && $parent_id > 0 ) { + if ( 'note' === $request['type'] && $parent_id ) { $parent_note = get_comment( $parent_id ); if ( ! $parent_note || 'note' !== $parent_note->comment_type || - ( ! empty( $request['post'] ) && $request['post'] !== (int) $parent_note->comment_post_ID ) + ( $request['post'] && $request['post'] !== (int) $parent_note->comment_post_ID ) ) { return new WP_Error( 'rest_comment_invalid_parent', diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index 4207c7ca4b6eb..9e1b87f6a46b8 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -248,7 +248,7 @@ public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) { * * @param int $post_id Post to attach the note to. * @param int $admin_id User who owns the note and reaction. - * @return int The reaction ID. + * @return positive-int The reaction ID. */ private function create_admin_reaction( int $post_id, int $admin_id ): int { $note_id = self::factory()->comment->create( @@ -293,11 +293,11 @@ public function test_cannot_edit_reaction(): void { $reaction_id = $this->create_admin_reaction( $draft_id, $admin_id ); $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $reaction_id; + $_POST['comment_ID'] = (string) $reaction_id; $_POST['content'] = 'rewritten'; $_POST['newcomment_author'] = 'Forged'; $_POST['newcomment_author_email'] = 'forged@example.org'; - $_POST['user_id'] = get_current_user_id(); + $_POST['user_id'] = (string) get_current_user_id(); try { $this->_handleAjax( 'edit-comment' ); @@ -561,7 +561,7 @@ public function test_cannot_move_reply_away_from_its_parent(): void { * * @dataProvider data_types_a_note_cannot_become * - * @param string $type The requested comment type. + * @param non-falsy-string $type The requested comment type. */ public function test_cannot_convert_note_into_another_type( string $type ): void { $this->_setRole( 'editor' ); @@ -595,7 +595,7 @@ public function test_cannot_convert_note_into_another_type( string $type ): void /** * Data provider. * - * @return array + * @return array */ public function data_types_a_note_cannot_become(): array { return array( diff --git a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php index b97967669a3ba..ec9175d72fc00 100644 --- a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php @@ -279,7 +279,7 @@ public function _pre_comment_approved_filter( $approved, $commentdata ) { * * @dataProvider data_restricted_comment_types * - * @param string $type The comment type to request. + * @param non-falsy-string $type The comment type to request. */ public function test_cannot_reply_with_restricted_comment_type( string $type ): void { $this->_setRole( 'administrator' ); @@ -316,7 +316,7 @@ public function test_cannot_reply_with_restricted_comment_type( string $type ): /** * Data provider. * - * @return array + * @return array */ public function data_restricted_comment_types(): array { return array( From 2cb2d8a313a2c8eb7d75e37f2bd9e7fce2d639b9 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 23 Nov 2026 09:51:58 -0800 Subject: [PATCH 25/31] Comments: Compare the reaction type without strtolower(). Core only ever writes the lowercase `reaction` type, and the request handlers no longer accept any other spelling, so the case-insensitive guard only covered rows written directly through the low-level functions, which are out of scope here. Dropping it keeps the REST update check as it is in trunk and the changeset smaller. The test for a `Reaction` row goes with it. Claude-Session: https://claude.ai/code/session_018vwYjTrHd5zg7SWqP62gDY --- src/wp-includes/class-wp-xmlrpc-server.php | 2 +- .../class-wp-rest-comments-controller.php | 5 ++- .../rest-api/rest-comments-controller.php | 34 ------------------- 3 files changed, 3 insertions(+), 38 deletions(-) diff --git a/src/wp-includes/class-wp-xmlrpc-server.php b/src/wp-includes/class-wp-xmlrpc-server.php index 7310030bb80ee..56448aa6e2520 100644 --- a/src/wp-includes/class-wp-xmlrpc-server.php +++ b/src/wp-includes/class-wp-xmlrpc-server.php @@ -3982,7 +3982,7 @@ public function wp_editComment( $args ) { } // Reactions are only validated on creation. Removing one is a delete. - if ( 'reaction' === strtolower( $existing_comment->comment_type ) ) { + if ( 'reaction' === $existing_comment->comment_type ) { return new IXR_Error( 403, __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ) ); } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 9400e8c327f0c..00043102a9b08 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -1169,10 +1169,9 @@ public function update_item_permissions_check( $request ) { * is re-checked here. Allowing an update would let anyone who can edit * the note's post reattribute a reaction to another user, move it to a * note on a post they cannot edit, or store a duplicate or invalid - * key. Removing a reaction is a delete. strtolower() matches the - * database's case-insensitive type comparison. + * key. Removing a reaction is a delete. */ - if ( 'reaction' === strtolower( $comment->comment_type ) ) { + if ( 'reaction' === $comment->comment_type ) { return new WP_Error( 'rest_comment_update_not_allowed', __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ), diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 81eb1d306365d..e2771b72292e1 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6764,40 +6764,6 @@ public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_ke $this->assertSame( $heart_id, $summary['2764']['current_user_reaction'] ); } - /** - * The database matches comment types case-insensitively, so the update - * guard has to as well, or a row typed `Reaction` is counted as a - * reaction but edited as a comment. - * - * @ticket 66635 - */ - public function test_cannot_update_reaction_regardless_of_type_case(): void { - wp_set_current_user( self::$editor_id ); - - list( $post_id, $note_id ) = $this->create_editor_note(); - - $reaction_id = self::factory()->comment->create( - array( - 'comment_post_ID' => $post_id, - 'comment_type' => 'Reaction', - 'comment_parent' => $note_id, - 'comment_approved' => 1, - 'user_id' => self::$editor_id, - 'comment_content' => '2764', - ) - ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $reaction_id ); - $request->set_param( 'content', '1f680' ); - $response = rest_get_server()->dispatch( $request ); - - $this->assertErrorResponse( 'rest_comment_update_not_allowed', $response, 403 ); - - $reaction = get_comment( $reaction_id ); - $this->assertInstanceOf( WP_Comment::class, $reaction ); - $this->assertSame( '2764', $reaction->comment_content ); - } - /** * A note reply has to sit on the same post as the note it answers, or a * user could thread their own note under one on a post they cannot read. From ed7aaa4080ed7ea5f04ce616c574b275a7c40319 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 23 Nov 2026 09:51:59 -0800 Subject: [PATCH 26/31] REST API: Allow 0 for the comment parent and declare the minimum on the item. A minimum of 1 on the `parent` collection parameter rejects `parent=0`, which is how top level comments are requested, for example by the Comment Template block. `test_get_note_with_children_link` fails with a 400 as a result. Use a minimum of 0 there, and declare the same minimum on the item's `parent` and `post` so a negative ID is refused by the schema. Both default to 0, so 0 has to stay valid. The QUnit fixture is regenerated to match. Claude-Session: https://claude.ai/code/session_018vwYjTrHd5zg7SWqP62gDY --- .../endpoints/class-wp-rest-comments-controller.php | 6 ++++-- tests/qunit/fixtures/wp-api-generated.js | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 00043102a9b08..6ac473162a49a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -734,7 +734,7 @@ public function create_item( $request ) { // A note reply must answer a note on the same post. $parent_id = $request['parent']; - if ( 'note' === $request['type'] && $parent_id ) { + if ( 'note' === $request['type'] && $parent_id ) { $parent_note = get_comment( $parent_id ); if ( @@ -2029,12 +2029,14 @@ public function get_item_schema() { '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, ), @@ -2209,7 +2211,7 @@ public function get_collection_params() { 'type' => 'array', 'items' => array( 'type' => 'integer', - 'minimum' => 1, + 'minimum' => 0, ), ); diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index bb83cd91b8728..bfd6f3e720d55 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -10616,7 +10616,8 @@ mockedApiResponse.Schema = { "description": "Limit result set to comments assigned to specific post IDs.", "type": "array", "items": { - "type": "integer" + "type": "integer", + "minimum": 0 }, "required": false }, @@ -10722,12 +10723,14 @@ mockedApiResponse.Schema = { "default": 0, "description": "The ID for the parent of the comment.", "type": "integer", + "minimum": 0, "required": false }, "post": { "default": 0, "description": "The ID of the associated post object.", "type": "integer", + "minimum": 0, "required": false }, "status": { @@ -10870,11 +10873,13 @@ mockedApiResponse.Schema = { "parent": { "description": "The ID for the parent of the comment.", "type": "integer", + "minimum": 0, "required": false }, "post": { "description": "The ID of the associated post object.", "type": "integer", + "minimum": 0, "required": false }, "status": { From 7ec614dd309b03062934148718eefb0c08254428 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 23 Nov 2026 09:51:59 -0800 Subject: [PATCH 27/31] Tests: Send slashed string values in the comment ajax tests. A real request only carries slashed strings, so integer IDs in `$_POST` let a handler pass a check it would fail in production. Name the heart reaction key, which was an unexplained literal. Add a test for moving a reply to another post while detaching it from its parent, the one path where the submitted parent rather than the stored one decides whether the move is allowed. Claude-Session: https://claude.ai/code/session_018vwYjTrHd5zg7SWqP62gDY --- .../phpunit/tests/ajax/wpAjaxEditComment.php | 168 +++++++++++++----- .../tests/ajax/wpAjaxReplytoComment.php | 26 ++- 2 files changed, 142 insertions(+), 52 deletions(-) diff --git a/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index 9e1b87f6a46b8..53117f453d119 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -243,6 +243,11 @@ public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) { return new WP_Error( 'comment_wrong', 'wp_update_comment_data filter fails for this comment.', 500 ); } + /** + * The curated reaction key for the heart emoji, as stored in a reaction's content. + */ + private const HEART_REACTION_KEY = '2764'; + /** * Creates an administrator's open note and approved reaction on a post. * @@ -268,7 +273,7 @@ private function create_admin_reaction( int $post_id, int $admin_id ): int { 'comment_parent' => $note_id, 'comment_approved' => 1, 'user_id' => $admin_id, - 'comment_content' => '2764', + 'comment_content' => self::HEART_REACTION_KEY, ) ); } @@ -292,12 +297,16 @@ public function test_cannot_edit_reaction(): void { ); $reaction_id = $this->create_admin_reaction( $draft_id, $admin_id ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = (string) $reaction_id; - $_POST['content'] = 'rewritten'; - $_POST['newcomment_author'] = 'Forged'; - $_POST['newcomment_author_email'] = 'forged@example.org'; - $_POST['user_id'] = (string) get_current_user_id(); + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $reaction_id, + 'content' => 'rewritten', + 'newcomment_author' => 'Forged', + 'newcomment_author_email' => 'forged@example.org', + 'user_id' => (string) get_current_user_id(), + ) + ); try { $this->_handleAjax( 'edit-comment' ); @@ -308,7 +317,7 @@ public function test_cannot_edit_reaction(): void { $reaction = get_comment( $reaction_id ); $this->assertInstanceOf( WP_Comment::class, $reaction ); - $this->assertSame( '2764', $reaction->comment_content ); + $this->assertSame( self::HEART_REACTION_KEY, $reaction->comment_content ); $this->assertSame( (string) $admin_id, $reaction->user_id ); $this->assertSame( '', $reaction->comment_author_email ); } @@ -332,10 +341,14 @@ public function test_cannot_move_comment_to_post_user_cannot_edit(): void { $admin_post_id = self::factory()->post->create( array( 'post_author' => $admin_id ) ); $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $draft_id ) ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $comment_id; - $_POST['content'] = 'Moved'; - $_POST['comment_post_ID'] = $admin_post_id; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $comment_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $admin_post_id, + ) + ); try { $this->_handleAjax( 'edit-comment' ); @@ -361,10 +374,14 @@ public function test_can_move_comment_to_post_user_can_edit(): void { $target_post_id = self::factory()->post->create(); $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $source_post_id ) ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $comment_id; - $_POST['content'] = 'Moved'; - $_POST['comment_post_ID'] = $target_post_id; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $comment_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $target_post_id, + ) + ); try { $this->_handleAjax( 'edit-comment' ); @@ -399,10 +416,14 @@ public function test_cannot_convert_comment_into_reaction(): void { ); // Step 1: reply beneath the note with an ordinary comment. - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_post_ID'] = $post_id; - $_POST['comment_ID'] = $note_id; - $_POST['content'] = 'Ordinary reply'; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_post_ID' => (string) $post_id, + 'comment_ID' => (string) $note_id, + 'content' => 'Ordinary reply', + ) + ); try { $this->_handleAjax( 'replyto-comment' ); @@ -423,12 +444,15 @@ public function test_cannot_convert_comment_into_reaction(): void { $reply_id = (int) $replies[0]; // Step 2: edit the reply into a reaction owned by the administrator. - $_POST = array(); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $reply_id; - $_POST['content'] = '2764'; - $_POST['comment_type'] = 'reaction'; - $_POST['user_id'] = $admin_id; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $reply_id, + 'content' => self::HEART_REACTION_KEY, + 'comment_type' => 'reaction', + 'user_id' => (string) $admin_id, + ) + ); try { $this->_handleAjax( 'edit-comment' ); @@ -469,10 +493,14 @@ public function test_cannot_detach_comment_from_its_post(): void { ) ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $note_id; - $_POST['content'] = 'Editorial note'; - $_POST['comment_post_ID'] = 0; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $note_id, + 'content' => 'Editorial note', + 'comment_post_ID' => '0', + ) + ); try { $this->_handleAjax( 'edit-comment' ); @@ -500,11 +528,15 @@ public function test_cannot_move_comment_under_parent_on_the_post_it_leaves(): v $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $comment_id; - $_POST['content'] = 'Moved'; - $_POST['comment_post_ID'] = $post_b_id; - $_POST['comment_parent'] = $parent_id; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $comment_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + 'comment_parent' => (string) $parent_id, + ) + ); try { $this->_handleAjax( 'edit-comment' ); @@ -538,10 +570,14 @@ public function test_cannot_move_reply_away_from_its_parent(): void { ) ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $reply_id; - $_POST['content'] = 'Moved'; - $_POST['comment_post_ID'] = $post_b_id; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $reply_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + ) + ); try { $this->_handleAjax( 'edit-comment' ); @@ -554,6 +590,48 @@ public function test_cannot_move_reply_away_from_its_parent(): void { $this->assertSame( (string) $post_a_id, $reply->comment_post_ID ); } + /** + * A reply can move to another post when the same request detaches it from + * its parent, since the submitted parent is the one that will be kept. + * + * @ticket 66635 + */ + public function test_can_move_reply_to_another_post_when_detached_from_its_parent(): void { + $this->_setRole( 'editor' ); + update_option( 'thread_comments', 1 ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_a_id ) ); + $reply_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_a_id, + 'comment_parent' => $parent_id, + ) + ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $reply_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + 'comment_parent' => '0', + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $reply = get_comment( $reply_id ); + $this->assertInstanceOf( WP_Comment::class, $reply ); + $this->assertSame( (string) $post_b_id, $reply->comment_post_ID ); + $this->assertSame( '0', $reply->comment_parent ); + } + /** * A note cannot stop being one, including by a change of case. * @@ -575,10 +653,14 @@ public function test_cannot_convert_note_into_another_type( string $type ): void ) ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $note_id; - $_POST['content'] = 'Converted'; - $_POST['comment_type'] = $type; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $note_id, + 'content' => 'Converted', + 'comment_type' => $type, + ) + ); try { $this->_handleAjax( 'edit-comment' ); diff --git a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php index ec9175d72fc00..5c6d335aa7e68 100644 --- a/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxReplytoComment.php @@ -287,11 +287,15 @@ public function test_cannot_reply_with_restricted_comment_type( string $type ): $post_id = self::factory()->post->create(); $parent_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $parent_id; - $_POST['content'] = '2764'; - $_POST['comment_post_ID'] = $post_id; - $_POST['comment_type'] = $type; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $parent_id, + 'content' => '2764', + 'comment_post_ID' => (string) $post_id, + 'comment_type' => $type, + ) + ); try { $this->_handleAjax( 'replyto-comment' ); @@ -341,10 +345,14 @@ public function test_reply_to_comment_on_another_post_is_not_threaded(): void { $other_post = self::factory()->post->create(); $other_comment = self::factory()->comment->create( array( 'comment_post_ID' => $other_post ) ); - $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); - $_POST['comment_ID'] = $other_comment; - $_POST['content'] = 'Reply across posts'; - $_POST['comment_post_ID'] = $post_id; + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $other_comment, + 'content' => 'Reply across posts', + 'comment_post_ID' => (string) $post_id, + ) + ); try { $this->_handleAjax( 'replyto-comment' ); From 3fb4fae08e7deb2e06fae506f2973e162c3cf96d Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 23 Nov 2026 09:53:52 -0800 Subject: [PATCH 28/31] Tests: Name the heart reaction key in the new REST reaction tests. The bare `2764` literal does not say what it stands for. The constant covers the tests added here; the older tests in the file keep the literal to avoid churn. Claude-Session: https://claude.ai/code/session_018vwYjTrHd5zg7SWqP62gDY --- .../tests/rest-api/rest-comments-controller.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index e2771b72292e1..e561ba1f439e6 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6636,6 +6636,11 @@ public function test_reaction_summary_excludes_trashed_reactions() { $this->assertSame( array(), $response->get_data()['reaction_summary'] ); } + /** + * The curated reaction key for the heart emoji, as stored in a reaction's content. + */ + private const HEART_REACTION_KEY = '2764'; + /** * Creates an open note by the editor on a new post. * @@ -6696,7 +6701,7 @@ public function test_reaction_summary_ignores_reactions_stored_on_another_post() 'comment_parent' => $note_id, 'comment_approved' => 1, 'user_id' => self::$subscriber_id, - 'comment_content' => '2764', + 'comment_content' => self::HEART_REACTION_KEY, ) ); @@ -6741,7 +6746,7 @@ public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_ke 'comment_parent' => $note_id, 'comment_approved' => 1, 'user_id' => self::$editor_id, - 'comment_content' => '2764', + 'comment_content' => self::HEART_REACTION_KEY, ) ); @@ -6752,16 +6757,16 @@ public function test_reaction_summary_accumulates_rows_that_strip_to_the_same_ke 'comment_parent' => $note_id, 'comment_approved' => 1, 'user_id' => self::$subscriber_id, - 'comment_content' => '2764', + 'comment_content' => '' . self::HEART_REACTION_KEY . '', ) ); $summary = $this->get_reaction_summary( $note_id ); $this->assertCount( 1, $summary ); - $this->assertArrayHasKey( '2764', $summary ); - $this->assertSame( 2, $summary['2764']['count'] ); - $this->assertSame( $heart_id, $summary['2764']['current_user_reaction'] ); + $this->assertArrayHasKey( self::HEART_REACTION_KEY, $summary ); + $this->assertSame( 2, $summary[ self::HEART_REACTION_KEY ]['count'] ); + $this->assertSame( $heart_id, $summary[ self::HEART_REACTION_KEY ]['current_user_reaction'] ); } /** From 3d11f729d24e416f5ab261a233ec225c6c8aa065 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 23 Nov 2026 10:28:15 -0800 Subject: [PATCH 29/31] Tests: Put the fixture's parent minimum on the right collection parameter. The regenerated `wp-api-generated.js` carried the new minimum on the `post` collection parameter instead of `parent`, so the PHPUnit jobs rewrote the file and failed the check for modified version-controlled files. This matches the fixture to what the generator test writes in CI. Claude-Session: https://claude.ai/code/session_018vwYjTrHd5zg7SWqP62gDY --- tests/qunit/fixtures/wp-api-generated.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index bfd6f3e720d55..ba63e6425b7fd 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -10598,7 +10598,8 @@ mockedApiResponse.Schema = { "description": "Limit result set to comments of specific parent IDs.", "type": "array", "items": { - "type": "integer" + "type": "integer", + "minimum": 0 }, "required": false }, @@ -10616,8 +10617,7 @@ mockedApiResponse.Schema = { "description": "Limit result set to comments assigned to specific post IDs.", "type": "array", "items": { - "type": "integer", - "minimum": 0 + "type": "integer" }, "required": false }, From 61f82d74ea39e89035bca251882a1bba1849d63e Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 23 Nov 2026 15:26:27 -0800 Subject: [PATCH 30/31] Comments: Keep a note's thread on one post when the note is updated. The REST API only checked on creation that a note reply answers a note on the same post, so an update could still set a parent on another post or move a reply away from its parent. Apply the same check to the parent and post an update results in. A note's replies and reactions do not move with it, and the reaction summary only counts reactions stored on the note's post. Refuse to move a note that has either, in the REST API and on the Edit Comment screen, so a move cannot separate them and empty the summary. --- src/wp-admin/includes/comment.php | 14 ++ .../class-wp-rest-comments-controller.php | 80 ++++++++-- .../phpunit/tests/ajax/wpAjaxEditComment.php | 74 +++++++++ .../rest-api/rest-comments-controller.php | 150 ++++++++++++++++++ 4 files changed, 303 insertions(+), 15 deletions(-) diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php index 7d554922ed9ec..49c2b3d32c421 100644 --- a/src/wp-admin/includes/comment.php +++ b/src/wp-admin/includes/comment.php @@ -301,6 +301,20 @@ static function ( $item ) { 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/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 6ac473162a49a..2b5405a35e7b1 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -731,22 +731,11 @@ public function create_item( $request ) { ); } - // A note reply must answer a note on the same post. - $parent_id = $request['parent']; + if ( 'note' === $request['type'] && $request['parent'] ) { + $parent_check = $this->check_note_parent( absint( $request['parent'] ), absint( $request['post'] ) ); - if ( 'note' === $request['type'] && $parent_id ) { - $parent_note = get_comment( $parent_id ); - - if ( - ! $parent_note || - 'note' !== $parent_note->comment_type || - ( $request['post'] && $request['post'] !== (int) $parent_note->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 ) - ); + if ( is_wp_error( $parent_check ) ) { + return $parent_check; } } @@ -1252,6 +1241,40 @@ public function update_item( $request ) { ); } + 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 @@ -2734,6 +2757,33 @@ protected function check_target_post_permission( int $post_id, WP_REST_Request $ 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/tests/phpunit/tests/ajax/wpAjaxEditComment.php b/tests/phpunit/tests/ajax/wpAjaxEditComment.php index 53117f453d119..155a7ccc278db 100644 --- a/tests/phpunit/tests/ajax/wpAjaxEditComment.php +++ b/tests/phpunit/tests/ajax/wpAjaxEditComment.php @@ -632,6 +632,80 @@ public function test_can_move_reply_to_another_post_when_detached_from_its_paren $this->assertSame( '0', $reply->comment_parent ); } + /** + * A note's reactions stay on its post, so moving the note would separate + * them and empty its reaction summary. + * + * @ticket 66635 + */ + public function test_cannot_move_note_that_has_reactions(): void { + $this->_setRole( 'editor' ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $reaction_id = $this->create_admin_reaction( $post_a_id, get_current_user_id() ); + $reaction = get_comment( $reaction_id ); + $this->assertInstanceOf( WP_Comment::class, $reaction ); + $note_id = (int) $reaction->comment_parent; + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $note_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPDieException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $post_a_id, $note->comment_post_ID ); + } + + /** + * A note with no replies or reactions can still move. + * + * @ticket 66635 + */ + public function test_can_move_note_without_children(): void { + $this->_setRole( 'editor' ); + + $post_a_id = self::factory()->post->create(); + $post_b_id = self::factory()->post->create(); + $note_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_a_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + ) + ); + + $_POST = wp_slash( + array( + '_ajax_nonce-replyto-comment' => wp_create_nonce( 'replyto-comment' ), + 'comment_ID' => (string) $note_id, + 'content' => 'Moved', + 'comment_post_ID' => (string) $post_b_id, + ) + ); + + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $post_b_id, $note->comment_post_ID ); + } + /** * A note cannot stop being one, including by a change of case. * diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index e561ba1f439e6..a71f5090e7af7 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6791,4 +6791,154 @@ public function test_cannot_create_note_reply_under_note_on_another_post(): void $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); } + + /** + * The same-post rule for note replies holds on update too. + * + * @ticket 66635 + */ + public function test_cannot_update_note_parent_to_note_on_another_post(): void { + list( , $note_a_id ) = $this->create_editor_note(); + list( , $note_b_id ) = $this->create_editor_note(); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_b_id ); + $request->set_param( 'parent', $note_a_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); + + $note_b = get_comment( $note_b_id ); + $this->assertInstanceOf( WP_Comment::class, $note_b ); + $this->assertSame( '0', $note_b->comment_parent ); + } + + /** + * A note cannot be threaded under a regular comment by an update. + * + * @ticket 66635 + */ + public function test_cannot_update_note_parent_to_a_comment(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) ); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'parent', $comment_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); + } + + /** + * Moving a note reply to another post would leave it under a note on the + * post it left. + * + * @ticket 66635 + */ + public function test_cannot_move_note_reply_away_from_its_parent(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + $reply_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'note', + 'comment_parent' => $note_id, + 'comment_approved' => 0, + 'user_id' => self::$editor_id, + ) + ); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $reply_id ); + $request->set_param( 'post', $other_post_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_invalid_parent', $response, 400 ); + + $reply = get_comment( $reply_id ); + $this->assertInstanceOf( WP_Comment::class, $reply ); + $this->assertSame( (string) $post_id, $reply->comment_post_ID ); + } + + /** + * A note's replies and reactions stay on its post, so moving the note + * would separate them and empty its reaction summary. + * + * @ticket 66635 + * + * @dataProvider data_note_child_types + * + * @param string $child_type Comment type of the note's child. + */ + public function test_cannot_move_note_that_has_children( string $child_type ): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => $child_type, + 'comment_parent' => $note_id, + 'comment_approved' => 'reaction' === $child_type ? 1 : 0, + 'user_id' => self::$subscriber_id, + 'comment_content' => 'reaction' === $child_type ? self::HEART_REACTION_KEY : 'Reply', + ) + ); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'post', $other_post_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_comment_has_children', $response, 400 ); + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $post_id, $note->comment_post_ID ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_note_child_types(): array { + return array( + 'a reaction' => array( 'reaction' ), + 'a reply' => array( 'note' ), + ); + } + + /** + * A note with no replies or reactions can still move. + * + * @ticket 66635 + */ + public function test_can_move_note_without_children(): void { + list( , $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id ); + $request->set_param( 'post', $other_post_id ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + + $note = get_comment( $note_id ); + $this->assertInstanceOf( WP_Comment::class, $note ); + $this->assertSame( (string) $other_post_id, $note->comment_post_ID ); + } + } From 572928ddbb9237f05b0ee0567dce8620ac843a66 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 23 Nov 2026 15:26:27 -0800 Subject: [PATCH 31/31] REST API: Ignore reactions on another post when checking for a duplicate. The reaction summary leaves out a reaction stored on a different post than its note, but the uniqueness check still matched it by parent and user. The user was refused a reaction they could not see or remove. Scope the check and the race cleanup to the note's post so both agree with the summary. --- .../class-wp-rest-comments-controller.php | 5 ++- .../rest-api/rest-comments-controller.php | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 2b5405a35e7b1..3bb44059052eb 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -849,10 +849,12 @@ public function create_item( $request ) { * * 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', @@ -1043,6 +1045,7 @@ public function create_item( $request ) { if ( null !== $reaction_key ) { $matching = get_comments( array( + 'post_id' => absint( $request['post'] ), 'parent' => $request['parent'], 'user_id' => get_current_user_id(), 'type' => 'reaction', diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index a71f5090e7af7..513bdbfd51220 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6941,4 +6941,39 @@ public function test_can_move_note_without_children(): void { $this->assertSame( (string) $other_post_id, $note->comment_post_ID ); } + /** + * The reaction summary ignores a reaction stored on another post, so the + * uniqueness check has to as well, or the user is refused a reaction they + * cannot see. + * + * @ticket 66635 + */ + public function test_reaction_stored_on_another_post_does_not_block_a_new_reaction(): void { + list( $post_id, $note_id ) = $this->create_editor_note(); + $other_post_id = self::factory()->post->create(); + + self::factory()->comment->create( + array( + 'comment_post_ID' => $other_post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $note_id, + 'comment_approved' => 1, + 'user_id' => self::$editor_id, + 'comment_content' => self::HEART_REACTION_KEY, + ) + ); + + wp_set_current_user( self::$editor_id ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/comments' ); + $request->set_param( 'type', 'reaction' ); + $request->set_param( 'post', $post_id ); + $request->set_param( 'parent', $note_id ); + $request->set_param( 'content', self::HEART_REACTION_KEY ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 201, $response->get_status() ); + $this->assertArrayHasKey( self::HEART_REACTION_KEY, $this->get_reaction_summary( $note_id ) ); + } }