This commit is contained in:
Javier Casares 2026-06-05 18:39:14 +00:00
commit 8e9c6bc472

View file

@ -102,6 +102,11 @@ class OTP_Manager {
/**
* Verify an OTP code against the stored secret.
*
* Accepted codes are recorded in a short-lived transient so the same
* counter step cannot be replayed within the ±1 time window.
*
* @since 1.0.0
*
* @param WP_User $user User attempting to authenticate.
* @param string $code Submitted verification code.
*
@ -131,9 +136,23 @@ class OTP_Manager {
for ( $offset = -1; $offset <= 1; $offset++ ) {
$otp = $this->generate_otp_for_counter( $binary_secret, $current_step + $offset );
if ( hash_equals( $otp, $code ) ) {
return true;
if ( ! hash_equals( $otp, $code ) ) {
continue;
}
// Replay guard: reject if this counter step was already accepted.
$step = $current_step + $offset;
$used_key = 'robotstxt_2fa_otp_used_' . $user->ID;
$last = get_transient( $used_key );
if ( false !== $last && is_numeric( $last ) && (int) $last === $step ) {
return false;
}
// Record the accepted step for the duration of 3 time windows (±1 + current).
set_transient( $used_key, $step, 3 * self::TIME_STEP );
return true;
}
return false;