Registry Mirror
Skip to content

Conversation

@lakhansamani

@lakhansamani lakhansamani commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #301

Root cause

login accepts roles and checks them against the user's roles. When MFA interrupts the login, though, the token is minted later by verify_otp, skip_mfa_setup or the passkey endpoints, and none of those see the original request. issueAuthResponse then used every role stored on the user. A non-MFA login uses the requested subset, or DefaultRoles when no roles are requested. So the same login gave a different, larger role set when MFA was involved. Scope was already carried across the MFA step (setMFASession/consumeMFAScope); roles were not.

Fix

Login's roles request is carried on the server with the MFA session, the same way scope is. It is resolved against the user only once the second factor is proven. No completion endpoint accepts roles from the client.

  • Carry: every MFA branch of login stores the raw request under mfa_roles:<mfa-session-id>, with the same TTL as the MFA session. An empty request is stored too, so "none requested" means DefaultRoles, as in a non-MFA login. Writing it fails closed, because the fallback (all of the user's roles) is broader than what was requested.
  • Resolve at completion: resolveCarriedRoles uses the same resolveLoginRoles that login uses. As a result:
    • a role revoked during the MFA window is checked again;
    • the unverified email/phone OTP branches behave the same whether the request is valid or not, with no early error and an identical write, so they reveal nothing about the account's roles;
    • "invalid roles" is returned only to a caller who has proven the code.
  • Read errors fail closed and can be retried: verify_otp loads the carried request before the code is spent. A memory-store read error therefore returns an error and does not burn the OTP. It is never treated as "nothing carried".
  • Completion endpoints: verify_otp, skip_mfa_setup, webauthn_registration_verify and webauthn_login_verify all resolve the carried roles, then clear them along with the session. A passkey used as the second factor only uses a Verified MFA session owned by the same user. A session owned by any other account is ignored and left untouched.
  • MFA enrollment during login: in cookie mode, email_otp_mfa_setup, sms_otp_mfa_setup and totp_mfa_setup create a fresh MFA session. They now copy the old session's scope and roles request into it. Before this change, a verify_otp on the new cookie issued all stored roles and the default scope. The values are copied rather than moved, and the old session is not retired: Test{Email,SMS}OTPMFASetupViaMfaSessionCookie pin the contract that the original cookie stays redeemable after setup, and clients that don't adopt the new cookie depend on it. Each session spends only its own copy. Bearer mode (the settings screen) carries nothing.
  • Unchanged: issueAuthResponse falls back to the user's stored roles when nothing was carried. That covers signup, magic link / verify_email, the OAuth gate and resend_otp. Their non-MFA paths already issue the stored roles, so MFA and non-MFA match for them.

No GraphQL or proto schema changes. No roles field was added to VerifyOTPRequest, because the value carried from login is what makes MFA and non-MFA match.

Behaviour changes to note

  • A login that passes no roles and goes through MFA now gets DefaultRoles, the same as the non-MFA login, instead of every role on the user. Integrations that relied on getting the full set after MFA should pass roles explicitly.
  • A login whose carried roles are no longer valid when MFA completes (the role was revoked during the window, or an unverified-email login asked for roles the user does not hold) fails with invalid roles once the code is proven, and the session is used up.

Known limitations / out of scope

  • DB-backed memory store: its GetCache cannot tell a failed read from a miss (documented in memory_store/db/cache.go). On that backend a read fault still looks like "nothing carried" and falls back to the stored roles. Closing that needs the cross-provider miss/error contract already tracked there. Redis and in-memory return the error, and this PR handles it.
  • Read-then-delete is not atomic: two concurrent completions using the same cookie can both read the carried request before either deletes it. Both would resolve the same server-side value against the same user, and the second factor is itself single-use, so this cannot widen a token. It is noted rather than fixed.
  • Magic link with roles= plus MFA, and the OAuth callback: EvaluateMFAGateForOAuth carries neither scope nor roles, which is the same gap scope already has. In those flows the roles parameter assigns roles to the account and does not attenuate the token, and the non-MFA path issues the stored roles too, so MFA and non-MFA already agree. Out of scope here.

Tests

internal/integration_tests/mfa_roles_carry_test.go. The original four role assertions failed on main ([user editor viewer]). The enrollment test was confirmed failing with the otp_mfa_setup.go change reverted. The remaining subtests are guards for the new behaviour.

  • login with roles=[editor] + TOTP, then verify_otp: token roles are [editor]
  • no roles requested: the MFA token equals the non-MFA baseline (DefaultRoles)
  • roles the user does not hold are still rejected at login
  • carried roles are used once: the next MFA login without roles gets the defaults
  • passkey as the second factor: [editor], and the MFA session can't be used again afterwards
  • passkey login with another account's Verified session: that session's roles are ignored, and its session and carried roles are left untouched
  • MFA enrollment during login: login with roles=[editor], then totp_mfa_setup through the cookie, adopting the new cookie, then verify_otp: [editor]. The original cookie still carries the roles. (This test fails with [user editor viewer] when the otp_mfa_setup.go change is reverted.)
  • unverified-email OTP branch: a valid request gives [viewer]; an invalid request does not error at login, then fails with invalid roles once the code is proven

Verification run locally:

  • go build ./... ✅
  • go vet ./... ✅
  • make test (SQLite) ✅
  • make lint (golangci-lint v2.15.0 built with Go 1.27): 0 issues, Prettier clean ✅

Not run: TEST_ENABLE_REDIS=1. It only gates the internal/memory_store provider tests, and the integration suite does not wire Redis, so it would not exercise this change.

When MFA interrupted a password login, the token was minted later by
verify_otp / skip_mfa_setup / webauthn, none of which see login's roles
input, so issueAuthResponse used every role on the user. An MFA login
therefore got a different role set than the identical non-MFA login.

Carry the roles login resolved and validated (requested subset, or the
default roles) server-side, bound to the MFA session, the same way
scope is carried. No completion endpoint accepts roles from the client.
A passkey completing a password login's MFA step now also honours the
carried scope and roles. Persisting the roles fails closed, since the
fallback is broader than what was requested.
…FA setup

Review follow-ups for #301:

- Carry login's raw roles request and resolve it against the user only
  after the second factor is proven, so a revoked role is re-checked and
  the unverified-contact OTP branches behave identically for valid and
  invalid requests.
- verify_otp loads the carried request before spending the code; a
  memory-store read error now fails retryably instead of falling back to
  every stored role.
- Cookie-mode email/sms/totp MFA setup copies the login's scope and roles
  onto the session it re-arms, so completing enrollment on the new cookie
  no longer issues all stored roles and the default scope.
- Tests: enrollment adopting the new cookie, passkey ignoring another
  account's session, session spent after passkey completion, and the
  unverified-email OTP branch with valid and invalid roles.
@lakhansamani
lakhansamani force-pushed the fix/301-mfa-preserve-roles branch from 8bd6c2a to 6955346 Compare November 26, 2026 08:21
@lakhansamani
lakhansamani merged commit 566e9f3 into main Nov 26, 2026
4 checks passed
@lakhansamani
lakhansamani deleted the fix/301-mfa-preserve-roles branch November 26, 2026 08:21
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.

Add support for roles in verify_otp

1 participant