Make WordPress Core

Opened 4 years ago

Last modified 12 days ago

#57413 new defect (bug)

Updating a user email via rest api doesn't trigger confirmation email

Reported by: dd32 Owned by:
Priority: normal Milestone: 7.3
Component: Users Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: rest-api

Description

When a user changes their email address through wp-admin/profile.php the email change is delayed until the user clicks a confirmation link in their email. This was implemented via #16470.

This doesn't happen when an administrator changes another users email address.

When a user updates their own email through the rest API (ie. POST /wp-json/wp/v2/users/1) the confirmation email is also not triggered, and the email change occurs instantly.

This is because the code for the email confirmation is a UI-only method, it's not attached to the user update event, simply the profile.php page submission.

The PR that will be attached to this will re-work the existing code to more generalise it and call it from the REST API.
As part of that, the confirmation link being changed from wp-admin/profile.php to /wp-login.php?action=confirm-email-change... makes more sense to me.

This could have unintended effects, as those using the REST API might be expecting changes to occur without any further action from the user, such as when there is a custom front-end, which this will break. Implementations would need to be updated to use a filter to remove the email confirmation step.

Change History (14)

This ticket was mentioned in ​PR #3813 on ​WordPress/wordpress-develop by ​@dd32.


4 years ago
#1

  • Keywords has-patch has-unit-tests added

​@dd32 commented on ​PR #3813:


4 years ago
#2

Some additional unit tests of send_user_email_change_confirmation_email() and send_user_email_change_confirmation_process() wouldn't go astray.

Strings, documentation, and function naming likely needs a second set of eyes.

#3 @dd32
4 years ago

Something of note here, is that bbPress has it's own implementation of this functionality for it's user-edit page, to mimic the WordPress core behaviour.

​https://github.com/bbpress/bbPress/blob/trunk/src/includes/users/functions.php#L215-L244

This ticket was mentioned in ​PR #13488 on ​WordPress/wordpress-develop by ​@techjewel.


2 months ago
#4

This is a refresh of #3813 by @dd32, which has been open since January 2023. That branch is on his fork so I could not push to it. The first commit here is his patch rebased onto trunk with his authorship intact, and everything after it is a fix, so the diff against that commit is exactly what changed. Props dd32.

### What the original patch does

WP_REST_Users_Controller::update_item() calls wp_update_user() directly, so a user changing their own email address over REST is never asked to confirm it, while the same change on the profile screen is held in the _new_email user meta until they follow a link sent to the new address. The patch splits send_confirmation_on_profile_email() into a function that sends the confirmation and one that processes a confirmed change, calls the former from the REST controller, and adds a wp-login.php?action=confirmemail route so a change started outside the admin can be confirmed.

### What needed fixing

  • Fatal error on Multisite. send_user_email_change_confirmation_process() uses $wpdb in the Multisite branch but has no global $wpdb; — the body was moved out of wp-admin/user-edit.php, where $wpdb was already in scope at the top level. Confirming an email change on Multisite called a method on null. Putting the global back is a one-line fix, but nothing caught it, so the new tests cover it: removing it again turns Tests_User::test_confirm_user_email_change_applies_the_pending_change into Undefined variable $wpdb on the Multisite run.
  • Regression against #44672. email_exists() is not case sensitive, so routing both paths through one check meant a user correcting the case of their own address was told it was already in use — a 400 on REST, an error on the profile screen. test_update_item_existing_email_case fails on the unmodified patch. A change of case is the same mailbox, so it is no longer treated as a change that needs confirming, matching the strcasecmp() guard wp_update_user() already applies to its own email_exists() check.
  • Profile screen behaviour lost in the split. The 0 === $current_user->ID || $current_user->ID !== (int) $user_id guard was dropped, so get_user_by( 'id', 0 ) returns false on a logged-out request and $user->user_email fatals on PHP 8. The addslashes() restore of $_POST['email'] on a rejected address, added in 7.0.3, was also dropped.
  • Rejected addresses were ignored over REST. update_item() discarded a WP_Error from the confirmation function and carried on, applying an address the confirmation step had just rejected. It now returns a 400 with the rest_user_invalid_email code the endpoint already uses.
  • Confirmation lost when logged out. Following a confirmation link while logged out redirected to a bare login form, dropping the user on the dashboard. The redirect now carries the confirmation URL through redirect_to.

### Other changes

  • send_user_email_change_confirmation_process() does not send anything, so it is renamed to confirm_user_email_change(). @dd32 asked for a second opinion on the naming in the PR.
  • The user_email_confirmed action moves from wp-login.php into that function, so it fires for both confirmation routes rather than only one. A change confirmed from the profile screen previously fired nothing.
  • @since x.x placeholders filled in, and the function and filter documentation expanded.
  • The unit tests asked for in the PR comment, for the two new functions rather than only their callers: an unchanged address, an invalid address, an address already in use and the pending change it discards, storing the pending change without touching the account, an administrator editing another user, the should_send_email_for_email_change filter and the arguments it receives, applying a confirmed change and the action it fires, a wrong hash, no pending change, and an unknown user. Plus REST coverage for other fields applied while the email change is pending, an unchanged address, and the filter turning the confirmation off.

