Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure there are no blank spaces or line breaks around the token #382

Merged
merged 3 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions providers/class-two-factor-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function authentication_page( $user ) {
<p><?php esc_html_e( 'A verification code has been sent to the email address associated with your account.', 'two-factor' ); ?></p>
<p>
<label for="authcode"><?php esc_html_e( 'Verification Code:', 'two-factor' ); ?></label>
<input type="tel" name="two-factor-email-code" id="authcode" class="input" value="" size="20" pattern="[0-9]*" />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow all kinds of inputs to avoid blocking users as they copy codes with wrapping empty spaces or line breaks from their email clients.

<input type="tel" name="two-factor-email-code" id="authcode" class="input" value="" size="20" />
<?php submit_button( __( 'Log In', 'two-factor' ) ); ?>
</p>
<p class="two-factor-email-resend">
Expand Down Expand Up @@ -317,7 +317,10 @@ public function validate_authentication( $user ) {
return false;
}

return $this->validate_token( $user->ID, $_REQUEST['two-factor-email-code'] );
// Ensure there are no spaces or line breaks around the code.
$code = trim( sanitize_text_field( $_REQUEST['two-factor-email-code'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, handled by the core method already.

return $this->validate_token( $user->ID, $code );
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/providers/class-two-factor-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ public function test_validate_authentication() {
unset( $_REQUEST['two-factor-email-code'] );
}

/**
* Can strip away blank spaces and new line characters in code input.
*
* @covers Two_Factor_Email::validate_authentication
*/
public function test_validate_authentication_code_with_spaces() {
$user = new WP_User( $this->factory->user->create() );

$token = $this->provider->generate_token( $user->ID );
$_REQUEST['two-factor-email-code'] = sprintf( ' %s ', $token );

$this->assertTrue( $this->provider->validate_authentication( $user ) );

unset( $_REQUEST['two-factor-email-code'] );
}

/**
* Verify that availability returns true.
*
Expand Down