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

View file

@ -65,9 +65,9 @@ class Login_Form_Manager {
private const QUERY_SWITCH = 'robotstxt-2fa-switch';
/**
* Query argument carrying the pending login username.
* Query argument carrying the opaque pending-stage token (replaces user_login in URLs).
*/
private const QUERY_LOGIN = 'robotstxt-2fa-login';
private const QUERY_PENDING = 'robotstxt-2fa-pending';
/**
* Form field containing the active stage marker.
@ -75,15 +75,25 @@ class Login_Form_Manager {
private const FIELD_STAGE = 'robotstxt_2fa_stage';
/**
* Form field containing the pending login username.
* Form field carrying the opaque pending-stage token.
*/
private const FIELD_LOGIN = 'robotstxt_2fa_login';
private const FIELD_PENDING = 'robotstxt_2fa_pending';
/**
* Form field specifying the verification method in use.
*/
private const FIELD_METHOD = 'robotstxt_2fa_method';
/**
* Transient key prefix for pending-stage tokens.
*/
private const PENDING_TRANSIENT_PREFIX = 'robotstxt_2fa_stage_';
/**
* Pending-stage token TTL in seconds — matches WP nonce lifetime (24 h).
*/
private const PENDING_TTL = DAY_IN_SECONDS;
/**
* Form field carrying the nonce that validates the verification stage.
*/
@ -164,20 +174,16 @@ class Login_Form_Manager {
/**
* Handle verification method switches requested on the login stage.
*
* @since 1.0.0
*
* @return void
*/
private function process_method_switch(): void {
$method = $this->get_requested_method();
$login = $this->get_requested_login();
$user = $this->resolve_pending_user();
if ( '' === $login ) {
return;
}
$user = get_user_by( 'login', $login );
if ( ! $user instanceof \WP_User ) {
if ( null === $user ) {
return;
}
@ -232,6 +238,8 @@ class Login_Form_Manager {
/**
* Render custom fields on the login form.
*
* @since 1.0.0
*
* @return void
*/
public function render_form_fields(): void {
@ -239,21 +247,18 @@ class Login_Form_Manager {
return;
}
$login = $this->get_requested_login();
$method = $this->get_requested_method();
$code_length = $this->get_method_code_length( $method );
$alternates = array();
$pending_user = $this->resolve_pending_user();
$pending_token = $this->get_requested_pending_token();
$method = $this->get_requested_method();
$code_length = $this->get_method_code_length( $method );
$alternates = array();
if ( '' !== $login ) {
$user = get_user_by( 'login', $login );
if ( $user instanceof WP_User ) {
$alternates = $this->get_alternate_method_links( $user, $method );
}
if ( $pending_user instanceof WP_User ) {
$alternates = $this->get_alternate_method_links( $pending_user, $method );
}
if ( '' !== $login ) {
printf( '<input type="hidden" name="%1$s" value="%2$s" />', esc_attr( self::FIELD_LOGIN ), esc_attr( $login ) );
if ( '' !== $pending_token ) {
printf( '<input type="hidden" name="%1$s" value="%2$s" />', esc_attr( self::FIELD_PENDING ), esc_attr( $pending_token ) );
}
printf( '<input type="hidden" name="%1$s" value="%2$s" />', esc_attr( self::FIELD_METHOD ), esc_attr( $method ) );
@ -393,6 +398,8 @@ class Login_Form_Manager {
/**
* Display a contextual login message during the verification stage.
*
* @since 1.0.0
*
* @param string $message Existing login form message HTML.
*
* @return string
@ -402,15 +409,9 @@ class Login_Form_Manager {
return $message;
}
$login = $this->get_requested_login();
$user = $this->resolve_pending_user();
$method = $this->get_requested_method();
if ( '' === $login ) {
return $message;
}
$user = get_user_by( 'login', $login );
if ( ! $user instanceof WP_User ) {
return $message;
}
@ -443,6 +444,8 @@ class Login_Form_Manager {
/**
* Output helper scripts used during the verification stage.
*
* @since 1.0.0
*
* @return void
*/
public function render_stage_scripts(): void {
@ -450,17 +453,14 @@ class Login_Form_Manager {
return;
}
$login = $this->get_requested_login();
$method = $this->get_requested_method();
$sent_at = 0;
$pending_user = $this->resolve_pending_user();
$method = $this->get_requested_method();
$login = $pending_user instanceof \WP_User ? $pending_user->user_login : '';
$sent_at = 0;
if ( 'email' === $method && '' !== $login ) {
$user = get_user_by( 'login', $login );
if ( $user instanceof \WP_User ) {
$challenge = $this->user_settings_repository->get_email_challenge( $user->ID );
$sent_at = $challenge['sent_at'];
}
if ( 'email' === $method && $pending_user instanceof \WP_User ) {
$challenge = $this->user_settings_repository->get_email_challenge( $pending_user->ID );
$sent_at = $challenge['sent_at'];
}
?>
<script>
@ -573,22 +573,12 @@ class Login_Form_Manager {
return new \WP_Error( 'robotstxt_2fa_invalid_nonce', __( 'The verification request expired. Please sign in again to request a new code.', 'robotstxt-2fa' ) );
}
$login = $this->get_requested_login();
$authenticated_user = $this->resolve_pending_user();
if ( '' === $login && '' !== $username ) {
$login = sanitize_user( $username, true );
}
if ( '' === $login ) {
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' ) );
}
$authenticated_user = get_user_by( 'login', $login );
if ( ! $authenticated_user instanceof \WP_User ) {
return new \WP_Error( 'robotstxt_2fa_unknown_user', __( 'We could not load the account requesting verification.', 'robotstxt-2fa' ) );
}
// Brute-force: check if the account is currently locked out.
$lock_key = 'robotstxt_2fa_locked_' . $authenticated_user->ID;
$locked = get_transient( $lock_key );
@ -687,6 +677,12 @@ class Login_Form_Manager {
// Brute-force: clear the attempt counter on success.
delete_transient( 'robotstxt_2fa_attempts_' . $authenticated_user->ID );
// 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->user_settings_repository->record_last_verification(
$authenticated_user->ID,
$method,
@ -754,6 +750,12 @@ class Login_Form_Manager {
return $user;
}
// Temporary bypass: administrators can grant a time-limited bypass via WP-CLI.
$bypass_until = get_transient( 'robotstxt_2fa_bypass_' . $user->ID );
if ( false !== $bypass_until && is_numeric( $bypass_until ) && (int) $bypass_until > time() ) {
return $user;
}
$required_methods = $this->config->get_required_methods_for_user( $user );
// Grace period: if forced but no methods configured yet, check whether the user is within their grace window.
@ -808,7 +810,7 @@ class Login_Form_Manager {
$query_args = array(
self::QUERY_STAGE => self::STAGE_VERIFY,
self::QUERY_METHOD => $method,
self::QUERY_LOGIN => sanitize_user( $user->user_login, true ),
self::QUERY_PENDING => $this->issue_pending_token( $user->ID ),
self::QUERY_STAGE_TOKEN => $stage_token,
);
@ -848,15 +850,9 @@ class Login_Form_Manager {
return;
}
$login = $this->get_requested_login();
$user = $this->resolve_pending_user();
if ( '' === $login ) {
return;
}
$user = get_user_by( 'login', $login );
if ( ! $user instanceof \WP_User ) {
if ( null === $user ) {
return;
}
@ -1230,24 +1226,67 @@ class Login_Form_Manager {
}
/**
* Retrieve the login identifier carried across requests.
* 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.
*
* @since 1.3.0
*
* @param int $user_id User identifier.
*
* @return string 32-character alphanumeric token.
*/
private function issue_pending_token( int $user_id ): string {
$token = wp_generate_password( 32, false, false );
set_transient( self::PENDING_TRANSIENT_PREFIX . $token, $user_id, self::PENDING_TTL );
return $token;
}
/**
* Retrieve the raw pending-stage token from the current request.
*
* @since 1.3.0
*
* @return string
*/
private function get_requested_login(): string {
$login = '';
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Login identifier helper; nonce verified in calling context.
if ( isset( $_REQUEST[ self::FIELD_LOGIN ] ) && is_string( $_REQUEST[ self::FIELD_LOGIN ] ) ) {
$login = sanitize_user( wp_unslash( $_REQUEST[ self::FIELD_LOGIN ] ), true );
private function get_requested_pending_token(): string {
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Opaque token helper; token-to-user mapping validated in resolve_pending_user().
if ( isset( $_REQUEST[ self::FIELD_PENDING ] ) && is_string( $_REQUEST[ self::FIELD_PENDING ] ) ) {
return sanitize_text_field( wp_unslash( $_REQUEST[ self::FIELD_PENDING ] ) );
}
if ( '' === $login && isset( $_REQUEST[ self::QUERY_LOGIN ] ) && is_string( $_REQUEST[ self::QUERY_LOGIN ] ) ) {
$login = sanitize_user( wp_unslash( $_REQUEST[ self::QUERY_LOGIN ] ), true );
if ( isset( $_REQUEST[ self::QUERY_PENDING ] ) && is_string( $_REQUEST[ self::QUERY_PENDING ] ) ) {
return sanitize_text_field( wp_unslash( $_REQUEST[ self::QUERY_PENDING ] ) );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
return $login;
return '';
}
/**
* Resolve the pending-stage token to its associated WP_User.
*
* @since 1.3.0
*
* @return \WP_User|null Null when no valid token is present.
*/
private function resolve_pending_user(): ?\WP_User {
$token = $this->get_requested_pending_token();
if ( '' === $token ) {
return null;
}
$user_id_raw = get_transient( self::PENDING_TRANSIENT_PREFIX . $token );
if ( ! is_numeric( $user_id_raw ) ) {
return null;
}
$user = get_user_by( 'ID', (int) $user_id_raw );
return $user instanceof \WP_User ? $user : null;
}
/**
@ -1328,10 +1367,10 @@ class Login_Form_Manager {
self::QUERY_METHOD => $this->get_requested_method(),
);
$login = $this->get_requested_login();
$pending_token = $this->get_requested_pending_token();
if ( '' !== $login ) {
$args[ self::QUERY_LOGIN ] = $login;
if ( '' !== $pending_token ) {
$args[ self::QUERY_PENDING ] = $pending_token;
}
$stage_token = $this->get_requested_stage_token();