From eb9a864341132aef90a4b8b2b647d4af61333cbf Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 13 Nov 2026 18:26:16 +0000 Subject: [PATCH 1/3] Abilities API: Reject empty slugs and usernames in `core/users-query`. The items of the `slugs` and `usernames` filters now require at least one character, like the `slugs` filter of `core/content-query`, so a list holding an empty string fails schema validation instead of returning an empty list. Follow-up to [64346]. See #64657. --- .../abilities/class-wp-abilities-users.php | 6 ++- .../wpRegisterCoreUsersAbility.php | 37 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-users.php b/src/wp-includes/abilities/class-wp-abilities-users.php index a0f21de801e95..0322f854c1b48 100644 --- a/src/wp-includes/abilities/class-wp-abilities-users.php +++ b/src/wp-includes/abilities/class-wp-abilities-users.php @@ -703,7 +703,8 @@ private function get_users_query_input_schema(): array { 'minItems' => 1, 'maxItems' => self::MAX_PER_PAGE, 'items' => array( - 'type' => 'string', + 'type' => 'string', + 'minLength' => 1, ), 'description' => __( 'Limit results to users with these slugs. If `pagination.per_page` is omitted, every matching user is returned on the first page.' ), ), @@ -713,7 +714,8 @@ private function get_users_query_input_schema(): array { 'minItems' => 1, 'maxItems' => self::MAX_PER_PAGE, 'items' => array( - 'type' => 'string', + 'type' => 'string', + 'minLength' => 1, ), 'description' => __( 'Limit results to users with these usernames. Requires permission to list users. If `pagination.per_page` is omitted, every matching user is returned on the first page.' ), ), diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php index 671db3381ec3b..99c9ad489ede6 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php @@ -292,6 +292,8 @@ public function test_core_users_input_schema_exposes_strict_filters(): void { } $this->assertSame( 1, $where['properties']['ids']['items']['minimum'], 'The ids option should contain positive user IDs.' ); + $this->assertSame( 1, $where['properties']['slugs']['items']['minLength'], 'The slugs option should not contain an empty slug.' ); + $this->assertSame( 1, $where['properties']['usernames']['items']['minLength'], 'The usernames option should not contain an empty username.' ); $roles = $where['properties']['roles']; @@ -1606,6 +1608,41 @@ public static function data_unsupported_where_keys(): array { ); } + /** + * A slug or username list that holds an empty string fails schema validation, even next + * to a value that names a user. + * + * @ticket 64657 + * + * @dataProvider data_identifier_lists_with_an_empty_string + * + * @param string $filter The `where` filter. + * @param string[] $values The values given for the filter. + */ + public function test_identifier_list_with_an_empty_string_fails_schema_validation( string $filter, array $values ): void { + wp_set_current_user( self::$fixture_ids['administrator'] ); + $this->register_ability(); + + $result = wp_get_ability( 'core/users-query' )->execute( array( 'where' => array( $filter => $values ) ) ); + + $this->assertWPError( $result, sprintf( 'An empty string in the %s filter should fail the request.', $filter ) ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), sprintf( 'An empty string in the %s filter should fail schema validation.', $filter ) ); + } + + /** + * Data provider for slug and username lists that hold an empty string. + * + * @return array + */ + public static function data_identifier_lists_with_an_empty_string(): array { + return array( + 'empty slug' => array( 'slugs', array( '' ) ), + 'empty and valid slug' => array( 'slugs', array( '', 'core-users-ability-admin' ) ), + 'empty username' => array( 'usernames', array( '' ) ), + 'empty and valid username' => array( 'usernames', array( '', 'core_users_ability_admin' ) ), + ); + } + /** * Filters and pagination values at the root fail schema validation. * From a2b760fba734323811519bcd144a23ed52fb6072 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 13 Nov 2026 18:50:03 +0000 Subject: [PATCH 2/3] Abilities API: Drop empty strings from the `core/users-query` list filters. Without schema validation, a slug or username list that holds only an empty string now fails with `users_invalid_filter`, like the `slugs` filter of `core/content-query`, instead of being queried. An empty string next to another value is dropped. See #64657. --- .../abilities/class-wp-abilities-users.php | 13 +++- .../wpRegisterCoreUsersAbility.php | 62 +++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-users.php b/src/wp-includes/abilities/class-wp-abilities-users.php index 0322f854c1b48..f96c4c0c73ec2 100644 --- a/src/wp-includes/abilities/class-wp-abilities-users.php +++ b/src/wp-includes/abilities/class-wp-abilities-users.php @@ -612,8 +612,10 @@ private function normalize_id_list( $value ): array { * Accepts arrays and CSV strings, which it parses with wp_parse_list(), as schema * validation does. Schema validation accepts a CSV string for an array, and only the * REST run controller converts input to the schema types, so callers that bypass it, - * such as a direct WP_Ability::execute() call, can pass one. Empty and duplicate - * items need no handling here, because validation has already rejected them. + * such as a direct WP_Ability::execute() call, can pass one. Empty strings name + * nothing, so they are dropped, and a list that holds only empty strings comes back + * empty. Duplicate items need no handling here, because validation has already + * rejected them. * * @since 7.2.0 * @@ -625,7 +627,12 @@ private function normalize_string_list( $value ): array { return array(); } - return array_values( array_filter( wp_parse_list( $value ), 'is_string' ) ); + return array_values( + array_filter( + wp_parse_list( $value ), + static fn( $item ) => is_string( $item ) && '' !== $item + ) + ); } /** diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php index 99c9ad489ede6..c4cb64b79e391 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php @@ -3111,6 +3111,68 @@ public function test_execute_callback_rejects_slug_and_username_lists_without_a_ } } + /** + * The execute callback rejects a slug or username list that holds only an empty string, + * instead of querying it, and drops an empty string next to a value that names a user. + * + * Schema validation rejects empty strings, so this only matters for callers that skip it. + * + * @ticket 64657 + * + * @dataProvider data_slug_and_username_filters + * + * @param string $filter The `where` filter. + * @param string $value A value of the filter that names the administrator. + */ + public function test_execute_callback_drops_empty_strings_from_slug_and_username_lists( string $filter, string $value ): void { + wp_set_current_user( self::$fixture_ids['administrator'] ); + + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( array( 'where' => array( $filter => array( '' ) ) ) ); + + $this->assertWPError( $result, sprintf( 'A list of %s holding only an empty string should fail the request.', $filter ) ); + $this->assertSame( 'users_invalid_filter', $result->get_error_code(), sprintf( 'A list of %s holding only an empty string should fail closed as an invalid filter.', $filter ) ); + $this->assertSame( + array( 'where[' . $filter . ']' => $result->get_error_message() ), + $result->get_error_data()['params'], + sprintf( 'The error data should map the list of %s holding only an empty string to the error message.', $filter ) + ); + + $result = $execute( + array( + 'where' => array( $filter => array( '', $value ) ), + 'fields' => array( 'id' ), + ) + ); + + $this->assertSame( + array( + 'items' => array( + array( 'id' => self::$fixture_ids['administrator'] ), + ), + 'pagination' => array( + 'total' => 1, + 'total_pages' => 1, + ), + ), + $result, + sprintf( 'An empty string next to a value in the list of %s should be dropped, leaving the named user.', $filter ) + ); + } + + /** + * Data provider for the slug and username filters, each with a value that names the administrator. + * + * @return array + */ + public static function data_slug_and_username_filters(): array { + return array( + 'slugs' => array( 'slugs', 'core-users-ability-admin' ), + 'usernames' => array( 'usernames', 'core_users_ability_admin' ), + ); + } + /** * Pagination values that are not whole numbers fall back to the defaults, instead of * being coerced to a page or page size the caller did not ask for. From 6dbdf7384e2381c2eee24d7318b371c8d210d885 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 20 Nov 2026 14:07:57 +0000 Subject: [PATCH 3/3] Abilities API: Drop whitespace-only items from the `core/users-query` list filters. A slug or username made only of whitespace passes schema validation but names no user. It is now dropped like an empty string, so a list holding only such a value fails with `users_invalid_filter`, like the `slugs` filter of `core/content-query`, instead of returning an empty list. The test of the dropped values now runs through the ability's `execute()` method instead of calling the execute callback directly. See #64657. --- .../abilities/class-wp-abilities-users.php | 10 +- .../wpRegisterCoreUsersAbility.php | 124 +++++++++--------- 2 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-users.php b/src/wp-includes/abilities/class-wp-abilities-users.php index f96c4c0c73ec2..51df002dfe762 100644 --- a/src/wp-includes/abilities/class-wp-abilities-users.php +++ b/src/wp-includes/abilities/class-wp-abilities-users.php @@ -612,10 +612,10 @@ private function normalize_id_list( $value ): array { * Accepts arrays and CSV strings, which it parses with wp_parse_list(), as schema * validation does. Schema validation accepts a CSV string for an array, and only the * REST run controller converts input to the schema types, so callers that bypass it, - * such as a direct WP_Ability::execute() call, can pass one. Empty strings name - * nothing, so they are dropped, and a list that holds only empty strings comes back - * empty. Duplicate items need no handling here, because validation has already - * rejected them. + * such as a direct WP_Ability::execute() call, can pass one. Empty and whitespace-only + * strings name nothing, so they are dropped, and a list that holds only such strings + * comes back empty. Duplicate items need no handling here, because validation has + * already rejected them. * * @since 7.2.0 * @@ -630,7 +630,7 @@ private function normalize_string_list( $value ): array { return array_values( array_filter( wp_parse_list( $value ), - static fn( $item ) => is_string( $item ) && '' !== $item + static fn( $item ) => is_string( $item ) && '' !== trim( $item ) ) ); } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php index c4cb64b79e391..9dedc9c7196e5 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php @@ -1643,6 +1643,68 @@ public static function data_identifier_lists_with_an_empty_string(): array { ); } + /** + * A slug or username made only of whitespace passes schema validation but names no user, + * so it is dropped: a list that holds only such a value fails as an invalid filter, and + * one next to a value that names a user still returns that user. + * + * @ticket 64657 + * + * @dataProvider data_slug_and_username_filters + * + * @param string $filter The `where` filter. + * @param string $value A value of the filter that names the administrator. + */ + public function test_whitespace_only_identifier_is_dropped( string $filter, string $value ): void { + wp_set_current_user( self::$fixture_ids['administrator'] ); + $this->register_ability(); + + $ability = wp_get_ability( 'core/users-query' ); + + $result = $ability->execute( array( 'where' => array( $filter => array( ' ' ) ) ) ); + + $this->assertWPError( $result, sprintf( 'A list of %s holding only a whitespace-only string should fail the request.', $filter ) ); + $this->assertSame( 'users_invalid_filter', $result->get_error_code(), sprintf( 'A list of %s holding only a whitespace-only string should fail closed as an invalid filter.', $filter ) ); + $this->assertSame( + array( 'where[' . $filter . ']' => $result->get_error_message() ), + $result->get_error_data()['params'], + sprintf( 'The error data should map the list of %s holding only a whitespace-only string to the error message.', $filter ) + ); + + $result = $ability->execute( + array( + 'where' => array( $filter => array( ' ', $value ) ), + 'fields' => array( 'id' ), + ) + ); + + $this->assertSame( + array( + 'items' => array( + array( 'id' => self::$fixture_ids['administrator'] ), + ), + 'pagination' => array( + 'total' => 1, + 'total_pages' => 1, + ), + ), + $result, + sprintf( 'A valid identifier alongside a whitespace-only string in the list of %s should still return the matching user.', $filter ) + ); + } + + /** + * Data provider for the slug and username filters, each with a value that names the administrator. + * + * @return array + */ + public static function data_slug_and_username_filters(): array { + return array( + 'slugs' => array( 'slugs', 'core-users-ability-admin' ), + 'usernames' => array( 'usernames', 'core_users_ability_admin' ), + ); + } + /** * Filters and pagination values at the root fail schema validation. * @@ -3111,68 +3173,6 @@ public function test_execute_callback_rejects_slug_and_username_lists_without_a_ } } - /** - * The execute callback rejects a slug or username list that holds only an empty string, - * instead of querying it, and drops an empty string next to a value that names a user. - * - * Schema validation rejects empty strings, so this only matters for callers that skip it. - * - * @ticket 64657 - * - * @dataProvider data_slug_and_username_filters - * - * @param string $filter The `where` filter. - * @param string $value A value of the filter that names the administrator. - */ - public function test_execute_callback_drops_empty_strings_from_slug_and_username_lists( string $filter, string $value ): void { - wp_set_current_user( self::$fixture_ids['administrator'] ); - - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( array( 'where' => array( $filter => array( '' ) ) ) ); - - $this->assertWPError( $result, sprintf( 'A list of %s holding only an empty string should fail the request.', $filter ) ); - $this->assertSame( 'users_invalid_filter', $result->get_error_code(), sprintf( 'A list of %s holding only an empty string should fail closed as an invalid filter.', $filter ) ); - $this->assertSame( - array( 'where[' . $filter . ']' => $result->get_error_message() ), - $result->get_error_data()['params'], - sprintf( 'The error data should map the list of %s holding only an empty string to the error message.', $filter ) - ); - - $result = $execute( - array( - 'where' => array( $filter => array( '', $value ) ), - 'fields' => array( 'id' ), - ) - ); - - $this->assertSame( - array( - 'items' => array( - array( 'id' => self::$fixture_ids['administrator'] ), - ), - 'pagination' => array( - 'total' => 1, - 'total_pages' => 1, - ), - ), - $result, - sprintf( 'An empty string next to a value in the list of %s should be dropped, leaving the named user.', $filter ) - ); - } - - /** - * Data provider for the slug and username filters, each with a value that names the administrator. - * - * @return array - */ - public static function data_slug_and_username_filters(): array { - return array( - 'slugs' => array( 'slugs', 'core-users-ability-admin' ), - 'usernames' => array( 'usernames', 'core_users_ability_admin' ), - ); - } - /** * Pagination values that are not whole numbers fall back to the defaults, instead of * being coerced to a page or page size the caller did not ask for.