From f0ce85f0a75933ed89f143e7c8dde68650fe0e9c Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 2 Nov 2026 16:32:08 -0800 Subject: [PATCH 1/3] REST API: Only let a reaction's author delete it A reaction speaks for its author, so being able to edit the note's post is no longer enough to remove someone else's. Reactions on a resolved thread are also frozen for deletion, matching the existing check on create. The thread-root walk moves into a shared is_thread_resolved() helper. This brings over the PHP changes from https://github.com/WordPress/gutenberg/pull/85415. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018vwYjTXU324rGneSjaye87 --- .../class-wp-rest-comments-controller.php | 66 +++++++--- .../rest-api/rest-comments-controller.php | 113 +++++++++++++++++- 2 files changed, 163 insertions(+), 16 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 1d035d3a6e1b7..e310e305507c0 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 @@ -787,21 +787,7 @@ public function create_item( $request ) { * reactions from then on. Hold requests from stale editor sessions * to that too, for the root note and for every reply in its thread. */ - $thread_root = $parent_comment; - $visited = array( (int) $thread_root->comment_ID => true ); - while ( $thread_root->comment_parent ) { - $ancestor = get_comment( $thread_root->comment_parent ); - - // Stop at a missing ancestor or a corrupt, cyclic chain. - if ( ! $ancestor || isset( $visited[ (int) $ancestor->comment_ID ] ) ) { - break; - } - - $visited[ (int) $ancestor->comment_ID ] = true; - $thread_root = $ancestor; - } - - if ( '1' === $thread_root->comment_approved ) { + if ( $this->is_thread_resolved( $parent_comment ) ) { return new WP_Error( 'rest_comment_invalid_parent', __( 'A reaction cannot be added to a resolved note.' ), @@ -1350,6 +1336,28 @@ public function delete_item_permissions_check( $request ) { array( 'status' => rest_authorization_required_code() ) ); } + + if ( 'reaction' === $comment->comment_type ) { + // A reaction speaks for its author, so only they can remove it. + if ( get_current_user_id() !== (int) $comment->user_id ) { + return new WP_Error( + 'rest_cannot_delete', + __( 'Sorry, you can only remove your own reactions.' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + + // Reactions on a resolved thread are frozen, like adding new ones. + $note = get_comment( $comment->comment_parent ); + if ( $note instanceof WP_Comment && $this->is_thread_resolved( $note ) ) { + return new WP_Error( + 'rest_cannot_delete', + __( 'A reaction cannot be removed from a resolved note.' ), + array( 'status' => 403 ) + ); + } + } + return true; } @@ -2652,6 +2660,34 @@ protected function check_target_post_permission( int $post_id, WP_REST_Request $ return true; } + /** + * Checks whether a note belongs to a resolved thread. + * + * Resolving a thread approves its root note. + * + * @since 7.2.0 + * + * @param WP_Comment $note A root note or a reply. + * @return bool True if the note's thread is resolved. + */ + private function is_thread_resolved( WP_Comment $note ): bool { + $thread_root = $note; + $visited = array( (int) $thread_root->comment_ID => true ); + while ( $thread_root->comment_parent ) { + $ancestor = get_comment( $thread_root->comment_parent ); + + // Stop at a missing ancestor or a corrupt, cyclic chain. + if ( ! $ancestor || isset( $visited[ (int) $ancestor->comment_ID ] ) ) { + break; + } + + $visited[ (int) $ancestor->comment_ID ] = true; + $thread_root = $ancestor; + } + + return '1' === $thread_root->comment_approved; + } + /** * Check if post type supports notes. * diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index ab4adc4c9dce1..57cc6b9cd646a 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -5996,6 +5996,116 @@ public function test_delete_own_reaction_on_another_users_note() { $this->assertNull( get_comment( $reaction_id ) ); } + /** + * A reaction speaks for its author, so being able to edit the post is not + * enough to remove someone else's. + * + * @ticket 64638 + * + * @dataProvider data_users_who_cannot_delete_another_users_reaction + * + * @param string $user Name of the static property holding the user ID. + */ + public function test_cannot_delete_another_users_reaction( string $user ): void { + $post_id = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); + $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', + ) + ); + $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', + ) + ); + + wp_set_current_user( self::${$user} ); + + $request = new WP_REST_Request( 'DELETE', '/wp/v2/comments/' . $reaction_id ); + $request->set_param( 'force', true ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); + $this->assertNotNull( get_comment( $reaction_id ), 'Reaction must still exist.' ); + } + + /** + * Data provider for test_cannot_delete_another_users_reaction(). + * + * @return array + */ + public static function data_users_who_cannot_delete_another_users_reaction(): array { + return array( + 'post author' => array( 'author_id' ), + 'administrator' => array( 'admin_id' ), + ); + } + + /** + * Reactions on a resolved thread are frozen, like adding new ones, on the + * root note and on its replies. + * + * @ticket 64638 + * + * @dataProvider data_resolved_thread_targets + * + * @param bool $on_reply Whether the reaction is on a reply rather than the root note. + */ + public function test_cannot_delete_reaction_on_resolved_thread( bool $on_reply ): void { + wp_set_current_user( self::$editor_id ); + + $post_id = self::factory()->post->create( array( 'post_author' => self::$editor_id ) ); + $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', + ) + ); + $target = $note_id; + if ( $on_reply ) { + $target = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_parent' => $note_id, + 'comment_type' => 'note', + 'comment_approved' => 0, + 'user_id' => self::$editor_id, + 'comment_content' => 'Test reply', + ) + ); + } + $reaction_id = self::factory()->comment->create( + array( + 'comment_post_ID' => $post_id, + 'comment_type' => 'reaction', + 'comment_parent' => $target, + 'comment_approved' => 1, + 'user_id' => self::$editor_id, + 'comment_content' => '2764', + ) + ); + wp_set_comment_status( $note_id, 'approve' ); + + $request = new WP_REST_Request( 'DELETE', '/wp/v2/comments/' . $reaction_id ); + $request->set_param( 'force', true ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); + $this->assertNotNull( get_comment( $reaction_id ), 'Reaction must still exist.' ); + } + /** * A reaction cannot be added to a trashed or spammed note, where it would * escape the trash cascade. @@ -6108,7 +6218,8 @@ public function test_create_reaction_on_resolved_thread( $on_reply ) { } /** - * Data provider for test_create_reaction_on_resolved_thread(). + * Data provider for test_create_reaction_on_resolved_thread() and + * test_cannot_delete_reaction_on_resolved_thread(). * * @return array[] */ From da383156f59b122b4e0958749bc4f8fcab133c9f Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Mon, 2 Nov 2026 17:00:24 -0800 Subject: [PATCH 2/3] Tests: Tag the reaction delete tests with their Trac ticket See #66447. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018vwYjTXU324rGneSjaye87 --- tests/phpunit/tests/rest-api/rest-comments-controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-comments-controller.php b/tests/phpunit/tests/rest-api/rest-comments-controller.php index 57cc6b9cd646a..898b344842f9a 100644 --- a/tests/phpunit/tests/rest-api/rest-comments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-comments-controller.php @@ -6000,7 +6000,7 @@ public function test_delete_own_reaction_on_another_users_note() { * A reaction speaks for its author, so being able to edit the post is not * enough to remove someone else's. * - * @ticket 64638 + * @ticket 66447 * * @dataProvider data_users_who_cannot_delete_another_users_reaction * @@ -6054,7 +6054,7 @@ public static function data_users_who_cannot_delete_another_users_reaction(): ar * Reactions on a resolved thread are frozen, like adding new ones, on the * root note and on its replies. * - * @ticket 64638 + * @ticket 66447 * * @dataProvider data_resolved_thread_targets * From 37c18ad391f0529f90f4b0aeff4cfb939746ddb5 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 3 Nov 2026 10:10:47 +0400 Subject: [PATCH 3/3] REST API: Match reaction type case-insensitively on delete. Mirrors check_edit_permission(), so a case-variant reaction type can't skip the author-only and resolved-thread checks. Co-Authored-By: Claude Opus 5.5 --- .../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 e310e305507c0..f5739e6b3a3f3 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 @@ -1337,7 +1337,7 @@ public function delete_item_permissions_check( $request ) { ); } - if ( 'reaction' === $comment->comment_type ) { + if ( 'reaction' === strtolower( $comment->comment_type ) ) { // A reaction speaks for its author, so only they can remove it. if ( get_current_user_id() !== (int) $comment->user_id ) { return new WP_Error(