Skip to content

fix(platform): confirm the password before adding a passkey - #4336

Merged
yannickmonney merged 2 commits into
mainfrom
fix/two-factor-enroll-fresh-session
Oct 5, 2026
Merged

yannickmonney merged 2 commits into
mainfrom
fix/two-factor-enroll-fresh-session

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Summary

Adding a passkey failed with a bare "Session is not fresh" once the session was more than a day old. Better Auth's passkey plugin only lets a session signed in within session.freshAge (24 h) register one (freshSessionMiddleware on both registration calls). The post-grace 2FA enrollment wall hit this every time: the people it catches stayed signed in through the whole grace window. Better Auth has no client-callable way to freshen a session short of a full sign-in. For an account with an authenticator, a full sign-in drops the session cookie until the code is entered, and it leaves the old session alive.

The passkey dialog is shared by the wall and Settings > Account > Passkeys. It now checks the session's age when it opens and asks for the password first. The new POST /api/auth/reauthenticate confirms the password and swaps in a fresh session.

What changed

  • POST /api/auth/reauthenticate: a small Better Auth plugin in backend/auth/reauthenticate.ts.
    • The password goes through the same throttle as sign-in: per-account lockout plus the per-IP flood guard, extracted unchanged into backend/auth/password-attempts.ts. It counts on the same failure counter.
    • Both outcomes audit login_attempt / login_lockout / login_success, stamped metadata.reauthentication: true.
    • On success the session is rotated, not re-dated: a new session carries the organization and proxy fields, its cookie replaces the old one, and the old token is deleted. A copied old cookie therefore does not become fresh with it.
    • An account without a password gets PASSWORD_NOT_SET.
  • freshAge is pinned from lib/shared/constants/session-freshness.ts (still Better Auth's 24 h default), so the app and the server agree on the window.
  • The passkey dialog gains a password step:
    • It is shown when the session is stale, or within 5 minutes of going stale, so the ceremony never fails after the person has touched their authenticator.
    • If the server still answers SESSION_NOT_FRESH (clock skew, a dialog left open), the dialog returns to that step and keeps the typed name.
    • A wrong password and a lockout are worded inline. The lockout sentence is shared with the log-in page through useLockoutMessage.
    • An account without a password (SSO-only or proxy) is offered Sign in again instead.
  • Messages and docs: EN/DE/FR. No ß or quotes in the new strings, so no de-CH override is needed. The docs also now name the account page's real Two-factor authentication / Passkeys sections; the page has no "Security" section.
  • Manual boxes AUTH-F29, AUTH-F30, AUTH-B17, plus an automation-register row.

Checklist

  • bun run check — partially, locally. The platform typecheck and oxlint --type-aware are clean. The affected server and UI suites, tests/guards, the i18n catalog tests, services/docs tests, lint:links and lint:manual are green. I left the full turbo run to CI. The machine was at load 100–130: three account-form.password tests (untouched; that file mocks the passkey section) timed out at 5 s and pass with --testTimeout=60000.
  • bun run lint:sast — Opengrep on the changed source files: 0 findings. The pre-commit hook ran it too.
  • Translations updated in services/platform/messages/{en,de,fr}.yml.
  • Docs updated in docs/{en,de,fr}/platform/admin/two-factor-authentication.md.
  • README — N/A, no README-level change.

Test plan

  • Unit and component tests:
    • backend/auth/reauthenticate.test.ts: real Better Auth with the memory adapter. Stale → 403 SESSION_NOT_FRESH; confirm → fresh session, old cookie 401; wrong password, throttle and no-password are refused without touching the session.
    • backend/auth/password-attempts.test.ts, backend/domains/login_attempts/service.reauthentication.test.ts, app/lib/auth/session-freshness.test.ts.
    • passkey-register-dialog.test.tsx: 7 of its 8 cases fail against the old dialog.
  • Real Postgres: ITEST_LANES=checkLoginThrottleAndAuditChain,checkTwoFactor,checkStaleSessionReauthentication → 27/27, including the refactored sign-in lockout lane.
  • Real browser (isolated stack, Chromium virtual WebAuthn authenticator, session dated back 25 h in SQL):
    • Settings → Add a passkey opens on the password step. Wrong password → inline error. Right password → name step → passkey registered.
    • The enrollment wall at grace 0 → Register a passkey instead → password → passkey → dashboard.
    • Server-side refusal → back to the password step, name kept.
    • DE/FR rendering; SSO-only account → Sign in again → /log-in?redirectTo=…; six wrong passwords → 429 lock message.
    • Audit rows stamped.
  • Manual: AUTH-F29, AUTH-F30, AUTH-B17 in services/platform/tests/manual/suites/auth.md.

Notes

  • /two-factor/enable, /two-factor/disable and /users/update-password still verify passwords without the lockout counter. That is pre-existing and left as is; this PR only makes the new door count.

Registering a passkey failed with a bare "Session is not fresh" once the
session was more than a day old. Better Auth's passkey plugin lets a
session register one only within `session.freshAge` (24 h) of signing in,
and everyone the post-grace two-factor wall catches stayed signed in
through the whole grace window, so the wall's "Register a passkey
instead" failed for exactly the people it is for. Better Auth has no
client-callable way to freshen a session short of a full sign-in.

- `POST /api/auth/reauthenticate` (a Better Auth plugin): a signed-in
  person confirms their password and the session is rotated into a fresh
  one. The password goes through the sign-in throttle (per-account
  lockout + per-IP flood guard, now shared in `password-attempts.ts`),
  counts on the same failure counter and audits `login_attempt` /
  `login_lockout` / `login_success` stamped `reauthentication`. The old
  token is deleted, so a copied cookie does not become fresh with it.
- The passkey dialog shared by the wall and Settings > Account checks the
  session's age when it opens (`SESSION_FRESH_AGE_SECONDS`, now pinned in
  the auth config, with a five-minute margin for the ceremony) and asks
  for the password first; a registration the server still refuses as not
  fresh comes back to that step with the typed name kept. An account
  without a password is offered Sign in again.
- EN/DE/FR messages and docs (the docs also name the account page's real
  Two-factor authentication and Passkeys sections, not a "Security" one),
  manual boxes AUTH-F29/F30/B17, and the checkStaleSessionReauthentication
  integration lane.
The REST error-code guard scans every module the /api/v1 handlers reach, auth.ts and its plugins included. INVALID_PASSWORD and PASSWORD_NOT_SET come from /api/auth/reauthenticate, which REST never mounts, so they join the other Better Auth-only codes in APP_ONLY_CODES. PASSWORD_NOT_SET is spelled out at its throw so the guard sees it; the endpoint test pins it to the shared constant the app reads.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant