Repository navigation
Description
Problem
storage.ErrInvalidBinding is documented as a caller-identity check: "returned when token binding validation fails (e.g., subject or client ID mismatch)". No backend returns it, and the reads it guards cannot make the check: they take no subject to compare with. upstreamtoken/service.go maps the error anyway, so there is handling for an error nobody produces, and no code reacts to upstreamtoken.ErrInvalidBinding.
Why it matters
Upstream rows are keyed by the token session ID (tsid). Whoever presents a token carrying that claim gets the credentials stored under it, and nothing checks that the presenting user is the user those credentials were stored for.
"Re-authorization walks the full upstream chain instead of evaluating existing credentials" (#6831) plans a stable tsid shared across a user's clients, and delegated tokens will later reach the same rows ("Ownership-checked credential read for delegated tokens, keyed by user and routed provider", #6407). A stable tsid is an address, not proof of ownership, and clients can see it in the token. The owner comparison has to land before or together with stable session IDs.
Four declarations are named ErrInvalidBinding, but there are three distinct errors. This issue is about storage.ErrInvalidBinding (storage/types.go) and upstreamtoken.ErrInvalidBinding (upstreamtoken/errors.go), which maps it. sessionbinding.ErrInvalidBinding and its vMCP alias mean malformed issuer or subject claims; leave them alone.
Decision
Reads of stored upstream tokens take the expected owner and compare it with the row's UserID. A mismatch is a hard failure, never a fallback. Treating the tsid as a bearer capability with no ownership check at this layer is rejected.
This does not wait for "Identity does not expose the upstream subject, so OAuth 2.0-only upstreams cannot have a correct Cedar principal" (#6053). It complements "Redis user-keyed upstream token read does not verify row ownership" (#6273, closed): the user-keyed read, GetLatestUpstreamTokensForUser, already takes the user ID and filters by owner in both backends. This issue gives the session-keyed reads the same property.
Tracked under "Delegated client access to a user's connections" (#6406).
What to change
1. The reads that take the owner
Two storage reads are keyed by tsid, and both need the owner:
GetUpstreamTokens(ctx, sessionID, providerName, expectedUserID).GetAllUpstreamTokens, which the live request path uses:TokenValidator.loadUpstreamTokens(pkg/auth/token.go) calls the service'sGetAllUpstreamCredentials, which reads it. It must check every row, or fail the whole read.
Behavior, for both reads and both backends (memory under its existing lock; Redis needs no index or script change):
- An empty session ID, provider or expected owner is
fosite.ErrInvalidRequest, as in the user-keyed read. An empty owner never means "skip the check". - A missing row is
ErrNotFound. A row with an emptyUserID, or a differentUserID, isstorage.ErrInvalidBindingwith a nil row and no other effect. - The owner check runs before the expiry check (
ErrExpired), and before anything that refreshes. - A null-marker row still returns
(nil, nil), as callers rely on today. Do not turn it into an error.
2. Which value is the owner
The validated token's sub, exactly as issued, passed explicitly from the token's claims. Today the callback and the device flow write the same value into the row's UserID and the token's sub: the native user ID, or the synthetic subject when an OAuth 2.0 upstream runs in synthesis mode. Comparing synthetic to synthetic is a valid binding. Do not special-case synthetic or issuer-qualified subjects, and do not use sub_id, upstream_subject or a value from the request context.
A sub means something only within its issuer. loadUpstreamTokens today reads tsid from any validated token. Read upstream rows only when the token's issuer is the embedded authorization server's, the way isSessionlessGrant does.
3. Every caller passes an owner. There is no bypass.
No "skip the check" parameter, and no second read that skips it. The callers:
| Caller | Owner it passes |
|---|---|
Request path, loadUpstreamTokens (token.go) |
the validated token's sub |
Refresh-grant guard (refresh_grant_guard.go, the recheck and the bulk read) |
the stored session's subject, requester.GetSession().GetSubject() |
Refresher (refresher.go, three re-reads) |
the user of the row it snapshotted; a re-read that finds a different user stops without redeeming, writing or deleting |
Service existence check after a failed refresh (service.go) |
the same owner as the call; a mismatch counts as gone, like ErrNotFound |
Interactive chain walk (callback.go, handlers/handler.go nextMissingUpstream) |
the user being authenticated in that flow |
The refresh triggered by an expired ID token is covered by the same rule: compare first, then refresh.
4. What each caller does on a mismatch
- Request path. A hard failure: a generic 500 whose body cannot be told apart from other server errors and carries no upstream re-authorization hint. Never a 503, which invites retries, and never a 401 that asks the user to re-authorize, which would loop. Log at ERROR, or as an audit event, with the provider, the token's
client_idandjti, and both owner IDs. Never thetsidor any token value. - Refresh-grant guard. Refuse the grant, without revoking or deleting. Today it reads "any unexpected error" as "grant"; a mismatch must not take that branch.
- Not "needs authorization". A mismatch must never prompt the user to connect.
5. The service seam
upstreamtoken.Service (GetValidTokens) and upstreamtoken.TokenReader (GetAllUpstreamCredentials) take the expected owner. GetValidTokens has no production caller today; change it anyway so the seam stays consistent. refreshOrFail wraps errors with %w, so errors.Is(err, storage.ErrInvalidBinding) keeps working.
6. Files that must change
- Interfaces:
storage.UpstreamTokenStorage,upstreamtoken.TokenReader,upstreamtoken.Service. - Implementations:
MemoryStorage,RedisStorage,InProcessService. - Generated mocks:
MockStorage,MockUpstreamTokenStorage,MockTokenReader(go generateinpkg/authserver/storageandpkg/auth/upstreamtoken). - Callers listed above, and about a dozen test files (roughly 180 call sites).
CIMDStorageDecoratorandSPIFFEStorageDecoratorembedStorageand need no edit.- Docs: the interface comments, and
docs/arch/11-auth-server-storage.md. - These interfaces are exported. Code outside this repository that implements them will stop compiling, for example a storage decorator keyed by gateway and user. Say so in the PR description.
Acceptance
Add a shared contract suite for the upstream token reads (none exists today), modelled on exchange_grant_contract_test.go and run against memory and miniredis:
- the owner matches: the row is returned;
- the owner differs:
ErrInvalidBinding, a nil row, identical on both backends, for both reads; - the row has no stored user:
ErrInvalidBinding; - the expected owner is empty:
fosite.ErrInvalidRequest; - the access token is expired and the owner differs: no row, no refresher call, row unchanged;
- a null-marker row still returns
(nil, nil); - the session-keyed and user-keyed reads agree on owner semantics.
At the callers:
- the request path passes the token's
sub, refuses a token whose issuer is not the embedded server's, and returns the generic 500 on a mismatch; - the guard refuses on a mismatch and does not revoke or delete;
- the refresher re-reads a row with a different owner and stops without a write or delete;
- a synthesis-mode user is still served;
- tokens minted by token exchange never carry a
tsid(a test pins this invariant).
Out of scope
Separate issues and PRs:
- Stable session ID derivation and its opt-in, compare-and-delete in the refresher, and explicit reconnect ("Re-authorization walks the full upstream chain instead of evaluating existing credentials", Re-authorization walks the full upstream chain instead of evaluating existing credentials #6831).
- Deletes keyed only by
tsid(the refresher delete, the guard's session drop,DeleteUpstreamTokens). - Client binding of shared rows, provider account binding, scopes and revocation ("User-owned upstream credential storage (RFC): shared rows, account binding, client scope, and lifecycle", User-owned upstream credential storage (RFC): shared rows, account binding, client scope, and lifecycle #6885).
- The
upstreamCredentialScopeopt-in and the broader issuer trust ("Add upstreamCredentialScope opt-in and enforce its issuer trust", Add upstreamCredentialScope opt-in and enforce its issuer trust #6403). - The cross-replica refresh lease ("Handle multi-pod refresh token rotation race condition", Handle multi-pod refresh token rotation race condition #4122).
For the pull request
The change is about 14 files: roughly 250 lines of production code and 450 of tests. Two PRs are better than one: first the storage reads and the contract suite, with callers updated mechanically; then the behavior at the request path, guard and refresher. Each PR's description lists every caller it changed. Tests assert behavior, not implementation details. No unrelated refactors.
Activity
hellouz818 commentedon Nov 2, 2026
@jhrozek I'd like to pick this up as the first prerequisite for the stable credential session ID work in #6831.
I'll implement option 1 as documented in the Decision section: the session-keyed read will take the token's canonical user as the expected owner and compare it with the row's user. A mismatch will return
ErrInvalidBindingas a hard failure, with no fallback.Could you assign this to me if no one is already working on it?
jhrozek commentedon Nov 3, 2026
@hellouz818, thanks for picking this up. I've assigned it to you.
I rewrote the description into a plan, so please read it before you start. The main points, because a few of them changed what the issue first said:
GetUpstreamTokensandGetAllUpstreamTokens. The request path uses the bulk read, so a check on the single read alone would not protect it.sub, exactly as issued. In synthesis mode that is the synthetic subject, and it is a valid owner. Please don't special-case it.Please send this as two PRs: first the storage reads, the contract suite and the mechanical caller updates; then the behavior at the request path, the guard and the refresher. Each PR description should list every caller it changed.
This is the first step of your four-PR order. If any point is unclear, or the code disagrees with the description, ask here before you start.
jhrozek commentedon Nov 24, 2026
Closing as superseded. The owner check now lives in the user-keyed storage
wrapper ("User-keyed upstream storage behind the embedder option" (#7263)): every
read compares the row's user with the canonical user before returning or
refreshing it. For storage keyed by login session we're not adding an
owner parameter, since that would change exported signatures embedders
implement and the session ID already scopes the read. The separate issuer
comparison isn't carried over either: the wrapper only ever gets a user for
tokens the embedded authorization server issued (the validator sets it for
those alone), so a token from another issuer reads nothing. Thanks for the
careful write-up, it shaped the check.