Repository navigation
Users: Unslash the email before checking for duplicates in wp_insert_user() - #14490
abhishekfdd wants to merge 3 commits into
Conversation
…_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.
|
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. |
nathanrice
left a comment
There was a problem hiding this comment.
Minor concern noted, but not blocking. Verified the issue raised, and tested the solution locally. Tests pass. Looks good to me.
| * @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 ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| * 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 ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Slashed input still comes out unslashed, because sanitize_email() drops backslashes before kses runs
Can you explain this differently? I don’t follow.
There was a problem hiding this comment.
Sorry, that was too compressed. Tracing o\'brien@example.com (slashed, as wp_insert_user() passes it) through the chain:
trim(): unchanged.sanitize_email(): the local part is filtered through a character allowlist that doesn't include\, so the backslash is deleted:o'brien@example.com.- 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.
…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.
Trac ticket: https://core.trac.wordpress.org/ticket/66337
What
wp_insert_user()checks for duplicate emails using the output of thepre_user_emailfilters. The last of those,wp_filter_kses(), returns a slashed value, soo'brien@example.comreaches the check aso\'brien@example.com,email_exists()never matches, and a duplicate is accepted viawp_insert_user(),wp_update_user()andPOST /wp/v2/users.Following review, this fixes the filter chain instead of working around it:
pre_user_emailandpre_comment_author_emailnow usewp_kses_data()instead ofwp_filter_kses(), so the chain returns unslashed data.sanitize_email()already removes backslashes, so slashed input also comes out unslashed.wp_slash()on$old_user_data->user_emailadded in [57711] is removed. It's no longer needed for the comparison, and since 6.5 it was passing a slashed email toprofile_updatecallbacks.check_comment()now unslashes$authorand$emailin 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_emailorpre_comment_author_emailafter priority 10 now receive unslashed values. Only apostrophe emails are affected.Testing
user_activation_keysurvives an unrelated update (guards #52529)profile_updatereceives the old email unslashedcheck_comment()andwp_new_comment()recognize a previously approved author with an apostrophe in the name, the email, or bothnpm 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.