Skip to content

Harden race condition when submitting 2FA during password reset - #68350

Merged
Youssef1313 merged 5 commits into
mainfrom
dev/ygerges/2fa
Sep 4, 2026
Merged

Youssef1313 merged 5 commits into
mainfrom
dev/ygerges/2fa

Conversation

@Youssef1313

Copy link
Copy Markdown
Member

Fixes #66824

Copilot AI lite review requested due to automatic review settings August 11, 2026 17:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TwoFactorUserIdScheme principal 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 TwoFactorUserIdScheme cookie 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.

Comment thread src/Identity/Core/src/SignInManager.cs Resolved
@premun

premun commented Aug 12, 2026

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
3 pipeline(s) were filtered out due to trigger conditions.

@cincuranet cincuranet left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@rolandVi

Copy link
Copy Markdown
Contributor

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 SignInManager.SignOutAsync(), which clears the application cookie, so with a lowered interval a signed in user who starts a passkey registration gets logged out of the app entirely.

We could drop the two OnValidatePrincipal lines. All four 2FA sign-in paths go through RetrieveTwoFactorInfoAsync, so the explicit check added there covers the reported race.

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.

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
@Youssef1313

Copy link
Copy Markdown
Member Author

@cincuranet @rolandVi Does it look good now?

@rolandVi

Copy link
Copy Markdown
Contributor

@cincuranet @rolandVi Does it look good now?

Looks good to me

@Youssef1313
Youssef1313 requested a review from halter73 September 2, 2026 17:23
@Youssef1313 Youssef1313 added the area-identity Includes: Identity and providers label Sep 2, 2026
@Youssef1313

Copy link
Copy Markdown
Member Author

@halter73 Please take a look if you can, and let me know if you think we should get it in .NET 11 RC2.

@Youssef1313
Youssef1313 merged commit 44337de into main Sep 4, 2026
34 checks passed
@Youssef1313
Youssef1313 deleted the dev/ygerges/2fa branch September 4, 2026 15:01
@Youssef1313

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-identity Includes: Identity and providers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harden race condition when submitting 2FA during password reset

8 participants