Repository navigation
Abilities API: Reject empty slugs and usernames in core/users-query - #15986
jorgefilipecosta wants to merge 3 commits into
Conversation
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.
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…lters. 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.
| return array_values( | ||
| array_filter( | ||
| wp_parse_list( $value ), | ||
| static fn( $item ) => is_string( $item ) && '' !== $item |
There was a problem hiding this comment.
maybe trim() on $item to cover also whitespaces.
There was a problem hiding this comment.
Good point, whitespace-only slugs and usernames are now dropped, so a list holding only " " fails as an invalid filter 👍
gziolo
left a comment
There was a problem hiding this comment.
Minor suggestion about the assertion wording.
| ), | ||
| ), | ||
| $result, | ||
| sprintf( 'An empty string next to a value in the list of %s should be dropped, leaving the named user.', $filter ) |
There was a problem hiding this comment.
Could we change the assertion message to ‘A valid identifier alongside an empty string in the list of %s should still return the matching user’? This checks the returned result, but doesn't prove the empty string was removed from the query: the valid user would also match if both values were passed through.
There was a problem hiding this comment.
The test was removed as you suggested in the other comment, I used this wording in the new whitespace test, which goes through the public call.
| 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']; |
There was a problem hiding this comment.
Could we remove this test? It calls the execute callback directly, bypassing schema validation, rather than using the normal wp_get_ability( 'core/users-query' )->execute() entry point. That makes the mixed-list case confusing: it expects results for a list containing an empty string, while the PR description says those inputs are rejected. I understand the callback behaves differently when validation is bypassed, but unless there is a practical caller that needs this path, I would keep the regression coverage focused on the public ability call.
There was a problem hiding this comment.
Agreed, the test was removed. The drop is now covered by a test that calls wp_get_ability( 'core/users-query' )->execute() with a whitespace-only value.
… 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.
|
Commit message for my own tracking: |
Follow-up to https://core.trac.wordpress.org/changeset/64346.
This PR makes the
slugsandusernamesfilters ofcore/users-queryreject an empty string, like theslugsfilter ofcore/content-query:where.slugsandwhere.usernamesrequire at least one character (minLength: 1).slugs: [""]andusernames: [""]used to pass validation and return an empty list. They now fail withability_invalid_input, also when the list holds a valid value next to the empty one." ", passes validation but names no user. The execute callback drops it, soslugs: [" "]fails withusers_invalid_filter, like theslugsfilter ofcore/content-query, instead of returning an empty list.Trac ticket: https://core.trac.wordpress.org/ticket/64657
Testing
Verify unit tests are passing:
Or run the ability in the editor console as an administrator:
Before:
{"items":[],"pagination":{"total":0,"total_pages":0}}After:
ability_invalid_input, "Reason: input[where][slugs][0] must be at least 1 character long."The same applies to
usernames: [ '' ]. A slug that names a user, such asslugs: [ 'admin' ](substitute a slug from your site), still returns that user.AI disclosure: the change and tests were drafted with AI assistance (Claude Code) and reviewed by me.