<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[33019] trunk: Expire password reset links after 24 hours (by default).</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/33019">33019</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/33019","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-07-01 06:32:07 +0000 (Wed, 01 Jul 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Expire password reset links after 24 hours (by default). This causes existing password reset links to become invalid.

Props markjaquith, voldemortensen, johnbillion, MikeHansenMe, dd32
See <a href="https://core.trac.wordpress.org/ticket/32429">#32429</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesuserphp">trunk/src/wp-includes/user.php</a></li>
<li><a href="#trunksrcwploginphp">trunk/src/wp-login.php</a></li>
<li><a href="#trunktestsphpunittestsauthphp">trunk/tests/phpunit/tests/auth.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesuserphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/user.php    2015-07-01 03:46:23 UTC (rev 33018)
+++ trunk/src/wp-includes/user.php      2015-07-01 06:32:07 UTC (rev 33019)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2458,18 +2458,42 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_hasher = new PasswordHash( 8, true );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( $wp_hasher->CheckPassword( $key, $row->user_activation_key ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Filter the expiration time of password reset keys.
+        *
+        * @since 4.3.0
+        *
+        * @param int $expiration The expiration time in seconds.
+        */
+       $expiration_duration = apply_filters( 'password_reset_expiration', DAY_IN_SECONDS );
+
+       if ( false !== strpos( $row->user_activation_key, ':' ) ) {
+               list( $pass_request_time, $pass_key ) = explode( ':', $row->user_activation_key, 2 );
+               $expiration_time = $pass_request_time + $expiration_duration;
+       } else {
+               $pass_key = $row->user_activation_key;
+               $expiration_time = false;
+       }
+
+       $hash_is_correct = $wp_hasher->CheckPassword( $key, $pass_key );
+
+       if ( $hash_is_correct && $expiration_time && time() < $expiration_time ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return get_userdata( $row->ID );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        } elseif ( $hash_is_correct && $expiration_time ) {
+               // Key has an expiration time that's passed
+               return new WP_Error( 'expired_key', __( 'Invalid key' ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( $key === $row->user_activation_key ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( hash_equals( $row->user_activation_key, $key ) || ( $hash_is_correct && ! $expiration_time ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $return = new WP_Error( 'expired_key', __( 'Invalid key' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $user_id = $row->ID;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Filter the return value of check_password_reset_key() when an
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * old-style key is used (plain-text key was stored in the database).
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * old-style key is used.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @since 3.7.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @since 3.7.0 Previously plain-text keys were stored in the database.
+                * @since 4.3.0 Previously key hashes were stored without an expiration time.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param WP_Error $return  A WP_Error object denoting an expired key.
</span><span class="cx" style="display: block; padding: 0 10px">                 *                          Return a WP_User object to validate the key.
</span></span></pre></div>
<a id="trunksrcwploginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-login.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-login.php    2015-07-01 03:46:23 UTC (rev 33018)
+++ trunk/src/wp-login.php      2015-07-01 06:32:07 UTC (rev 33019)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -363,7 +363,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                require_once ABSPATH . WPINC . '/class-phpass.php';
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_hasher = new PasswordHash( 8, true );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $hashed = $wp_hasher->HashPassword( $key );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
</ins><span class="cx" style="display: block; padding: 0 10px">         $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user_login ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -528,10 +528,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( isset( $_GET['error'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( 'invalidkey' == $_GET['error'] )
-                       $errors->add( 'invalidkey', __( 'Sorry, that key does not appear to be valid.' ) );
-               elseif ( 'expiredkey' == $_GET['error'] )
-                       $errors->add( 'expiredkey', __( 'Sorry, that key has expired. Please try again.' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( 'invalidkey' == $_GET['error'] ) {
+                       $errors->add( 'invalidkey', __( 'Your password reset link appears to be invalid. Please request a new lnk below.' ) );
+               } elseif ( 'expiredkey' == $_GET['error'] ) {
+                       $errors->add( 'expiredkey', __( 'Your password reset link has expired. Please request a new link below.' ) );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $lostpassword_redirect = ! empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
</span></span></pre></div>
<a id="trunktestsphpunittestsauthphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/auth.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/auth.php        2015-07-01 03:46:23 UTC (rev 33018)
+++ trunk/tests/phpunit/tests/auth.php  2015-07-01 06:32:07 UTC (rev 33019)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,10 +6,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Auth extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px">        var $user_id;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        var $wp_hasher;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function setUp() {
</span><span class="cx" style="display: block; padding: 0 10px">                parent::setUp();
</span><span class="cx" style="display: block; padding: 0 10px">                $this->user_id = $this->factory->user->create();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               require_once ABSPATH . WPINC . '/class-phpass.php';
+               $this->wp_hasher = new PasswordHash( 8, true );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function test_auth_cookie_valid() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -159,4 +163,124 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Password broken by setting it to be too long.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertInstanceOf( 'WP_Error', $user );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 32429
+        */
+       function test_user_activation_key_is_checked() {
+               global $wpdb;
+
+               $key  = wp_generate_password( 20, false );
+               $user = $this->factory->user->create_and_get();
+               $wpdb->update( $wpdb->users, array(
+                       'user_activation_key' => strtotime( '-1 hour' ) . ':' . $this->wp_hasher->HashPassword( $key ),
+               ), array(
+                       'ID' => $user->ID,
+               ) );
+
+               // A valid key should be accepted
+               $check = check_password_reset_key( $key, $user->user_login );
+               $this->assertInstanceOf( 'WP_User', $check );
+               $this->assertSame( $user->ID, $check->ID );
+
+               // An invalid key should be rejected
+               $check = check_password_reset_key( 'key', $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+
+               // An empty key should be rejected
+               $check = check_password_reset_key( '', $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+
+               // A truncated key should be rejected
+               $partial = substr( $key, 0, 10 );
+               $check = check_password_reset_key( $partial, $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+       }
+
+       /**
+        * @ticket 32429
+        */
+       function test_expired_user_activation_key_is_rejected() {
+               global $wpdb;
+
+               $key  = wp_generate_password( 20, false );
+               $user = $this->factory->user->create_and_get();
+               $wpdb->update( $wpdb->users, array(
+                       'user_activation_key' => strtotime( '-48 hours' ) . ':' . $this->wp_hasher->HashPassword( $key ),
+               ), array(
+                       'ID' => $user->ID,
+               ) );
+
+               // An expired but otherwise valid key should be rejected
+               $check = check_password_reset_key( $key, $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+       }
+
+       /**
+        * @ticket 32429
+        */
+       function test_empty_user_activation_key_fails_key_check() {
+               global $wpdb;
+
+               $user = $this->factory->user->create_and_get();
+
+               // An empty user_activation_key should not allow any key to be accepted
+               $check = check_password_reset_key( 'key', $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+
+               // An empty user_activation_key should not allow an empty key to be accepted
+               $check = check_password_reset_key( '', $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+       }
+
+       /**
+        * @ticket 32429
+        */
+       function test_legacy_user_activation_key_is_rejected() {
+               global $wpdb;
+
+               // A legacy user_activation_key is one without the `time()` prefix introduced in WordPress 4.3.
+
+               $key  = wp_generate_password( 20, false );
+               $user = $this->factory->user->create_and_get();
+               $wpdb->update( $wpdb->users, array(
+                       'user_activation_key' => $this->wp_hasher->HashPassword( $key ),
+               ), array(
+                       'ID' => $user->ID,
+               ) );
+
+               // A legacy user_activation_key should not be accepted
+               $check = check_password_reset_key( $key, $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+
+               // An empty key with a legacy user_activation_key should be rejected
+               $check = check_password_reset_key( '', $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+       }
+
+       /**
+        * @ticket 32429
+        * @ticket 24783
+        */
+       function test_plaintext_user_activation_key_is_rejected() {
+               global $wpdb;
+
+               // A plaintext user_activation_key is one stored before hashing was introduced in WordPress 3.7.
+
+               $key  = wp_generate_password( 20, false );
+               $user = $this->factory->user->create_and_get();
+               $wpdb->update( $wpdb->users, array(
+                       'user_activation_key' => $key,
+               ), array(
+                       'ID' => $user->ID,
+               ) );
+
+               // A plaintext user_activation_key should not allow an otherwise valid key to be accepted
+               $check = check_password_reset_key( $key, $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+
+               // A plaintext user_activation_key should not allow an empty key to be accepted
+               $check = check_password_reset_key( '', $user->user_login );
+               $this->assertInstanceOf( 'WP_Error', $check );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>