WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/06/2014 05:42:55 AM (6 years ago)
Author:
nacin
Message:

Constant time for wp_verify_nonce().

Merges [29384] to the 3.8 branch.

Location:
branches/3.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.8

  • branches/3.8/src/wp-includes/pluggable.php

    r28054 r29386  
    544544    $hash = hash_hmac('md5', $username . '|' . $expiration, $key);
    545545
    546     if ( hash_hmac( 'md5', $hmac, $key ) !== hash_hmac( 'md5', $hash, $key ) ) {
     546    if ( ! hash_equals( $hash, $hmac ) ) {
    547547        do_action('auth_cookie_bad_hash', $cookie_elements);
    548548        return false;
     
    13431343
    13441344    // Nonce generated 0-12 hours ago
    1345     if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) === $nonce )
     1345    $expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce'), -12, 10 );
     1346    if ( hash_equals( $expected, $nonce ) ) {
    13461347        return 1;
     1348    }
     1349
    13471350    // Nonce generated 12-24 hours ago
    1348     if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) === $nonce )
     1351    $expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
     1352    if ( hash_equals( $expected, $nonce ) ) {
    13491353        return 2;
     1354    }
     1355
    13501356    // Invalid nonce
    13511357    return false;
     
    18311837endif;
    18321838
     1839if ( ! function_exists( 'hash_equals' ) ) :
     1840/**
     1841 * Compare two strings in constant time.
     1842 *
     1843 * This function is NOT pluggable. It is in this file (in addition to
     1844 * compat.php) to prevent errors if, during an update, pluggable.php
     1845 * copies over but compat.php does not.
     1846 *
     1847 * This function was added in PHP 5.6.
     1848 * It can leak the length of a string.
     1849 *
     1850 * @since 3.9.2
     1851 *
     1852 * @param string $a Expected string.
     1853 * @param string $b Actual string.
     1854 * @return bool Whether strings are equal.
     1855 */
     1856function hash_equals( $a, $b ) {
     1857    $a_length = strlen( $a );
     1858    if ( $a_length !== strlen( $b ) ) {
     1859        return false;
     1860    }
     1861    $result = 0;
     1862
     1863    // Do not attempt to "optimize" this.
     1864    for ( $i = 0; $i < $a_length; $i++ ) {
     1865        $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
     1866    }
     1867
     1868    return $result === 0;
     1869}
     1870endif;
Note: See TracChangeset for help on using the changeset viewer.