This commit is contained in:
Javier Casares 2026-11-04 14:36:03 +00:00
commit 1ff471299e

View file

@ -85,9 +85,21 @@ class Login_Form_Manager {
private const FIELD_METHOD = 'robotstxt_2fa_method';
/**
* Transient key prefix for pending-stage tokens.
* Option key storing the pending-stage token map.
*
* Maps token => array( user ID, expiry ). Persistent storage instead of a
* transient: external object caches can evict transients at any time,
* which would kill live verification stages and lock users out with
* "We could not determine which account you are trying to access."
*/
private const PENDING_TRANSIENT_PREFIX = 'robotstxt_2fa_stage_';
private const PENDING_OPTION = 'robotstxt_2fa_pending_stages';
/**
* Maximum concurrent pending-stage tokens kept in the map.
*
* The map is pruned on every write; the cap bounds its size.
*/
private const PENDING_MAX_TOKENS = 20;
/**
* Pending-stage token TTL in seconds — matches WP nonce lifetime (24 h).
@ -649,7 +661,7 @@ class Login_Form_Manager {
$authenticated_user = $this->resolve_pending_user();
if ( null === $authenticated_user ) {
return new \WP_Error( 'robotstxt_2fa_unknown_user', __( 'We could not determine which account you are trying to access.', 'robotstxt-2fa' ) );
return new \WP_Error( 'robotstxt_2fa_unknown_user', __( 'Your verification session expired or could not be found. Please sign in again.', 'robotstxt-2fa' ) );
}
// Brute-force: check if the account is currently locked out.
@ -753,7 +765,7 @@ class Login_Form_Manager {
// Consume the pending token so it cannot be replayed.
$pending_token = $this->get_requested_pending_token();
if ( '' !== $pending_token ) {
delete_transient( self::PENDING_TRANSIENT_PREFIX . $pending_token );
$this->consume_pending_token( $pending_token );
}
$this->user_settings_repository->record_last_verification(
@ -1333,21 +1345,99 @@ class Login_Form_Manager {
/**
* Issue an opaque pending-stage token that maps to a user ID.
*
* Stores the user ID in a short-lived transient so the login username
* is never transmitted in the URL query string.
* Stores the token in a persistent option map (see PENDING_OPTION) so the
* login username is never transmitted in the URL query string and object
* cache eviction cannot kill a live stage.
*
* @since 1.3.0
* @since 1.6.8 Stored in a persistent option map instead of a transient.
*
* @param int $user_id User identifier.
*
* @return string 32-character alphanumeric token.
*/
private function issue_pending_token( int $user_id ): string {
$tokens = $this->get_pending_token_map();
$now = time();
foreach ( $tokens as $existing_token => $data ) {
if ( $data['e'] <= $now ) {
unset( $tokens[ $existing_token ] );
}
}
$token = wp_generate_password( 32, false, false );
set_transient( self::PENDING_TRANSIENT_PREFIX . $token, $user_id, self::PENDING_TTL );
$tokens[ $token ] = array(
'u' => $user_id,
'e' => $now + self::PENDING_TTL,
);
if ( count( $tokens ) > self::PENDING_MAX_TOKENS ) {
uasort(
$tokens,
static function ( array $a, array $b ): int {
return (int) $a['e'] <=> (int) $b['e'];
}
);
$tokens = array_slice( $tokens, -self::PENDING_MAX_TOKENS, null, true );
}
update_option( self::PENDING_OPTION, $tokens, false );
return $token;
}
/**
* Retrieve the raw pending-stage token map stored in the database.
*
* @since 1.6.8
*
* @return array<string, array{u: int, e: int}> Token => user ID + expiry.
*/
private function get_pending_token_map(): array {
$raw = get_option( self::PENDING_OPTION, array() );
if ( ! is_array( $raw ) ) {
return array();
}
$tokens = array();
foreach ( $raw as $token => $data ) {
if ( is_string( $token ) && is_array( $data ) && isset( $data['u'], $data['e'] ) && is_numeric( $data['u'] ) && is_numeric( $data['e'] ) ) {
$tokens[ $token ] = array(
'u' => (int) $data['u'],
'e' => (int) $data['e'],
);
}
}
return $tokens;
}
/**
* Remove a consumed pending-stage token from the map.
*
* @since 1.6.8
*
* @param string $token Token to consume.
*
* @return void
*/
private function consume_pending_token( string $token ): void {
$tokens = $this->get_pending_token_map();
if ( ! isset( $tokens[ $token ] ) ) {
return;
}
unset( $tokens[ $token ] );
update_option( self::PENDING_OPTION, $tokens, false );
}
/**
* Retrieve the raw pending-stage token from the current request.
*
@ -1373,6 +1463,7 @@ class Login_Form_Manager {
* Resolve the pending-stage token to its associated WP_User.
*
* @since 1.3.0
* @since 1.6.8 Reads the persistent token map instead of a transient.
*
* @return \WP_User|null Null when no valid token is present.
*/
@ -1383,13 +1474,13 @@ class Login_Form_Manager {
return null;
}
$user_id_raw = get_transient( self::PENDING_TRANSIENT_PREFIX . $token );
$data = $this->get_pending_token_map()[ $token ] ?? null;
if ( ! is_numeric( $user_id_raw ) ) {
if ( null === $data || $data['e'] <= time() ) {
return null;
}
$user = get_user_by( 'ID', (int) $user_id_raw );
$user = get_user_by( 'ID', $data['u'] );
return $user instanceof \WP_User ? $user : null;
}