Skip to content

Commit eb7e9aa

Browse files
committed
fix: bound REST TOTP confirmation codes
1 parent 1940948 commit eb7e9aa

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

providers/class-two-factor-totp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function rest_setup_totp( $request ) {
283283
return new WP_Error( 'invalid_key', __( 'Invalid Two Factor Authentication secret key.', 'two-factor' ), array( 'status' => 400 ) );
284284
}
285285

286-
if ( ! $this->is_valid_authcode( $key, $code ) ) {
286+
if ( ! preg_match( '/^[0-9]{' . self::DEFAULT_DIGIT_COUNT . '}$/', $code ) || ! $this->is_valid_authcode( $key, $code ) ) {
287287
return new WP_Error( 'invalid_key_code', __( 'Invalid Two Factor Authentication code.', 'two-factor' ), array( 'status' => 400 ) );
288288
}
289289

tests/providers/class-two-factor-totp-rest-api.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ public function test_user_two_factor_rest_set_key_bad_auth_code() {
142142
$this->assertFalse( self::$provider->is_available_for_user( wp_get_current_user() ) );
143143
}
144144

145+
/**
146+
* Verify oversized auth codes are rejected before TOTP calculation.
147+
*
148+
* @ticket 936
149+
* @covers Two_Factor_Totp::rest_setup_totp
150+
*/
151+
public function test_user_two_factor_rest_rejects_oversized_auth_code() {
152+
wp_set_current_user( self::$admin_id );
153+
154+
$request = new WP_REST_Request( 'POST', '/' . Two_Factor_Core::REST_NAMESPACE . '/totp' );
155+
$request->set_body_params(
156+
array(
157+
'user_id' => self::$admin_id,
158+
'key' => self::$provider->generate_key(),
159+
'code' => str_repeat( '1', 1000 ),
160+
)
161+
);
162+
163+
$response = rest_do_request( $request );
164+
165+
$this->assertErrorResponse( 'invalid_key_code', $response, 400 );
166+
$this->assertFalse( self::$provider->is_available_for_user( wp_get_current_user() ) );
167+
}
168+
145169
/**
146170
* Verify setting up TOTP with an authcode.
147171
*

0 commit comments

Comments
 (0)