Skip to content

bug: Token-exchange cache serves one route's token to another, and default exchange sends broad tokens anywhere #1563

Closed

Description

@huang195

Problem

The token-exchange plugin can hand a user a token they should not get. The cause is that the OAuth client and token cache exist twice, and the safety checks landed in only one copy. Line numbers are at ab5f01f26.

1. The cache key ignores scopes and the token endpoint

core/plugins/tokenexchange/cache/cache.go:107-113 keys on (subjectToken, audience) only, which is how core/auth/auth.go:418-428, 471-473 calls Get/Set. Each route also has its own Scopes and TokenEndpoint (tokenexchange/plugin.go:554-558).

  • Case: route A (host: a-svc, audience X, scopes openid) and route B (host: b-svc, audience X, scopes openid X-admin). If the user calls b-svc first, a-svc gets the admin-scoped token from the cache until it expires.
  • Two routes with the same audience but different token_url (two realms or IdPs) are served each other's tokens.
  • No test covers either case.

Fix: put scopes and the token endpoint into the key.

2. default_policy: exchange sends a token with no audience restriction to any host

For an unrouted host with audience_from_host: false (the default), the audience is "" (auth.go:393-400, 443-448), so exchange/client.go:88-90 leaves audience out of the form.

The sibling plugin documents what that request returns and refuses to send it: "an exchange without one succeeds and returns a token for every audience the caller holds" (tokenexchangeaiac/idp.go:123-130). So an agent calling any unrouted host, attacker-controlled hosts included, sends that broad token there.

docs/token-exchange-plugin.md:12 calls this "empty-audience client-credentials", but when a bearer is present the code does a token exchange.

Fix: on the exchange path, deny when the resolved audience is "", as tokenexchangeaiac does. Alternatively, require audience_from_host when default_policy=exchange.

3. tokenexchangeaiac re-implements the client and cache

Copies:

  • Client: tokenexchangeaiac/idp.go:127-200 and tokenexchange/exchange/client.go:77-185.
  • Cache: tokenexchangeaiac/cache.go:25-85 and tokenexchange/cache/cache.go:16-113.

Both post an RFC 8693 / client-credentials form, read the response with a size limit, parse error/error_description, and keep a 10k-entry cache with 30s skew keyed by a SHA-256 of the subject.

What differs:

  • aiac refuses an empty audience and checks aud after issue (audienceContains). The shared client does neither, which is why problem 2 exists.
  • aiac clears the whole map when it is full; tokenexchange evicts about 25% at random.
  • aiac has drop().

Fix: move the empty-audience guard and the post-issue aud check into exchange.Client, add Drop to cache.Cache, and make aiac use both. That fixes problem 2 and removes the drift.

Found by a code audit of main at ab5f01f. Problem 2 relies on the codebase's own comment about Keycloak's behaviour; it was not reproduced against Keycloak.

Assisted-By: Claude (Anthropic AI) [email protected]

Activity

  1. added
    bug
    Identity
    on Oct 29, 2026
  2. self-assigned this
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Identitybug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions