Skip to content

Commit 6e66fd2

Browse files
committed
Explicitly require the hash extension.
1 parent e114dfb commit 6e66fd2

File tree

8 files changed

+19
-193
lines changed

8 files changed

+19
-193
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"issues": "https://core.trac.wordpress.org/"
1111
},
1212
"require": {
13+
"ext-hash": "*",
1314
"ext-json": "*",
1415
"php": ">=7.2.24"
1516
},

src/wp-admin/includes/class-wp-site-health.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ public function get_test_php_extensions() {
923923
),
924924
'hash' => array(
925925
'function' => 'hash',
926-
'required' => false,
926+
'required' => true,
927927
),
928928
'imagick' => array(
929929
'extension' => 'imagick',

src/wp-admin/includes/update-core.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,19 @@ function update_core( $from, $to ) {
11941194
);
11951195
}
11961196

1197+
// Add a warning when the hash PHP extension is missing (only affects PHP < 7.4).
1198+
if ( ! extension_loaded( 'hash' ) ) {
1199+
return new WP_Error(
1200+
'php_not_compatible_hash',
1201+
sprintf(
1202+
/* translators: 1: WordPress version number, 2: The PHP extension name needed. */
1203+
__( 'The update cannot be installed because WordPress %1$s requires the %2$s PHP extension.' ),
1204+
$wp_version,
1205+
'hash'
1206+
)
1207+
);
1208+
}
1209+
11971210
/** This filter is documented in wp-admin/includes/update-core.php */
11981211
apply_filters( 'update_feedback', __( 'Preparing to install the latest version&#8230;' ) );
11991212

