Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
fab4a1d
Comments: Enforce reaction invariants in every comment writer.
adamsilverstein Nov 9, 2026
1d53146
REST API: Bind reaction summaries to the note's post and curated keys.
adamsilverstein Nov 9, 2026
738b1ab
Comments: Limit reply comment types to ASCII keys.
adamsilverstein Nov 9, 2026
98e9c0f
Comments: Refuse type changes into or out of notes and reactions.
adamsilverstein Nov 9, 2026
281923b
Comments: Limit new comment types to ASCII keys on update.
adamsilverstein Nov 9, 2026
4b266f8
Comments: Anchor the comment type pattern to the end of the string.
adamsilverstein Nov 9, 2026
4b873e1
Comments: Refuse case-only changes to note and reaction types.
adamsilverstein Nov 9, 2026
946c74d
Comments: Move a note's reactions with it.
adamsilverstein Nov 9, 2026
144ec65
Comments: Validate the destination post and parent when moving a comm…
adamsilverstein Nov 9, 2026
83c8c40
Comments: Anchor the reply type pattern the same way as on update.
adamsilverstein Nov 9, 2026
96e7e1f
REST API: Require a note reply's parent to be a note on the same post.
adamsilverstein Nov 9, 2026
b7ac133
REST API: Use a Yoda condition for the note parent post check.
adamsilverstein Nov 9, 2026
ba24806
Comments: Shorten the inline comments added for the reaction fixes.
adamsilverstein Nov 9, 2026
22c761e
Tests: Reference the Trac ticket for the note and reaction hardening …
adamsilverstein Nov 9, 2026
3544bc9
Comments: Move note and reaction rules out of wp_update_comment().
adamsilverstein Nov 10, 2026
f0fbb5b
Comments: Drop a cross-post reply parent in the ajax handler.
adamsilverstein Nov 10, 2026
4db6f95
Apply batched suggestions from code review
adamsilverstein Nov 12, 2026
5383e31
Comments: Limit reply comment types to lowercase ASCII keys.
adamsilverstein Nov 13, 2026
9c697bb
Comments: Send a 403 when edit_comment() refuses a request.
adamsilverstein Nov 13, 2026
2e27bfd
REST API: Quote the note type as a string in the reaction summary query.
adamsilverstein Nov 13, 2026
e2bdfcb
Merge remote-tracking branch 'origin/trunk' into HEAD
adamsilverstein Nov 22, 2026
eb12398
Comments: Follow trunk's policy for moving notes and comments.
adamsilverstein Nov 22, 2026
d3254fd
Merge branch 'trunk' into fix/harden-note-reaction-permissions
westonruter Nov 23, 2026
290f638
Add types for wpdb::get_results() return values
westonruter Nov 23, 2026
789bc4a
Enforce minimum int for comment parent ID
westonruter Nov 23, 2026
290628f
Apply batched suggestions from code review
adamsilverstein Nov 23, 2026
2cb2d8a
Comments: Compare the reaction type without strtolower().
adamsilverstein Nov 23, 2026
ed7aaa4
REST API: Allow 0 for the comment parent and declare the minimum on t…
adamsilverstein Nov 23, 2026
7ec614d
Tests: Send slashed string values in the comment ajax tests.
adamsilverstein Nov 23, 2026
3fb4fae
Tests: Name the heart reaction key in the new REST reaction tests.
adamsilverstein Nov 23, 2026
3d11f72
Tests: Put the fixture's parent minimum on the right collection param…
adamsilverstein Nov 23, 2026
61f82d7
Comments: Keep a note's thread on one post when the note is updated.
adamsilverstein Nov 23, 2026
572928d
REST API: Ignore reactions on another post when checking for a duplic…
adamsilverstein Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/wp-admin/includes/ajax-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1310,12 +1310,33 @@ function wp_ajax_replyto_comment( $action ) {

$comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : 'comment';

/*
* Notes and reactions are only created through the REST API. The lowercase
* ASCII limit also catches spellings the case- and accent-insensitive
* collation would match.
*/
if (
! preg_match( '/^[a-z0-9_-]+\z/', $comment_type ) ||
in_array( $comment_type, array( 'note', 'reaction' ), true )
) {
wp_die( -1 );
}

$comment_parent = 0;

if ( isset( $_POST['comment_ID'] ) ) {
$comment_parent = absint( $_POST['comment_ID'] );
}

// A comment on another post cannot be the parent.
if ( $comment_parent ) {
$parent = get_comment( $comment_parent );

if ( $parent && (int) $parent->comment_post_ID !== $comment_post_id ) {
$comment_parent = 0;
}
}

$comment_auto_approved = false;

$commentdata = array(
Expand Down
34 changes: 33 additions & 1 deletion src/wp-admin/includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ function edit_comment() {
$comment_id = (int) $_POST['comment_ID'];
$comment = get_comment( $comment_id );

// Reactions are only validated on creation. Removing one is a delete.
if ( $comment && 'reaction' === $comment->comment_type ) {
wp_die( __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ), 403 );
}

/*
* The comment type is not editable from the Edit Comment screen. Changing it
* would turn a private note into a public comment or vice versa, and would
Expand Down Expand Up @@ -135,7 +140,7 @@ function edit_comment() {
$comment_status = $_POST['comment_approved'] ?? $comment->comment_approved;
$comment_is_approved = in_array( $comment_status, array( 1, '1', 'approve' ), true );

// The parent must be a comment of the same type, on the same post, and publicly visible if the comment is approved.
// The parent must be a comment of the same type, on the post the comment will be on, and publicly visible if the comment is approved.
if (
! $parent
|| (int) $parent->comment_post_ID !== $comment_post_id
Expand Down Expand Up @@ -196,6 +201,19 @@ function edit_comment() {
}
}

// The checks above only run when the parent changes. A kept parent must be on the destination post too.
if ( $comment && $comment_post_id !== (int) $comment->comment_post_ID ) {
$kept_parent = (int) ( isset( $_POST['comment_parent'] ) && is_numeric( $_POST['comment_parent'] ) ? $_POST['comment_parent'] : $comment->comment_parent );

if ( $kept_parent ) {
$parent = get_comment( $kept_parent );

if ( ! $parent || (int) $parent->comment_post_ID !== $comment_post_id ) {
return new WP_Error( 'comment_parent_invalid', __( 'Invalid parent comment.' ) );
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved
}
}
}

foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) {
if ( ! empty( $_POST[ 'hidden_' . $timeunit ] ) && $_POST[ 'hidden_' . $timeunit ] !== $_POST[ $timeunit ] ) {
$_POST['edit_date'] = '1';
Expand Down Expand Up @@ -283,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;
}

Expand Down
9 changes: 8 additions & 1 deletion src/wp-includes/class-wp-xmlrpc-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -3971,14 +3971,21 @@ 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.' ) );
}

if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
}

// Reactions are only validated on creation. Removing one is a delete.
if ( 'reaction' === $existing_comment->comment_type ) {
return new IXR_Error( 403, __( 'Reactions cannot be edited. Remove the reaction and add a new one instead.' ) );
}

/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
do_action( 'xmlrpc_call', 'wp.editComment', $args, $this );
$comment = array(
Expand Down
124 changes: 106 additions & 18 deletions src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,14 @@ public function create_item( $request ) {
);
}

if ( 'note' === $request['type'] && $request['parent'] ) {
$parent_check = $this->check_note_parent( absint( $request['parent'] ), absint( $request['post'] ) );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The absint() checks are unnecessary given the schema, no?


if ( is_wp_error( $parent_check ) ) {
return $parent_check;
}
}

/*
* The canonical reaction key, populated once validated below so the
* stored content matches what was validated (not the raw input).
Expand Down Expand Up @@ -841,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',
Expand Down Expand Up @@ -1035,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',
Expand Down Expand Up @@ -1233,6 +1244,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;
Comment on lines +1248 to +1249

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the absint() calls are redundant here.

Suggested change
$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;
$post_id = isset( $request['post'] ) ? $request['post'] : (int) $comment->comment_post_ID;
$parent_id = isset( $request['parent'] ) ? $request['parent'] : (int) $comment->comment_parent;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i might have missed this before committing. if so i'll try to add it in a follow up commit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I added it as a post-commit comment.

$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
Expand Down Expand Up @@ -2010,12 +2055,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,
),
Expand Down Expand Up @@ -2189,7 +2236,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' => 0,
),
);

Expand Down Expand Up @@ -2374,26 +2422,27 @@ 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' ) ) . ')',
// 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'";
Comment thread
adamsilverstein marked this conversation as resolved.
Resolved
$where = "WHERE r.comment_type = 'reaction' AND r.comment_approved = '1' AND ";
$where .= $wpdb->prepare(
'r.comment_parent IN (' . implode( ',', array_fill( 0, count( $note_ids ), '%d' ) ) . ')',
...$note_ids
);

// Query 1: Get aggregated counts per emoji per note.
$query = "SELECT comment_parent, comment_content, COUNT(*) AS reaction_count FROM $wpdb->comments WHERE comment_type = 'reaction' AND comment_approved = '1' AND ";
$query .= $parent_in;
$query .= ' GROUP BY comment_parent, comment_content';
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$counts = $wpdb->get_results( $query );
$query = "SELECT r.comment_parent, r.comment_content, COUNT(*) AS reaction_count $from $where GROUP BY r.comment_parent, r.comment_content";
/** @var list<object{ comment_parent: decimal-int-string, comment_content: string, reaction_count: decimal-int-string }&stdClass>|null $counts */
$counts = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is prepared above.

// Query 2: Get the current user's own reaction IDs (if logged in).
$my_reactions = array();
if ( $current_user_id ) {
$query = "SELECT comment_ID, comment_parent, comment_content FROM $wpdb->comments WHERE comment_type = 'reaction' AND comment_approved = '1' AND ";
$query .= $parent_in;
$query .= $wpdb->prepare( ' AND user_id = %d', $current_user_id );
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$user_rows = $wpdb->get_results( $query );
$query = "SELECT r.comment_ID, r.comment_parent, r.comment_content $from $where";
$query .= $wpdb->prepare( ' AND r.user_id = %d', $current_user_id );
/** @var list<object{ comment_ID: decimal-int-string, comment_parent: decimal-int-string, comment_content: string }&stdClass>|null $user_rows */
$user_rows = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is prepared above.

if ( $user_rows ) {
foreach ( $user_rows as $row ) {
Expand All @@ -2412,15 +2461,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 curated emoji keys are summarized.
if ( ! in_array( $emoji_key, $allowed_keys, true ) ) {
continue;
}

// Rows are grouped on the raw content, so several groups can strip to one key.
if ( ! isset( $this->reaction_summaries[ $note_id ][ $emoji_key ] ) ) {
$this->reaction_summaries[ $note_id ][ $emoji_key ] = array(
'count' => 0,
'current_user_reaction' => $my_reactions[ $key ] ?? 0,
);
}

$this->reaction_summaries[ $note_id ][ $emoji_key ]['count'] += (int) $row->reaction_count;
}
}

Expand Down Expand Up @@ -2699,6 +2760,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.
*
Expand Down
Loading
Loading