Skip to content

Fix certificate auth cache key corrupted by reentrant scheme calls - #69280

Merged
rokonec merged 2 commits into
dotnet:mainfrom
Selahattinozdmr:fix/certificate-cache-key-reentrancy
Nov 4, 2026
Merged

rokonec merged 2 commits into
dotnet:mainfrom
Selahattinozdmr:fix/certificate-cache-key-reentrancy

Conversation

@Selahattinozdmr

Copy link
Copy Markdown
Contributor
  • 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.

Fix certificate auth cache key corrupted by reentrant scheme calls

Description

CertificateAuthenticationHandler stores the current scheme's name in the shared Context.Items[CertificateSchemeCacheKeyItem] slot right before reading from CertificateValidationCache, since the cache computes its key from that slot. If an event callback (OnCertificateValidated or OnAuthenticationFailed) triggers authentication for a different certificate scheme that shares the same cache, that reentrant call overwrites the shared slot before the outer scheme's own result is written to the cache — causing the outer scheme's result to be cached under the wrong scheme's key (or the reentrant scheme's cache write to be misattributed).

The fix re-captures the outer scheme's name immediately before the cache write as well as before the cache read, so the key used for Put always reflects the scheme that is actually being authenticated, regardless of what ran in between.

Added three regression tests in CertificateTests.cs covering:

  • A scheme whose OnCertificateValidated callback re-entrantly authenticates a second scheme before the outer scheme's success is cached.
  • The same hazard through the OnAuthenticationFailed callback on the non-exception failure path.
  • That no cache entry is written at all when a callback throws, so a later identical request re-validates instead of replaying a result that was never actually produced.

Fixes #69273

CertificateAuthenticationHandler stores the current scheme's name in a
shared Context.Items slot right before reading the cache, so
CertificateValidationCache can compute a scheme-scoped key. If a
callback (OnCertificateValidated or OnAuthenticationFailed) triggers
authentication for another certificate scheme sharing the same cache,
that reentrant call overwrites the shared slot before the outer
scheme's result is cached, causing it to be stored under the wrong
scheme's key.

Re-capture the scheme name immediately before the cache write as well,
so it reflects the outer scheme regardless of what ran in between.

Fixes dotnet#69273
Copilot AI lite review requested due to automatic review settings September 14, 2026 11:24
@github-actions github-actions Bot added the area-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation label Sep 14, 2026
@Selahattinozdmr

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

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.

🟢 Approval recommended

Only minor nit-level comments remain.

Pull request overview

Fixes certificate-authentication cache key corruption caused by reentrant scheme calls.

Changes:

  • Re-establishes the scheme before cache writes.
  • Adds regression tests for reentrant callbacks and callback exceptions.
File summaries
File Summary Final comments
src/Security/Authentication/test/CertificateTests.cs Adds reentrancy and exception-path regression coverage. None
src/Security/Authentication/Certificate/src/CertificateAuthenticationHandler.cs Stabilizes scheme-specific cache writes. Two nit comments, 2 votes each: clarify the capture invariant and use is not null.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

The comment said the scheme name was captured "before any await,"
but GetClientCertificateAsync is already awaited above it — the
actual invariant is that it's captured before any application event
callback runs. Also switch the newly added _cache null check to the
repo's is not null convention instead of != null.
@javiercn

Copy link
Copy Markdown
Member

/azp run aspnetcore-ci

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@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 Sep 30, 2026

@rokonec rokonec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@rokonec
rokonec requested a review from DeagleGross November 4, 2026 18:17
@rokonec
rokonec merged commit 07ea0b4 into dotnet:main Nov 4, 2026
26 checks passed
@rokonec
rokonec temporarily deployed to copilot-pat-pool November 4, 2026 18:18 — with GitHub Actions Inactive
@rokonec
rokonec temporarily deployed to copilot-pat-pool November 4, 2026 18:18 — with GitHub Actions Inactive
@rokonec
rokonec temporarily deployed to copilot-pat-pool November 4, 2026 18:19 — with GitHub Actions Inactive
@rokonec
rokonec temporarily deployed to copilot-pat-pool November 4, 2026 18:21 — with GitHub Actions Inactive
@rokonec
rokonec temporarily deployed to copilot-pat-pool November 4, 2026 18:22 — with GitHub Actions Inactive
@rokonec
rokonec temporarily deployed to copilot-pat-pool November 4, 2026 18:23 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 10%

No docs needed: internal bug fix restoring intended certificate validation cache behavior, with regression tests only.

Documentation surfaces considered

  • Conceptual article: Not required — Bug fix in CertificateAuthenticationHandler.cs; no public API or documented behavior change.
  • Migration guidance: Not required — No action needed by users upgrading.
  • Breaking change: Not required — No compatibility impact; corrects internal cache key handling.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 82f9b202 Deployed Nov 4, 2026 by rokonec via conclusion #191
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 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.

Certificate validation cache key is not stable across application event callbacks

4 participants