Repository navigation
Fix fatal error when the stored TOTP secret is not base32 - #1038
faisalahammad wants to merge 1 commit into
Conversation
…ling Solid / Kadence Security store an encrypted TOTP secret under the same _two_factor_totp_key meta key. After switching to Two-Factor, entering a code at login or revalidation threw an uncaught base32_decode() exception. validate_code_for_user() now returns false when the stored secret is not base32. TOTP stays available, so the user does not drop to one factor.
|
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.
🟢 Approval recommended
No active review issues are reported for the change addressing fatal errors from invalid stored TOTP secrets.
No open issues 🎉
What changed in this PR
Prevents fatal errors during TOTP verification when a stored secret is not valid base32, addressing the migration scenario reported in #747.
Changes:
- Validates stored secrets before calculating codes, returning
falsefor invalid secrets while preserving lowercase-secret support. - Keeps TOTP available so invalid secrets do not bypass two-factor authentication, with regression tests for both cases.
| File | Description |
|---|---|
tests/providers/class-two-factor-totp.php |
Adds tests for invalid stored secrets, continued TOTP availability, and lowercase secrets. |
providers/class-two-factor-totp.php |
Rejects invalid stored secrets before TOTP calculation can throw. |
Review effort: Balanced
What?
Stops a fatal error when a user's stored TOTP secret is not valid base32.
validate_code_for_user()now rejects the code instead of lettingbase32_decode()throw.Addresses #747 (closed as "not planned", but the fatal error is in Two-Factor and still happens on 0.17.0).
Why?
Solid Security (now Kadence Security) is a fork of this plugin and uses the same meta keys. Since 8.1.7 it encrypts the TOTP secret before saving it to
_two_factor_totp_key, so the stored value looks like$t1$....When a site turns off Kadence Security and turns on Two-Factor:
_two_factor_enabled_providersalready containsTwo_Factor_Totp, so the user counts as having 2FA. The Profile page asks them to revalidate.is_available_for_user()only checks that the secret is not empty, so the TOTP prompt is shown.calc_totp()→base32_decode(), which throwsException: Invalid characters in the base32 string.Nothing catches it, sowp-login.php?action=revalidate_2fa(and normal 2FA login) shows a critical error.Real report, WordPress 7.1.4, PHP 8.5, Two-Factor 0.17.0:
How?
validate_code_for_user()checks the stored secret with the existingis_valid_key_format()before computing codes, and returnsfalseif the check fails. The secret is uppercased first becausebase32_decode()accepts lowercase, so lowercase secrets keep working.is_available_for_user()is deliberately unchanged. If an unreadable secret made TOTP "unavailable", a user whose only method is TOTP would fall back to password-only login. With this change the check fails closed: the user sees "Invalid verification code." and can use another enabled method, or an admin can reset TOTP (for example withwp two-factor disable).Tests added:
validate_code_for_user()returnsfalsewithout throwing.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus
Used for: Tracing the root cause, writing the fix and tests, and reproducing the bug in wp-env. I reviewed and tested the change.
Testing Instructions
Invalid characters in the base32 string). After: "Invalid verification code." is shown on the login form.Automated:
npm testpasses on both single site and multisite (355 tests). PHPStan and PHPCS report no errors on the changed files.Screenshots or screencast
Fatal error: Uncaught Exception: Invalid characters in the base32 string.ERROR: Invalid verification code.Changelog Entry