Skip to content

Refuse delegated-access tokens in exchanges - #7180

Merged
jhrozek merged 1 commit into
mainfrom
as2-redemption-hardening-1-marker-refusals
Nov 19, 2026
Merged

jhrozek merged 1 commit into
mainfrom
as2-redemption-hardening-1-marker-refusals

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A gateway that holds a user's stored upstream credentials needs a way to tell a token from an approved delegated redemption apart from every other token. This adds that marker, and makes a marked token terminal before anything issues one, so that it can never become a longer-lived token or a token that names another party.

  • Adds the claim https://toolhive.dev/claims/delegated_access as a presence-only marker. Consumers check only that it is there, never its value. It is kept out of Identity.Claims, and out of the serialized Identity, like tsid.
  • Refuses a marked token as subject_token and as actor_token in RFC 8693 exchange, as the subject token when opening an exchange grant (reason subject_token_delegated_access), and as the actor token at ID-JAG issuance, before the client's actor validator runs (reason actor_token_delegated_access). All use invalid_request, as RFC 8693 section 2.2.2 requires for a token that policy makes unacceptable.
  • Nothing issues the marker yet, so no existing token is affected. The refusals go in first so that no marked token can ever be exchanged.
  • Corrects the RFC 7519 citation on the claim names. A URI under a domain the project controls is a Public Claim Name (section 4.2), not a Private one (section 4.3). This also fixes the same wrong citation on no_upstream_session.

Closes #6404 ("Add a delegated-access marker that gates credential lookup for approved delegated tokens")

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe): Security hardening ahead of issuing the marker

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Unit tests ran on the packages this PR touches (pkg/auth, pkg/authserver, pkg/authserver/claimnames, pkg/authserver/server/tokenexchange), with the race detector. The end-to-end flow test in pkg/authserver signs a marked login token with the server's own key and checks that grant opening refuses it, since nothing can mint one over HTTP yet.

Changes

File Change
pkg/authserver/claimnames/claimnames.go The marker claim name; RFC 7519 citation fixed
pkg/authserver/server/tokenexchange/handler.go Refusal of a marked subject or actor token in RFC 8693 exchange
pkg/authserver/server/tokenexchange/refresh_token_handler.go Refusal when opening an exchange grant; new reason subject_token_delegated_access
pkg/authserver/server/tokenexchange/refresh_token_id_jag.go, validator.go Refusal of a marked ID-JAG actor token before the actor validator; signature-and-issuer-only check
pkg/auth/context.go, pkg/auth/identity.go The marker is left out of Identity.Claims and of the serialized identity
docs/arch/17-token-exchange-delegation.md New "Delegated-access marker" section; error tables and check order updated
*_test.go Tests for each refusal, the ordering of the checks, and claim stripping

Does this introduce a user-facing change?

Nothing changes yet, because nothing issues the marker. Once something does:

  • Before: the claim would be an ordinary claim. A token carrying it could be exchanged, used as an actor token, or passed to a client's actor validator.
  • After: the token endpoint answers 400 invalid_request for such a token. At grant opening the reason is subject_token_delegated_access. At ID-JAG issuance it is actor_token_delegated_access. In RFC 8693 exchange it is a hint, "The subject token is a delegated-access token and cannot be exchanged." (or "The actor token ..."). A client should not retry with a refreshed delegated token; it needs a user's login access token.

docs/arch/17-token-exchange-delegation.md describes the behavior and the error tables.

Special notes for reviewers

  • The refusal is unconditional on purpose. There is no profile flag. Only a ToolHive authorization server emits the claim, and the refusal protects against whatever mints it. A flag kept separate from minting would allow a setup where marked tokens exist and nothing refuses them. If anything is gated on the delegated-access profile later, it should be minting.
  • The authorization server enforces the refusal; the gateway decides what the marker means. ToolHive never acts on the marker beyond refusing it and hiding it.
  • External issuers. Only the RFC 8693 subject_token path accepts trusted external issuers, so only there is an external token with the claim refused as a delegated-access token. The other paths validate against this server's own keys, so an external token fails there as an invalid token.
  • The ID-JAG actor check differs from the RFC 8693 actor check on purpose. At ID-JAG issuance the actor token belongs to the client's validator, so the server must not reject tokens it did not sign. The check verifies only the signature against this server's keys and the issuer, and refuses a marked token even when it is expired or names another audience. A verified signature is enough to refuse a token, though not to accept one. A token this server did not sign still goes to the validator unchanged. The cost is one extra signature check per ID-JAG request that has an actor token. RefreshTokenHandler keeps a single selfValidator, typed as a small interface that adds this signature-and-issuer check to the usual Validate.
  • No new policy structure. The check is a small function with four call sites, each with its own error format. A second terminal claim would be the time to generalize it.

@jhrozek
jhrozek requested a review from ChrisJBurns as a code owner November 18, 2026 15:37
@github-actions github-actions Bot added the size/M Medium PR: 300-599 lines changed label Nov 18, 2026
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.55%. Comparing base (2e0d657) to head (ca301f2).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7180      +/-   ##
==========================================
- Coverage   81.57%   81.55%   -0.03%     
==========================================
  Files         895      895              
  Lines       91356    91415      +59     
==========================================
+ Hits        74526    74549      +23     
- Misses      16825    16861      +36     
  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.

rdimitrov
rdimitrov previously approved these changes Nov 18, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve: the refusals are in the right places, cover every path that validates a token this server issued, and change nothing visible until something starts minting the marker.

Design: The authorization server owns the rule that a delegated-access token is terminal, so enforcing it at the token endpoint (subject token, actor token, exchange-grant opening, and the ID-JAG actor token) is the right layer. The ID-JAG actor check only uses "signed by this server" to refuse, never to accept, which keeps it safe. JWT-bearer and ID-JAG assertions only accept trusted external issuers, so a self-issued marked token cannot arrive there. On upgrade nothing changes, since no token carries the claim yet; only two new error reasons appear.

Optional, not blocking:

  • The marker is stripped from Identity.Claims (pkg/auth/context.go:244), so Cedar policies and audit cannot see it. That matches tsid, but a future gateway check must read the raw jwt.MapClaims, as isSessionlessGrant does. A one-line comment in claimsToIdentity would save someone a debugging session.
  • RefreshTokenHandler keeps both selfValidator and selfIssued for the same object. A handler built without selfIssued would nil-panic in signedByThisServer. Only test struct literals can do that today.

🤖 Reviewed with Claude Code

Delegated access to a user's stored upstream credentials needs a way to
tell a token from an approved delegated redemption apart from every
other token. Add the claim https://toolhive.dev/claims/delegated_access
as a presence-only marker, kept out of Identity.Claims like tsid.

Make a marked token terminal before anything issues one, so it can never
become a longer-lived token or one naming another party. It is refused
as subject and actor token in RFC 8693 exchange, as actor token at
ID-JAG issuance before the actor validator runs, and as the first case
when opening an exchange grant, with the client-visible reason
subject_token_delegated_access.

This follows "Add a delegated-access marker that gates credential lookup
for approved delegated tokens" (#6404). Nothing issues the marker yet.

Closes #6404

Co-authored-by: Mecatl <[email protected]>
@jhrozek
jhrozek force-pushed the as2-redemption-hardening-1-marker-refusals branch from 64129c8 to ca301f2 Compare November 18, 2026 20:56
@jhrozek

jhrozek commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I took both suggestions and amended them into the commit, so the head is now ca301f2.

  • claimsToIdentity now says the marker is stripped like tsid, and that a check which needs either has to read the raw validated claims instead of Identity.Claims.
  • You were right about the two fields. I replaced selfValidator and selfIssued with one selfValidator, typed as a small interface that adds signedByThisServer to Validate. The factory only sets the one field now, so a handler can't end up with half of the pair, and the test stub just answers "not signed by us". I updated the PR description to match.

Lint and the tests for the touched packages pass on the new head. Since it changed after your approval, could you take another look when you get a chance?

@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Nov 18, 2026

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of ca301f2: still approve.

The push replaces the duplicate selfIssued field on RefreshTokenHandler with one narrow unexported interface (selfIssuedValidator, which adds the refuse-only signedByThisServer), simplifies the test setup, and adds a comment in pkg/auth/context.go saying a check that needs the delegated-access marker must read the raw validated claims, since Identity.Claims strips it. Refusal behaviour is unchanged, and both actor-token paths (the RFC 8693 handler and ID-JAG) are still covered. The factory always sets a non-nil validator, so there is no nil path.

CI: the only failure is E2E Tests Core, one spec failing with listen tcp 127.0.0.1:50328: bind: address already in use (a port collision between parallel E2E workloads, in code this PR doesn't touch). A re-run should clear it.

🤖 Reviewed with Claude Code

@jhrozek
jhrozek merged commit 01b1d76 into main Nov 19, 2026
77 of 79 checks passed
@jhrozek
jhrozek deleted the as2-redemption-hardening-1-marker-refusals branch November 19, 2026 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Medium PR: 300-599 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a delegated-access marker that gates credential lookup for approved delegated tokens

2 participants