WordPress.org

Make WordPress Core

Changeset 29384


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

Constant time for wp_verify_nonce().

Merges [29382] to the 3.9 branch.

Adds a second copy of hash_equals() to pluggable.php in case compat.php is not copied over in an update. (The general goal is no cross-file dependencies for minor releases.)

Location:
branches/3.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3.9

  • branches/3.9/src/wp-includes/compat.php

    r18404 r29384  
    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;
  • branches/3.9/src/wp-includes/pluggable.php

    r28053 r29384  
    648648    $hash = hash_hmac('md5', $username . '|' . $expiration, $key);
    649649
    650     if ( hash_hmac( 'md5', $hmac, $key ) !== hash_hmac( 'md5', $hash, $key ) ) {
     650    if ( ! hash_equals( $hash, $hmac ) ) {
    651651        /**
    652652         * Fires if a bad authentication cookie hash is encountered.
     
    16591659
    16601660    // Nonce generated 0-12 hours ago
    1661     if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) === $nonce )
     1661    $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 );
     1662    if ( hash_equals( $expected, $nonce ) ) {
    16621663        return 1;
     1664    }
     1665
    16631666    // Nonce generated 12-24 hours ago
    1664     if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) === $nonce )
     1667    $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
     1668    if ( hash_equals( $expected, $nonce ) ) {
    16651669        return 2;
     1670    }
     1671
    16661672    // Invalid nonce
    16671673    return false;
     
    22012207endif;
    22022208
     2209if ( ! function_exists( 'hash_equals' ) ) :
     2210/**
     2211 * Compare two strings in constant time.
     2212 *
     2213 * This function is NOT pluggable. It is in this file (in addition to
     2214 * compat.php) to prevent errors if, during an update, pluggable.php
     2215 * copies over but compat.php does not.
     2216 *
     2217 * This function was added in PHP 5.6.
     2218 * It can leak the length of a string.
     2219 *
     2220 * @since 3.9.2
     2221 *
     2222 * @param string $a Expected string.
     2223 * @param string $b Actual string.
     2224 * @return bool Whether strings are equal.
     2225 */
     2226function hash_equals( $a, $b ) {
     2227    $a_length = strlen( $a );
     2228    if ( $a_length !== strlen( $b ) ) {
     2229        return false;
     2230    }
     2231    $result = 0;
     2232
     2233    // Do not attempt to "optimize" this.
     2234    for ( $i = 0; $i < $a_length; $i++ ) {
     2235        $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
     2236    }
     2237
     2238    return $result === 0;
     2239}
     2240endif;
Note: See TracChangeset for help on using the changeset viewer.