This commit is contained in:
Javier Casares 2026-11-04 14:36:03 +00:00
commit 1ff471299e
8 changed files with 230 additions and 24 deletions

View file

@ -1,5 +1,29 @@
== Changelog ==
= 1.6.8 =
_Release date: 2026-09-21_
**Added**
* Administrator visibility when email delivery fails: failed verification-code deliveries record a marker and administrators see a persistent, self-clearing notice ("2FA: email delivery is failing…") in wp-admin and the network admin, since forced-email users cannot sign in while delivery is broken. The notice disappears automatically once a code is delivered successfully.
**Fixed**
* "We could not determine which account you are trying to access" during verification: the pending-stage token was stored in a transient, which external object caches (Redis/Memcached) can evict at any time, killing live verification sessions. Tokens now live in a persistent option map (capped, pruned, consumed on success, cleaned on uninstall) and the error message now says the session expired and asks the user to sign in again.
**Compatibility**
* WordPress: 5.6 – 7.1
* PHP: 8.0 – 8.5
**Tests**
* PHP Coding Standards: PHP_CodeSniffer 3.13.6 / WPCS 3.4.1 — 0 errors
* PHPStan: level 9 — 0 errors
* PHPCompatibility: 8.0–8.5 — 0 issues
* PHPUnit: 9.6.36 — 110 tests, 220 assertions
= 1.6.7 =
_Release date: 2026-09-17_

View file

