Skip to content

Notes: Validate reaction keys instead of stripping tags - #86775

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

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

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Claude Code wrote this up after working through the review feedback on #83169:

What?

Follow up to #76767, from @dmsnell's review on #83169 (comment).

Stop reading reaction keys through wp_strip_all_tags() in the comments controller. A stored reaction now counts only when its content has the shape of a reaction key, and validated keys are compared exactly.

Why?

A reaction stores its emoji as a hex key in comment_content (eg. 2764 for a heart). The controller validates that key against the curated set on create, but everywhere it read one back it ran the content through wp_strip_all_tags() and used whatever came out.

That has two problems. The summary trusted any row: a reaction written by something other than this controller with heart or 1F680 as its content showed up in reaction_summary as another emoji. And stripping folded different content together, so a <b>2764</b> row landed on the same key as a real 2764 row and one count overwrote the other.

How?

  • Add a private is_reaction_key() that checks the shape: lowercase hex code points joined by -, each one a Unicode scalar value (nothing past U+10FFFF, no surrogates). prefetch_reaction_summaries() skips rows that fail it.
  • The check is on the shape and not on the curated set, so reactions stay readable if the set changes later (Add emoji picker with Autocomplete grid  #83538 widens it).
  • The duplicate check and the concurrent-request cleanup compare the stored content to the validated key directly.
  • On create the content is trimmed and then has to match a curated key exactly. This is a small behavior change: <b>2764</b> used to be accepted and stored as 2764, now it gets rest_comment_invalid_reaction. The editor only ever sends the bare key.

Core trunk has the same five calls since reactions landed there in https://core.trac.wordpress.org/changeset/64488, so this needs a Core ticket and patch as well.

Testing Instructions

Test in WordPress Playground

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.

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

  • test_reaction_summary_omits_malformed_reaction_content inserts a row with content that is not a key (markup around a key, a name, uppercase hex, a value past the last code point, a surrogate, a trailing separator) next to a real reaction and expects only the real one in the summary.
  • test_cannot_create_reaction_with_invalid_input gains a case for markup around a valid key.

Six of those seven cases fail on trunk. The markup-in-summary case passes there only because the two rows collapse onto one key.

AI Use

Claude Code wrote the code, the tests and this description while I was working through review feedback with it. I will review and test.

Session: https://claude.ai/code/session_018vwYjVcDMRyJisiSbUUeky

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 7ac412f trunk % Change
timeToFirstByte 35.1 ms +8.97% -3.56% 36.35 ms +12.65% -6.19% -3.44%
largestContentfulPaint 60 ms +10% -6.67% 62 ms +3.23% -3.23% -3.23%
lcpMinusTtfb 23.9 ms +17.15% -10.88% 25.15 ms +6.36% -12.72% -4.97%
wpBeforeTemplate 18.34 ms +17.99% -2.89% 18.62 ms +24.33% -5.05% -1.5%
wpTemplate 13.49 ms +4.6% -5.71% 13.93 ms +5.46% -7.39% -3.16%
wpTotal 32.02 ms +9.68% -3.47% 33.31 ms +11.38% -6.45% -3.87%
wpMemoryUsage 7.75 MB +0% -0% 7.72 MB +0% -0% 0.45%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 7ac412f trunk % Change
timeToFirstByte 28.7 ms +9.58% -1.05% 29.4 ms +10.54% -2.72% -2.38%
largestContentfulPaint 54 ms +7.41% -3.7% 54 ms +14.81% -3.7% 0%
lcpMinusTtfb 24.2 ms +13.02% -3.31% 24.9 ms +6.63% -6.02% -2.81%
wpBeforeTemplate 16.01 ms +5.75% -0.94% 16.07 ms +12.07% -1.12% -0.37%
wpTemplate 10.18 ms +4.81% -3.54% 10.29 ms +6.9% -3.69% -1.07%
wpTotal 26.12 ms +10.03% -0.8% 26.8 ms +10.97% -2.69% -2.54%
wpMemoryUsage 6.44 MB +0% -0% 6.40 MB +0% -0% 0.56%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 7ac412f trunk % Change
mediaProcessingJpeg 228.3 ms +1.45% -2.15% 222.57 ms +1.68% -0.49% 2.57%
mediaProcessingAvif 3721.37 ms +0.59% -0.95% 3664.17 ms +1.18% -0.69% 1.56%
mediaProcessingJpegToAvif 2598.56 ms +0.72% -1.2% 2574.32 ms +0.43% -0.45% 0.94%

media-upload

Metric 7ac412f trunk % Change
jpegUploadProcessing 1443.27 ms +3.08% -0.82% 1439.55 ms +1.13% -0.95% 0.26%
pngUploadProcessing 189.14 ms +2.6% -2.98% 208.75 ms +6.64% -3.57% -9.39%
largeJpegUploadProcessing 1445.26 ms +0.4% -1.5% 1444.07 ms +0.95% -0.45% 0.08%
multipleImageUploadProcessing 1504.19 ms +1.22% -1.95% 1528.53 ms +0.41% -0.99% -1.59%

post-editor

Metric 7ac412f trunk % Change
serverResponse 496.21 ms +2.34% -6.78% 483.2 ms +4.25% -3.19% 2.69%
firstPaint 438.1 ms +9.42% -7.97% 415.77 ms +6.56% -5.46% 5.37%
domContentLoaded 821.27 ms +1.59% -1.24% 814.1 ms +2.62% -0.6% 0.88%
loaded 822.42 ms +1.57% -1.26% 815.04 ms +2.66% -0.55% 0.91%
firstContentfulPaint 469.39 ms +2.12% -4.01% 463.18 ms +3.55% -3.53% 1.34%
firstBlock 2250.91 ms +8.87% -0.96% 2207.16 ms +1.39% -0.58% 1.98%
type 17.55 ms +6.95% -8.83% 18.82 ms +3.08% -15.67% -6.75%
typeWithoutInspector 18.38 ms +2.29% -11.04% 16.93 ms +7.5% -7.21% 8.56%
typeWithTopToolbar 24.78 ms +2.42% -12.43% 23.95 ms +3.92% -15.24% 3.47%
typeContainer 8.45 ms +9.7% -10.18% 8.08 ms +5.69% -3.22% 4.58%
focus 119.47 ms +5.09% -3.67% 122.55 ms +11.43% -7.76% -2.51%
firstFocus 279.89 ms +0% -0% 261.54 ms +0% -0% 7.02%
selectAll 422.78 ms +2.64% -4.18% 445.88 ms +0.28% -7.41% -5.18%
undo 29.14 ms +3.74% -4.56% 30.17 ms +5.44% -6.13% -3.41%
listViewOpen 78.34 ms +8.91% -4.26% 75.76 ms +15.75% -6.64% 3.41%
inserterOpen 40.92 ms +16.94% -6.67% 41.17 ms +2.99% -13.97% -0.61%
inserterHover 11.38 ms +6.41% -9.23% 11.21 ms +7.31% -9.9% 1.52%
inserterSearch 8.17 ms +7.83% -4.65% 7.66 ms +20.89% -9.14% 6.66%
loadPatterns 634.36 ms +7.34% -2.64% 643.94 ms +4.32% -3.01% -1.49%
wpTotal 486.52 ms +2.3% -6.92% 473.6 ms +4.03% -3.28% 2.73%
wpMemoryUsage 14.81 MB +0% -0% 14.77 MB +0% -0% 0.25%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 7ac412f trunk % Change
serverResponse 467.78 ms +1.85% -4.99% 466.41 ms +1.02% -0.62% 0.29%
firstPaint 430.96 ms +10.71% -8.13% 431.11 ms +7.04% -8.28% -0.03%
domContentLoaded 824.13 ms +6.13% -1.21% 820.36 ms +2.56% -0.6% 0.46%
loaded 825.17 ms +6.11% -1.2% 821.35 ms +2.58% -0.6% 0.47%
firstContentfulPaint 474.82 ms +7.26% -5.64% 461.53 ms +1.68% -0.89% 2.88%
firstBlock 2572.1 ms +4.99% -1.25% 2572.93 ms +1.23% -0.86% -0.03%
type 16.4 ms +5.98% -8.11% 16.71 ms +4.91% -6.94% -1.86%
navigate 111.88 ms +1.33% -1.79% 112.35 ms +2.97% -6.35% -0.42%
loadPatterns 1372.5 ms +10.13% -4.17% 1306.56 ms +3.95% -10.82% 5.05%
loadPages 943.25 ms +2.88% -0.82% 956.28 ms +1.65% -3.01% -1.36%
wpTotal 458.42 ms +1.84% -5.1% 456.72 ms +1.05% -0.58% 0.37%
wpMemoryUsage 12.83 MB +0% -0% 12.80 MB +0% -0% 0.25%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

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

As a user I want to be able to create a navigation overlay for a specific navigation block in /test/e2e/specs/site-editor/navigation-overlay-template-part.spec.js, passed after 1 failed attempt.
TimeoutError: locator.click: Timeout 10000ms exceeded.
Call log:
  - waiting for getByRole('region', { name: /(Editor publish|Save panel)/ }).getByRole('button', { name: 'Save', exact: true })

    at Editor.saveSiteEditorEntities (/home/runner/work/gutenberg/gutenberg/packages/e2e-test-utils-playwright/src/editor/site-editor.ts:48:5)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/navigation-overlay-template-part.spec.js:91:4
List in /test/e2e/specs/editor/blocks/query.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 22
+ Received  +  1

  Array [
    Object {
-     "innerBlocks": Array [
-       Object {
-         "innerBlocks": Array [
-           Object {
-             "name": "core/post-title",
-           },
-           Object {
-             "name": "core/post-featured-image",
-           },
-           Object {
-             "name": "core/post-excerpt",
-           },
-           Object {
-             "name": "core/separator",
-           },
-           Object {
-             "name": "core/post-date",
-           },
-         ],
-         "name": "core/post-template",
-       },
-     ],
+     "innerBlocks": Array [],
      "name": "core/query",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/query.spec.js:226:42
region-scoped focus that refrains on traversals in /test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts, passed after 1 failed attempt.
Error: expect(locator).toHaveCount(expected) failed

Locator:  locator('[data-wp-router-region="lifecycle-a"] :focus')
Expected: 0
Received: 1
Timeout:  5000ms

Call log:
  - Expect "toHaveCount" locator('[data-wp-router-region="lifecycle-a"] :focus') with timeout 5000ms
  - waiting for locator('[data-wp-router-region="lifecycle-a"] :focus')
    14 × locator resolved to 1 element
       - unexpected value "1"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts:1652:37
should select synced pattern in /test/e2e/specs/editor/various/writing-flow.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeFocused() failed

Locator:  locator('[name="editor-canvas"]').contentFrame().locator('[data-type="core/block"]')
Expected: focused
Received: inactive
Timeout:  5000ms

Call log:
  - Expect "toBeFocused" locator('[name="editor-canvas"]').contentFrame().locator('[data-type="core/block"]') with timeout 5000ms
  - waiting for locator('[name="editor-canvas"]').contentFrame().locator('[data-type="core/block"]')
    14 × locator resolved to <div tabindex="0" role="document" draggable="true" data-title="Pattern" data-type="core/block" data-is-drop-zone="true" aria-label="Block: Pattern" id="block-d74913b3-6c32-4dbd-b80c-0dc4f3600823" data-block="d74913b3-6c32-4dbd-b80c-0dc4f3600823" class="block-editor-block-list__block wp-block is-selected is-reusable block-library-block__reusable-block-container wp-block-block block-editor-block-list__layout">…</div>
       - unexpected value "inactive"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/writing-flow.spec.js:1464:5

29f9a52 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

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

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

None yet

Development

Successfully merging this pull request may close these issues.

2 participants