src/wp-includes/class-wp-session-tokens.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@ final public static function get_instance( $user_id ) {
6868
* @return string A hash of the session token (a verifier).
6969
*/
7070
private function hash_token( $token ) {
71-
// If ext/hash is not present, use sha1() instead.
72-
if ( function_exists( 'hash' ) ) {
73-
return hash( 'sha256', $token );
74-
} else {
75-
return sha1( $token );
76-
}
71+
return hash( 'sha256', $token );
7772
}
7873

7974
/**

src/wp-includes/class-wpdb.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,12 +2406,10 @@ public function placeholder_escape() {
24062406
static $placeholder;
24072407

24082408
if ( ! $placeholder ) {
2409-
// If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
2410-
$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
24112409
// Old WP installs may not have AUTH_SALT defined.
24122410
$salt = defined( 'AUTH_SALT' ) && AUTH_SALT ? AUTH_SALT : (string) rand();
24132411

2414-
$placeholder = '{' . hash_hmac( $algo, uniqid( $salt, true ), $salt ) . '}';
2412+
$placeholder = '{' . hash_hmac( 'sha256', uniqid( $salt, true ), $salt ) . '}';
24152413
}
24162414

24172415
/*

src/wp-includes/compat.php

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -263,118 +263,6 @@ function _mb_strlen( $str, $encoding = null ) {
263263
return --$count;
264264
}
265265

266-
if ( ! function_exists( 'hash_hmac' ) ) :
267-
/**
268-
* Compat function to mimic hash_hmac().
269-
*
270-
* The Hash extension is bundled with PHP by default since PHP 5.1.2.
271-
* However, the extension may be explicitly disabled on select servers.
272-
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
273-
* longer be disabled.
274-
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
275-
* and the associated `_hash_hmac()` function can be safely removed.
276-
*
277-
* @ignore
278-
* @since 3.2.0
279-
*
280-
* @see _hash_hmac()
281-
*
282-
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
283-
* @param string $data Data to be hashed.
284-
* @param string $key Secret key to use for generating the hash.
285-
* @param bool $binary Optional. Whether to output raw binary data (true),
286-
* or lowercase hexits (false). Default false.
287-
* @return string|false The hash in output determined by `$binary`.
288-
* False if `$algo` is unknown or invalid.
289-
*/
290-
function hash_hmac( $algo, $data, $key, $binary = false ) {
291-
return _hash_hmac( $algo, $data, $key, $binary );
292-
}
293-
endif;
294-
295-
/**
296-
* Internal compat function to mimic hash_hmac().
297-
*
298-
* @ignore
299-
* @since 3.2.0
300-
*
301-
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
302-
* @param string $data Data to be hashed.
303-
* @param string $key Secret key to use for generating the hash.
304-
* @param bool $binary Optional. Whether to output raw binary data (true),
305-
* or lowercase hexits (false). Default false.
306-
* @return string|false The hash in output determined by `$binary`.
307-
* False if `$algo` is unknown or invalid.
308-
*/
309-
function _hash_hmac( $algo, $data, $key, $binary = false ) {
310-
$packs = array(
311-
'md5' => 'H32',
312-
'sha1' => 'H40',
313-
);
314-
315-
if ( ! isset( $packs[ $algo ] ) ) {
316-
return false;
317-
}
318-
319-
$pack = $packs[ $algo ];
320-
321-
if ( strlen( $key ) > 64 ) {
322-
$key = pack( $pack, $algo( $key ) );
323-
}
324-
325-
$key = str_pad( $key, 64, chr( 0 ) );
326-
327-
$ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) );
328-
$opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) );
329-
330-
$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );
331-
332-
if ( $binary ) {
333-
return pack( $pack, $hmac );
334-
}
335-
336-
return $hmac;
337-
}
338-
339-
if ( ! function_exists( 'hash_equals' ) ) :
340-
/**
341-
* Timing attack safe string comparison.
342-
*
343-
* Compares two strings using the same time whether they're equal or not.
344-
*
345-
* Note: It can leak the length of a string when arguments of differing length are supplied.
346-
*
347-
* This function was added in PHP 5.6.
348-
* However, the Hash extension may be explicitly disabled on select servers.
349-
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
350-
* longer be disabled.
351-
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
352-
* can be safely removed.
353-
*
354-
* @since 3.9.2
355-
*
356-
* @param string $known_string Expected string.
357-
* @param string $user_string Actual, user supplied, string.
358-
* @return bool Whether strings are equal.
359-
*/
360-
function hash_equals( $known_string, $user_string ) {
361-
$known_string_length = strlen( $known_string );
362-
363-
if ( strlen( $user_string ) !== $known_string_length ) {
364-
return false;
365-
}
366-
367-
$result = 0;
368-
369-
// Do not attempt to "optimize" this.
370-
for ( $i = 0; $i < $known_string_length; $i++ ) {
371-
$result |= ord( $known_string[ $i ] ) ^ ord( $user_string[ $i ] );
372-
}
373-
374-
return 0 === $result;
375-
}
376-
endif;
377-
378266
// sodium_crypto_box() was introduced in PHP 7.2.
379267
if ( ! function_exists( 'sodium_crypto_box' ) ) {
380268
require ABSPATH . WPINC . '/sodium_compat/autoload.php';

src/wp-includes/pluggable.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,7 @@ function wp_validate_auth_cookie( $cookie = '', $scheme = '' ) {
768768

769769
$key = wp_hash( $username . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
770770

771-
// If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
772-
$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
773-
$hash = hash_hmac( $algo, $username . '|' . $expiration . '|' . $token, $key );
771+
$hash = hash_hmac( 'sha256', $username . '|' . $expiration . '|' . $token, $key );
774772

775773
if ( ! hash_equals( $hash, $hmac ) ) {
776774
/**
@@ -871,9 +869,7 @@ function wp_generate_auth_cookie( $user_id, $expiration, $scheme = 'auth', $toke
871869

872870
$key = wp_hash( $user->user_login . '|' . $pass_frag . '|' . $expiration . '|' . $token, $scheme );
873871

874-
// If ext/hash is not present, compat.php's hash_hmac() does not support sha256.
875-
$algo = function_exists( 'hash' ) ? 'sha256' : 'sha1';
876-
$hash = hash_hmac( $algo, $user->user_login . '|' . $expiration . '|' . $token, $key );
872+
$hash = hash_hmac( 'sha256', $user->user_login . '|' . $expiration . '|' . $token, $key );
877873

878874
$cookie = $user->user_login . '|' . $expiration . '|' . $token . '|' . $hash;
879875

tests/phpunit/tests/compat/hashHmac.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)