Skip to content

Notes: Tighten note and reaction rules, compare reaction keys exactly - #86775

Open
adamsilverstein wants to merge 13 commits into
trunkfrom
fix/notes-reaction-key-read-validation
Open

adamsilverstein wants to merge 13 commits into
trunkfrom
fix/notes-reaction-key-read-validation

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Test in WordPress Playground

Claude Code (Fable 5.2) reworked this PR into the port and rewrote the description:

What?

Bring the REST API changes from https://core.trac.wordpress.org/changeset/65750 (WordPress/wordpress-develop#15853) over to Gutenberg's comments controller, and stop reading reaction keys through wp_strip_all_tags().

This PR used to add an is_reaction_key() shape check. That is gone. Core now only counts reactions that carry one of the curated keys, and an allow list covers everything the shape check was guarding, so the review threads on that function no longer apply to the code here.

Why?

The plugin's controller replaces core's, so without the port it does not have the rules that landed in core: a reaction stored against another post still counted toward a note, any string in a reaction row showed up in reaction_summary as another emoji, and a note reply could be parented to a note on a different post.

Separately, the controller validated a reaction key against the curated set and then ran the stored content through wp_strip_all_tags() wherever it read one back. A key is one of seven fixed strings, so there is nothing to strip: the content either is a curated key or it is not a reaction this controller wrote.

How?

Ported from core, in lib/compat/wordpress-7.2/class-gutenberg-rest-comment-controller-7-2.php:

  • A note reply needs a note on the same post as its parent, on create and on update (check_note_parent()).
  • A note with replies or reactions cannot be moved to another post.
  • reaction_summary joins each reaction to its note and only counts reactions stored on the note's post that carry a curated key.
  • The duplicate check and the concurrent-request cleanup are scoped to the post.
  • parent and post get minimum: 0 in the schema and the parent collection param.

The Ajax, XML-RPC and Edit Comment screen changes from the same core commit are not ported. Those handlers live in core files the plugin does not override.

On top of the port:

  • The five wp_strip_all_tags() calls on reaction content are removed. Stored content is compared to the curated keys exactly.
  • Behavior change on create: <b>2764</b> used to be accepted and stored as 2764. It now gets rest_comment_invalid_reaction. The editor only ever sends the bare key.
  • A stored <b>2764</b> row is left out of the summary. With stripping it counted toward the heart.

The same removal for core is in WordPress/wordpress-develop#15985.

Testing Instructions

There is no visible change in the editor, so the quick check is that reactions still work:

  1. Add a note to a block and react to it with a couple of emoji. The pills and counts show up as before.
  2. Remove a reaction, reload, and confirm the counts are still right.
  3. Reply to a note and confirm the reply saves.

The rest is covered by PHPUnit: npm run test:unit:php:base -- --filter WP_Test_REST_Comments_Controller_Gutenberg

The REST tests from the core commit are ported as they are, apart from test_reaction_summary_accumulates_rows_that_strip_to_the_same_key, which is replaced by test_reaction_summary_ignores_markup_around_a_curated_key. test_cannot_create_reaction_with_invalid_input gains a case for markup around a valid key.

Locally the class passes (101 tests) against a core checkout that already has r65750. The update rules for notes have not been run locally against a core without that commit, where only the plugin's override enforces them. CI covers that.

AI Use

Claude Code (Fable 5.2) wrote the port, 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.
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.
@adamsilverstein adamsilverstein added [Type] Bug An existing feature does not function as intended [Feature] Notes Phase 3 of the Gutenberg roadmap around block commenting labels Nov 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-authored-by: dmsnell <dmsnell@git.wordpress.org>

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 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.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 5d44173 trunk % Change
timeToFirstByte 58 ms +5.78% -2.67% 56.85 ms +7.21% -3.69% 2.02%
largestContentfulPaint 90 ms +6.67% -4.44% 88 ms +9.09% -4.55% 2.27%
lcpMinusTtfb 30.15 ms +26.2% -3.81% 30.55 ms +21.6% -8.18% -1.31%
wpBeforeTemplate 30.36 ms +5.93% -2.14% 30.34 ms +4.85% -3.33% 0.07%
wpTemplate 23.26 ms +2.79% -4.17% 22.36 ms +3.26% -2.82% 4.03%
wpTotal 54.33 ms +5.34% -3.28% 53.02 ms +6.28% -3.66% 2.47%
wpMemoryUsage 7.75 MB +0% -0% 7.72 MB +0% -0% 0.47%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 5d44173 trunk % Change
timeToFirstByte 48.1 ms +3.74% -3.64% 46.85 ms +7.04% -0.85% 2.67%
largestContentfulPaint 84 ms +4.76% -4.76% 82 ms +4.88% -2.44% 2.44%
lcpMinusTtfb 35.3 ms +4.39% -3.54% 34.3 ms +4.37% -2.33% 2.92%
wpBeforeTemplate 27.64 ms +4.38% -1.66% 27.56 ms +1.56% -1.6% 0.29%
wpTemplate 16.56 ms +4.95% -4.23% 16.76 ms +3.46% -4.3% -1.19%
wpTotal 44.64 ms +4.59% -3.23% 43.92 ms +5.94% -1.05% 1.64%
wpMemoryUsage 6.44 MB +0% -0% 6.41 MB +0% -0% 0.58%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 5d44173 trunk % Change
mediaProcessingJpeg 399.07 ms +0.85% -1.03% 396.9 ms +0.61% -0.3% 0.55%
mediaProcessingAvif 6152.97 ms +0.52% -0.25% 6123.15 ms +0.13% -0.43% 0.49%
mediaProcessingJpegToAvif 4244.87 ms +0.27% -0.37% 4220.5 ms +0.09% -0.04% 0.58%

