Skip to content

Return invalid_target when token exchange refuses an audience - #7050

Merged
ChrisJBurns merged 2 commits into
stacklok:mainfrom
pcbeingused333:fix/token-exchange-audience-invalid-target
Nov 5, 2026
Merged

ChrisJBurns merged 2 commits into
stacklok:mainfrom
pcbeingused333:fix/token-exchange-audience-invalid-target

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Summary

RFC 8693 §2.2.2 says invalid_target SHOULD be returned when the authorization server refuses a requested resource or audience. The token exchange handler already did this for resource, but for audience it passed fosite's audience-matching error through unchanged, and that error is invalid_request. A client that branches on the error code saw a different code depending on which parameter it used.

  • New server.ErrAudienceNotPermitted(audience), next to errResourceNotPermitted: ErrInvalidTarget with a hint naming the refused value. Like the resource hint, it does not say which list refused it.
  • grantAudiences returns it when the audience strategy refuses a requested audience. When several are requested, the hint names the first refused one.
  • An audience that is not a parseable URI keeps invalid_request, because that is a malformed request.
  • Default audience (grantDefaultAudience): when the request names no target and the server's sole default audience is not one of the client's audiences, the refusal stays invalid_request. The client did not request a target, and the existing hint already tells it to send an explicit resource or audience. This case is tested and documented.
  • resource=X outside the client's audiences is still invalid_target. That is unchanged and now covered in the same test.
  • Docs: new "Token exchange audience refusal" bullet in docs/arch/17-token-exchange-delegation.md.

Before (audience=https://other.example, which is not one of the client's audiences):

400 {"error":"invalid_request","error_description":"The request is missing a required parameter, ... or is otherwise malformed. Requested audience 'https://other.example' has not been whitelisted by the OAuth 2.0 Client."}

After:

400 {"error":"invalid_target","error_description":"The requested resource is invalid, unknown, or malformed. The client is not permitted to request audience \"https://other.example\""}

Fixes #7027

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (go test ./pkg/authserver/server/..., plus go vet and gofmt -l on the package; not the full task test)
  • E2E tests (task test-e2e)
  • Linting (golangci-lint run ./pkg/authserver/server/..., v2.15.0 with the repo .golangci.yml: 0 issues)
  • Manual testing (describe below)

The new TestTokenExchangeHandler_AudienceRefusalErrorCode has 5 cases: refused audience, second of two audiences refused, refused resource, unparsable audience, and refused default audience. The two audience cases fail without the fix. The audience rows in handler_test.go and handler_source_policy_test.go asserted the old invalid_request. They are updated, and they also fail without the fix.

Does this introduce a user-facing change?

Yes. A token exchange request whose audience is outside the client's audiences now gets invalid_target instead of invalid_request, the same as resource.


This change was prepared with the help of an AI coding assistant (Claude); the submitter is responsible for it.

RFC 8693 section 2.2.2 says invalid_target SHOULD be returned when the
authorization server refuses a requested resource or audience. The token
exchange handler surfaced fosite's invalid_request instead.

Map a refused audience or resource to invalid_target with a hint naming
the offending value. Unparsable audience URIs and the default-audience
path keep invalid_request, since no valid target was requested there.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Alex Castillo <[email protected]>
@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.76%. Comparing base (773c944) to head (2a2378e).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7050      +/-   ##
==========================================
+ Coverage   80.62%   80.76%   +0.13%     
==========================================
  Files         857      859       +2     
  Lines       87455    87925     +470     
==========================================
+ Hits        70512    71012     +500     
+ Misses      16938    16908      -30     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ChrisJBurns
ChrisJBurns merged commit 9d4eeab into stacklok:main Nov 5, 2026
43 checks passed
@ChrisJBurns

Copy link
Copy Markdown
Collaborator

Thank you @pcbeingused333 !! 🚀

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return invalid_target when token exchange refuses an audience parameter

2 participants