Repository navigation
Description
Summary
The embedded auth server persists upstream provider refresh tokens without recording which upstream DCR client generated them. If a DCR-sensitive provider setting changes (most visibly scopes), ToolHive resolves a new DCR client but keeps existing user refresh tokens. Later refreshes present those old tokens with the current client, and the provider rejects them with invalid_grant / client_id mismatch.
This leaves every grant from the previous client generation latent-broken until its access token expires.
Verified on ToolHive v0.44.0. I also checked v0.46.0 and current main: storage.UpstreamTokens still has no upstream-client binding field, and upstreamTokenRefresher still refreshes through the currently resolved provider client.
Reproduction
- Configure an embedded-auth OAuth2 upstream using runtime DCR:
upstreamProviders:
- name: example
type: oauth2
oauth2Config:
dcrConfig:
registrationEndpoint: https://provider.example/register
scopes: [read]- Authorize a user and retain the upstream refresh token.
- Change scopes to
[read, write]and restart/reconcile the proxy. - The DCR cache key changes because it includes
ScopesHash, so ToolHive registers a new upstream OAuth client. - Let the user's old access token expire and make a request.
- ToolHive calls the provider token endpoint with the old refresh token and the new client ID. Providers that bind refresh tokens to clients reject it.
Observed provider error:
upstream token refresh failed: token request failed: invalid_grant - client_id mismatch
Root cause
pkg/authserver/server/handlers/callback.go stores:
storageTokens := &storage.UpstreamTokens{
// ...
ClientID: pending.ClientID,
}That ClientID is the downstream client (for example LibreChat), not the upstream DCR client used by BaseOAuth2Provider for the authorization-code exchange.
pkg/authserver/refresher.go later selects the provider by ProviderID and calls:
provider.RefreshTokens(ctx, expired.RefreshToken, expired.UpstreamSubject)The provider contains the current DCR-resolved oauth2.Config.ClientID. Nothing verifies that it matches the client that minted expired.RefreshToken.
There is a related reauthorization hazard: maybeCarryForwardRefreshToken can copy the latest prior refresh token into a newly authorized session when the provider omits a new refresh token, without checking its upstream DCR generation. That can reintroduce an obsolete refresh token immediately after a successful reconnect.
Fleet evidence
One deployment accumulated three upstream client generations for the same provider:
- original statically configured DCR client: 92 sessions
- first runtime-DCR client (
read): 28 sessions - current runtime-DCR client (
read + write): 79 sessions
The provider's refresh tokens are JWTs containing the public client_id claim, so classification is exact: 120 stale sessions versus 79 current sessions. Every observed client_id mismatch belonged to one of the stale generations.
The DCR client IDs themselves are long-lived; this was caused by config generations, not client expiration.
Other providers may issue opaque refresh tokens, making the same drift impossible to classify after the fact.
Expected behavior
An upstream grant should be bound to the exact upstream client/config generation that obtained it. Before refresh, ToolHive should either:
- refresh with that same upstream client while it remains registered, or
- detect a generation mismatch, delete/invalidate the stale provider grant, and return a typed reauthorization-required error without calling the token endpoint.
Proposed fix
- Add an upstream binding field to
UpstreamTokens, such asUpstreamClientIDor a stable DCR/config-generation fingerprint. - Populate it from the resolved upstream provider during callback storage.
- Validate it before refresh.
- Validate it before
maybeCarryForwardRefreshTokenreuses a prior refresh token. - On mismatch, remove the stale provider row and return a typed reauthorization-required result rather than retrying
invalid_grant. - Add provider/user-scoped revocation or migration APIs so operators do not need to manipulate Redis storage directly.
- Add tests for:
- scope changes
- static-client to runtime-DCR migration
- redirect URI / registration endpoint changes
- reauthorization where the provider omits a new refresh token
- opaque refresh-token providers
Operational impact
The failure is delayed until access-token expiry, so a rollout can look healthy for hours or days while every pre-change user grant is already doomed. Operators need a controlled reconnect migration every time a DCR-sensitive field changes until the binding is represented in storage.
Priority
Land this before the delegated credential read (#6407 "Ownership-checked credential read for delegated tokens, keyed by user and routed provider"). Once rows are shared across a user's clients (#6831's stable session IDs, #6885's storage design), a refresh token from an obsolete DCR client generation affects every client using that row.
Tracked under #6406 "Delegated client access to a user's connections".
Activity
aron-muon commentedon Sep 4, 2026
Additional verified fleet data and operational cleanup results:
client_idclaim:grafana:read): 28 rowsgrafana:read + grafana:write): 79 rowsclient_id mismatchfailures belonged to obsolete generations.query + readsubset.tsidvalues. Verification afterward: 79 current upstream rows remain, zero obsolete upstream rows remain, zero downstream refresh sessions reference an obsoletetsid, and the current DCR row is preserved.This also exposed an important migration concern for opaque-refresh-token providers: after a DCR-sensitive config change there is no way to classify old grants from token contents. The durable fix needs the upstream client/config generation persisted alongside
UpstreamTokens, not provider-specific token introspection.Add rotationKey for upstream DCR re-registration
Add dcrConfig.rotationKey to re-register upstream DCR clients (#6825)
aponcedeleonch commentedon Nov 12, 2026
Sorry for the slow response on this one, @aron-muon, and thanks for the fleet data, it makes the impact very clear.
We went back through this against current main and the core gap is still there.
UpstreamTokens.ClientIDis the downstream client, nothing records which upstream client minted a refresh token, andmaybeCarryForwardRefreshTokenonly checks the upstream subject. #6930 has landed since you filed this: an upstreaminvalid_granton refresh now drops the session's upstream rows and sends the client back through a full authorization, so sessions recover instead of staying stuck. That doesn't fix the binding, though, and there's one more way the client can change now:registrationProbeIntervalcan switch clients in place while the server runs.Here's the shape we think the fix should take:
client_idthat minted it, stamped from the client actually used for the code exchange or refresh. That's the binding the IdP enforces (RFC 6749 §6 and §10.4), and including the issuer covers a provider being pointed at a different IdP. A config fingerprint would invalidate tokens that still work, e.g. a scope change on a static client, and a client secret rotation under the sameclient_idshouldn't invalidate anything either.invalid_grantanyway.invalid_granttoo, since the IdP can still revoke tokens on its own.I'd leave out refreshing with the previous client while it's still registered. Most re-registrations happen because the old client is gone or being retired on purpose, RFC 7592 expects the old credentials to be discarded, and it would pin users to old scopes after a scope change.
@tgrunnagle, since this builds on your refresh guard in #6930, does this approach match what you had in mind?
Given your DCR work here, @aron-muon, if you'd like to take this on, let us know and I'll assign it to you. Happy to look at a draft PR early.