Repository navigation
Conversation
The shape check for the authorization_details form parameter is copied by hand in two places, and the hook Subject is built field by field in two places. The two Subject literals have already drifted: the one for token exchange sets External, the one for exchange-grant opening does not. The planned work to narrow a JWT-bearer access token to one MCP call (#7061) needs the same reading and the same Subject in two more places: - ID-JAG redemption will refuse the authorization_details parameter, because the entries come from the assertion's claim instead. It reads the parameter with readDetailsParam, so a repeated parameter, two empty values included, is treated the same as here. - ID-JAG redemption will build the hook Subject for the assertion with detailsSubject, then set External to true and remove the authorization_details key from Claims. detailsSubject returns a value so that the caller can change it. - ID-JAG minting will read the parameter with readDetailsParam and sign the entries into the ID-JAG. That replaces its current rule, under which two empty values count as absent, so all readers then agree on what "repeated" means. Without the helpers each of those adds another copy of the check and of the Subject literal, and the existing copies show that they drift. Add readDetailsParam, which only reports the shape (absent, repeated or one value), and detailsSubject, which returns the hook Subject as a value. Token exchange and exchange-grant opening use them and keep their own policy and wire text. ID-JAG minting keeps its own rule for now. At exchange-grant opening External is now set from the claims. It is always false there, because the login token check refuses external tokens first, and only ToolHive's internal shape hook sees it. No error, description, status or log line changes. Add tests for the two-empty-values shape and for the repeated and malformed WARN fields, which no test asserted before. Co-authored-by: Mecatl <noreply@mecatl.dev>
The hook Subject at exchange-grant opening had no test, so a later change to detailsSubject could drop a field or stop copying Claims or Audience without any test failing. The later ID-JAG redemption change builds on detailsSubject, so this is the pin that matters most. Add a test that records the Request the exchange-grant hook gets and checks every Subject field, that External is false, and that Claims and Audience are copies. Add a test for the WARN line when an exchange-grant shape is refused, rows for ["",""] and ["","x"] in the token-exchange and exchange-grant tables, and require the token exchange WARN line to appear exactly once. Co-authored-by: Mecatl <noreply@mecatl.dev>
rdimitrov
left a comment
There was a problem hiding this comment.
Approve: a behaviour-preserving refactor that removes real drift between the two copies.
Design: The authorization_details parameter check and the authorizationdetails.Subject construction both belong in pkg/authserver/server/tokenexchange, which owns the parameter and the claims, so the shared helpers sit in the right package. Both readers (parseAuthorizationDetails, parseExchangeGrantDetails) and both claim-based Subject builders now use them. decideChildEntry correctly keeps its own minimal Subject because there are no validated claims on that path, and checkIDJAGExtraParameters is deliberately left alone, as the PR says. The one semantic change, Subject.External now being set in decideEntry, is unreachable today because checkLoginToken refuses a non-empty ExternalIssuer before decideEntry runs. There is no wire or operator impact on upgrade.
Optional, not blocking:
authdetails.go:75andexchange_grant.go:101treat any error fromreadDetailsParamas "repeated". If it ever returns a second error kind, both callers will misreport it.errors.Is(err, errRepeatedDetailsParam)would keep that safe.- A one-line comment on
decideChildEntrysaying why it doesn't use the shared Subject helper would stop someone "fixing" it later.
🤖 Reviewed with Claude Code
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7217 +/- ##
=======================================
Coverage 81.59% 81.59%
=======================================
Files 898 898
Lines 91621 91622 +1
=======================================
+ Hits 74760 74761 +1
Misses 16856 16856
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Part of #7061 (Authorization server: narrow a JWT-bearer access token to one MCP call with authorization_details).
Why. The shape check for the
authorization_detailsform parameter is copied by hand in two places (token exchange and exchange-grant opening). The hookauthorizationdetails.Subjectis built field by field in two places. The twoSubjectliterals have already drifted: token exchange setsExternal, exchange-grant opening does not.The next steps for #7061 need the same reading and the same
Subjectin two more places. This PR adds one helper for each, so those steps reuse them instead of adding a third and fourth copy:authorization_detailsparameter (the entries come from the assertion's claim)readDetailsParam""passes. One value, or any repeat (["",""]included), is refused. "Repeated" means the same thing as in token exchange.detailsSubjectSubjectfrom the assertion's claims, then setsExternal: trueand removes theauthorization_detailskey fromClaims. This is whydetailsSubjectreturns a value, not a pointer.readDetailsParam["",""]counts as absent. After that, every reader of the parameter agrees on what "repeated" means.Without the helpers, each of those rows is another hand-written copy. The two copies that already exist have drifted.
What.
readDetailsParam(form) (raw, present, err)only reports the shape: absent (or one""), repeated (errRepeatedDetailsParam), or one value. Each caller keeps its own policy and wire error. Token exchange ignores an absent parameter. Exchange-grant opening requires it.detailsSubject(claims)builds the hookSubjectand copiesAudienceandClaims, so a hook cannot change the validated claims.parseAuthorizationDetails,grantAuthorizationDetails) and exchange-grant opening (parseExchangeGrantDetails,decideEntry) use them.checkIDJAGExtraParameters) is not changed in this PR.Subjectat exchange-grant opening, thereason=repeatedandreason=malformedWARN fields, the exchange-grant refusal log line, and the["",""]and["","x"]shapes.Type of change
Test plan
task test)task lint-fix)Ran
go test -race -count=1 ./pkg/authserver/server/tokenexchange/... ./pkg/authserver/on the branch head. Both packages pass.Does this introduce a user-facing change?
No. Every shape of the parameter (absent,
[""],["",""],["","x"],["x"],["x","y"], malformed, no registry configured) gives the same error code, description, status and log line as before.Special notes for reviewers
Subject.Externalis now set from the claims rather than left at its zero value. It is alwaysfalsethere, becausecheckLoginTokenrefuses tokens from an external issuer before the hook runs. ThatSubjectgoes only to ToolHive's internal exchange-grant shape hook, which doesn't read it. An embedder's registry is never called on this path. The newTestRefreshTokenHandlerHookSubjectpins every field.["",""]as absent. That difference already exists and stays until the minting change in the table above replaces it.