@ -7,6 +7,7 @@
namespace Robotstxt\TwoFA;
use Robotstxt\TwoFA\Login\Email_Code_Sender;
use Robotstxt\TwoFA\User\Two_Factor_Config;
if ( ! defined( 'ABSPATH' ) ) {
@ -61,6 +62,42 @@ class Email_Notifications {
add_action( self::DIGEST_CRON_HOOK, array( $this, 'send_digest' ) );
add_filter( 'cron_schedules', array( $this, 'add_cron_schedules' ) );
add_action( 'init', array( $this, 'maybe_schedule_digest' ) );
add_action( 'admin_notices', array( $this, 'render_delivery_failure_notice' ) );
add_action( 'network_admin_notices', array( $this, 'render_delivery_failure_notice' ) );
}
/**
* Warn administrators while verification-code delivery is failing.
*
* When a role forces email verification and wp_mail() fails, users are
* locked out at the login challenge with an error that points them at
* their administrator — while the real fault is the site's mail
* configuration. The failure marker is written by
* {@see Email_Code_Sender} on failed delivery and removed automatically
* once a code is delivered successfully.
*
* @since 1.6.8
*
* @return void
*/
public function render_delivery_failure_notice(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$failure = get_option( Email_Code_Sender::MAIL_FAILURE_OPTION, array() );
if ( ! is_array( $failure ) || empty( $failure['at'] ) ) {
return;
}
?>
<div class="notice notice-error robotstxt-2fa-mail-failure">
<p>
<strong><?php esc_html_e( '2FA (by ROBOTSTXT): email delivery is failing.', 'robotstxt-2fa' ); ?></strong>
<?php esc_html_e( 'Verification code emails cannot be sent, so users required to sign in with an email code cannot access the site. Check the site mail or SMTP configuration — this notice disappears automatically once a code is delivered successfully.', 'robotstxt-2fa' ); ?>
</p>
</div>
<?php
}
/**

View file

@ -17,6 +17,13 @@ if ( ! defined( 'ABSPATH' ) ) {
* Handles generation and delivery of email-based verification codes.
*/
class Email_Code_Sender {
/**
* Option key recording the last failed delivery attempt. Kept while mail
* delivery fails so administrators can see why forced-email users cannot
* sign in; cleared automatically once a code is delivered successfully.
*/
public const MAIL_FAILURE_OPTION = 'robotstxt_2fa_last_mail_failure';
/**
* Default length of the generated numeric code.
*/
@ -187,14 +194,57 @@ class Email_Code_Sender {
$headers = array( 'Content-Type: text/plain; charset=UTF-8' );
if ( ! wp_mail( $user->user_email, $email_subject, $message, $headers ) ) {
$this->record_mail_failure( $user->user_email );
return false;
}
$this->clear_mail_failure();
$this->user_settings_repository->store_email_challenge( $user->ID, $code, $expires );
return true;
}
/**
* Record a failed verification-code delivery for administrator visibility.
*
* When a role forces email verification, a failing wp_mail() locks users
* out of their accounts with no site-side signal. Store the failure so an
* admin notice can explain the root cause.
*
* @since 1.6.8
*
* @param string $to Intended recipient.
*
* @return void
*/
private function record_mail_failure( string $to ): void {
$existing = get_option( self::MAIL_FAILURE_OPTION, array() );
$record = is_array( $existing ) ? $existing : array();
update_option(
self::MAIL_FAILURE_OPTION,
array(
'at' => time(),
'to' => $to,
'count' => ( isset( $record['count'] ) && is_numeric( $record['count'] ) ? (int) $record['count'] : 0 ) + 1,
),
false
);
}
/**
* Clear the delivery failure marker after a successful delivery.
*
* @since 1.6.8
*
* @return void
*/
private function clear_mail_failure(): void {
delete_option( self::MAIL_FAILURE_OPTION );
}
/**
* Verify a submitted code against the stored challenge.
*

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;
}

View file

@ -4,7 +4,7 @@ Tags: security, two-factor authentication, login, otp
Requires at least: 5.6
Tested up to: 7.0
Requires PHP: 8.0
Stable tag: 1.6.7
Stable tag: 1.6.8
License: GPLv3 or later
License URI: https://www.gnu.org/licenses/gpl-3.0.html
@ -59,6 +59,18 @@ Yes. Activate the plugin at the network level. Network administrators can set an
== Changelog ==
= 1.6.8 =
_Release date: 2026-09-21_
**Added**
* Administrator visibility when email delivery fails: failed verification-code deliveries record a marker and administrators see a persistent, self-clearing notice ("2FA: email delivery is failing…") in wp-admin and the network admin, since forced-email users cannot sign in while delivery is broken.
**Fixed**
* "We could not determine which account you are trying to access" during verification: the pending-stage token was stored in a transient, which external object caches (Redis/Memcached) can evict at any time, killing live verification sessions. Tokens now live in a persistent option map and the error message asks the user to sign in again.
= 1.6.7 =
_Release date: 2026-09-17_
@ -75,15 +87,6 @@ _Release date: 2026-09-17_
* Forced-enrollment warning on the profile 2FA section: when a user's role enforces 2FA but they have no verification method configured yet, the 2FA section of their profile (wp-admin and the frontend shortcode) opens with a warning notice asking them to activate at least one method. While a grace period is active, the notice appends the remaining days.
= 1.6.5 =
_Release date: 2026-09-17_
**Fixed**
* Fatal error on the login screen with newer WordPress versions: the `login_message` filter can deliver null when no message is set (observed on WordPress 7.x with PHP 8.4, TypeError on a plain visit to wp-login.php). The message callback now accepts `string|null` and coalesces null to an empty string.
* Hardened all externally-fed filter callbacks against null payloads: the four `authenticate` callbacks now accept `string|null` credentials (custom REST/SSO endpoints are known to apply the filter with null), and the `wp_redirect` filter callback coalesces a null location.
= Previous versions =
For the full changelog see the [changelog](https://www.robotstxt.software/plugins/robotstxt-2fa/) page.

View file

@ -4,7 +4,7 @@
* Plugin URI: https://www.robotstxt.software/plugins/robotstxt-2fa/
* Update URI: https://www.robotstxt.software/plugins/robotstxt-2fa/
* Description: Adds two-factor authentication to the WordPress login flow.
* Version: 1.6.7
* Version: 1.6.8
* Author: ROBOTSTXT
* Author URI: https://www.robotstxt.software/
* Text Domain: robotstxt-2fa
@ -25,7 +25,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
if ( ! defined( 'ROBOTSTXT_2FA_VERSION' ) ) {
define( 'ROBOTSTXT_2FA_VERSION', '1.6.7' );
define( 'ROBOTSTXT_2FA_VERSION', '1.6.8' );
}
if ( ! defined( 'ROBOTSTXT_2FA_FILE' ) ) {

View file

@ -45,6 +45,7 @@ if ( ! robotstxt_2fa_should_delete_data() ) {
delete_option( 'robotstxt_2fa_settings' );
delete_option( 'robotstxt_2fa_failed_log' );
delete_option( 'robotstxt_2fa_geoip_status' );
delete_option( 'robotstxt_2fa_pending_stages' );
delete_site_option( 'robotstxt_2fa_settings' );
// Clear the GeoIP auto-download cron event.

View file

@ -3,7 +3,7 @@
'name' => 'robotstxt/robotstxt-2fa',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '2b6e97d03ffbc5f7734bbd30413c979f50de3d15',
'reference' => '70737383aa72a4fb75363c9d084ec3a7112f8d3b',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
@ -40,7 +40,7 @@
'robotstxt/robotstxt-2fa' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => '2b6e97d03ffbc5f7734bbd30413c979f50de3d15',
'reference' => '70737383aa72a4fb75363c9d084ec3a7112f8d3b',
'type' => 'wordpress-plugin',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),