Repository navigation
Harden race condition when submitting 2FA during password reset - #68350
Conversation
There was a problem hiding this comment.
Pull request overview
Addresses a race where a stale 2FA “user id” cookie could still complete sign-in after a password reset changed the user’s security stamp, by embedding and validating the stamp in the 2FA flow and enabling stamp validation for the 2FA cookie.
Changes:
- Embed the security stamp into the
TwoFactorUserIdSchemeprincipal when issuing the 2FA cookie. - Validate the embedded stamp when retrieving 2FA info so stale 2FA cookies can’t complete sign-in.
- Enable security-stamp validation for the
TwoFactorUserIdSchemecookie in both identity cookie registration paths, and add a regression test.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Identity/test/Identity.Test/SignInManagerTest.cs | Adds a regression test and updates helper usage to issue 2FA principals containing the security stamp. |
| src/Identity/Core/src/SignInManager.cs | Stores security stamp in the 2FA principal and validates it during 2FA info retrieval. |
| src/Identity/Core/src/SecurityStampValidator.cs | Fixes XML doc for SecurityStampValidator.ValidateAsync<TValidator>. |
| src/Identity/Core/src/IdentityServiceCollectionExtensions.cs | Wires OnValidatePrincipal for TwoFactorUserIdScheme in AddIdentityCookies. |
| src/Identity/Core/src/IdentityCookiesBuilderExtensions.cs | Wires OnValidatePrincipal for TwoFactorUserIdScheme in the authentication builder cookie helpers. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
/azp run |
|
Azure Pipelines: Successfully started running 2 pipeline(s). 3 pipeline(s) were filtered out due to trigger conditions. |
There was a problem hiding this comment.
I think this possibly breaks passkeys, no?
Passkeys reuse the TwoFactorUserId cookie to stash ceremony state, and the stored principal is empty. The PR attaches OnValidatePrincipal = ValidateAsync<ITwoFactorSecurityStampValidator> to that exact scheme. When that validator runs, ValidateTwoFactorSecurityStampAsync sees principal.Identity?.Name == null → returns null → the base validator calls context.RejectPrincipal() and SignOutAsync(). Then RetrievePasskeyAuthenticationInfoAsync gets no properties and returns null, so PerformPasskeyAssertion/AttestationAsync throws. Hence passkey sign-in and registration break.
Luckily default ValidationInterval = 30 min, but the TwoFactorUserId cookie's ExpireTimeSpan = 5 min. So under defaults the cookie always expires before the interval elapses → validate is never true → the branch is a no-op → passkeys keep working "by accident". But if an app lowers ValidationInterval (e.g. TimeSpan.Zero to validate every request - a supported, not-uncommon setting), validate becomes true and passkeys break.
The safest fixes are either to use a separate passkey-state scheme or explicitly bypass stamp validation for passkey-state tickets. The original race is still prevented by the new explicit validation inside RetrieveTwoFactorInfoAsync; therefore simply not adding the cookie event would also fix the reported scenario without breaking passkeys.
Maybe worth adding tests for ValidationInterval = TimeSpan.Zero as well as another validation mechanism.
cc @rolandVi
I confirmed this. Worth adding that when the cookie is rejected it also calls We could drop the two Longer term a separate passkey scheme could be the cleaner fix, since ceremony state doesn't really fit in a cookie that expects a user. |
Co-authored-by: Korolev Dmitry <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
153cda8 to
2637a29
Compare
Resolve conflict in FunctionalTest.cs by keeping both upstream's new security-stamp/passkey tests and the passkey ceremony regression test. Co-authored-by: Copilot <[email protected]> Copilot-Session: dc58c61b-e0b5-4bfd-9ad5-8ad9ba46c931
|
@cincuranet @rolandVi Does it look good now? |
Looks good to me |
|
@halter73 Please take a look if you can, and let me know if you think we should get it in .NET 11 RC2. |
|
/backport to release/11.0 |
|
Started backporting to |
Fixes #66824