Skip to content

Commit f11e3f6

Browse files
Users: Use wp_check_password() for proper password comparison
1 parent bd4e3c9 commit f11e3f6

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

src/wp-includes/user.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,26 +2741,37 @@ function wp_update_user( $userdata ) {
27412741
// Escape data pulled from DB.
27422742
$user = add_magic_quotes( $user );
27432743

2744-
if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) {
2745-
// If password is changing, hash it now.
2746-
$plaintext_pass = $userdata['user_pass'];
2747-
$userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );
2744+
if ( ! empty( $userdata['user_pass'] ) ) {
2745+
// Check if the password is actually changing.
2746+
if ( $userdata['user_pass'] === $user_obj->user_pass
2747+
|| wp_check_password( $userdata['user_pass'], $user_obj->user_pass, $user_id )
2748+
) {
2749+
// Password is the same, remove it so wp_insert_user() doesn't update it.
2750+
unset( $userdata['user_pass'] );
2751+
} else {
2752+
// Used downstream to clear cookies.
2753+
$changed_password = true;
27482754

2749-
/** This action is documented in wp-includes/pluggable.php */
2750-
do_action( 'wp_set_password', $plaintext_pass, $user_id, $user_obj );
2755+
// Store plaintext for the action, then hash for wp_insert_user().
2756+
$plaintext_pass = $userdata['user_pass'];
2757+
$userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );
27512758

2752-
/**
2753-
* Filters whether to send the password change email.
2754-
*
2755-
* @since 4.3.0
2756-
*
2757-
* @see wp_insert_user() For `$user` and `$userdata` fields.
2758-
*
2759-
* @param bool $send Whether to send the email.
2760-
* @param array $user The original user array.
2761-
* @param array $userdata The updated user array.
2762-
*/
2763-
$send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata );
2759+
/** This action is documented in wp-includes/pluggable.php */
2760+
do_action( 'wp_set_password', $plaintext_pass, $user_id, $user_obj );
2761+
2762+
/**
2763+
* Filters whether to send the password change email.
2764+
*
2765+
* @since 4.3.0
2766+
*
2767+
* @see wp_insert_user() For `$user` and `$userdata` fields.
2768+
*
2769+
* @param bool $send Whether to send the email.
2770+
* @param array $user The original user array.
2771+
* @param array $userdata The updated user array.
2772+
*/
2773+
$send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata );
2774+
}
27642775
}
27652776

27662777
if ( isset( $userdata['user_email'] ) && $user['user_email'] !== $userdata['user_email'] ) {
@@ -2920,7 +2931,7 @@ function wp_update_user( $userdata ) {
29202931
// Update the cookies if the password changed.
29212932
$current_user = wp_get_current_user();
29222933
if ( $current_user->ID === $user_id ) {
2923-
if ( isset( $plaintext_pass ) ) {
2934+
if ( isset( $changed_password ) ) {
29242935
/*
29252936
* Here we calculate the expiration length of the current auth cookie and compare it to the default expiration.
29262937
* If it's greater than this, then we know the user checked 'Remember Me' when they logged in.

0 commit comments

Comments
 (0)