Skip to content

Refresh tokens are sent to a repointed upstream's new IdP #7333

Backlog

Description

@jhrozek

Bug description

Stored upstream token rows don't record which identity provider issued them. The auth server picks the provider for a refresh by the upstream's name only (refresher.go, r.providers[expired.ProviderID]). If an operator points an upstream at a different IdP but keeps the name and the store, the next refresh of every existing session sends the old IdP's refresh token to the new IdP's token endpoint.

The refresh grant guard (refresh_grant_guard.go) makes the same choice when the stored upstream access token has expired, and callback carry-forward (maybeCarryForwardRefreshToken) can copy an earlier refresh token into a new row on the same basis.

This was already the case before "Bind provider-identity rows to the issuer and subject claim that created them" (#7256). That work binds identity-link rows to their issuer and subject claim, but upstream token rows have no such binding.

Steps to reproduce

  1. Run the embedded auth server with Redis storage and an OIDC upstream named default at IdP A. Sign in, so a session holds an upstream refresh token from A.
  2. Change default's issuer to IdP B. Keep the name and the Redis store, then restart.
  3. Use the session until its upstream access token expires, then use a ToolHive refresh token (or any request that triggers an upstream refresh).

Expected behavior

A refresh token is only sent to the authority that issued it. A row from a different issuer is treated as unusable: the refresh fails and the user signs in again.

Actual behavior

The auth server sends IdP A's refresh token to IdP B's token endpoint. B normally rejects it, but A's credential has still been disclosed to B. For a claim-only change on the same IdP, the refresh succeeds and the session keeps working for the old account.

Additional context

Possible fix: record the issuer (or the same binding the identity rows now use) on UpstreamTokens when the row is written. Then refuse to refresh, or to carry a refresh token forward, when it doesn't match the upstream's current configuration. Rows written before the change carry no issuer and need a rule, like the stamp-on-first-match rule in #7256.

Until then, operators should revoke existing sessions before repointing an upstream.

Activity

  1. added
    bug
    on Nov 26, 2026
  2. added
    needs-triage
    on Nov 26, 2026
  3. 01men commented on Dec 3, 2026

    @01men

    The issuer half is right, but recording and comparing it does not close the path in three places — and one of them is not on the refresh path at all.

    1. A pure OAuth2 upstream has no issuer to compare against

    OIDCConfig.Issuer exists (pkg/authserver/upstream/oidc.go:43), but OAuth2Provider does not expose it: the interface (oauth2.go:72-95) is Type / AuthorizationURL / ExchangeCodeForIdentity / RefreshTokens, and OAuth2Config carries only TokenEndpoint (oauth2.go:176). So the refresher resolves the provider as r.providers[expired.ProviderID] (refresher.go:275) and has nothing to compare an issuer against for a non-OIDC upstream.

    Adding Issuer() string to the interface is the obvious fix and it is the one thing this package rules out: the IdleConnectionCloser comment (oauth2.go:364-378) says widening OAuth2Provider breaks the external implementations doc.go invites, which is why that capability is optional and type-asserted. So either make the issuer an optional capability, or do not ask the provider at all — newUpstreamTokenRefresher (server_impl.go:1332-1336) already receives []handlers.NamedUpstream and iterates it into the provider map (:1340-1343), so the configured UpstreamConfig is in hand at construction and can be the stamping source. For a pure OAuth2 upstream the binding is then TokenEndpoint, which is the field that actually changes when an operator repoints it.

    2. The sign-in callback carries a refresh token forward with less checking than the refresher

    maybeCarryForwardRefreshToken (pkg/authserver/server/handlers/callback.go:459) copies a prior refresh token into the new row, and its guard at :483 is the package-level priorMatches (:525-527):

    return synthetic || prior.UpstreamSubject == providerSubject

    For a synthetic upstream — OAuth2 with no userinfo/identity config — the comparison is skipped and a non-empty prior token is enough. That is a login-time write of the old IdP's refresh token into the new row and it never reaches refresher.go, so a stamp added only in refreshAndStore (refresher.go:316-326) leaves this path behaving exactly as today.

    Worth noting the connect entry point already closed this: connectWrite.priorMatches (callback.go:541-546) returns !synthetic && prior.UpstreamSubject == providerSubject. The sign-in path is the one still on the loose rule, so the fix here is to apply the same !synthetic conjunct rather than to invent new logic.

    3. The subject check that does exist is conditional on the response

    OIDCProviderImpl.RefreshTokens (oidc.go:614) only compares expectedSubject when the refreshed response carries an ID token and a verifier is set (:654). OIDC Core §12.2 makes a refresh-response ID token optional, and a pure OAuth2 upstream has none to check — BaseOAuth2Provider.RefreshTokens (oauth2.go:965) ignores the argument outright. So on a repointed upstream, a claim-only change on the same IdP can already pass today with expectedSubject never compared. The IdP owed you no id_token, so nothing was compared.

    On the legacy-row rule

    #7256's stamp-on-first-match works there because a wrong stamp costs one re-login. Here a wrong stamp costs a refresh token disclosed to a different IdP, so stamping a legacy row with the current configuration on first use assumes the operator repointed after the last login — for the pod-B-restarts case in the report that assumption is exactly backwards.

    Recording the generation at write time fails closed instead of adopting. clientGeneration (refresher.go:80, bumped by bumpClientGeneration at :97) is already bumped from UpdateUpstreamClientCredentials (server_impl.go:1385, bump at :1398-1401) once a provider has swapped its client registration — the swap path exists, this only adds one caller for an issuer change. A row carrying the generation it was written under then refuses to refresh on a mismatch instead of silently inheriting the new config. Rows with no generation still need a rule, but it becomes "unknown ⇒ refuse, require re-login", and the blast radius is bounded to sessions predating the change rather than to every session that outlives the rollout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    authenticationbugneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions