From 7c38675141abce1f71cec91bca93684910089448 Mon Sep 17 00:00:00 2001 From: Dan Knauss Date: Sun, 12 Apr 2026 12:09:33 -0600 Subject: [PATCH] Fail closed when CSPRNG is unavailable during nonce generation Replace the weak nonce fallback in create_login_nonce() with a hard failure. The fallback used wp_hash() with predictable inputs (user_id, wp_rand, microtime) and was defensive code from the PHP 5.x era. On PHP 7+ (the plugin minimum is 7.2), random_bytes() uses OS-level CSPRNG sources that do not fail under normal conditions. If the CSPRNG is broken, generating a weak nonce is worse than refusing to proceed. Both callers already handle the false return with wp_die(). Closes #860 Co-Authored-By: Claude Opus 4.6 --- class-two-factor-core.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class-two-factor-core.php b/class-two-factor-core.php index bcd89301..795e9099 100644 --- a/class-two-factor-core.php +++ b/class-two-factor-core.php @@ -1208,7 +1208,7 @@ public static function create_login_nonce( $user_id ) { try { $login_nonce['key'] = bin2hex( random_bytes( 32 ) ); } catch ( Exception $ex ) { - $login_nonce['key'] = wp_hash( $user_id . wp_rand() . microtime(), 'nonce' ); + return false; } // Store the nonce hashed to avoid leaking it via database access.