Skip to content

Users: Unslash the email before checking for duplicates in wp_insert_user() - #14490

Open
abhishekfdd wants to merge 3 commits into
WordPress:trunkfrom
abhishekfdd:66337-unslash-email-duplicate-check
Open

abhishekfdd wants to merge 3 commits into
WordPress:trunkfrom
abhishekfdd:66337-unslash-email-duplicate-check

Conversation

@abhishekfdd

@abhishekfdd abhishekfdd commented Oct 19, 2026 •

Copy link
Copy Markdown

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

What

wp_insert_user() checks for duplicate emails using the output of the pre_user_email filters. The last of those, wp_filter_kses(), returns a slashed value, so o'brien@example.com reaches the check as o\'brien@example.com, email_exists() never matches, and a duplicate is accepted via wp_insert_user(), wp_update_user() and POST /wp/v2/users.

Following review, this fixes the filter chain instead of working around it:

  • pre_user_email and pre_comment_author_email now use wp_kses_data() instead of wp_filter_kses(), so the chain returns unslashed data. sanitize_email() already removes backslashes, so slashed input also comes out unslashed.
  • The wp_slash() on $old_user_data->user_email added in [57711] is removed. It's no longer needed for the comparison, and since 6.5 it was passing a slashed email to profile_update callbacks.
  • check_comment() now unslashes $author and $email in the previously approved query. This was left over from [38738], so commenters with an apostrophe in their name or email were never recognized as previously approved.

Compat note: callbacks on pre_user_email or pre_comment_author_email after priority 10 now receive unslashed values. Only apostrophe emails are affected.

