fix(auth): verify TOTP session-lessly in dual-factor gate#1394
Merged
Conversation
requireDualFactor called auth.api.verifyTOTP, which requires either an active Better Auth session or the TWO_FACTOR_COOKIE_NAME cookie. The OAuth-MFA finalize path runs after interceptOauthCallback has destroyed the session and leaves only the pending_oauth_mfa cookie, so verifyTOTP threw for every request and every OAuth-with-TOTP sign-in failed with "Invalid authenticator code" regardless of the code submitted. Live in prod since 2026-05-26 22:17 UTC (KEEP-619 PR #1355). Replace the verifyTOTP call with a direct read of two_factor.secret plus verifyUserTotp() — the same session-less primitive that strict-signin already uses in production. Same RFC 6238 window, same constant-time compare, no behavior change for the 15 callers that already have a session. Test: tests/unit/dual-factor.test.ts covers the regression case (valid TOTP + email OTP without a session ⇒ ok=true) plus mfa_code_invalid on wrong code, mfa_code_invalid on missing two_factor row (don't leak enrollment state), email_code_invalid on missing verifications row, and factors_required on missing codes. Refs KEEP-619.
Three small follow-ups from the multi-agent review on PR #1394: - Refresh the requireDualFactor docstring; it still claimed TOTP was verified via Better Auth's verifyTOTP endpoint after the fix moved that off to verifyUserTotp. - Assert resetDualFactor is called on the happy path. Without this, the rate-limit counter could silently stop clearing on success and the suite would still go green. - The "no two_factor row" test was passing a live TOTP code that was never reached; replace with "000000" so the test name and the input match what is actually being asserted.
🧹 PR Environment Cleaned UpThe PR environment has been successfully deleted. Deleted Resources:
All resources have been cleaned up and will no longer incur costs. |
ℹ️ No PR Environment to Clean UpNo PR environment was found for this PR. This is expected if:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
requireDualFactorcalledauth.api.verifyTOTP, which requires either an active Better Auth session or theTWO_FACTOR_COOKIE_NAMEcookie. The OAuth-MFA finalize path runs afterinterceptOauthCallbackhas destroyed the session and leaves onlypending_oauth_mfa, so verifyTOTP threw on every request and every OAuth user with TOTP enrolled got "Invalid authenticator code" regardless of the code they submitted.auth.api.verifyTOTPfor a direct read oftwo_factor.secretplusverifyUserTotp()— the same session-less primitive thatstrict-signinalready uses in production atapp/api/auth/strict-signin/route.ts:187. Same RFC 6238 window, same constant-time compare, no behavior change for the 15 callers that already have a session.tests/unit/dual-factor.test.tswith the regression case (valid TOTP + email OTP without a session =>ok: true) plus negative cases. The new test would have failed under the broken code path: importing@/lib/authin a unit-test context pulled in the full Better Auth runtime and threw without a session.Root cause
The fix-pack PR that landed on 2026-05-25 shipped two halves that contradict each other: the OAuth callback now destroys the Better Auth session before routing to the MFA finalize endpoint, but the dual-factor gate behind that endpoint still verifies TOTP through a code path that requires a session. The bug is identical in shape to a previous strict-signin issue that was already resolved by moving to
verifyUserTotp; this PR brings the OAuth-MFA finalize path in line with that pattern.Impact
17 users have TOTP enrolled in production. Any of them attempting OAuth sign-in over the last ~33 hours hit the bug. Affected users were unblockable except by switching to the email/password sign-in path.
Test plan
pnpm type-checkpassespnpm test:unit— 5380 pass / 1 skipped, 0 regressionstests/unit/dual-factor.test.ts— 5 tests, all passingnpx @biomejs/biome checkon changed files: no new diagnosticspending_oauth_mfafor a TOTP-enrolled user, POST/api/auth/oauth-mfa-finalizewith a correct TOTP and a wrong email OTP). Expected:code: "email_code_invalid"(TOTP now passes, email OTP fails) instead of the pre-fixcode: "mfa_code_invalid".Out of scope (follow-ups)
verifyMfaPair(userId, totpCode, emailOtp, action)helper used by both strict-signin and oauth-mfa-finalize. The original fix-pack shipped two divergent TOTP-verify code paths and they drifted; one was session-required (broke for 33h), one was session-less. P2/Medium./verify-mfa->/api/auth/oauth-mfa-finalizeflow against a real session-required path (not mocked). The unit test in this PR catches the import-level explosion, but an integration test would catch this class of bug earlier when the contributor only has unit-test coverage in mind.