media-upload

Metric 5d44173 trunk % Change
jpegUploadProcessing 1994.48 ms +1.4% -1.56% 1508.73 ms +1.13% -1.55% 32.2%
pngUploadProcessing 270.92 ms +0.98% -2.95% 267.5 ms +4.22% -0.94% 1.28%
largeJpegUploadProcessing 1516.12 ms +30.14% -1.38% 1503.25 ms +30.78% -1.43% 0.86%
multipleImageUploadProcessing 2057.86 ms +0.73% -0.28% 2070.34 ms +2.2% -5.09% -0.6%

post-editor

Metric 5d44173 trunk % Change
serverResponse 449.8 ms +5.88% -5.03% 476.78 ms +0.62% -10.05% -5.66%
firstPaint 422.97 ms +8.18% -7.93% 418.63 ms +8.61% -11.66% 1.04%
domContentLoaded 803.56 ms +1.14% -1.31% 805.9 ms +2.54% -0.42% -0.29%
loaded 804.57 ms +1.15% -1.3% 806.94 ms +2.52% -0.4% -0.29%
firstContentfulPaint 453.87 ms +2.54% -1.92% 453.82 ms +6.04% -2.64% 0.01%
firstBlock 2160.22 ms +0.62% -0.56% 2177.03 ms +1.9% -0.38% -0.77%
type 16.18 ms +8.71% -4.94% 17.23 ms +5.75% -14.28% -6.09%
typeWithoutInspector 16.36 ms +3.3% -8.8% 17.22 ms +5.57% -12.54% -4.99%
typeWithTopToolbar 21.58 ms +4.12% -3.24% 21.5 ms +6.05% -4.6% 0.37%
typeContainer 8.01 ms +6.87% -3.62% 7.66 ms +11.88% -5.61% 4.57%
focus 111.23 ms +5.49% -4.16% 109.88 ms +7.4% -6.06% 1.23%
firstFocus 262.55 ms +0% -0% 245.96 ms +0% -0% 6.74%
selectAll 398.51 ms +0.32% -3.15% 381.22 ms +5.83% -1.41% 4.54%
undo 27.45 ms +6.38% -3.61% 27.54 ms +6.54% -3.81% -0.33%
listViewOpen 38.21 ms +2.59% -1.88% 39.35 ms +5.46% -5.74% -2.9%
inserterOpen 46.23 ms +13.69% -22.54% 37.95 ms +4.72% -4.98% 21.82%
inserterHover 11.04 ms +15.94% -6.34% 10.63 ms +7.9% -9.88% 3.86%
inserterSearch 7.76 ms +11.21% -4.25% 8.3 ms +6.27% -5.66% -6.51%
loadPatterns 626.82 ms +2.93% -4.7% 613.62 ms +0.41% -2.62% 2.15%
wpTotal 440.16 ms +6.05% -5.1% 467.1 ms +0.65% -10.21% -5.77%
wpMemoryUsage 14.81 MB +0% -0% 14.77 MB +0% -0% 0.33%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 5d44173 trunk % Change
serverResponse 337.17 ms +0.35% -10.26% 317.48 ms +6.42% -6.35% 6.2%
firstPaint 396.42 ms +2.69% -16.48% 333.39 ms +9.69% -3.07% 18.91%
domContentLoaded 711.59 ms +6.51% -1.01% 689.72 ms +1.06% -0.34% 3.17%
loaded 712.53 ms +6.5% -1.01% 690.63 ms +1.06% -0.33% 3.17%
firstContentfulPaint 403.17 ms +2.06% -2.04% 386.82 ms +3.97% -1.16% 4.23%
firstBlock 2205.8 ms +3.96% -1.03% 2165.37 ms +1.24% -0.83% 1.87%
type 17.68 ms +4.86% -13.57% 16.85 ms +7.54% -6.94% 4.93%
navigate 99.44 ms +8.4% -2.96% 95.48 ms +6.15% -1.07% 4.15%
loadPatterns 1160.28 ms +2.86% -17.84% 1216.26 ms +6.31% -16.1% -4.6%
loadPages 942.53 ms +0.69% -1.02% 971.84 ms +1.93% -2.01% -3.02%
wpTotal 328.68 ms +0.37% -10.52% 308.99 ms +6.56% -6.53% 6.37%
wpMemoryUsage 12.84 MB +0% -0% 12.80 MB +0% -0% 0.29%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

