From 504110d0c0d4c175009a44c13d0d69e5cf76d245 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 17 Nov 2026 16:39:20 +0530 Subject: [PATCH 1/4] Editor: Validate the entity in the entity fields reads and REST route. 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 --- .../class-wp-entity-fields-registry.php | 10 ++++- ...class-wp-rest-entity-fields-controller.php | 44 +++++++++++++++++++ .../entity-fields/wpEntityFieldsRegistry.php | 33 ++++++++++++++ .../rest-entity-fields-controller.php | 36 +++++++++++++++ 4 files changed, 122 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index 864828ca1c243..1cc9b629e086e 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -445,8 +445,12 @@ public function get_collection_fields( $directory ) { * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). * @return array[] The list of field definitions, in registration order. + * Empty when the entity is invalid. */ public function get_registered( $kind, $name ) { + if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) { + return array(); + } $this->initialize(); return array_values( $this->fields[ $kind ][ $name ] ?? array() ); } @@ -478,7 +482,8 @@ static function ( $entities ) { * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). * @return array[] { - * The script modules, in registration order. + * The script modules, in registration order. Empty when the entity + * is invalid. * * @type array ...$0 { * @type string $id The id of the script module. @@ -488,6 +493,9 @@ static function ( $entities ) { * } */ public function get_registered_field_modules( $kind, $name ) { + if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) { + return array(); + } $this->initialize(); $modules = array(); foreach ( $this->field_modules[ $kind ][ $name ] ?? array() as $module => $field_ids ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php index c9738f2a7bb6e..37231d7ce363e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php @@ -39,6 +39,10 @@ public function __construct() { * @since 7.2.0 */ public function register_routes() { + if ( $this->is_route_of_entity_items() ) { + return; + } + register_rest_route( $this->namespace, '/' . $this->rest_base, @@ -51,11 +55,13 @@ public function register_routes() { 'kind' => array( 'description' => __( 'Entity kind.' ), 'type' => 'string', + 'minLength' => 1, 'required' => true, ), 'name' => array( 'description' => __( 'Entity name.' ), 'type' => 'string', + 'minLength' => 1, 'required' => true, ), ), @@ -65,6 +71,44 @@ public function register_routes() { ); } + /** + * Checks whether a post type or taxonomy has its items on the route of + * the controller, reporting it with _doing_it_wrong(). + * + * The route is then left to the post type or taxonomy: registering it + * as well would not serve the fields, as the route of the items comes + * first, and would replace the schema of that route. + * + * @since 7.2.0 + * + * @return bool Whether a post type or taxonomy has its items on the route. + */ + protected function is_route_of_entity_items() { + $objects = array_merge( + array_values( get_post_types( array( 'show_in_rest' => true ), 'objects' ) ), + array_values( get_taxonomies( array( 'show_in_rest' => true ), 'objects' ) ) + ); + foreach ( $objects as $object ) { + $namespace = $object->rest_namespace ? $object->rest_namespace : 'wp/v2'; + $base = $object->rest_base ? $object->rest_base : $object->name; + if ( $this->namespace === $namespace && $this->rest_base === $base ) { + _doing_it_wrong( + __METHOD__, + sprintf( + /* translators: 1: REST route, e.g. /wp/v2/entity-fields. 2: Post type or taxonomy name. */ + __( 'The %1$s route serves the items of the "%2$s" post type or taxonomy, so the fields of entities are not available.' ), + '/' . $this->namespace . '/' . $this->rest_base, + $object->name + ), + '7.2.0' + ); + return true; + } + } + + return false; + } + /** * Checks if a given request has access to read the fields of an entity. * diff --git a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php index 6a035503d1f1f..f12d6731bc365 100644 --- a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php +++ b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php @@ -548,6 +548,39 @@ static function () use ( &$results ) { $this->assertContains( 'author', array_column( wp_get_registered_entity_fields( 'postType', 'page' ), 'id' ), 'The default field is kept.' ); } + /** + * Reading with an entity kind or name that is not a non-empty string + * returns empty lists with a notice rather than a fatal error. + * + * @ticket 66229 + * + * @dataProvider data_invalid_entities + * + * @param mixed $kind The entity kind. + * @param mixed $name The entity name. + */ + public function test_reading_with_an_invalid_entity_returns_empty_lists( $kind, $name ) { + $this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::get_registered' ); + $this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::get_registered_field_modules' ); + + $this->assertSame( array(), wp_get_registered_entity_fields( $kind, $name ), 'No fields should be returned.' ); + $this->assertSame( array(), wp_get_registered_entity_field_modules( $kind, $name ), 'No script modules should be returned.' ); + } + + /** + * Data provider. + * + * @return array The entity kind and name. + */ + public static function data_invalid_entities() { + return array( + 'an array kind' => array( array(), 'page' ), + 'an array name' => array( 'postType', array() ), + 'an empty kind' => array( '', 'page' ), + 'an integer name' => array( 'postType', 1 ), + ); + } + /** * An id that is not a non-empty string is reported and skipped: the * other ids of the call are unregistered. diff --git a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php index f5ac37779f8b3..072ca01607250 100644 --- a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php +++ b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php @@ -182,6 +182,36 @@ public function test_register_routes() { $this->assertInstanceOf( 'WP_REST_Entity_Fields_Controller', $routes[ self::ROUTE ][0]['callback'][0] ); } + /** + * A taxonomy whose items are on the route keeps it: the route of the + * fields is not registered, and that is reported. + * + * @covers ::register_routes + * @covers ::is_route_of_entity_items + * + * @ticket 66229 + */ + public function test_the_route_of_a_taxonomy_with_the_same_base_is_kept() { + global $wp_rest_server; + $this->setExpectedIncorrectUsage( 'WP_REST_Entity_Fields_Controller::is_route_of_entity_items' ); + register_taxonomy( 'entity-fields', 'post', array( 'show_in_rest' => true ) ); + + try { + $wp_rest_server = null; + $handlers = rest_get_server()->get_routes()[ self::ROUTE ]; + $response = rest_get_server()->dispatch( new WP_REST_Request( 'GET', self::ROUTE ) ); + } finally { + unregister_taxonomy( 'entity-fields' ); + $wp_rest_server = null; + } + + $this->assertCount( 2, $handlers, 'Only the route of the terms is registered.' ); + $this->assertInstanceOf( WP_REST_Terms_Controller::class, $handlers[0]['callback'][0] ); + $this->assertArrayHasKey( 'POST', $handlers[1]['methods'], 'Terms can still be created.' ); + $this->assertSame( 200, $response->get_status(), 'The terms are listed.' ); + $this->assertSame( array(), $response->get_data() ); + } + /** * Editors (with `edit_posts`) can read the fields. * @@ -405,6 +435,12 @@ public function test_get_items_requires_kind_and_name() { $missing_kind = $this->dispatch_request( null, 'page' ); $this->assertErrorResponse( 'rest_missing_callback_param', $missing_kind, 400 ); + + $empty_kind = $this->dispatch_request( '', 'page' ); + $this->assertErrorResponse( 'rest_invalid_param', $empty_kind, 400 ); + + $empty_name = $this->dispatch_request( 'postType', '' ); + $this->assertErrorResponse( 'rest_invalid_param', $empty_name, 400 ); } /** From e09663e5fcc7cc65d4590488909624d2256a9ff9 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 17 Nov 2026 16:54:40 +0530 Subject: [PATCH 2/4] Tests: Update the REST API client fixture for the entity fields arguments. Co-Authored-By: Claude Opus 5.5 --- tests/qunit/fixtures/wp-api-generated.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 807eda9d2cf92..bb83cd91b8728 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -13134,11 +13134,13 @@ mockedApiResponse.Schema = { "kind": { "description": "Entity kind.", "type": "string", + "minLength": 1, "required": true }, "name": { "description": "Entity name.", "type": "string", + "minLength": 1, "required": true } } From 21a9795c68630cceff96710582343626fe7d5018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:21:49 +0100 Subject: [PATCH 3/4] REST API: Remove the route collision check from the entity actions endpoint. 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 --- ...lass-wp-rest-entity-actions-controller.php | 42 ------------------- .../rest-entity-actions-controller.php | 30 ------------- 2 files changed, 72 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php index bf9ebcf895a16..121421fc00576 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php @@ -39,10 +39,6 @@ public function __construct() { * @since 7.2.0 */ public function register_routes() { - if ( $this->is_route_of_entity_items() ) { - return; - } - register_rest_route( $this->namespace, '/' . $this->rest_base, @@ -71,44 +67,6 @@ public function register_routes() { ); } - /** - * Checks whether a post type or taxonomy has its items on the route of - * the controller, reporting it with _doing_it_wrong(). - * - * The route is then left to the post type or taxonomy: registering it - * as well would not serve the actions, as the route of the items comes - * first, and would replace the schema of that route. - * - * @since 7.2.0 - * - * @return bool Whether a post type or taxonomy has its items on the route. - */ - protected function is_route_of_entity_items() { - $objects = array_merge( - array_values( get_post_types( array( 'show_in_rest' => true ), 'objects' ) ), - array_values( get_taxonomies( array( 'show_in_rest' => true ), 'objects' ) ) - ); - foreach ( $objects as $object ) { - $namespace = $object->rest_namespace ? $object->rest_namespace : 'wp/v2'; - $base = $object->rest_base ? $object->rest_base : $object->name; - if ( $this->namespace === $namespace && $this->rest_base === $base ) { - _doing_it_wrong( - __METHOD__, - sprintf( - /* translators: 1: REST route, e.g. /wp/v2/entity-actions. 2: Post type or taxonomy name. */ - __( 'The %1$s route serves the items of the "%2$s" post type or taxonomy, so the actions of entities are not available.' ), - '/' . $this->namespace . '/' . $this->rest_base, - $object->name - ), - '7.2.0' - ); - return true; - } - } - - return false; - } - /** * Checks if a given request has access to read the actions of an entity. * diff --git a/tests/phpunit/tests/rest-api/rest-entity-actions-controller.php b/tests/phpunit/tests/rest-api/rest-entity-actions-controller.php index c67107147493a..15f58d7a47cc7 100644 --- a/tests/phpunit/tests/rest-api/rest-entity-actions-controller.php +++ b/tests/phpunit/tests/rest-api/rest-entity-actions-controller.php @@ -144,36 +144,6 @@ public function test_register_routes() { $this->assertInstanceOf( 'WP_REST_Entity_Actions_Controller', $routes[ self::ROUTE ][0]['callback'][0] ); } - /** - * A taxonomy whose items are on the route keeps it: the route of the - * actions is not registered, and that is reported. - * - * @covers ::register_routes - * @covers ::is_route_of_entity_items - * - * @ticket 66343 - */ - public function test_the_route_of_a_taxonomy_with_the_same_base_is_kept() { - global $wp_rest_server; - $this->setExpectedIncorrectUsage( 'WP_REST_Entity_Actions_Controller::is_route_of_entity_items' ); - register_taxonomy( 'entity-actions', 'post', array( 'show_in_rest' => true ) ); - - try { - $wp_rest_server = null; - $handlers = rest_get_server()->get_routes()[ self::ROUTE ]; - $response = rest_get_server()->dispatch( new WP_REST_Request( 'GET', self::ROUTE ) ); - } finally { - unregister_taxonomy( 'entity-actions' ); - $wp_rest_server = null; - } - - $this->assertCount( 2, $handlers, 'Only the route of the terms is registered.' ); - $this->assertInstanceOf( WP_REST_Terms_Controller::class, $handlers[0]['callback'][0] ); - $this->assertArrayHasKey( 'POST', $handlers[1]['methods'], 'Terms can still be created.' ); - $this->assertSame( 200, $response->get_status(), 'The terms are listed.' ); - $this->assertSame( array(), $response->get_data() ); - } - /** * Editors (with `edit_posts`) can read the actions. * From 26266c28eb01fb77fd7cab3858c1c6ba85dc18a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:21:49 +0100 Subject: [PATCH 4/4] REST API: Remove the route collision check from the entity fields endpoint. 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 --- ...class-wp-rest-entity-fields-controller.php | 42 ------------------- .../rest-entity-fields-controller.php | 30 ------------- 2 files changed, 72 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php index 37231d7ce363e..cbc16bee7c01b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php @@ -39,10 +39,6 @@ public function __construct() { * @since 7.2.0 */ public function register_routes() { - if ( $this->is_route_of_entity_items() ) { - return; - } - register_rest_route( $this->namespace, '/' . $this->rest_base, @@ -71,44 +67,6 @@ public function register_routes() { ); } - /** - * Checks whether a post type or taxonomy has its items on the route of - * the controller, reporting it with _doing_it_wrong(). - * - * The route is then left to the post type or taxonomy: registering it - * as well would not serve the fields, as the route of the items comes - * first, and would replace the schema of that route. - * - * @since 7.2.0 - * - * @return bool Whether a post type or taxonomy has its items on the route. - */ - protected function is_route_of_entity_items() { - $objects = array_merge( - array_values( get_post_types( array( 'show_in_rest' => true ), 'objects' ) ), - array_values( get_taxonomies( array( 'show_in_rest' => true ), 'objects' ) ) - ); - foreach ( $objects as $object ) { - $namespace = $object->rest_namespace ? $object->rest_namespace : 'wp/v2'; - $base = $object->rest_base ? $object->rest_base : $object->name; - if ( $this->namespace === $namespace && $this->rest_base === $base ) { - _doing_it_wrong( - __METHOD__, - sprintf( - /* translators: 1: REST route, e.g. /wp/v2/entity-fields. 2: Post type or taxonomy name. */ - __( 'The %1$s route serves the items of the "%2$s" post type or taxonomy, so the fields of entities are not available.' ), - '/' . $this->namespace . '/' . $this->rest_base, - $object->name - ), - '7.2.0' - ); - return true; - } - } - - return false; - } - /** * Checks if a given request has access to read the fields of an entity. * diff --git a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php index 072ca01607250..5e3d5561ce435 100644 --- a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php +++ b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php @@ -182,36 +182,6 @@ public function test_register_routes() { $this->assertInstanceOf( 'WP_REST_Entity_Fields_Controller', $routes[ self::ROUTE ][0]['callback'][0] ); } - /** - * A taxonomy whose items are on the route keeps it: the route of the - * fields is not registered, and that is reported. - * - * @covers ::register_routes - * @covers ::is_route_of_entity_items - * - * @ticket 66229 - */ - public function test_the_route_of_a_taxonomy_with_the_same_base_is_kept() { - global $wp_rest_server; - $this->setExpectedIncorrectUsage( 'WP_REST_Entity_Fields_Controller::is_route_of_entity_items' ); - register_taxonomy( 'entity-fields', 'post', array( 'show_in_rest' => true ) ); - - try { - $wp_rest_server = null; - $handlers = rest_get_server()->get_routes()[ self::ROUTE ]; - $response = rest_get_server()->dispatch( new WP_REST_Request( 'GET', self::ROUTE ) ); - } finally { - unregister_taxonomy( 'entity-fields' ); - $wp_rest_server = null; - } - - $this->assertCount( 2, $handlers, 'Only the route of the terms is registered.' ); - $this->assertInstanceOf( WP_REST_Terms_Controller::class, $handlers[0]['callback'][0] ); - $this->assertArrayHasKey( 'POST', $handlers[1]['methods'], 'Terms can still be created.' ); - $this->assertSame( 200, $response->get_status(), 'The terms are listed.' ); - $this->assertSame( array(), $response->get_data() ); - } - /** * Editors (with `edit_posts`) can read the fields. *