Conversation
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
force-pushed
the
fix/301-mfa-preserve-roles
branch
from
November 26, 2026 08:21
8bd6c2a to
6955346
Compare
This was referenced Nov 26, 2026
Merged
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.
Fixes #301
Root cause
loginacceptsrolesand checks them against the user's roles. When MFA interrupts the login, though, the token is minted later byverify_otp,skip_mfa_setupor the passkey endpoints, and none of those see the original request.issueAuthResponsethen used every role stored on the user. A non-MFAloginuses the requested subset, orDefaultRoleswhen 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.
loginstores the raw request undermfa_roles:<mfa-session-id>, with the same TTL as the MFA session. An empty request is stored too, so "none requested" meansDefaultRoles, 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.resolveCarriedRolesuses the sameresolveLoginRolesthatloginuses. As a result:verify_otploads 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".verify_otp,skip_mfa_setup,webauthn_registration_verifyandwebauthn_login_verifyall resolve the carried roles, then clear them along with the session. A passkey used as the second factor only uses aVerifiedMFA session owned by the same user. A session owned by any other account is ignored and left untouched.email_otp_mfa_setup,sms_otp_mfa_setupandtotp_mfa_setupcreate a fresh MFA session. They now copy the old session's scope and roles request into it. Before this change, averify_otpon 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}OTPMFASetupViaMfaSessionCookiepin 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.issueAuthResponsefalls back to the user's stored roles when nothing was carried. That covers signup, magic link /verify_email, the OAuth gate andresend_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
rolesfield was added toVerifyOTPRequest, because the value carried from login is what makes MFA and non-MFA match.Behaviour changes to note
rolesand goes through MFA now getsDefaultRoles, 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 passrolesexplicitly.invalid rolesonce the code is proven, and the session is used up.Known limitations / out of scope
GetCachecannot tell a failed read from a miss (documented inmemory_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.roles=plus MFA, and the OAuth callback:EvaluateMFAGateForOAuthcarries neither scope nor roles, which is the same gap scope already has. In those flows therolesparameter 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 onmain([user editor viewer]). The enrollment test was confirmed failing with theotp_mfa_setup.gochange reverted. The remaining subtests are guards for the new behaviour.roles=[editor]+ TOTP, thenverify_otp: token roles are[editor]DefaultRoles)[editor], and the MFA session can't be used again afterwardsVerifiedsession: that session's roles are ignored, and its session and carried roles are left untouchedroles=[editor], thentotp_mfa_setupthrough the cookie, adopting the new cookie, thenverify_otp:[editor]. The original cookie still carries the roles. (This test fails with[user editor viewer]when theotp_mfa_setup.gochange is reverted.)[viewer]; an invalid request does not error at login, then fails withinvalid rolesonce the code is provenVerification 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 theinternal/memory_storeprovider tests, and the integration suite does not wire Redis, so it would not exercise this change.