Skip to content

Users: Validate the whitespace-normalized username in add_user()#12630

Open
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/65672-admin-user-creation-username-validation
Open

Users: Validate the whitespace-normalized username in add_user()#12630
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/65672-admin-user-creation-username-validation

Conversation

@irozum

@irozum irozum commented Jul 22, 2026

Copy link
Copy Markdown

Description

edit_user() (used by add_user() when creating a new user from wp-admin) stores the sanitized username:

$user->user_login = sanitize_user( wp_unslash( $_POST['user_login'] ), true );

but validated the raw, un-unslashed $_POST['user_login'] value instead:

if ( ! $update && isset( $_POST['user_login'] ) && ! validate_username( $_POST['user_login'] ) ) {

validate_username() does a strict equality check against sanitize_user(), and sanitize_user() trims leading/trailing whitespace and collapses repeated internal whitespace. Since the raw input still has that whitespace, the check fails even though the same value would be stored successfully once sanitized — a common false rejection when a username is pasted with a trailing space.

I initially tried validating the fully-sanitized $user->user_login value directly, but that turned out to be too broad: since wp_insert_user() re-sanitizes on insert regardless, the fully-sanitized value trivially equals itself, so it silently stopped rejecting any illegal characters too (e.g. bob@#&99 would silently become bob@99 instead of erroring). That's a bigger behavior change than the ticket asks for.

This PR instead normalizes only the whitespace (trim + collapse, matching what sanitize_user() does) before validating, so:

  • Leading/trailing and repeated internal whitespace no longer cause a false rejection.
  • Genuinely illegal characters are still caught and still produce the "illegal characters" error, unchanged from current behavior.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Implementation, tests, and PR description. Reviewed by irozum.


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.

Copilot AI review requested due to automatic review settings July 22, 2026 02:32
@github-actions

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. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props irozum.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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.

Pull request overview

This PR updates user creation via add_user()/edit_user() in wp-admin to validate usernames against the same whitespace normalization that is applied when storing the username, avoiding false “illegal characters” rejections caused solely by leading/trailing or repeated internal whitespace.

Changes:

  • Normalize whitespace (trim + collapse) before calling validate_username() during new-user creation in edit_user().
  • Add PHPUnit coverage to confirm usernames with normalized whitespace are accepted, while usernames with illegal characters are still rejected.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/wp-admin/includes/user.php Normalizes whitespace in $_POST['user_login'] before validating, matching stored normalization behavior.
tests/phpunit/tests/admin/includes/user/addUser.php Adds tests for whitespace-tolerant username validation and continued rejection of illegal characters.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

* @group admin
* @group user
*/
class Tests_Admin_Includes_User_AddUser extends WP_UnitTestCase {
Comment on lines +9 to +14
public function set_up() {
parent::set_up();

$admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $admin_id );
}
@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.

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