Repository navigation
fix(identity): find the pending 2fa cookie by better-auth's own cookie name - #404
Merged
Merged
Conversation
…e name
Over HTTPS better-auth names the pending challenge cookie
`__Secure-better-auth.two_factor`, which the exact match on
`better-auth.two_factor` never found. Verify then rejected a code better-auth
had already accepted, skipped the lockout count, and keyed every pending
attempt on the shared anonymous rate-limit bucket.
Derive the name from `createAuthCookie('two_factor')` on the auth context so
the cookie prefix and secure prefix match what the two-factor plugin sets,
keeping the exact-name match against look-alike cookies.
okapitula
requested review from
jakubfilinger-b,
klaudia-blazyczek-blurify,
marek-chmielowski-blurify,
mp-blurify and
zaxovaiko
as code owners
November 5, 2026 17:13
okapitula
enabled auto-merge
November 5, 2026 17:55
jakubfilinger-b
approved these changes
Nov 6, 2026
jakubfilinger-b
left a comment
Collaborator
There was a problem hiding this comment.
The cookie lookup now follows Better Auth’s configured cookie name, including the HTTPS prefix. The regression tests cover verification and rate limiting.
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
Why
The lookup matched
better-auth.two_factorexactly. Over HTTPS better-auth sets the cookie as__Secure-better-auth.two_factor, so the lookup found nothing:POST /identity/2fa/verifyanswered 401Invalid two-factor challengeafter better-auth had already accepted the code, the failed-attempt lockout was never counted, and every pending verify shared theverify2fa:anonymousrate-limit bucket. Tests run over plain HTTP, so they never saw the prefixed name.The name now comes from
createAuthCookie('two_factor')on the auth context, which applies the configured cookie prefix and the__Secure-prefix the same way the two-factor plugin does when it sets the cookie. The exact-name match stays, so a look-alike*.two_factorcookie still cannot change the challenge identity or the rate-limit key.Alternatives considered
better-auth.two_factorand__Secure-better-auth.two_factor: still breaks under a customcookiePrefixor a per-cookie name override..two_factorsuffix match: reopens the junk-cookie rate-limit churn the exact match closed.Risks
None. Plain-HTTP deployments resolve the same name as before.