Skip to content

Introspection answers only authenticate on the scheme and host that asked for them - #393

Merged
Erwinvandervalk merged 2 commits into
mainfrom
pg/intro-scheme-isolation
Nov 16, 2026
Merged

Erwinvandervalk merged 2 commits into
mainfrom
pg/intro-scheme-isolation

Conversation

@Erwinvandervalk

Copy link
Copy Markdown
Contributor

An API can register more than one introspection scheme, each with its own authorization server and client credentials. Each scheme now only trusts answers from its own introspection endpoint. Before this change, a token that one scheme accepted could also authenticate on another scheme, without that scheme ever asking its own authorization server.

Explanation

There were two ways for an answer to cross over:

  • The cache key was {CacheKeyPrefix}{sha256(token)}. Nothing in it says which scheme stored the entry, so with the default empty prefix, two schemes read and write the same entry.
  • Concurrent requests for the same token share one introspection call through a static map, keyed by the token only. A request on a second scheme joined the call of the first scheme and got its answer, even with different prefixes. It then cached that answer under its own key, so later requests kept passing.

The handler now puts the scheme name in front of whatever CacheKeyGenerator returns, and the shared calls are keyed by the scheme's options instance and the token.

Before and after

A token is active at the partner authorization server and inactive at the internal one. Before: after a request to the partner endpoint, the same token got a 200 on the internal endpoint and the internal authorization server was never called. After: the internal scheme asks its own server and returns 401. This holds for requests in a row and at the same time, and also for two hosts in one process that use the same scheme name.

Decisions

  • The scheme goes into the key in the handler, not in the default CacheKeyPrefix, so custom key generators and schemes with equal prefixes are covered too.
  • The scheme name is written with its length (6:Bearer:...), so a scheme named a:b can't produce the same key as scheme a with prefix b:.
  • The shared calls are keyed by the options instance, the same place the handler already keeps per-scheme state such as the client assertion. That is one entry per scheme per host.

Upgrade notes

  • Entries cached by earlier versions are not read any more, so each token is introspected once more after the upgrade. Old entries expire as before.
  • APIs that share one distributed cache and use the same scheme name still need a different CacheKeyPrefix each, as before.

Where to look

  1. OAuth2IntrospectionHandler.cs: the cache key and the in-flight key.
  2. Infrastructure/CacheExtensions.cs: now takes the key instead of building it.

An introspection answer is only valid for the scheme whose own endpoint
and client credentials produced it (RFC 7662 section 2.2). Two places
let it cross that boundary:

- The cache key had no scheme in it. The handler now puts the scheme
  name, with its length, in front of the CacheKeyGenerator output. The
  length keeps names that contain ':' from spelling another scheme's key.
  This also covers custom key generators.
- The static in-flight map was keyed by token only, so a request on one
  scheme, or in another host in the same process, could join a call made
  for someone else. It is now keyed by the options instance and token,
  which is one entry per scheme per host, like the client assertion state
  the handler already keeps on the options.

Cached entries written by earlier versions are not read any more; each
token is introspected once more after the upgrade.
@Erwinvandervalk
Erwinvandervalk merged commit b640d8d into main Nov 16, 2026
5 checks passed
@Erwinvandervalk
Erwinvandervalk deleted the pg/intro-scheme-isolation branch November 16, 2026 17:47

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.

🟡 Changes recommended

A shared-cache key collision with legacy hosts can cause the new handler to accept another authority’s active response, requiring a collision-safe key format, custom-generator migration guidance, and a regression test.

1 open issue
What changed in this PR

This PR isolates introspection answers between authentication schemes and prevents concurrent requests in separate hosts from sharing an authority’s response.

Changes:

  • Adds a length-prefixed scheme name to cache keys, including custom-generated keys.
  • Keys shared in-flight calls by options instance and token.
  • Adds scheme and host isolation tests and updates existing cache-key assertions.
File Description
introspection/​test/​AspNetCore.Authentication.OAuth2Introspection.Tests/​IntrospectionAnswerIsolation.cs Adds sequential, concurrent, cross-host, and ambiguous-key isolation tests.
introspection/​test/​AspNetCore.Authentication.OAuth2Introspection.Tests/​Introspection.cs Updates cache assertions for scheme-qualified keys.
introspection/​src/​AspNetCore.Authentication.OAuth2Introspection/​OAuth2IntrospectionHandler.cs Scopes cache keys by scheme and in-flight calls by options instance.
introspection/​src/​AspNetCore.Authentication.OAuth2Introspection/​Infrastructure/​CacheExtensions.cs Accepts precomputed keys for cache reads and writes.

Review effort: Balanced

}

var claims = await _cache.GetClaimsAsync(Options, token).ConfigureAwait(false);
var cacheKey = $"{Scheme.Name.Length}:{Scheme.Name}:{Options.CacheKeyGenerator(Options, token)}";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants