Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#21022 Switch to using bcrypt for hashing passwords #7333

Open
wants to merge 86 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
fe1c16e
Begin switching the password hashing mechanism from phpass to bcrypt.
johnbillion Sep 11, 2024
82d937f
Update the handling of user passwords, password reset keys, and user …
johnbillion Sep 11, 2024
f12e93d
Update the handling of the recovery mode key.
johnbillion Sep 11, 2024
5a46809
Update the handling of post passwords.
johnbillion Sep 11, 2024
d4e4e47
Automatically rehash user passwords and application passwords after t…
johnbillion Sep 11, 2024
638421d
Docs.
johnbillion Sep 11, 2024
316cb41
Juggle this a bit.
johnbillion Sep 11, 2024
76fa518
Trying to get these tests in order.
johnbillion Sep 11, 2024
d707939
More docs.
johnbillion Sep 11, 2024
4a796b7
Retain validity of phpass hashed password reset keys.
johnbillion Sep 11, 2024
64dfd57
Retain validity of a phpass hashed recovery mode key.
johnbillion Sep 11, 2024
e161994
Retain validity of phpass hashed user request keys.
johnbillion Sep 11, 2024
548f937
More docs.
johnbillion Sep 11, 2024
fe9b053
Add tests for password rehashing when signing in with a username or e…
johnbillion Sep 12, 2024
33cf000
Docs.
johnbillion Sep 12, 2024
d51cbc2
Ensure `wp_check_password()` remains compatible with changes to the d…
johnbillion Sep 12, 2024
6b5441c
Simplify this logic.
johnbillion Sep 12, 2024
89e0645
Add tests for Argon2 support.
johnbillion Sep 12, 2024
085b73c
Always pass the return value of `wp_check_password()` through the `ch…
johnbillion Sep 12, 2024
3e078a7
Reintroduce support for the `$wp_hasher` global if it's set.
johnbillion Sep 12, 2024
be636be
Test the tests.
johnbillion Sep 12, 2024
4265787
Docs.
johnbillion Sep 12, 2024
fd6bcdd
Start splitting up and fixing these tests.
johnbillion Sep 12, 2024
392e612
Coding standards.
johnbillion Sep 12, 2024
9040193
Add a todo.
johnbillion Sep 13, 2024
bc99ca1
Add some more assertions to the application password auth tests.
johnbillion Sep 17, 2024
58a89e2
Correct and add tests for the application password rehashing.
johnbillion Sep 17, 2024
49871d0
Done.
johnbillion Sep 17, 2024
b42e57b
Coding standards.
johnbillion Sep 17, 2024
2baeb19
Allow either the name or password to change in order to allow an appl…
johnbillion Sep 17, 2024
95f6d94
Docs.
johnbillion Sep 17, 2024
f8974eb
PHP 7.2 compatibility.
johnbillion Oct 8, 2024
ff85df2
Docs.
johnbillion Oct 8, 2024
ebcdd26
Add a test to ensure the user's account password isn't touched when r…
johnbillion Oct 8, 2024
140b9d4
Merge branch 'trunk' into 21022-bcrypt
johnbillion Oct 8, 2024
ea8c56c
One fewer regexes in the world.
johnbillion Oct 8, 2024
2ef2077
Merge branch 'trunk' into 21022-bcrypt
johnbillion Oct 9, 2024
3a951ef
Allow the password hashing options to be filtered.
johnbillion Oct 11, 2024
53acf70
Docs.
johnbillion Oct 11, 2024
5944d9a
Merge branch 'trunk' into 21022-bcrypt
johnbillion Oct 21, 2024
820f48f
Cease hashing passwords as md5 during the upgrade routine and/or the …
johnbillion Oct 21, 2024
c07e618
More tests for empty values.
johnbillion Oct 21, 2024
5d33621
More updates to the tests.
johnbillion Oct 21, 2024
0063154
Add a test to verify that a password gets rehashed when the default c…
johnbillion Oct 22, 2024
1aea250
This might as well go here.
johnbillion Oct 22, 2024
d6c7837
Add tests for post password handling.
johnbillion Oct 22, 2024
8b1dbd2
Merge branch 'trunk' into 21022-bcrypt
johnbillion Oct 22, 2024
5824f4c
Clear the cookie value before performing the assertions.
johnbillion Oct 22, 2024
2502179
Implement pre-hashing with sha384 to retain entropy of passwords over…
johnbillion Nov 5, 2024
2321412
Implement domain separation for the password to protect against passw…
johnbillion Nov 11, 2024
d86ef6d
Remove assertions that are unnecessarily specific to bcrypt.
johnbillion Nov 25, 2024
09ddc5e
The default bcrypt cost was increased in PHP 8.4.
johnbillion Nov 25, 2024
a27411d
Merge branch 'trunk' into 21022-bcrypt-sha2
johnbillion Dec 12, 2024
e5f8d5c
Merge branch 'trunk' into 21022-bcrypt
johnbillion Dec 12, 2024
9bb86a8
Merge branch '21022-bcrypt' into 21022-bcrypt-sha2
johnbillion Dec 12, 2024
1cebd80
Switch to HMAC in place of manually prepending the domain separation …
johnbillion Dec 12, 2024
537ee4a
Introduce the `wp_hash_password_algorithm` filter for controlling the…
johnbillion Dec 18, 2024
8a99edd
Why.
johnbillion Dec 18, 2024
68634f0
Vanilla bcrypt hashes should be rehashed to use pre-hashing.
johnbillion Dec 18, 2024
adf983f
Let's bring this more inline with the other tests.
johnbillion Dec 18, 2024
754519b
No need to perform a prefix check here, just let `wp_check_password()…
johnbillion Dec 18, 2024
35d1885
Merge branch 'trunk' into 21022-bcrypt-algo-filter
johnbillion Jan 6, 2025
0c49b94
Docs.
johnbillion Jan 6, 2025
f85a6cb
Merge branch 'trunk' into 21022-bcrypt
johnbillion Jan 6, 2025
413e81f
Switch to using a fast sha1 hash for password reset keys, user reques…
johnbillion Jan 6, 2025
4880c6e
Switch from SHA-1 to SHA-256 in the security key HMACs.
johnbillion Jan 7, 2025
cb46077
Use a more fitting hash prefix.
johnbillion Jan 7, 2025
a9a1c17
Update some more tests.
johnbillion Jan 7, 2025
8b4d40e
Introduce wrapper functions for hashing and checking a value with BLA…
johnbillion Jan 7, 2025
9994922
Replace SHA-256 and SHA-1 with BLAKE2b when hashing of password reset…
johnbillion Jan 7, 2025
d80491d
Switch application passwords over to BLAKE2b instead of phpass.
johnbillion Jan 7, 2025
f222f64
Remove opportunistic rehashing of application passwords.
johnbillion Jan 8, 2025
c966123
The hash extension is now required due to the use of sha384.
johnbillion Jan 8, 2025
7b3eb9a
Enforce an upper key length.
johnbillion Jan 8, 2025
e3fcdca
Ensure the salt satisfies the constraints for key length in Sodium.
johnbillion Jan 9, 2025
1d1ab91
Add some type safety.
johnbillion Jan 9, 2025
a94d7ff
Merge branch 'trunk' into 21022-bcrypt
johnbillion Jan 13, 2025
8e4652e
Merge branch 'trunk' into 21022-application-passwords
johnbillion Jan 13, 2025
20eb361
Remove salting from the fast hashing function.
johnbillion Jan 14, 2025
37e0cd7
Docs.
johnbillion Jan 14, 2025
9a58b91
Tidying up.
johnbillion Jan 14, 2025
d62c5d3
Rename these functions.
johnbillion Jan 14, 2025
5f1a8ee
Docs.
johnbillion Jan 14, 2025
8931037
More docs.
johnbillion Jan 14, 2025
141b031
Merge branch '21022-application-passwords' into 21022-bcrypt
johnbillion Jan 14, 2025
a5c105b
Merge branch 'trunk' into 21022-bcrypt
johnbillion Jan 14, 2025
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"issues": "https://core.trac.wordpress.org/"
},
"require": {
"ext-hash": "*",
"ext-json": "*",
"php": ">=7.2.24"
},
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/includes/class-wp-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ public function get_test_php_extensions() {
),
'hash' => array(
'function' => 'hash',
'required' => false,
'required' => true,
),
'imagick' => array(
'extension' => 'imagick',
Expand Down
8 changes: 1 addition & 7 deletions src/wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ function upgrade_101() {
*
* @ignore
* @since 1.2.0
* Since x.y.z User passwords are no longer hashed with md5.
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
Expand All @@ -980,13 +981,6 @@ function upgrade_110() {
}
}

$users = $wpdb->get_results( "SELECT ID, user_pass from $wpdb->users" );
foreach ( $users as $row ) {
if ( ! preg_match( '/^[A-Fa-f0-9]{32}$/', $row->user_pass ) ) {
$wpdb->update( $wpdb->users, array( 'user_pass' => md5( $row->user_pass ) ), array( 'ID' => $row->ID ) );
}
}

// Get the GMT offset, we'll use that later on.
$all_options = get_alloptions_110();

Expand Down
53 changes: 52 additions & 1 deletion src/wp-includes/class-wp-application-passwords.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static function is_in_use() {
*
* @since 5.6.0
* @since 5.7.0 Returns WP_Error if application name already exists.
* @since x.y.z The hashed password value now uses wp_fast_hash() instead of phpass.
*
* @param int $user_id User ID.
* @param array $args {
Expand Down Expand Up @@ -95,7 +96,7 @@ public static function create_new_application_password( $user_id, $args = array(
}

$new_password = wp_generate_password( static::PW_LENGTH, false );
$hashed_password = wp_hash_password( $new_password );
$hashed_password = self::hash_password( $new_password );

$new_item = array(
'uuid' => wp_generate_uuid4(),
Expand Down Expand Up @@ -124,6 +125,7 @@ public static function create_new_application_password( $user_id, $args = array(
* Fires when an application password is created.
*
* @since 5.6.0
* @since x.y.z @since x.y.z The hashed password value now uses wp_fast_hash() instead of phpass.
*
* @param int $user_id The user ID.
* @param array $new_item {
Expand Down Expand Up @@ -249,6 +251,7 @@ public static function application_name_exists_for_user( $user_id, $name ) {
* Updates an application password.
*
* @since 5.6.0
* @since x.y.z The actual password should now be hashed using wp_fast_hash().
*
* @param int $user_id User ID.
* @param string $uuid The password's UUID.
Expand Down Expand Up @@ -284,6 +287,11 @@ public static function update_application_password( $user_id, $uuid, $update = a
$save = true;
}

if ( ! empty( $update['password'] ) && $item['password'] !== $update['password'] ) {
$item['password'] = $update['password'];
$save = true;
}

if ( $save ) {
$saved = static::set_user_application_passwords( $user_id, $passwords );

Expand All @@ -296,6 +304,8 @@ public static function update_application_password( $user_id, $uuid, $update = a
* Fires when an application password is updated.
*
* @since 5.6.0
* @since x.y.z The password is now hashed using wp_fast_hash() instead of phpass.
* Existing passwords may still be hashed using phpass.
*
* @param int $user_id The user ID.
* @param array $item {
Expand Down Expand Up @@ -464,4 +474,45 @@ public static function chunk_password( $raw_password ) {

return trim( chunk_split( $raw_password, 4, ' ' ) );
}

/**
* Hashes a plaintext application password.
*
* @since x.y.z
*
* @param string $password Plaintext password.
* @return string Hashed password.
*/
public static function hash_password( string $password ): string {
return wp_fast_hash( $password );
}

/**
* Checks a plaintext application password against a hashed password.
*
* @since x.y.z
*
* @param string $password Plaintext password.
* @param string $hash Hash of the password to check against.
* @return bool Whether the password matches the hashed password.
*/
public static function check_password( string $password, string $hash ): bool {
return wp_verify_fast_hash( $password, $hash );
}

/**
* Checks whether a password hash needs to be rehashed.
*
* A password hashed in a prior version of WordPress may still be hashed with phpass and will
* need to be rehashed. If the algorithm is changed in WordPress then a password hashed in a
* previous version will need to be rehashed.
*
* @since x.y.z
*
* @param string $hash Hash of a password to check.
* @return bool Whether the hash needs to be rehashed.
*/
public static function password_needs_rehash( string $hash ): bool {
return ! str_starts_with( $hash, '$generic$' );
}
}
57 changes: 29 additions & 28 deletions src/wp-includes/class-wp-recovery-mode-key-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,18 @@ public function generate_recovery_mode_token() {
* Creates a recovery mode key.
*
* @since 5.2.0
*
* @global PasswordHash $wp_hasher Portable PHP password hashing framework instance.
* @since x.y.z The stored key is now hashed using wp_fast_hash() instead of phpass.
*
* @param string $token A token generated by {@see generate_recovery_mode_token()}.
* @return string Recovery mode key.
*/
public function generate_and_store_recovery_mode_key( $token ) {

global $wp_hasher;

$key = wp_generate_password( 22, false );

if ( empty( $wp_hasher ) ) {
require_once ABSPATH . WPINC . '/class-phpass.php';
$wp_hasher = new PasswordHash( 8, true );
}

$hashed = $wp_hasher->HashPassword( $key );

$records = $this->get_keys();

$records[ $token ] = array(
'hashed_key' => $hashed,
'hashed_key' => wp_fast_hash( $key ),
'created_at' => time(),
);

Expand All @@ -85,16 +74,12 @@ public function generate_and_store_recovery_mode_key( $token ) {
*
* @since 5.2.0
*
* @global PasswordHash $wp_hasher Portable PHP password hashing framework instance.
*
* @param string $token The token used when generating the given key.
* @param string $key The unhashed key.
* @param string $key The plain text key.
* @param int $ttl Time in seconds for the key to be valid for.
* @return true|WP_Error True on success, error object on failure.
*/
public function validate_recovery_mode_key( $token, $key, $ttl ) {
global $wp_hasher;

$records = $this->get_keys();

if ( ! isset( $records[ $token ] ) ) {
Expand All @@ -109,12 +94,7 @@ public function validate_recovery_mode_key( $token, $key, $ttl ) {
return new WP_Error( 'invalid_recovery_key_format', __( 'Invalid recovery key format.' ) );
}

if ( empty( $wp_hasher ) ) {
require_once ABSPATH . WPINC . '/class-phpass.php';
$wp_hasher = new PasswordHash( 8, true );
}

if ( ! $wp_hasher->CheckPassword( $key, $record['hashed_key'] ) ) {
if ( ! wp_verify_fast_hash( $key, $record['hashed_key'] ) ) {
return new WP_Error( 'hash_mismatch', __( 'Invalid recovery key.' ) );
}

Expand Down Expand Up @@ -169,9 +149,20 @@ private function remove_key( $token ) {
* Gets the recovery key records.
*
* @since 5.2.0
* @since x.y.z Each key is now hashed using wp_fast_hash() instead of phpass.
* Existing keys may still be hashed using phpass.
*
* @return array {
* Associative array of token => data pairs, where the data is an associative
* array of information about the key.
*
* @return array Associative array of $token => $data pairs, where $data has keys 'hashed_key'
* and 'created_at'.
* @type array ...$0 {
* Information about the key.
*
* @type string $hashed_key The hashed value of the key.
* @type int $created_at The timestamp when the key was created.
* }
* }
*/
private function get_keys() {
return (array) get_option( $this->option_name, array() );
Expand All @@ -181,9 +172,19 @@ private function get_keys() {
* Updates the recovery key records.
*
* @since 5.2.0
* @since x.y.z Each key should now be hashed using wp_fast_hash() instead of phpass.
*
* @param array $keys {
* Associative array of token => data pairs, where the data is an associative
* array of information about the key.
*
* @type array ...$0 {
* Information about the key.
*
* @param array $keys Associative array of $token => $data pairs, where $data has keys 'hashed_key'
* and 'created_at'.
* @type string $hashed_key The hashed value of the key.
* @type int $created_at The timestamp when the key was created.
* }
* }
* @return bool True on success, false on failure.
*/
private function update_keys( array $keys ) {
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/class-wp-user-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ final class WP_User_Request {
* Key used to confirm this request.
*
* @since 4.9.6
* @since x.y.z The key is now hashed using wp_fast_hash() instead of phpass.
*
* @var string
*/
public $confirm_key = '';
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/class-wp-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Core class used to implement the WP_User object.
*
* @since 2.0.0
* @since x.y.z The `user_pass` property is now hashed using bcrypt instead of phpass.
*
* @property string $nickname
* @property string $description
Expand Down
49 changes: 49 additions & 0 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9085,3 +9085,52 @@ function wp_is_heic_image_mime_type( $mime_type ) {

return in_array( $mime_type, $heic_mime_types, true );
}

/**
* Returns a cryptographically secure hash of a message using a fast generic hash function.
*
* This function does not salt the value prior to being hashed, therefore input to this function should be generated
* with sufficiently high entropy if the data is sensitive, preferably greater than 128 bits. This function is used
* internally in WordPress to hash security keys and application passwords which are generated with high entropy.
*
* This function must not be used for hashing user-generated passwords. Use wp_hash_password() for that.
*
* Use the wp_verify_fast_hash() function to verify the hash.
*
* The BLAKE2b algorithm is used by Sodium to hash the message.
*
* @since x.y.z
*
* @throws TypeError Thrown by Sodium if the message is not a string.
*
* @param string $message The message to hash.
* @return string The hash of the message.
*/
function wp_fast_hash( string $message ): string {
return '$generic$' . sodium_bin2hex( sodium_crypto_generichash( $message ) );
}

/**
* Checks whether a plaintext message matches the hashed value. Used to verify values hashed via wp_fast_hash().
*
* The function uses Sodium to hash the message and compare it to the hashed value. If the hash is not a generic hash,
* the hash is treated as a phpass portable hash in order to provide backward compatibility for application passwords
* which were hashed using phpass prior to WordPress x.y.z.
*
* @since x.y.z
*
* @throws TypeError Thrown by Sodium if the message is not a string.
*
* @param string $message The plaintext message.
* @param string $hash Hash of the message to check against.
* @return bool Whether the message matches the hashed message.
*/
function wp_verify_fast_hash( string $message, string $hash ): bool {
if ( ! str_starts_with( $hash, '$generic$' ) ) {
// Back-compat for old phpass hashes.
require_once ABSPATH . WPINC . '/class-phpass.php';
return ( new PasswordHash( 8, true ) )->CheckPassword( $message, $hash );
}

return hash_equals( substr( $hash, 9 ), wp_fast_hash( $message ) );
}
Loading
Loading