Skip to content

Abilities API: Reject empty slugs and usernames in core/users-query - #15986

Open
jorgefilipecosta wants to merge 3 commits into
WordPress:trunkfrom
jorgefilipecosta:fix/users-query-empty-identifiers
Open

jorgefilipecosta wants to merge 3 commits into
WordPress:trunkfrom
jorgefilipecosta:fix/users-query-empty-identifiers

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Follow-up to https://core.trac.wordpress.org/changeset/64346.

This PR makes the slugs and usernames filters of core/users-query reject an empty string, like the slugs filter of core/content-query:

  • The items of where.slugs and where.usernames require at least one character (minLength: 1).
  • slugs: [""] and usernames: [""] used to pass validation and return an empty list. They now fail with ability_invalid_input, also when the list holds a valid value next to the empty one.
  • A slug or username made only of whitespace, such as " ", passes validation but names no user. The execute callback drops it, so slugs: [" "] fails with users_invalid_filter, like the slugs filter of core/content-query, instead of returning an empty list.
  • Tests cover the schema and the rejected inputs.

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

Testing

Verify unit tests are passing:

vendor/bin/phpunit tests/phpunit/tests/abilities-api/wpRegisterCoreUsersAbility.php

Or run the ability in the editor console as an administrator:

wp.apiFetch( { path: wp.url.addQueryArgs( '/wp-abilities/v1/abilities/core/users-query/run', { input: { where: { slugs: [ '' ] } } } ) } );

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 as slugs: [ '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.

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.
@github-actions

github-actions Bot commented Nov 13, 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 jorgefilipecosta, gziolo.

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.

…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

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.

maybe trim() on $item to cover also whitespaces.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point, whitespace-only slugs and usernames are now dropped, so a list holding only " " fails as an invalid filter 👍

@gziolo gziolo left a comment

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.

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 )

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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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'];

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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.
@jorgefilipecosta

Copy link
Copy Markdown
Member Author

Commit message for my own tracking:

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. A slug or username made only of whitespace names no user, so it is dropped before the query, and a list holding only such a value fails with `users_invalid_filter`.

Developed in https://github.com/WordPress/wordpress-develop/pull/15986.

Follow-up to [64346].

Props jorgefilipecosta, gziolo.
See #64657.

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.

2 participants