Repository navigation
Comments: Harden reaction and note permissions. - #15853
adamsilverstein wants to merge 33 commits into
Conversation
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
`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
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
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
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
"$" 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
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
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
…ent. 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
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
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
…tests. See #66635. Claude-Session: https://claude.ai/code/session_018vwYjSnX8ne8xPxaUhsmSX
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
t-hamano
left a comment
There was a problem hiding this comment.
Thanks for the PR!
Relaxing a strict rule later is easy, but tightening a loose one after release is hard. Reactions may be attached to regular comments in the future, but for now it seems best to keep the rules as strict as possible.
I haven't reviewed the code in detail yet. Below is an AI-assisted analysis of how each write path behaves for reactions before and after this PR. The three rows marked 🤔 are points that may need changes, and all of them look reasonable to me.
| Path: scenario | Reaction (before) | Reaction (after) |
|---|---|---|
| REST create: set another user as the author | Rejected. Specifying another user as author returns an error. The author is also overwritten with the current user on save |
No change |
| REST create: emoji key not in the allowed list | Rejected. Anything other than the 7 allowed keys (get_note_reaction_keys()) returns an error |
No change |
| REST create: parent on a different post | Rejected. Returns an error if the parent note's post differs from the request's post |
No change |
| REST create: parent of the wrong type | Rejected. Returns an error if the parent is not a note | No change |
| REST update: any edit (including author, parent, post and type changes) | Partial. Reaction updates are refused, but the type is compared with ===, so a row with uppercase letters such as Reaction is treated as a regular comment and can be updated |
Rejected. The type is compared in lowercase, so Reaction rows are refused too. The lower-level wp_update_comment() also refuses reaction updates |
| ajax reply: create a note or reaction | Pass. comment_type can be set freely from the request, so reactions can be created |
Rejected. Refused when the type is note or reaction (case-insensitive), or contains characters other than letters, digits, _ and - |
| ajax reply: set another user as the author | Rejected. The author is always the current user | No change |
| ajax reply: emoji key not in the allowed list | Pass. Any string can be saved as the content | Rejected. Reactions can no longer be created via ajax |
| ajax reply: parent on a different post | Pass. Only the parent's status is checked | Rejected. Reactions can no longer be created via ajax |
| ajax reply: parent of the wrong type | Pass. The parent's type is not checked | Rejected. Reactions can no longer be created via ajax |
edit_comment(): change the author ($_POST['user_id'] etc.) |
Pass. user_id and other fields in $_POST are saved as is |
Rejected. The lower-level wp_update_comment() refuses all reaction updates |
edit_comment(): change the content |
Pass. The content can be rewritten to any string | Rejected. The lower-level wp_update_comment() refuses all reaction updates |
edit_comment(): change the parent |
Partial. Checks that the new parent has the same type and is on the same post. But when the post is changed at the same time, the check uses the post the comment is leaving | Rejected. The lower-level wp_update_comment() refuses all reaction updates |
edit_comment(): move to another post |
Pass. Edit permission on the destination post is not checked. The post can even be set to 0 | Rejected. The lower-level wp_update_comment() refuses all reaction updates |
edit_comment(): change the type |
Pass. comment_type can be changed to any value |
Rejected. The lower-level wp_update_comment() refuses all reaction updates |
wp.editComment: change the content, author name or email |
Pass. The content can be rewritten to any string, and the author name and email to someone else's | Rejected. The lower-level wp_update_comment() refuses all reaction updates |
wp_update_comment() directly: change a reaction |
Pass. Saved without any validation | Rejected. Updating a reaction returns a comment_reaction_immutable error (remove and re-add instead) |
wp_update_comment() directly: change the type |
Pass. Can be changed to any type | Rejected. Changing to or from note/reaction returns a comment_type_immutable error |
wp_update_comment() directly: parent and post consistency |
Pass. The parent and post combination is not validated | Rejected. Reactions cannot be updated at all |
wp_update_comment() directly: move a note to another post |
Pass. When a note is moved to another post, its reactions stay on the original post and keep being counted in the summary | 🤔 When a note is moved to another post, the post of its child reactions is rewritten too. However, this only applies when the moved comment is a note. Suggestion: to allow for reactions on regular comments in the future, move child reactions along with any comment that moves to another post, regardless of the parent's type |
wp_insert_comment() directly: all fields |
Pass. Nothing is validated | 🤔 No change. Suggestion: when a plugin or other code saves a reaction directly without going through REST, refuse to save it unless it meets the same rules as REST (the parent is a note on the same post, and the emoji is in the allowed list) |
reaction_summary read: reaction rows on a different post |
Pass. Counts by parent ID only, so reaction rows on other posts are counted too | Rejected. Joins the parent note and counts only rows where the reaction and its parent note are on the same post |
reaction_summary read: keys not in the allowed list |
Pass. Disallowed keys appear in the summary as is | Rejected. Keys outside the allowed list are excluded from the counts |
reaction_summary read: inconsistent author (user_id 0 etc.) |
Pass. Rows with user_id = 0 and duplicate rows from the same user are counted |
🤔 No change. Suggestion: skip rows with an unknown author, and count duplicate rows from the same user once (one vote per user per key) |
|
P.S. Should we publish a 7.2 dev note to explain how the |
|
I'm not really sure about changes for APIs like |
I agree we should let this function behave with limited restrictions. having access to that function implies the user already has direct access to code and can do whatever they want including direct database manipulation.
Probably worth a dev note for reactions. +1
I would favor preventing notes from being moved to another post int he first place, thats not something we support currently; we can reconsider if we want to change that later. For now that sees simpler than trying to move all of the chile reactions.
As discussed above, lets not add restrictions to
same.
Feels outside scope of this pr, will review. |
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.
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.
|
@t-hamano here is a first pass at the dev note you suggested. I also pushed changes so the low-level functions are left unrestricted and the checks sit in the request handlers instead; the description is updated to match. I had Claude draft the dev note from the code as it stands in this PR, then I edited it a bit:
|
Co-authored-by: Weston Ruter <westonruter@gmail.com>
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
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
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
# Conflicts: # src/wp-admin/includes/comment.php
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.
Co-authored-by: Weston Ruter <westonruter@gmail.com>
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
…he 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
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
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
|
Thanks for the thorough second pass @westonruter! I applied your suggestions as a batch and dropped the remaining Claude worked through the follow-ups, here is what it ran into:
I've asked Clause to stop adding these questions at the end which it seems to like to do to copy my style. |
Yeah, that's fine. Now that it's doing a truthy check, it will work with either |
westonruter
left a comment
There was a problem hiding this comment.
Changes in src look good to me. I skimmed changes in tests.
There was a problem hiding this comment.
🟡 Changes recommended
Critical authorization gaps still let post editors rewrite other users’ mixed-case legacy reactions and attach notes to threads on posts they cannot edit.
5 open issues
What changed in this PR
This PR hardens WordPress’s comment request handlers for editorial notes and reactions while leaving low-level comment-writing functions unrestricted.
Changes:
- Restricts Ajax comment creation and blocks reaction edits through admin and XML-RPC handlers.
- Adds parent/post validation, nonnegative REST ID constraints, and filtering and accumulation of reaction summaries.
- Adds regression tests and updates the generated REST schema fixture.
| File | Description |
|---|---|
tests/qunit/fixtures/wp-api-generated.js |
Updates mocked REST schema constraints. |
tests/phpunit/tests/xmlrpc/wp/editComment.php |
Tests rejection of reaction edits through XML-RPC. |
tests/phpunit/tests/rest-api/rest-comments-controller.php |
Tests note reply validation and reaction summaries. |
tests/phpunit/tests/comment.php |
Verifies low-level reaction updates remain unrestricted. |
tests/phpunit/tests/ajax/wpAjaxReplytoComment.php |
Tests restricted types and cross-post reply handling. |
tests/phpunit/tests/ajax/wpAjaxEditComment.php |
Tests reaction edits, type changes, and comment moves. |
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Validates note parents and filters reaction summaries. |
src/wp-includes/class-wp-xmlrpc-server.php |
Rejects reaction edits through XML-RPC. |
src/wp-admin/includes/comment.php |
Blocks reaction edits and checks retained parents when moving comments. |
src/wp-admin/includes/ajax-actions.php |
Restricts reply types and drops cross-post parents. |
Review effort: Balanced
…eter. 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
|
I don't feel like Copilot's suggestions are valid or impactful enough to warrant further changes. |
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.
…ate. 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.
|
@westonruter I added two small additional commits hardening a couple more cases, then ran some manual testing to make sure everything worked as expected. I'm going to get this committed before the next RC, |
| } | ||
|
|
||
| if ( 'note' === $request['type'] && $request['parent'] ) { | ||
| $parent_check = $this->check_note_parent( absint( $request['parent'] ), absint( $request['post'] ) ); |
There was a problem hiding this comment.
The absint() checks are unnecessary given the schema, no?
| $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; |
There was a problem hiding this comment.
I believe the absint() calls are redundant here.
| $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; |


Trac ticket: https://core.trac.wordpress.org/ticket/66635
Hardens the
noteandreactioncomment types so the request handlers that write comments apply the rules the REST API already enforces for them.The low-level comment functions (
wp_insert_comment(),wp_new_comment(),wp_update_comment()) are left alone, the checks live in the handlers that take user input.Claude rewrote this summary to match the code after the latest review rounds:
How has this been tested
vendor/bin/phpunit --group ajax --filter 'Tests_Ajax_wpAjax(Replyto|Edit)Comment'vendor/bin/phpunit --group xmlrpc --filter Tests_XMLRPC_wp_editCommentvendor/bin/phpunit tests/phpunit/tests/comment.phpvendor/bin/phpunit --filter WP_Test_REST_Comments_Controllerget_comment( $parent_id )increate_item()receivesmixed, the same as the existing call a few lines below.Commit message
AI Use
Code, tests and this description came out of a few rounds with 🤖 Claude Code, with the direction set by me and the reviewers. I will review and test.