Skip to content

Commit 064dd7a

Browse files
authored
Use hash_equals() for email confirmation, approval and denial token comparisons (#1406)
* Use hash_equals() for email confirmation, approval and denial token comparisons * Return early when the email token has no second chunk to decrypt
1 parent 75c940d commit 064dd7a

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

includes/class-ur-email-approval.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static function approve_user_after_verification() {
4949

5050
$saved_token = get_user_meta( $user_id, 'ur_confirm_approval_token', true );
5151

52-
if ( $ur_approval_token_raw === $saved_token ) {
52+
if ( hash_equals( (string) $saved_token, $ur_approval_token_raw ) ) {
5353
$user_manager = new UR_Admin_User_Manager( $user_id );
5454
$user_manager->save_status( UR_Admin_User_Manager::APPROVED, true );
5555

@@ -94,7 +94,7 @@ public static function deny_user_after_verification() {
9494

9595
$saved_token = get_user_meta( $user_id, 'ur_confirm_denial_token', true );
9696

97-
if ( $ur_denial_token_raw === $saved_token ) {
97+
if ( hash_equals( (string) $saved_token, $ur_denial_token_raw ) ) {
9898
$user_manager = new UR_Admin_User_Manager( $user_id );
9999
$user_manager->save_status( UR_Admin_User_Manager::DENIED, true );
100100

includes/class-ur-email-confirmation.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,14 @@ public function check_token_before_authenticate() {
213213
if ( ! isset( $_GET['ur_token'] ) || empty( $_GET['ur_token'] ) ) {
214214
return;
215215
} else {
216-
$ur_token = str_split( sanitize_text_field( wp_unslash( $_GET['ur_token'] ) ), 50 );
216+
$ur_token_raw = sanitize_text_field( wp_unslash( $_GET['ur_token'] ) );
217+
$ur_token = str_split( $ur_token_raw, 50 );
218+
219+
// A token of 50 characters or fewer has no second chunk, so there is nothing to decrypt.
220+
if ( count( $ur_token ) < 2 ) {
221+
return;
222+
}
223+
217224
$token_string = $ur_token[1];
218225

219226
if ( 2 < count( $ur_token ) ) {
@@ -234,7 +241,7 @@ public function check_token_before_authenticate() {
234241
// Check if the token matches the token value stored in db.
235242
$login_option = ur_get_user_login_option( $user_id );
236243

237-
if ( $user_token === $_GET['ur_token'] && ( 'email_confirmation' === $login_option || 'admin_approval_after_email_confirmation' === $login_option ) ) {
244+
if ( hash_equals( (string) $user_token, $ur_token_raw ) && ( 'email_confirmation' === $login_option || 'admin_approval_after_email_confirmation' === $login_option ) ) {
238245
$token_expiration_duration = 24 * 60 * 60;
239246
/**
240247
* Filter hook to modify the token expiration duration.

0 commit comments

Comments
 (0)