Repository navigation
Conversation
When CookieAuthenticationOptions.SessionStore is configured, the handler caches the session key from the incoming cookie in _sessionKey and a subsequent SignInAsync renews the ticket under that same key (dotnet#22135). A sign-in that changes who the principal is therefore stores the new ticket under a key an earlier cookie already holds, so that cookie keeps resolving to the new ticket. dotnet#67049 fixed the sign-out leg of this by clearing _sessionKey, which covers SignOutAsync followed by SignInAsync in one request. It does not cover a bare SignInAsync, which is what ASP.NET Core Identity does on password sign-in. Add an opt-in AuthenticationProperties.RotateSessionKey so a caller can ask for a fresh session key. When set, the handler removes the incoming key from the store before storing the new ticket, so the previous key no longer resolves. The default is unchanged: without the flag the key is still renewed in place and concurrent requests holding the same cookie keep working. The flag is opt-in rather than automatic because the privilege-rotation case in dotnet#47503 signs in the same subject with different claims, which is indistinguishable from the refresh case in dotnet#22135. Addresses dotnet#47503.
|
Thanks for your PR, @earbona23. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
|
Status check from my side, in case this is waiting on something I can do. The branch is mergeable and the checks are settled: 28 of the 30 are green and the other two are neutral. The state is blocked, which I read as waiting on review rather than on the build. Since the change adds a public member, I am also happy to change the shape if the team prefers something else. Two alternatives I considered and did not pick:
The opt-in property was the least disruptive of the three. I will follow whichever the team prefers. |
|
/review |
Brings the branch up to date with main. No conflicts: none of the four files this branch changes were touched upstream since the fork point.
There was a problem hiding this comment.
Reviewed frozen head 570ba24245f66801cf08156ecaa8bb8f0ae40529 using the immutable bundle guidance at 480401c0a53ddd2e52d2ad34aaef4424edfead21.
One validated P2 finding is attached: remove-before-store does not guarantee rotation for an ITicketStore that reuses a released identifier. Frozen feedback contained no equivalent report. API-review approval for the new public member remains human-owned and unresolved; this is not a code finding.
The cross-cutting guide completed with gpt-5.6-sol (runtime effort telemetry unavailable). Cookie-auth-specific specialist coverage was not routed. Worker execution and security assessment were excluded by the trusted guidance.
Coordinator validation used the exact frozen-head worktree and a temporary CookieTests.cs repro with a key-reusing store. The final targeted test ran 1 case and failed at the observable cookie boundary: replaying Alice's original cookie returned Bob. Repository-pinned SDK setup and generated-file setup were required before the test could run. No repository-wide suite was run. Source limitations remain tracked Git bytes only; symlink, submodule, and LFS targets were not evaluated.
Generated by ASP.NET Core Pull Request Review for #70105 · copilot · gpt56 · 162 AIC · ⌖ 12.2 AIC · ⊞ 20K
Comment /review to run again
| // See https://github.com/dotnet/aspnetcore/issues/47503. | ||
| if (_sessionKey != null && signInContext.Properties.RotateSessionKey == true) | ||
| { | ||
| await Options.SessionStore.RemoveAsync(_sessionKey, Context, Context.RequestAborted); |
There was a problem hiding this comment.
P2 — Require a distinct key before deleting the old session
ITicketStore.StoreAsync only promises to return the associated key; it does not promise a fresh one. Because this removes the old key before calling StoreAsync, a conforming store that recycles released identifiers can immediately return _sessionKey again. The old cookie then resolves to the newly stored ticket even though RotateSessionKey was requested. A targeted repro using a store that reuses its last removed key failed with the replayed Alice cookie authenticating as Bob. Please allocate/store while the old key is still reserved and enforce a distinct result before removing it, or add an atomic/fresh-key store contract, and cover the key-reuse case.
There was a problem hiding this comment.
Addressed in 0617d43. Rotation now keeps the incoming session key reserved, stores the new ticket, and removes the old key only once StoreAsync has returned a different one, so a store that recycles released keys cannot hand the incoming key straight back. Added SignInRotatesSessionKeyWhenStoreRecyclesReleasedKeys, which uses a key-recycling store and fails on the previous commit, and merged the branch up to main.
ITicketStore.StoreAsync only promises to return the key the ticket was stored under, not a key that has never been used before. Removing the incoming key before calling StoreAsync therefore let a store that recycles released keys hand the incoming key straight back, so the cookie that came in on the request still resolved to the newly stored ticket and nothing was rotated. Keep the incoming key reserved, store the new ticket, and release the old key only once the new one differs from it. Add a regression test with a store that recycles the keys it releases.
|
Correction to my status comment above: the "28 of the 30 are green" line described The five red checks are two distinct failing jobs plus three roll-ups of those two:
I do not think either job is failing because of this change. The evidence rather than the assertion:
The I cannot trigger a re-run myself. Happy for one to be kicked, or to rebase onto current Separately, and not about CI: the automated review flagged that removing the incoming key before The offer in my earlier comment still stands: |
|
Following up on my own offer above rather than leaving it as a question: the API proposal is filed as #70373, and this PR's description now links it. I went ahead because
So the gap was mine. #70373 follows the Two things there that need someone with write access:
Nothing else is outstanding on the code. The P2 from the automated review is fixed in Happy to change the API shape to whatever the review prefers, including moving the flag off |
|
Correcting my own comment above, because it is already out of date: I wrote that #70373 carries no labels and no issue type. That was true when I posted it and stopped being true four minutes later — the triage bot applied So the only one still missing is |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
Allow rotating the cookie session key on sign-in
Addresses #47503. API proposal: #70373.
Problem
When
CookieAuthenticationOptions.SessionStoreis configured,ReadCookieTicketcaches thesession key that arrived on the request:
https://github.com/dotnet/aspnetcore/blob/main/src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs#L178
HandleSignInAsyncthen renews the ticket under that same key rather than minting a new one,which is the behavior #22732 introduced for #22135:
So a sign-in that changes who the principal is stores the new ticket under a key that an
earlier cookie already holds, and that earlier cookie keeps resolving to the new ticket. That is
the session fixation concern in #47503, and OWASP's session management guidance asks for the
session id to be renewed on any privilege level change.
#67049 fixed the sign-out leg by clearing
_sessionKey, which coversSignOutAsyncfollowed bySignInAsyncin one request. It does not cover a bareSignInAsync— which is what ASP.NET CoreIdentity does on password sign-in, since
SignInManager.SignInWithClaimsAsynccallsContext.SignInAsyncwithout signing the application scheme out first.Change
Adds an opt-in
AuthenticationProperties.RotateSessionKey. When set, the handler removes theincoming key from the store before storing the new ticket, so the previous key stops resolving:
The default is unchanged. Without the flag the key is still renewed in place, so concurrent
requests holding the same cookie keep working and #22135 stays fixed.
Why opt-in rather than automatic
The privilege-rotation case in #47503 signs in the same subject with different claims. That is
indistinguishable from the refresh case in #22135, which also signs in the same subject. Any
automatic rule based on comparing the incoming and outgoing principal would either miss the
privilege change or break the refresh, so the caller has to say which one it means. This matches
what was suggested in #67049:
I am happy to move the flag out of
Microsoft.AspNetCore.Authentication.Abstractionsand into thecookie package (for example as extension methods over
AuthenticationProperties.Items) if addingto the shared type is not wanted. This adds public API, so it needs API review.
Tests
SignInRotatesSessionKeyOnlyWhenRequestedis a[Theory]over both values of the flag. Itestablishes a session as Alice, then signs in as Bob while Alice's cookie is attached to the
request — no sign-out — and asserts:
i.e. current behavior;