Skip to content

REST API: Compare note reaction keys exactly instead of stripping tags - #15985

Open
adamsilverstein wants to merge 10 commits into
WordPress:trunkfrom
adamsilverstein:fix/66695-reaction-key-validation
Open

adamsilverstein wants to merge 10 commits into
WordPress:trunkfrom
adamsilverstein:fix/66695-reaction-key-validation

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Claude Code (Fable 5.2) cut this PR down after r65750 landed, updated description below:

Stop reading note reaction keys through wp_strip_all_tags() in WP_REST_Comments_Controller. Stored content is compared to the curated keys exactly.

A reaction stores its emoji as a hex key in comment_content (eg. 2764 for a heart), and the controller only accepts one of seven curated keys. There is nothing to strip from a value like that: the content either is a curated key or it is not a reaction this controller wrote. Trunk still runs it through wp_strip_all_tags() in five places.

This PR used to add an is_reaction_key() shape check for the summary. That is gone. Since https://core.trac.wordpress.org/changeset/65750 reaction_summary only counts curated keys, and that allow list covers everything the shape check was guarding.

What changes:

  • On create the content is trimmed and has to match a curated key exactly.
  • The duplicate check, the concurrent-request cleanup and the summary compare the stored content directly.
  • The summary assigns each count and no longer adds rows up. Rows are grouped on the raw content, and without stripping two groups cannot land on one key.

Two behavior changes to look at:

  • <b>2764</b> on create used to be accepted and stored as 2764. It now returns rest_comment_invalid_reaction. test_create_reaction_stores_canonical_key from [64488] is renamed to test_create_reaction_rejects_markup_around_key. The editor only ever sends the bare key.
  • A stored <b>2764</b> row is left out of the summary. On trunk it counts toward the heart, which test_reaction_summary_accumulates_rows_that_strip_to_the_same_key from [65750] expected. That test is replaced by test_reaction_summary_ignores_markup_around_a_curated_key.

Testing: vendor/bin/phpunit --filter WP_Test_REST_Comments_Controller passes locally (302 tests).

Gutenberg PR: WordPress/gutenberg#86775, which also ports the REST changes from [65750].
Raised in review at WordPress/gutenberg#83169 (comment)

Trac ticket: https://core.trac.wordpress.org/ticket/66695

AI Use

Claude Code (Fable 5.2) wrote the code, the tests and this description. I will review and test.

🤖 Generated with Claude Code

https://claude.ai/code/session_018vwYjUFvgZaeA8cQrCjN2u

A reaction row whose content is not a key should be left out of the
summary, and markup around a key should be rejected on create.

See #66695.
wp_strip_all_tags() let any stored content through as a summary key and
folded "<b>2764</b>" into "2764". Check the shape of a stored key and
compare validated keys exactly.

See #66695.
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

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:

Props adamsilverstein, dmsnell.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

*
* A key is one or more Unicode scalar values in lowercase hex, joined
* by `-`. The shape is checked, not the curated set, so a reaction
* stays readable if the set changes.

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.

this sounds like AI speak, and answers a question that seems unlikely to be asked. maybe the entire thing could be tersely and more-helpfully described, though I know that others are sure to have different expectations than me.

/**
 * Determines if a given value represents a valid reaction key.
 *
 * Reaction keys are described by a sequence of one or more Unicode
 * Scalar Values joined by a hyphen (U+2D).
 * 
 * @since 7.2.0
 *
 * @param string $value Potentially contains a valid reaction key.
 * @param bool Whether the given value comprises a well-formed reaction key.
 */

@adamsilverstein adamsilverstein Nov 24, 2026 •

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.

Good suggestion, thanks

* @return bool True for a well-formed key.
*/
private static function is_reaction_key( string $value ): bool {
if ( 1 !== preg_match( '/^[0-9a-f]{4,6}(?:-[0-9a-f]{4,6})*$/D', $value ) ) {

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.

just realized I am confused because there are two PRs 😆
https://github.com/WordPress/gutenberg/pull/86775/changes#r4633747523

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.

right this is the backport, its probably better to keep all the feedback on that Gutenberg PR, then I can make sure the backport is synced accordingly.

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 will try and remember, thank you!

adamsilverstein and others added 6 commits November 24, 2026 12:00
The pattern accepted several spellings of one code point (`02764`
next to `2764`). Accept a single spelling per code point - lowercase
hex, zero-padded to four digits - so equal reactions always share a
summary bucket, and drop the regular expression for a span check.

See #66695.
Describe the rows wpdb returns so the summary code passes PHPStan at
level 10, which makes the is_string() guard redundant.

See #66695.
Keys are no longer stripped of tags, so each group of rows maps to its
own key and nothing is left to add up.

See #66695.
Since r65750 the summary only counts curated keys, an allow list that
covers everything the shape check guarded. Compare stored content to
the curated keys exactly and remove the helper.

See #66695.
@adamsilverstein adamsilverstein changed the title REST API: Validate note reaction keys instead of stripping tags REST API: Compare note reaction keys exactly instead of stripping tags Nov 24, 2026
@adamsilverstein

adamsilverstein commented Nov 24, 2026 •

Copy link
Copy Markdown
Member Author

@dmsnell This PR now mostly removes the wp_strip_all_tags for core. I believe the sanitization is no longer needed since we already reject emojis that aren't on our list of emoji, so any additional tags would cause the incorrect value to be rejected which is the desired behavior.

what do you think?

@dmsnell

dmsnell commented Nov 24, 2026

Copy link
Copy Markdown
Member

I believe the sanitization is no longer needed since we already reject emojis that aren't on our list of emoji

answering here instead of the Gutenberg patch since you asked here.

that’s a fair choice and I don’t see an obvious problem with it, though it does contradict the previously-worded description

so a reaction stays readable if the set changes.

it’s probably likely we will expand this set, and I suspect people will end up opening it first to all Unicode code points, and then restrict it to valid extended grapheme clusters, and then expand it to slugs like :happy:

if that’s the case then the parsing aspect would still be valuable, but that can be addressed when the functionality changes in the future.

at a minimum, however, since we are talking about removing the runtime guards against inputs whose contents cannot always be trusted, it would be worth noting somewhere that when the rules change, additional parsing and/or validation should apply.

the only particular case that’s really worth ensuring is that we don’t invite a future change to introduce values that will be invalid if nobody remembers to update the corresponding code.

I’m trusting you can make these calls and not trying to hide a demand that you add comments or change things. you would know better than I do if there’s a data-flow gap or reasonable estimation that untrusted comment content could reach this in a way that breaks things in a way that matters.

Exact comparison against the fixed list is the only guard on request
and stored reaction content. Note that at both comparisons so widening
the set does not leave unvalidated keys behind.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants