Skip to content

Device-code tokens carry every client audience and ignore client audience limits #6982

Open

Description

@jhrozek

Bug description

Device-code tokens from the embedded authorization server get every audience the client is registered with. For DCR and CIMD clients that is the server's whole allowed_audiences list.

  • The resource sent to /oauth/device_authorization is ignored. The server stores client.GetAudience() (pkg/authserver/server/handlers/device_authorization.go:126) and grants all of it when the code is redeemed (pkg/authserver/server/deviceflow/handler.go:154).
  • A resource sent to /oauth/token is checked only against the server's allowed_audiences and is then added to the audience list instead of narrowing it. It is never checked against the client's own audiences (pkg/authserver/server/handlers/token.go:178-216).

The other grants check the client's audiences: authorization code (fosite, at the callback), refresh (fosite), and token exchange (tokenexchange/handler.go, which also honors Resources() for SPIFFE clients). The device flow uses the repo's own device handler and never calls GetAudienceStrategy.

The current behavior is described, as a fact, in docs/arch/11-auth-server-storage.md (the note on sub_id: "A device-flow token gets every audience its client is registered with"). I found no stated design reason for it.

Steps to reproduce

Verified with a handler-level scratch test (not an end-to-end run):

  1. Configure allowed_audiences: [A, B, C] and enable the device flow.
  2. Register a device_code DCR client. Request a device code with resource=A, approve it, and redeem it with no resource. The access token's audience is [A, B, C].
  3. With a client whose stored audiences are [A] (a DCR row written before B was added), redeem with resource=B. The access token's audience is [A, B], and the refresh token issued with it later fails with invalid_request.

Only DCR and CIMD clients can use the device grant today. Configured delegate clients and SPIFFE clients cannot.

Expected behavior

A device token has one audience. It is chosen at /oauth/device_authorization by the same rules as /oauth/authorize, checked against the client's audiences and the server's current allowed_audiences, and a resource at the token endpoint must match it.

Actual behavior

A token carries several audiences, so one token is accepted by every configured resource server, and the client's own audiences are not enforced. A removed audience can still be granted to a DCR client whose row was stored before the removal.

Proposed fix

  • At /oauth/device_authorization, pick exactly one resource the way authorize.go does (one resource, or the default when only one audience is allowed). Run ValidateAudienceURI and ValidateAudienceAllowed, then the configured GetAudienceStrategy against the client's resources (Resources() for resource-scoped clients). Reject with invalid_target. Store only that audience.
  • In applyResourceAudience, treat device_code like authorization_code: a resource must equal the bound audience, and nothing is added. Device requests created before an upgrade (10 minute TTL) may hold several audiences. For those, grant only if exactly one is stored or the resource is one of them, otherwise return invalid_target.
  • Update docs/arch/11-auth-server-storage.md.

Behavior changes for existing deployments (release note needed):

  • One allowed audience: no change.
  • Several allowed audiences and the client sends no resource: today it gets a token for all of them; after the fix it gets invalid_target.
  • A resource sent only at the token endpoint must match the audience bound at device authorization.

Acceptance criteria

  • A device token has exactly one audience.
  • A resource outside the client's audiences returns invalid_target at both endpoints.
  • No resource with more than one allowed audience returns invalid_target.
  • An audience removed from allowed_audiences cannot be granted to a stale DCR client.
  • Refreshing a device token succeeds.
  • Tests cover DCR, CIMD and stale-row cases, and the docs page is updated.

Activity

  1. added
    bug
    on Oct 28, 2026
  2. added
    needs-triage
    on Oct 28, 2026
  3. 01men commented on Oct 31, 2026

    @01men

    The resource handling here maps onto a blindness that will bite the agent case specifically, not just device flow.

    The structural point. For the three grants that delegate to fosite, the client's own audience list is the outer bound — narrowing happens at the point where the request is bound to the client. The device flow reimplements the grant in this repo and calls GetAudience instead of GetAudienceStrategy, so the client registration becomes the effective authority and the request becomes advisory. That inversion is the bug; the specific allowed_audiences behaviour is the symptom.

    Why it matters more for agents than for humans. For a human device-flow login, the registered audience list is usually one or two values, so "every audience this client is registered with" is close to "the one thing it needs". An MCP client that fronts multiple upstream servers is registered against all of them by necessity. So the same code path hands a device-flow token the union of every upstream's audience, and the resource indicator — the only mechanism the caller has to say "I want this and only this" — is discarded twice (once at /oauth/device_authorization, once at /oauth/token, where it is validated against the server's list and then added rather than used to narrow).

    The result is a token whose audience set is determined by how the client was registered, not by what the user asked for. With wildcard/hostile-URL protection otherwise in place, an audience-union token is the piece that turns a per-server authorisation into an any-server one.

    Three things I'd pin down in the fix:

    1. Narrowing must be request ∩ client, not request ∪ client. Concretely: resource present → the granted set is exactly the resources named, each of which must appear in the client's audiences and the server's allowed_audiences. Absent → keep current behaviour (otherwise every existing device-flow client breaks on upgrade, which is a separate decision).

    2. Consistency with token exchange is the acceptance test, not a code review. tokenexchange/handler.go already honours Resources() for SPIFFE clients. The natural assertion is: for a client registered with audiences {A, B}, requesting resource=A yields the same audience set via device flow as via token exchange. If that assertion is not in the suite, this regression returns the next time someone touches the device handler.

    3. The documented fact needs to change in the same commit. The note on sub_id in docs/arch/11-auth-server-storage.md states the current behaviour as a fact of the design. Leaving it after the fix means the next person reading the doc implements the bug back. Given that "no stated design reason" was found, the doc line should become an explicit "this was the behaviour until Device-code tokens carry every client audience and ignore client audience limits #6982; it is not intended", not silently deleted — a reader who remembers it will otherwise assume it still holds.

    One question, since it changes the shape of the fix: is resource supposed to be required for clients with more than one audience in the future? The "absent → current behaviour" fallback above preserves compatibility, but it also preserves the failure mode for any client that never sends the parameter. Making it required for multi-audience clients is the only version that closes the hole rather than documenting it.

  4. removed
    needs-triage
    on Nov 24, 2026
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

    authenticationbugoauth

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions