Skip to content

Upstream token read cannot verify ownership: ErrInvalidBinding documents a subject check the signature cannot make #6176

Not planned

Description

@jhrozek

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's GetAllUpstreamCredentials, 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 empty UserID, or a different UserID, is storage.ErrInvalidBinding with 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_id and jti, and both owner IDs. Never the tsid or 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 generate in pkg/authserver/storage and pkg/auth/upstreamtoken).
  • Callers listed above, and about a dozen test files (roughly 180 call sites).
  • CIMDStorageDecorator and SPIFFEStorageDecorator embed Storage and 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:

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

  1. added
    needs-triage
    on Aug 3, 2026
  2. added
    bug
    and removed
    needs-triage
    on Aug 6, 2026
  3. hellouz818 commented on Nov 2, 2026

    @hellouz818
    Contributor

    @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 ErrInvalidBinding as a hard failure, with no fallback.

    Could you assign this to me if no one is already working on it?

  4. jhrozek commented on Nov 3, 2026

    @jhrozek
    ContributorAuthor

    @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:

    • Two reads need the owner, not one: GetUpstreamTokens and GetAllUpstreamTokens. The request path uses the bulk read, so a check on the single read alone would not protect it.
    • The owner is the validated token's sub, exactly as issued. In synthesis mode that is the synthetic subject, and it is a valid owner. Please don't special-case it.
    • No bypass: every caller passes an owner. The table in the description lists each caller and what it passes.
    • On a mismatch the request path returns a generic 500, never a 503 and never a 401 that asks the user to sign in again. The refresh-grant guard refuses the grant.
    • The description also lists the files that must change, the tests I'd like (including a new shared contract suite for these reads), and what is out of scope.

    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.

  5. jhrozek commented on Nov 24, 2026

    @jhrozek
    ContributorAuthor

    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.

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions