Make WordPress Core

Changeset 29388


Ignore:
Timestamp:
08/06/2014 05:44:39 AM (10 years ago)
Author:
nacin
Message:

Constant time for wp_verify_nonce().

Merges [29384] to the 3.7 branch.

Location:
branches/3.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3.7

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

    r18404 r29388  
    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.7/src/wp-includes/pluggable.php

    r28055 r29388  
    547547    $hash = hash_hmac('md5', $username . '|' . $expiration, $key);
    548548
    549     if ( hash_hmac( 'md5', $hmac, $key ) !== hash_hmac( 'md5', $hash, $key ) ) {
     549    if ( ! hash_equals( $hash, $hmac ) ) {
    550550        do_action('auth_cookie_bad_hash', $cookie_elements);
    551551        return false;
     
    12981298
    12991299    // Nonce generated 0-12 hours ago
    1300     if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) === $nonce )
     1300    $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 );
     1301    if ( hash_equals( $expected, $nonce ) ) {
    13011302        return 1;
     1303    }
     1304
    13021305    // Nonce generated 12-24 hours ago
    1303     if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) === $nonce )
     1306    $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
     1307    if ( hash_equals( $expected, $nonce ) ) {
    13041308        return 2;
     1309    }
     1310
    13051311    // Invalid nonce
    13061312    return false;
     
    17851791endif;
    17861792
     1793if ( ! function_exists( 'hash_equals' ) ) :
     1794/**
     1795 * Compare two strings in constant time.
     1796 *
     1797 * This function is NOT pluggable. It is in this file (in addition to
     1798 * compat.php) to prevent errors if, during an update, pluggable.php
     1799 * copies over but compat.php does not.
     1800 *
     1801 * This function was added in PHP 5.6.
     1802 * It can leak the length of a string.
     1803 *
     1804 * @since 3.9.2
     1805 *
     1806 * @param string $a Expected string.
     1807 * @param string $b Actual string.
     1808 * @return bool Whether strings are equal.
     1809 */
     1810function hash_equals( $a, $b ) {
     1811    $a_length = strlen( $a );
     1812    if ( $a_length !== strlen( $b ) ) {
     1813        return false;
     1814    }
     1815    $result = 0;
     1816
     1817    // Do not attempt to "optimize" this.
     1818    for ( $i = 0; $i < $a_length; $i++ ) {
     1819        $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
     1820    }
     1821
     1822    return $result === 0;
     1823}
     1824endif;
Note: See TracChangeset for help on using the changeset viewer.