Testing

  • Duplicate insert rejected (slashed and unslashed input), update onto another user's email rejected, case change of own email allowed, REST create rejects the duplicate
  • user_activation_key survives an unrelated update (guards #52529)
  • profile_update receives the old email unslashed
  • Both email filters return the unslashed email for slashed and unslashed input
  • check_comment() and wp_new_comment() recognize a previously approved author with an apostrophe in the name, the email, or both

npm run test:php -- --filter 'apostrophe|unslashed_email'

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Opus 5.5
Used for: tracing the issue, writing the fix and tests, and running the test suite. I reviewed and verified the changes.

…_user()`.

The default `pre_user_email` filters include `wp_filter_kses()`, which returns a slashed value, so an email address containing an apostrophe reaches the duplicate check as `o\'brien@example.com`. `email_exists()` then never matches the stored address, and `wp_insert_user()`, `wp_update_user()` and the REST API users endpoint accept an email address that is already in use.

This commit compares and looks up the unslashed email instead. It also stops slashing `$old_user_data->user_email`, which was added in [57711] so the comparison would match, and which passed a slashed email to `profile_update` callbacks.

Follow-up to [57711].

See #66337.
@github-actions

github-actions Bot commented Oct 19, 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 abhishekfdd, nathanrice, dmsnell.

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.

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

Minor concern noted, but not blocking. Verified the issue raised, and tested the solution locally. Tests pass. Looks good to me.

Comment thread src/wp-includes/user.php
* @param WP_User $old_user_data Object containing user's data prior to update.
* @param array $userdata The raw array of data passed to wp_insert_user().
*/
do_action( 'profile_update', $user_id, $old_user_data, $userdata );

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since $old_user_data now contains a not-slashed user_email, anything that uses this action hook might be caught off guard. Probably minor. I don't think core consumes the email in any callbacks for this action, so it would mostly be plugins or themes. But again, I think this is probably safe.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for testing. On profile_update: before 6.5 the old email passed there was unslashed. [57711] started slashing it as a side effect of its comparison fix, so this PR restores the earlier behavior rather than introducing a new one. There's a test asserting the unslashed value now.

Comment thread src/wp-includes/user.php Outdated
* containing an apostrophe becomes `o\'brien@example.com`. Compare and look it up
* unslashed so it matches the value stored in the database.
*/
$unslashed_user_email = wp_unslash( $user_email );

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.

This removes the slashes at the end of the filter, but we have another problem in default_filters.php

// Email saves.
foreach ( array( 'pre_comment_author_email', 'pre_user_email' ) as $filter ) {
	add_filter( $filter, 'trim' );
	add_filter( $filter, 'sanitize_email' );
	add_filter( $filter, 'wp_filter_kses' );
}

This pipeline isn’t coherent, as trim() and sanitize_email() both operate on unslashed data.

A safer fix would be to replace wp_filter_kses() in that filter with a function that wraps it with slashes and un-slashes, then unslashes before calling the filter, and documenting in all of these function what the expectations are.

Any other change like this is going to solve some broken cases and break some new ones. That’s a lot of slashes-juggling but it’s pretty much necessary. This would all be easier if we required unslashed data everywhere.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, agreed. Fixing the pipeline is cleaner than patching around it in wp_insert_user().

Pushed a follow-up that swaps wp_filter_kses for wp_kses_data on pre_user_email and pre_comment_author_email. It's the same kses call without the strip/add slashes, which is effectively the wrapper you described. Slashed input still comes out unslashed, because sanitize_email() drops backslashes before kses runs. With that, wp_insert_user() no longer needs its own wp_unslash().

Checking for new breakage turned up one side effect: on trunk, wp_new_comment() never recognizes a previously approved commenter whose email has an apostrophe, because check_comment() queries with the slashed email. With this change it does. Added a test for it.

The compat risk I see: callbacks hooked to these two filters after priority 10 now receive unslashed values. Only apostrophe emails are affected, since nothing else in a sanitized email needs slashing, but it's probably worth a mention in a dev note.

@dmsnell dmsnell Nov 10, 2026 •

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.

The issue in check_comment() is something I had previously identified: it’s the result of a partial refactor (r38738) that should have removed the invalid comment suggesting that slashes are expected. It wrapped the query in $wpdb->prepare() but overlooked unslashed the data.

-				// expected_slashed ($author, $email)
 				$ok_to_comment = $wpdb->get_var(
 					$wpdb->prepare(
 						"SELECT comment_approved
@@ -154,8 +157,8 @@ function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent,
 						AND comment_author_email = %s
 						AND comment_approved = '1'
 						LIMIT 1",
-						$author,
-						$email
+						wp_unslash( $author ),
+						wp_unslash( $email )
 					)
 				);

it would be appropriate to merge this into this PR and add tests confirming the failure in the database before the patch as well as the restoration after

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.

Slashed input still comes out unslashed, because sanitize_email() drops backslashes before kses runs

Can you explain this differently? I don’t follow.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sorry, that was too compressed. Tracing o\'brien@example.com (slashed, as wp_insert_user() passes it) through the chain:

  1. trim(): unchanged.
  2. sanitize_email(): the local part is filtered through a character allowlist that doesn't include \, so the backslash is deleted: o'brien@example.com.
  3. Old: wp_filter_kses() strips slashes (nothing left to strip), runs kses, then adds them back: o\'brien@example.com.
    New: wp_kses_data() runs kses only: o'brien@example.com.

Unslashed input gives the same result, so after this change the chain returns the unslashed email either way.

To be clear, step 2 isn't really unslashing. It works because a sanitized email can never contain a backslash, so deleting it happens to be the same as unslashing. If you'd rather not rely on that, I can make it explicit in the docblocks, or add a wp_unslash() before sanitize_email(), which is a no-op for unslashed input.

I've also pulled in the check_comment() change with tests. The new data provider asserts the approved row is stored unslashed, then shows check_comment() with slashed author/email failing before the patch and passing after, for an apostrophe in the name, the email, and both.

Replaces `wp_filter_kses()` with `wp_kses_data()` in the `pre_user_email` and `pre_comment_author_email` filters, so the filter chain works on unslashed data from start to finish, as suggested in review. `sanitize_email()` already removes backslashes, so slashed input also comes out unslashed.

`wp_insert_user()` no longer needs to unslash the filtered email itself. As a side effect, `wp_new_comment()` now recognizes a previously approved comment author whose email contains an apostrophe.

See #66337.
@abhishekfdd
abhishekfdd requested a review from dmsnell October 29, 2026 10:43
…sly approved query.

`check_comment()` receives slashed data, but since [38738] the previously approved query passes `$author` and `$email` to `$wpdb->prepare()` without unslashing them, so a comment author whose name or email contains an apostrophe is never matched to their approved comments. The leftover `expected_slashed` note is removed.

See #66337.
@abhishekfdd
abhishekfdd requested a review from dmsnell November 18, 2026 10:47
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.

3 participants