Repository navigation
Description
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
resourcesent to/oauth/device_authorizationis ignored. The server storesclient.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
resourcesent to/oauth/tokenis checked only against the server'sallowed_audiencesand 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):
- Configure
allowed_audiences: [A, B, C]and enable the device flow. - Register a
device_codeDCR client. Request a device code withresource=A, approve it, and redeem it with noresource. The access token's audience is[A, B, C]. - With a client whose stored audiences are
[A](a DCR row written before B was added), redeem withresource=B. The access token's audience is[A, B], and the refresh token issued with it later fails withinvalid_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 wayauthorize.godoes (oneresource, or the default when only one audience is allowed). RunValidateAudienceURIandValidateAudienceAllowed, then the configuredGetAudienceStrategyagainst the client's resources (Resources()for resource-scoped clients). Reject withinvalid_target. Store only that audience. - In
applyResourceAudience, treatdevice_codelikeauthorization_code: aresourcemust 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 theresourceis one of them, otherwise returninvalid_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 getsinvalid_target. - A
resourcesent only at the token endpoint must match the audience bound at device authorization.
Acceptance criteria
- A device token has exactly one audience.
- A
resourceoutside the client's audiences returnsinvalid_targetat both endpoints. - No
resourcewith more than one allowed audience returnsinvalid_target. - An audience removed from
allowed_audiencescannot 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
01men commentedon Oct 31, 2026
The
resourcehandling 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
GetAudienceinstead ofGetAudienceStrategy, so the client registration becomes the effective authority and the request becomes advisory. That inversion is the bug; the specificallowed_audiencesbehaviour 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:
Narrowing must be
request ∩ client, notrequest ∪ client. Concretely:resourcepresent → the granted set is exactly the resources named, each of which must appear in the client's audiences and the server'sallowed_audiences. Absent → keep current behaviour (otherwise every existing device-flow client breaks on upgrade, which is a separate decision).Consistency with token exchange is the acceptance test, not a code review.
tokenexchange/handler.goalready honoursResources()for SPIFFE clients. The natural assertion is: for a client registered with audiences {A, B}, requestingresource=Ayields 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.The documented fact needs to change in the same commit. The note on
sub_idindocs/arch/11-auth-server-storage.mdstates 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
resourcesupposed 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.