### Not changed

The confirmation hash is still md5( $email . time() . wp_rand() ), carried over from the existing code, and _new_email still never expires. Both are worth looking at — the first overlaps #12285 — but neither is this ticket.

wp_xmlrpc_server::wp_editProfile() has the same gap and is left alone pending a scope decision on the ticket.

### Testing

Single site and Multisite, against trunk:

  • --group 57413 — 18 tests, 62 assertions
  • --group 16470, --group 44672, --group 40015 — pass
  • WP_Test_REST_Users_Controller — 136 tests, 960 assertions
  • Tests_User — 133 tests, 452 assertions
  • phpcs — no new errors or warnings on the changed files

#5 @techjewel
2 months ago

Still reproduces on current trunk. WP_REST_Users_Controller::update_item() checks email_exists() for a collision and then calls wp_update_user() directly, so _new_email is never written and no confirmation is ever sent. send_confirmation_on_profile_email() picked up a $user_id parameter in 7.0.3, but it is still hooked only to personal_options_update and still reads $_POST['email'], so the REST path is untouched.

I ran into this from the plugin side. Adding two things the ticket does not have yet: what it costs downstream, and a review of the patch in PR 3813, which I have rebased onto trunk with fixes at ​https://github.com/WordPress/wordpress-develop/pull/13488.

Why this matters outside of core

The profile screen verifies an email change, so plugin authors reasonably read user_email as an address the account has proven it controls, and use it as an identity key. That assumption holds for profile.php and is silently false for REST.

I maintain Fluent Support, a helpdesk plugin. It resolved a support record to a WordPress account by email, which is the obvious thing to do and what a lot of CRM, membership, and e-commerce plugins do. Because a subscriber-level account can set its own user_email through /wp/v2/users/<id> with no verification, that lookup could be pointed at an address the account does not own. We have fixed it on our side by refusing to move a contact address on an unverified signal and adding our own confirmation flow, but that fix is a plugin reimplementing _new_email because it cannot trust core's value. That is a lot of machinery for every plugin that keys on an email address to carry independently.

The account-takeover framing in the ticket is real, but this second class is broader and quieter: nothing in core breaks, so nobody notices until someone audits a plugin.

Review of PR 3813

The shape is right, and splitting the send from the process step is the correct move. Five things needed fixing, all of which I have addressed in the refreshed PR:

  • send_user_email_change_confirmation_process() uses $wpdb in the Multisite branch but has no global $wpdb;. The body was moved out of wp-admin/user-edit.php, where $wpdb was already in scope at the top level. Confirming an email change on Multisite calls a method on null. Reproduced: removing the global again turns Tests_User::test_confirm_user_email_change_applies_the_pending_change into Undefined variable $wpdb on the Multisite run.
  • Routing the profile screen and REST through one email_exists() check breaks #44672. email_exists() is not case sensitive, so a user correcting the case of their own address is told it is already in use: a 400 on REST, an error on the profile screen. test_update_item_existing_email_case fails on the unmodified patch. A change of case is the same mailbox, so it is now not treated as a change needing confirmation, which is what wp_update_user() already does with its own strcasecmp() guard.
  • The split dropped the 0 === $current_user->ID || $current_user->ID !== (int) $user_id guard, so get_user_by( 'id', 0 ) returns false on a logged-out request and $user->user_email fatals on PHP 8. It also dropped the addslashes() restore of $_POST['email'] on a rejected address, added in 7.0.3.
  • WP_REST_Users_Controller::update_item() discarded a WP_Error from the new function and carried on, applying an address the confirmation step had just rejected.
  • A confirmation link followed while logged out redirected to a bare login form, so the user landed on the dashboard and the confirmation was lost. The redirect now carries the confirmation URL through redirect_to.

Smaller things: the unit tests asked for in the PR comment were never added, the @since x.x placeholders are still there, and send_user_email_change_confirmation_process() does not send anything, so I have renamed it to confirm_user_email_change(). The user_email_confirmed action moved into that function so it fires for both confirmation routes rather than only the wp-login.php one.

The confirmation hash is still md5( $email . time() . wp_rand() ), carried over from the existing code. I have left it alone to keep the diff to the ticket, but if this is being touched anyway it may be worth aligning with the token generation work in PR 12285. Related: _new_email never expires, so a confirmation link stays valid indefinitely. Also out of scope here, but worth its own ticket.

Scope question

wp_xmlrpc_server::wp_editProfile() calls wp_update_user() on the same terms, so it has the same gap. Worth deciding whether this ticket closes that too or whether it is a follow-up, because "the confirmation applies everywhere except X" is how we got here.

Testing

