Fail closed when CSPRNG is unavailable during nonce generation#877
Fail closed when CSPRNG is unavailable during nonce generation#877dknauss wants to merge 2 commits intoWordPress:masterfrom
Conversation
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 WordPress#860 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Pull request overview
This PR hardens Two_Factor_Core::create_login_nonce() by removing the weak entropy fallback used when random_bytes() fails and instead returning false, ensuring nonce generation fails closed when a CSPRNG is unavailable.
Changes:
- Replace the
wp_hash( $user_id . wp_rand() . microtime(), 'nonce' )fallback withreturn falsewhenrandom_bytes()throws. - Preserve existing caller behavior: both in-file call sites already treat a
falsereturn as fatal viawp_die().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
This PR replaces the weak fallback in
create_login_nonce()with a hard failure.The existing fallback uses
wp_hash()with inputs including$user_id,wp_rand(), andmicrotime(). That defensive path dates back to the PHP 5.x /random_compatera. On the plugin’s current minimum PHP version (7.2+),random_bytes()should use the OS CSPRNG and should not fail under normal conditions.If secure randomness is unavailable, failing closed is safer than generating a weaker nonce. Both existing call sites already handle a
falsereturn withwp_die().Change
try { $login_nonce['key'] = bin2hex( random_bytes( 32 ) ); } catch ( Exception $ex ) { - $login_nonce['key'] = wp_hash( $user_id . wp_rand() . microtime(), 'nonce' ); + return false; }