Skip to content

Editor: Validate the entity in the entity fields reads and REST route - #16067

Open
mukeshpanchal27 wants to merge 4 commits into
WordPress:trunkfrom
mukeshpanchal27:fix/entity-fields-input-validation
Open

mukeshpanchal27 wants to merge 4 commits into
WordPress:trunkfrom
mukeshpanchal27:fix/entity-fields-input-validation

Conversation

@mukeshpanchal27

@mukeshpanchal27 mukeshpanchal27 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

The entity fields API is new in 7.2. It is missing two input checks that the entity actions API already has. This PR adds them, matching the actions code. It also removes the route-collision check from the entity actions endpoint, so that both endpoints behave like the other core endpoints.

1. Reading with an invalid entity causes a fatal error

WP_Entity_Fields_Registry::get_registered() and ::get_registered_field_modules() do not validate $kind and $name. With wp_get_registered_entity_fields( array(), 'page' ), PHP 8 throws TypeError: Cannot access offset of type array on array.

The actions registry calls validate_entity() in both read methods and returns array() with a _doing_it_wrong() notice. The fields registry already has the same validate_entity() method, so both read methods now call it.

2. Empty kind and name return 200

GET /wp/v2/entity-fields?kind=&name= returns 200 with an empty field list, because the arguments have no minLength. The same request to /wp/v2/entity-actions returns 400. This PR adds 'minLength' => 1 to both arguments.

3. Remove the route-collision check from the entity actions endpoint

WP_REST_Entity_Actions_Controller::is_route_of_entity_items() skipped registering /wp/v2/entity-actions when a post type or taxonomy with show_in_rest uses entity-actions as its REST base. An earlier version of this PR added the same check to the fields endpoint.

No other core endpoint with a fixed base, such as /wp/v2/settings, /wp/v2/search, /wp/v2/block-types or /wp/v2/global-styles, has this check. The collision needs a plugin to choose that REST base explicitly. So this PR removes the check, and its test, from the actions endpoint instead of copying it to the fields endpoint. Both endpoints now behave like the rest of core.

This removes one translatable string (the actions _doing_it_wrong() message) and adds none.

Tests

  • test_reading_with_an_invalid_entity_returns_empty_lists() covers an array kind, an array name, an empty kind, and an integer name.
  • test_get_items_requires_kind_and_name() now also covers an empty kind and an empty name.
  • test_the_route_of_a_taxonomy_with_the_same_base_is_kept() is removed from the actions controller tests, along with the check.
  • The REST API client fixture (wp-api-generated.js) is updated for the new minLength arguments.
  • The branch is rebased onto trunk at 7.2 RC2. The entity fields tests pass. Twelve entity actions tests fail in my environment, both on this branch and on trunk without it.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Finding the issues while reviewing 7.2 changes, drafting the fix and tests, and running the tests. I reviewed the final change.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 17, 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 mukesh27, oandregal.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the entity-fields validation and route-collision changes.

No open issues 🎉

What changed in this PR

This PR aligns the new entity fields API with the entity actions API’s validation and route-collision safeguards for WordPress 7.2.

Changes:

  • Validate entity kinds and names before registry reads, returning empty lists with an incorrect-usage notice for invalid input.
  • Reject empty REST arguments and preserve conflicting post type or taxonomy routes.
  • Add regression tests for invalid registry inputs, empty REST arguments, and taxonomy route collisions.
File Description
tests/​phpunit/​tests/​rest-api/​rest-entity-fields-controller.php Tests empty arguments and preservation of a conflicting taxonomy route.
tests/​phpunit/​tests/​entity-fields/​wpEntityFieldsRegistry.php Tests invalid entity inputs in both registry read methods.
src/​wp-includes/​rest-api/​endpoints/​class-wp-rest-entity-fields-controller.php Adds minimum argument lengths and a route-collision guard.
src/​wp-includes/​class-wp-entity-fields-registry.php Validates entity inputs before reads and documents empty results.

Review effort: Balanced

* Empty when the entity is invalid.
*/
public function get_registered( $kind, $name ) {
if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) {

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.

These checks are good, we should have them. Thanks.

'kind' => array(
'description' => __( 'Entity kind.' ),
'type' => 'string',
'minLength' => 1,

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 is good, thanks for bringing them in for entity fields.

*
* @return bool Whether a post type or taxonomy has its items on the route.
*/
protected function is_route_of_entity_items() {

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 is not necessary and actually counter-productive. We want the endpoint to be registered. It should also be removed in actions as it's an old artifact from when the endpoint as named just actions and we had a client-side fallback. Right now, both fields and actions are registered server-side and need these endpoints to be registered.

Given how time-sensitive this is (we want it in 7.2 RC cycle), I've pushed directly the fixes.

mukeshpanchal27 and others added 4 commits November 19, 2026 17:36
The entity fields API lacked checks that the entity actions API has:

- `WP_Entity_Fields_Registry::get_registered()` and `::get_registered_field_modules()` did not validate the entity kind and name, so an array caused a fatal `TypeError`. They now return an empty list with a notice.
- The `kind` and `name` arguments of `/wp/v2/entity-fields` had no `minLength`, so empty values returned a 200 response. They now return a 400 error.
- The `/wp/v2/entity-fields` route was registered even when a post type or taxonomy uses `entity-fields` as its REST base. It now leaves the route to the post type or taxonomy and reports it, like the actions route.

Follow-up to [64528].

See #66229.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dpoint.

The other core endpoints with a fixed base do not check whether a post type or taxonomy already serves its items on their route, so the entity actions endpoint no longer does either.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…point.

The other core endpoints with a fixed base do not check whether a post type or taxonomy already serves its items on their route, so the entity fields endpoint no longer does either.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oandregal
oandregal force-pushed the fix/entity-fields-input-validation branch from 7e58509 to 26266c2 Compare November 19, 2026 16:39
@oandregal

Copy link
Copy Markdown
Member

@mukeshpanchal27 I've rebased this onto trunk to speed things up.

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.

3 participants