WordPress.org

Make WordPress Core

Changeset 29382


Ignore:
Timestamp:
08/06/2014 05:25:03 AM (4 years ago)
Author:
nacin
Message:

Constant time for wp_verify_nonce().

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r18404 r29382  
    9595    }
    9696}
     97
     98if ( ! function_exists( 'hash_equals' ) ) :
     99/**
     100 * Compare two strings in constant time.
     101 *
     102 * This function was added in PHP 5.6.
     103 * It can leak the length of a string.
     104 *
     105 * @since 3.9.2
     106 *
     107 * @param string $a Expected string.
     108 * @param string $b Actual string.
     109 * @return bool Whether strings are equal.
     110 */
     111function hash_equals( $a, $b ) {
     112    $a_length = strlen( $a );
     113    if ( $a_length !== strlen( $b ) ) {
     114        return false;
     115    }
     116    $result = 0;
     117
     118    // Do not attempt to "optimize" this.
     119    for ( $i = 0; $i < $a_length; $i++ ) {
     120        $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
     121    }
     122
     123    return $result === 0;
     124}
     125endif;
  • trunk/src/wp-includes/pluggable.php

    r29221 r29382  
    672672    $hash = hash_hmac( 'sha256', $username . '|' . $expiration . '|' . $token, $key );
    673673
    674     if ( hash_hmac( 'sha256', $hmac, $key ) !== hash_hmac( 'sha256', $hash, $key ) ) {
     674    if ( ! hash_equals( $hash, $hmac ) ) {
    675675        /**
    676676         * Fires if a bad authentication cookie hash is encountered.
     
    17121712
    17131713    // Nonce generated 0-12 hours ago
    1714     if ( $nonce === substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 ) ) {
     1714    $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 );
     1715    if ( hash_equals( $expected, $nonce ) ) {
    17151716        return 1;
    17161717    }
    17171718
    17181719    // Nonce generated 12-24 hours ago
    1719     if ( $nonce === substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 ) ) {
     1720    $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
     1721    if ( hash_equals( $expected, $nonce ) ) {
    17201722        return 2;
    17211723    }
Note: See TracChangeset for help on using the changeset viewer.