Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions src/wp-includes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -2221,8 +2221,15 @@ function wp_insert_user( $userdata ) {
$user_pass = ! empty( $userdata['user_pass'] ) ? $userdata['user_pass'] : $old_user_data->user_pass;
} else {
$update = false;

$pre_hash_password = _wp_filter_pre_hash_password( $userdata['user_pass'] );

if ( is_wp_error( $pre_hash_password ) ) {
return $pre_hash_password;
}

// Hash the password.
$user_pass = wp_hash_password( $userdata['user_pass'] );
$user_pass = wp_hash_password( $pre_hash_password );
}

$sanitized_user_login = sanitize_user( $userdata['user_login'], true );
Expand Down Expand Up @@ -2680,9 +2687,16 @@ function wp_update_user( $userdata ) {
$user = add_magic_quotes( $user );

if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) {

$pre_hash_password = _wp_filter_pre_hash_password( $userdata['user_pass'] );

if ( is_wp_error( $pre_hash_password ) ) {
return $pre_hash_password;
}

// If password is changing, hash it now.
$plaintext_pass = $userdata['user_pass'];
$userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] );
$plaintext_pass = $pre_hash_password;
$userdata['user_pass'] = wp_hash_password( $pre_hash_password );

/**
* Filters whether to send the password change email.
Expand Down Expand Up @@ -5199,3 +5213,29 @@ function wp_is_password_reset_allowed_for_user( $user ) {
*/
return apply_filters( 'allow_password_reset', $allow, $user->ID );
}

/**
* Filters and validates a password before hashing.
*
* @since 6.8.0
* @access private
*
* @param string $password The password to filter and validate.
* @return string|WP_Error The filtered password on success, WP_Error on failure.
*/
function _wp_filter_pre_hash_password( $password ) {
/**
* Filters a password before hashing it.
*
* @since 6.8.0
*
* @param string $password The user's password.
*/
$pre_hash_password = apply_filters( 'pre_hash_password', $password );

if ( empty( $pre_hash_password ) ) {
return new WP_Error( 'empty_pre_hash_password', __( 'Cannot save user with an empty password.' ) );
}

return $pre_hash_password;
}
4 changes: 2 additions & 2 deletions tests/phpunit/tests/user/slashes.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function test_wp_insert_user() {
'nickname' => self::SLASH_5,
'display_name' => self::SLASH_7,
'description' => self::SLASH_3,
'user_pass' => '',
'user_pass' => 'password',
)
);
$user = get_user_to_edit( $user_id );
Expand All @@ -175,7 +175,7 @@ public function test_wp_insert_user() {
'nickname' => self::SLASH_6,
'display_name' => self::SLASH_2,
'description' => self::SLASH_4,
'user_pass' => '',
'user_pass' => 'password',
)
);
$user = get_user_to_edit( $user_id );
Expand Down