From 70c578405d135d577fa1c728f0606351b965a012 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 5 Jun 2026 16:10:29 -0700 Subject: [PATCH 1/3] Clear cached session key in cookie auth handler sign-out When CookieAuthenticationOptions.SessionStore is configured, the cookie auth handler caches the incoming session key in _sessionKey during EnsureCookieTicket so it can be reused on a refresh-style SignInAsync (see #22135). HandleSignOutAsync called ITicketStore.RemoveAsync(_sessionKey) but did not clear _sessionKey. A subsequent SignInAsync in the same request would then call ITicketStore.RenewAsync against the just-removed key instead of generating a new key via StoreAsync. That made it easy to introduce a session fixation bug after an explicit sign-out, and left the renew-after-remove behavior dependent on the ticket store implementation. Mirror the existing handling in ReadCookieTicket for expired sessions and null out _sessionKey immediately after RemoveAsync so the next sign-in in the same request creates a fresh session key. Addresses the sign-out leg of #47503. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/CookieAuthenticationHandler.cs | 5 +++ .../Authentication/test/CookieTests.cs | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs b/src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs index 0e9ad0883808..989a604f061f 100644 --- a/src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs +++ b/src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs @@ -390,6 +390,11 @@ protected override async Task HandleSignOutAsync(AuthenticationProperties? prope if (Options.SessionStore != null && _sessionKey != null) { await Options.SessionStore.RemoveAsync(_sessionKey, Context, Context.RequestAborted); + + // Clear the cached session key so a subsequent SignInAsync call in the same request + // generates a new key via StoreAsync rather than reusing the just-removed key via RenewAsync. + // See https://github.com/dotnet/aspnetcore/issues/47503. + _sessionKey = null; } var context = new CookieSigningOutContext( diff --git a/src/Security/Authentication/test/CookieTests.cs b/src/Security/Authentication/test/CookieTests.cs index 9f209173fa35..f25e8b4fc992 100644 --- a/src/Security/Authentication/test/CookieTests.cs +++ b/src/Security/Authentication/test/CookieTests.cs @@ -306,6 +306,45 @@ public async Task SessionStoreRemovesExpired() Assert.Null(FindClaimValue(transaction3, ClaimTypes.Name)); } + [Fact] + public async Task SignOutClearsSessionKeySoSubsequentSignInGeneratesNewKey() + { + var sessionStore = new TestTicketStore(); + using var host = await CreateHostWithServices(s => + { + s.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(o => + { + o.TimeProvider = _timeProvider; + o.SessionStore = sessionStore; + }); + }, async context => + { + if (context.Request.Query.ContainsKey("signoutfirst")) + { + await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + } + await SignInAsAlice(context); + }); + + using var server = host.GetTestServer(); + + // Establish an initial session with one key in the store. + var transaction1 = await SendAsync(server, "http://example.com/testpath"); + var key1 = Assert.Single(sessionStore.Store.Keys); + + // In the same request, sign out and then sign back in while attaching the existing cookie. + // SignOutAsync must clear the cached session key so that the subsequent SignInAsync + // generates a fresh key via StoreAsync rather than reusing the just-removed key via RenewAsync. + // See https://github.com/dotnet/aspnetcore/issues/47503. + var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com/testpath?signoutfirst=1"); + request.Headers.Add("Cookie", transaction1.CookieNameValue); + var response = await server.CreateClient().SendAsync(request); + response.EnsureSuccessStatusCode(); + + var key2 = Assert.Single(sessionStore.Store.Keys); + Assert.NotEqual(key1, key2); + } + [Fact] public async Task CustomAuthSchemeEncodesCookieName() { From a4a4bbac3e6af0444093f6c8220e8483c84bd6e9 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 5 Jun 2026 17:26:13 -0700 Subject: [PATCH 2/3] Use test helper Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/Security/Authentication/test/CookieTests.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Security/Authentication/test/CookieTests.cs b/src/Security/Authentication/test/CookieTests.cs index f25e8b4fc992..5fe64ca8d1b0 100644 --- a/src/Security/Authentication/test/CookieTests.cs +++ b/src/Security/Authentication/test/CookieTests.cs @@ -336,10 +336,8 @@ public async Task SignOutClearsSessionKeySoSubsequentSignInGeneratesNewKey() // SignOutAsync must clear the cached session key so that the subsequent SignInAsync // generates a fresh key via StoreAsync rather than reusing the just-removed key via RenewAsync. // See https://github.com/dotnet/aspnetcore/issues/47503. - var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com/testpath?signoutfirst=1"); - request.Headers.Add("Cookie", transaction1.CookieNameValue); - var response = await server.CreateClient().SendAsync(request); - response.EnsureSuccessStatusCode(); + var transaction2 = await SendAsync(server, "http://example.com/testpath?signoutfirst=1", transaction1.CookieNameValue); + Assert.Equal(HttpStatusCode.OK, transaction2.Response.StatusCode); var key2 = Assert.Single(sessionStore.Store.Keys); Assert.NotEqual(key1, key2); From 8656f65f2717670863791ec012ad48c61f9b4ed1 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Fri, 5 Jun 2026 18:19:57 -0700 Subject: [PATCH 3/3] Strengthen SignOutClearsSessionKey test Switch the post-signout sign-in to a different principal (Bob) so the test mirrors the privilege-rotation scenario described in #47503, and add a follow-up authenticated request to verify the new cookie actually resolves to the new identity (not just that a new store key was issued). The same-request SignOut + SignIn emits two Set-Cookie headers, so the shared SendAsync helper's SingleOrDefault is bypassed for that one request and the auth-cookie (last Set-Cookie value) is extracted directly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Authentication/test/CookieTests.cs | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Security/Authentication/test/CookieTests.cs b/src/Security/Authentication/test/CookieTests.cs index 5fe64ca8d1b0..ef50711195af 100644 --- a/src/Security/Authentication/test/CookieTests.cs +++ b/src/Security/Authentication/test/CookieTests.cs @@ -322,25 +322,53 @@ public async Task SignOutClearsSessionKeySoSubsequentSignInGeneratesNewKey() if (context.Request.Query.ContainsKey("signoutfirst")) { await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + + // Sign back in as a different principal to mirror the privilege-rotation + // scenario from https://github.com/dotnet/aspnetcore/issues/47503. + var bob = new ClaimsIdentity(new GenericIdentity("Bob", "Cookies")); + await context.SignInAsync( + CookieAuthenticationDefaults.AuthenticationScheme, + new ClaimsPrincipal(bob), + new AuthenticationProperties()); + } + else + { + await SignInAsAlice(context); } - await SignInAsAlice(context); }); using var server = host.GetTestServer(); - // Establish an initial session with one key in the store. + // Establish an initial session as Alice with one key in the store. var transaction1 = await SendAsync(server, "http://example.com/testpath"); var key1 = Assert.Single(sessionStore.Store.Keys); - // In the same request, sign out and then sign back in while attaching the existing cookie. - // SignOutAsync must clear the cached session key so that the subsequent SignInAsync - // generates a fresh key via StoreAsync rather than reusing the just-removed key via RenewAsync. + // In the same request, sign out and then sign back in as Bob while attaching the + // existing cookie. SignOutAsync must clear the cached session key so that the + // subsequent SignInAsync generates a fresh key via StoreAsync rather than reusing + // the just-removed key via RenewAsync. // See https://github.com/dotnet/aspnetcore/issues/47503. - var transaction2 = await SendAsync(server, "http://example.com/testpath?signoutfirst=1", transaction1.CookieNameValue); - Assert.Equal(HttpStatusCode.OK, transaction2.Response.StatusCode); + // + // Same-request SignOut + SignIn emits two Set-Cookie headers (the sign-out + // delete-cookie followed by the sign-in auth-cookie), so the shared SendAsync + // helper's SingleOrDefault on Set-Cookie can't be used here. Extract the + // auth-cookie (the last Set-Cookie value) manually. + var request2 = new HttpRequestMessage(HttpMethod.Get, "http://example.com/testpath?signoutfirst=1"); + request2.Headers.Add("Cookie", transaction1.CookieNameValue); + using var response2 = await server.CreateClient().SendAsync(request2); + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); var key2 = Assert.Single(sessionStore.Store.Keys); Assert.NotEqual(key1, key2); + + var setCookies = response2.Headers.GetValues("Set-Cookie").ToArray(); + Assert.Equal(2, setCookies.Length); + var authCookieNameValue = setCookies[^1].Split(';', 2)[0]; + + // The new cookie must actually authenticate, and it must resolve to Bob rather + // than the pre-signout Alice identity. + var transaction3 = await SendAsync(server, "http://example.com/me/Cookies", authCookieNameValue); + Assert.Equal("Bob", FindClaimValue(transaction3, ClaimTypes.Name)); } [Fact]