[PR #9122] [MERGED] fix(two-factor): enforce 2FA on all sign-in paths #16700

Closed
opened 2026-04-13 10:38:59 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/9122
Author: @gustavovalverde
Created: 4/11/2026
Status: Merged
Merged: 4/11/2026
Merged by: @gustavovalverde

Base: mainHead: fix/2fa-sign-in-hook-bypass


📝 Commits (2)

  • eb264c2 fix(two-factor): enforce 2FA on all sign-in paths, not just credentials
  • 96756f8 chore: add changeset for 2FA bypass fix

📊 Changes

3 files changed (+102 additions, -3 deletions)

View changed files

.changeset/fix-2fa-bypass.md (+7 -0)
📝 packages/better-auth/src/plugins/two-factor/index.ts (+8 -3)
📝 packages/better-auth/src/plugins/two-factor/two-factor.test.ts (+87 -0)

📄 Description

Summary

  • Replace the hardcoded 3-path matcher in the 2FA sign-in hook with a session-based matcher
  • The hook now triggers on any endpoint that creates a new session, excluding two-factor endpoints themselves
  • Covers magic-link, OAuth, passkey, email-OTP, SIWE, and all future sign-in methods

Closes #8627


Summary by cubic

Enforce 2FA on all sign-in methods by triggering the 2FA hook whenever a new session is created. Blocks bypasses (magic link, OAuth, passkeys, email OTP, SIWE, etc.) and avoids false positives on authenticated non-sign-in requests.

  • Bug Fixes
    • Switch from path allowlist to a session-based matcher (context.context.newSession != null); exclude /two-factor/ routes and skip requests with an existing session.
    • Add tests for magic-link enforcement and for not enforcing 2FA on authenticated non-sign-in endpoints (e.g., updateUser).
    • Add changeset to publish a patch for better-auth.

Written for commit 96756f8e24. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/9122 **Author:** [@gustavovalverde](https://github.com/gustavovalverde) **Created:** 4/11/2026 **Status:** ✅ Merged **Merged:** 4/11/2026 **Merged by:** [@gustavovalverde](https://github.com/gustavovalverde) **Base:** `main` ← **Head:** `fix/2fa-sign-in-hook-bypass` --- ### 📝 Commits (2) - [`eb264c2`](https://github.com/better-auth/better-auth/commit/eb264c22e0daac87bbfd8472636d45d121f1a750) fix(two-factor): enforce 2FA on all sign-in paths, not just credentials - [`96756f8`](https://github.com/better-auth/better-auth/commit/96756f8e24740fcf380383f4b8e7c7f3545e4e3e) chore: add changeset for 2FA bypass fix ### 📊 Changes **3 files changed** (+102 additions, -3 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/fix-2fa-bypass.md` (+7 -0) 📝 `packages/better-auth/src/plugins/two-factor/index.ts` (+8 -3) 📝 `packages/better-auth/src/plugins/two-factor/two-factor.test.ts` (+87 -0) </details> ### 📄 Description ## Summary - Replace the hardcoded 3-path matcher in the 2FA sign-in hook with a session-based matcher - The hook now triggers on any endpoint that creates a new session, excluding two-factor endpoints themselves - Covers magic-link, OAuth, passkey, email-OTP, SIWE, and all future sign-in methods Closes #8627 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Enforce 2FA on all sign-in methods by triggering the 2FA hook whenever a new session is created. Blocks bypasses (magic link, OAuth, passkeys, email OTP, SIWE, etc.) and avoids false positives on authenticated non-sign-in requests. - **Bug Fixes** - Switch from path allowlist to a session-based matcher (`context.context.newSession != null`); exclude `/two-factor/` routes and skip requests with an existing session. - Add tests for magic-link enforcement and for not enforcing 2FA on authenticated non-sign-in endpoints (e.g., `updateUser`). - Add changeset to publish a patch for `better-auth`. <sup>Written for commit 96756f8e24740fcf380383f4b8e7c7f3545e4e3e. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 10:38:59 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#16700