Skip to content

Cancel pending cookie renewal when principal validation fails - #70028

Open
GrantTotinov wants to merge 2 commits into
dotnet:mainfrom
GrantTotinov:cancel-cookie-renewal-on-reject-69701
Open

GrantTotinov wants to merge 2 commits into
dotnet:mainfrom
GrantTotinov:cancel-cookie-renewal-on-reject-69701

Conversation

@GrantTotinov

@GrantTotinov GrantTotinov commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Cancel pending cookie renewal when principal validation fails

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Don't renew the cookie when ValidatePrincipal rejects the principal or throws

Description

CookieAuthenticationHandler.HandleAuthenticateAsync runs the sliding expiration check before ValidatePrincipal, so the refresh ticket is cloned before any per-request changes to the principal. That pending refresh was kept in two cases where validation didn't succeed:

  • ValidatePrincipal rejected the principal. The handler returned NoPrincipal, but unless sign-in or sign-out was also called in that request, FinishResponseAsync still wrote a renewed cookie for the rejected ticket, and with a SessionStore it also called ITicketStore.RenewAsync for it.
  • ValidatePrincipal threw, for example on a transient store failure in SecurityStampValidator before it reaches RejectPrincipal() and SignOutAsync(). If exception handling middleware such as UseExceptionHandler then wrote an error response, FinishResponseAsync still ran when that response started and renewed the ticket the same way, moving IssuedUtc forward.

Changes:

  • A new private CancelRefresh() clears the pending refresh (_shouldRefresh, _refreshTicket and the two refresh timestamps).
  • HandleAuthenticateAsync calls it before returning NoPrincipal for a rejected principal, and in a catch around Events.ValidatePrincipal(context) before rethrowing.
  • The snapshot taken before validation and the successful validation path are unchanged. Neither case signs out, so the existing cookie and stored session are left as they are and keep their original expiration. An explicit SignOutAsync still deletes the cookie and removes the stored session.

Tests:

  • CookieTests.CookieNotRenewedWhenRejectedByValidator, for both cookie-only and session store configurations with sliding expiration on. It rejects once before a renewal is due, once after it's due (no Set-Cookie, stored ticket not replaced), and then rejects and signs out (deletion cookie, stored session removed).
  • CookieTests.CookieNotRenewedWhenValidatorThrows, for the same two configurations. ValidatePrincipal throws and UseExceptionHandler writes a 500 response. It throws once before a renewal is due and once after it's due (no Set-Cookie, stored ticket not replaced), and then a request where validation succeeds still gets the renewal.
  • Without the handler change, both tests fail in both configurations on the request where the renewal is due: a renewed cookie is written, and with the session store RenewAsync also replaces the stored ticket with one that has a later IssuedUtc and ExpiresUtc.
  • Microsoft.AspNetCore.Authentication.Test (716 passed, 3 skipped in source) and Microsoft.AspNetCore.Identity.InMemory.Test (226 passed) pass locally.

Compatibility

The only observable change is that the response for a principal that was rejected, or whose validation threw, no longer carries a renewed cookie or triggers RenewAsync. There's no public API change, and the ticket format and data protection purposes are the same, so existing cookies and stored tickets keep working.

SecurityStampValidator already signs out when the stamp check fails, so the usual Identity flow behaves the same. When SecurityStampValidatorOptions.OnRefreshingPrincipal sets NewPrincipal to null, authentication fails without a sign-out, and that path now skips the renewal as well.

I only targeted main. Whether this should go to release/8.0 or release/10.0 is a servicing call, so I've left that to you. Happy to file a breaking change note if you think it needs one.

Fixes #69701

CookieAuthenticationHandler runs the sliding expiration check before
ValidatePrincipal so that the refresh ticket is cloned without any
per-request changes to the principal. When ValidatePrincipal then
rejected the principal, HandleAuthenticateAsync returned NoPrincipal
but kept the pending refresh. Unless sign-in or sign-out was also
called, FinishResponseAsync still wrote a renewed cookie for the
rejected ticket and, with a session store, called
ITicketStore.RenewAsync for it.

Clear the pending refresh state when validation leaves no principal.
The snapshot taken before validation and the successful path are
unchanged, and RejectPrincipal still doesn't sign out, so the existing
cookie and stored session keep their original expiration.

Fixes dotnet#69701
Copilot AI balanced review requested due to automatic review settings October 24, 2026 01:19

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions github-actions Bot added the area-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation label Oct 24, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 24, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @GrantTotinov. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 31, 2026
If ValidatePrincipal threw, for example on a transient store failure in
SecurityStampValidator before it rejects and signs out, the refresh
requested by CheckForRefreshAsync was left pending. When exception
handling middleware such as UseExceptionHandler then wrote an error
response, FinishResponseAsync renewed the cookie and, with a session
store, called ITicketStore.RenewAsync, moving IssuedUtc forward.

Move the reset into a CancelRefresh helper and also call it from a
catch around ValidatePrincipal before rethrowing.
@GrantTotinov GrantTotinov changed the title Cancel pending cookie renewal when the principal is rejected Cancel pending cookie renewal when principal validation fails Nov 7, 2026

This branch was previously deployed

1 inactive (outdated) deployment
copilot-pat-pool — ef7e3256 Deployed Oct 24, 2026 by dotnet-policy-service[bot] via pat_pool #1964
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Hardening] Cancel pending cookie renewal when principal validation rejects authentication

2 participants