9853f2b Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

should undo without broken temporary state in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:image -->
- <figure class="wp-block-image"><img alt=""/></figure>
+ <!-- wp:image {"id":21,"sizeSlug":"full","linkDestination":"none"} -->
+ <figure class="wp-block-image size-full"><img src="http://localhost:8889/wp-content/uploads/2026/11/30b3545f-1277-49b3-aba8-815093b83f96.png" alt="" class="wp-image-21"/></figure>
  <!-- /wp:image -->
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/image.spec.js:432:49

9853f2b Run

Link the Core PR for Trac #66695 so the backport check can find it.
@adamsilverstein
adamsilverstein requested review from dmsnell and removed request for spacedmonkey November 13, 2026 18:35
* @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.

this appears to limit the code points to those requiring 4–6 characters, but still lets through code points such as 000a. that’s not problematic, but lets certain syntax forms through while rejecting others when they refer to the same code point.

if the goal is a fast rejection of strings containing unallowed characters, strspn() can be used, and then the only additional check would be inside the foreach for empty spans

if ( strlen( $value ) !== strspn( $value, '-01234567789abcdef' ) ) {
	return false;
}

foreach ( explode( '-', $value ) as $hex ) {
	$code_point = hexdec( $hex );

	if ( '' === $hex || $code_point > 0x10FFFF || ( ... ) ) {
		return false;
	}
}

return true;

nothing significant, but it leaves the PCRE in an awkward spot because it rejects some values which are allowed as syntactical variations, while it allows some forms that may not be expected.

making the PCRE tighter would involve [1-9a-f][0-9a-f]{3,5} to ensure there aren’t leading zeros. relaxing it would be pointless compared to strspn() which performs the check with less complexity and zero allocation.

fun fact! \A and \Z do what I think you are looking for here with the combination of $ and /D. they always work. I have found the PCRE docs way more useful than the PHP docs for the preg_ functions too. anchors and simple assertions

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 like the strspn suggestion, I'll give that a try!

// Rows written outside this controller may hold anything.
if ( ! is_string( $slug ) || ! self::is_reaction_key( $slug ) ) {
continue;
}

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.

first thought: is it important to reject values like 1F170 vs. 1f170?

second thought: if this were self::parse_reaction_key() then the trimming and normalization and rejection could all happen in that method.

$reaction_key = self::parse_reaction_key( $slug );
if ( null === $reaction_key ) {
	continue;
}

// now $reaction_key is lowercase, trimmed, and fully-normative

adamsilverstein and others added 4 commits November 24, 2026 12:02
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.
Describe the rows wpdb returns so the summary code passes PHPStan at
level 10, which makes the is_string() guard redundant.
Core trunk annotates these rows as decimal-int-string objects; use the
same annotation so the backport stays line for line.
@adamsilverstein

Copy link
Copy Markdown
Member Author

Coming back to this again I realized it may not be needed yet, now that this change when enforces emojis are one of the predefined set was merged in on WordPress/wordpress-develop#15853.

If we enable extensibility, we might need to change the approach, but in theory we should be able to use an allow list approach to be even more precise and declarative than the matching in this PR.

We can probably close the core backport of this, and bring the core fixes from 15853 back over to Gutenberg's compat files.

Core now only counts reactions that carry a curated key (r65750), an
allow list that covers what the shape check was guarding. Return the
controller to trunk ahead of porting that change.
Port the REST API half of core r65750: a note reply needs a note on
the same post as its parent, a note with replies or reactions stays on
its post, and reaction_summary only counts reactions on their note's
post that carry a curated key. The Ajax and XML-RPC handlers that
commit also changed live in core files the plugin does not override.
A reaction's content is a curated key or it is not a reaction the
controller wrote. Stripping tags first let markup around a key pass on
create and count toward the real key in the summary.
@adamsilverstein adamsilverstein changed the title Notes: Validate reaction keys instead of stripping tags Notes: Tighten note and reaction rules, compare reaction keys exactly Nov 24, 2026
@adamsilverstein

Copy link
Copy Markdown
Member Author

I realized it may not be needed yet, now that this change

I repurposed this PR to bring back over the changes from core that tighten and improve reaction handling, including matching the emoji from the list exactly and the wp_strip_all_tags removal. These will be used when reactions are active with the Gutenberg plugin on sites running WordPress < 7.2.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Notes Phase 3 of the Gutenberg roadmap around block commenting [Type] Bug An existing feature does not function as intended

Projects

Status: 🔎 Needs Review

Development

Successfully merging this pull request may close these issues.

2 participants