Against trunk, single site and Multisite: groups 57413 (18 tests), 16470, 44672 and 40015 pass, as do WP_Test_REST_Users_Controller (136 tests) and Tests_User (133 tests). phpcs reports no new errors or warnings on the changed files.

​@techjewel commented on ​PR #3813:


2 months ago
#6

I rebased this onto trunk at #13488. Your commits are the base there with authorship intact, so the diff against the first commit is only what I changed. I couldn't push here since the branch is on your fork.

Two things needed fixing beyond the rebase.

send_user_email_change_confirmation_process() uses $wpdb in the multisite branch but there's no global $wpdb;. The body came out of wp-admin/user-edit.php where $wpdb was already in scope at the top level, so confirming an email change on multisite calls a method on null. The PHPStan baseline already has Variable $wpdb might not be defined for user-edit.php at count 12, and moving this code out drops it to 6. The new function only escaped the same report because it was new.

The other one is #44672. email_exists() isn't case sensitive, so putting the profile screen and REST through the same check means a user fixing the case of their own address gets told it's already in use. That's a 400 on REST and an error on the profile screen. test_update_item_existing_email_case fails on the patch as it stands. I made a case-only change not count as a change that needs confirming, which is what wp_update_user() already does with its own strcasecmp() guard.

Smaller things. The split dropped the 0 === $current_user->ID
$current_user->ID !== (int) $user_id guard, so get_user_by( 'id', 0 ) returns false and fatals on PHP 8 when logged out. It also dropped the addslashes() restore of $_POSTemail on a rejected address that went in with 7.0.3. update_item() was throwing away the WP_Error and applying the address anyway. And a confirmation link followed while logged out lost the confirmation, so the redirect now carries it through redirect_to.

I added the unit tests you asked for on the two new functions rather than just their callers, filled in the @since x.x placeholders, and renamed send_user_email_change_confirmation_process() to confirm_user_email_change() since it doesn't send anything. You'd said the naming needed another look. The user_email_confirmed action moved into that function so it fires for both routes instead of only the wp-login.php one.

Tests pass on single site and multisite, and PHPStan is clean.

If you'd rather pull the commits back onto this branch and close mine, that works for me. Either way I'll keep the discussion on the ticket.

​@techjewel commented on ​PR #13488:


3 weeks ago
#7

Rebased onto current trunk. The only conflict was the PHPStan baseline: trunk now declares global $wpdb; in user-edit.php, so the $wpdb entry this branch was lowering is gone. The last commit therefore no longer touches the baselines and is renamed to match. Re-tested on single site and multisite: groups 57413, 16470, 44672 and 40015, WP_Test_REST_Users_Controller and Tests_User pass. phpcs adds no warnings, and PHPStan reports no errors.

#8 @techjewel
3 weeks ago

Rebased PR 13488 onto current trunk, and it merges cleanly again. The same groups pass on single site and multisite: 57413, 16470, 44672 and 40015, plus WP_Test_REST_Users_Controller and Tests_User. It also fits well with the recent change that shows the pending email change notice and a "Cancel request" link on the profile screen: a change made over REST now shows up there too.

One more data point since my last comment: we have now hit the same gap in a second plugin, a CRM that links a logged-in user to their own contact record by email. The fix was the same workaround again: treat a self-made email change as unverified until the user proves the address. Every plugin that keys on user_email has to find this on its own.

Could this get a milestone? The patch has unit tests and is ready for review. The open scope question remains: should wp_xmlrpc_server::wp_editProfile() be covered here, or in a follow-up ticket?

This ticket was mentioned in ​Slack in #core-php by techjewel. ​View the logs.


3 weeks ago

This ticket was mentioned in ​Slack in #core by techjewel. ​View the logs.


12 days ago

#11 @masteradhoc
12 days ago

  • Milestone Awaiting Review → 7.2

Adding this to the 7.2 Milestone to raise visibility.

#12 follow-ups: @johnbillion
12 days ago

This has a behavioural impact on the REST API that is quite significant. Feels like too late a change for 7.2, but I'm interested to hear thoughts from others.

#13 in reply to: ↑ 12 @techjewel
12 days ago

Replying to johnbillion:

This has a behavioural impact on the REST API that is quite significant. Feels like too late a change for 7.2, but I'm interested to hear thoughts from others.

Yes, but this is a real security issue, so I think this should be prioritized. Anyone can actually claim any email address without verifying themselves and many plugins depends on the email address of the users. The web UI fixed it so the REST API should follow that.

I think this should be fixed in 7.2

#14 in reply to: ↑ 12 @jorbin
12 days ago

  • Milestone 7.2 → 7.3

Replying to johnbillion:

This has a behavioural impact on the REST API that is quite significant. Feels like too late a change for 7.2, but I'm interested to hear thoughts from others.

Agreed, a behavior change such as this is not something that should be rushed in during the middle of beta. Updating the milestone accordingly.

Note: See TracTickets for help on using tickets.