From 7cab186b7a185209774c0c2cb58c00515012f123 Mon Sep 17 00:00:00 2001 From: kanywst <45947799+kanywst@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:36:14 +0900 Subject: [PATCH] Bind JWT-bearer resource to the assertion claim A JWT-bearer redemption checked the requested resource only against the subject binding, so an ID-JAG issued for one resource could be redeemed for any other resource the binding allowed. The request also had to carry a resource parameter, which Okta's documented redemption request omits. When the assertion carries a resource claim, the requested resource must be one of its values, or the grant fails with invalid_target. Without the parameter, a string claim is used; an array claim, or no claim, is refused with invalid_request. A malformed claim is invalid_grant. Ref: #6892 Signed-off-by: kanywst <45947799+kanywst@users.noreply.github.com> --- docs/arch/17-token-exchange-delegation.md | 14 +++- .../tokenexchange/id_jag_handler_test.go | 73 ++++++++++++++++++ .../tokenexchange/jwt_bearer_handler.go | 76 +++++++++++++++++-- 3 files changed, 153 insertions(+), 10 deletions(-) diff --git a/docs/arch/17-token-exchange-delegation.md b/docs/arch/17-token-exchange-delegation.md index bc6e6d7e3d..133cbe746a 100644 --- a/docs/arch/17-token-exchange-delegation.md +++ b/docs/arch/17-token-exchange-delegation.md @@ -1184,10 +1184,16 @@ spec: Beyond cryptographic and claim validation, `JWTBearerGrantPolicy.SubjectBindings` is an exact-match allowlist: an assertion's `sub` must have a configured - binding, and the request's `resource` parameter (required, exactly one) - must be one of that binding's `allowed_resources`. There is no wildcard - subject or resource — every accepted assertion subject and resource pair is - named explicitly. + binding, and the requested resource must be one of that binding's + `allowed_resources`. There is no wildcard subject or resource — every + accepted assertion subject and resource pair is named explicitly. + + When the assertion carries a `resource` claim (a string or an array), the + `resource` parameter must name one of its values, or the grant fails with + `invalid_target`. Without the parameter, a string claim is used; an array + claim, or no claim, fails with `invalid_request`. The resolved resource then + goes through the subject binding and, for an ID-JAG, the client audience + check. - **Synthetic subject and client identity.** The issued token's `sub` is `#` (`session.New`'s first argument, matching the delegated-token subject qualification pattern in [Accepted diff --git a/pkg/authserver/server/tokenexchange/id_jag_handler_test.go b/pkg/authserver/server/tokenexchange/id_jag_handler_test.go index 178b711ccf..9e653fa588 100644 --- a/pkg/authserver/server/tokenexchange/id_jag_handler_test.go +++ b/pkg/authserver/server/tokenexchange/id_jag_handler_test.go @@ -328,6 +328,79 @@ func TestIDJAGHandler_ClientGrantAndAudience(t *testing.T) { } } +func TestIDJAGHandler_ResourceClaim(t *testing.T) { + t.Parallel() + + const otherResource = "https://other.example.com" + tests := []struct { + name string + claim any // nil leaves the assertion without a resource claim + omitResource bool + clientAudience []string // nil keeps idJAGTestResource + wantErrIs error + }{ + {name: "requested resource equal to the claim is accepted", claim: idJAGTestResource}, + {name: "requested resource different from the claim is refused", claim: otherResource, wantErrIs: server.ErrInvalidTarget}, + {name: "omitted resource uses a single-valued claim", claim: idJAGTestResource, omitResource: true}, + { + name: "one-element array claim with omitted resource is refused", + claim: []any{idJAGTestResource}, + omitResource: true, + wantErrIs: fosite.ErrInvalidRequest, + }, + {name: "omitted resource without a claim is refused", omitResource: true, wantErrIs: fosite.ErrInvalidRequest}, + { + name: "defaulted resource outside the client audiences is refused", + claim: idJAGTestResource, + omitResource: true, + clientAudience: []string{otherResource}, + wantErrIs: server.ErrInvalidTarget, + }, + {name: "array claim naming the requested resource is accepted", claim: []any{otherResource, idJAGTestResource}}, + { + name: "array claim not naming the requested resource is refused", + claim: []any{otherResource, "https://third.example.com"}, + wantErrIs: server.ErrInvalidTarget, + }, + { + name: "array claim with omitted resource is refused as ambiguous", + claim: []any{otherResource, idJAGTestResource}, + omitResource: true, + wantErrIs: fosite.ErrInvalidRequest, + }, + {name: "non-string claim is refused", claim: 42.0, wantErrIs: fosite.ErrInvalidGrant}, + {name: "empty array claim is refused", claim: []any{}, wantErrIs: fosite.ErrInvalidGrant}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + tj := newTestJWKS(t) + claims := validIDJAGClaims() + if tt.claim != nil { + claims.Extra["resource"] = tt.claim + } + consumer := &recordingAssertionConsumer{} + h := newIDJAGTestHandler(t, &testJWTBearerAssertionValidator{claims: claims}, consumer) + req := newIDJAGRequest(t, tj, idJAGTestClientID) + if tt.omitResource { + req.Form.Del("resource") + } + if tt.clientAudience != nil { + req.Client = &fosite.DefaultClient{ID: idJAGTestClientID, Audience: tt.clientAudience} + } + + err := h.HandleTokenEndpointRequest(t.Context(), req) + if tt.wantErrIs == nil { + require.NoError(t, err) + assert.Equal(t, fosite.Arguments{idJAGTestResource}, req.GetGrantedAudience()) + return + } + assert.ErrorIs(t, err, tt.wantErrIs) + assert.Empty(t, consumer.key, "a refused redemption must not consume the assertion") + }) + } +} + func TestIDJAGHandler_IssuesBoundSession(t *testing.T) { t.Parallel() diff --git a/pkg/authserver/server/tokenexchange/jwt_bearer_handler.go b/pkg/authserver/server/tokenexchange/jwt_bearer_handler.go index 5e309f2988..a043776feb 100644 --- a/pkg/authserver/server/tokenexchange/jwt_bearer_handler.go +++ b/pkg/authserver/server/tokenexchange/jwt_bearer_handler.go @@ -229,18 +229,17 @@ func validateJWTBearerPolicy(form url.Values, claims *ValidatedClaims, policy *J if claims.IssuedAt.IsZero() || claims.Expiry.IsZero() || claims.Expiry.Sub(claims.IssuedAt) > policy.maxAssertionAge { return "", errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("The JWT bearer assertion exceeds the configured maximum age.")) } - resources, present := form["resource"] - if !present || len(resources) != 1 || resources[0] == "" { - return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint( - "Exactly one resource parameter is required for the JWT-bearer grant.")) + requested, err := resolveJWTBearerResource(form, claims) + if err != nil { + return "", err } - if err := server.ValidateAudienceURI(resources[0]); err != nil { + if err := server.ValidateAudienceURI(requested); err != nil { return "", errorsx.WithStack(err) } for _, binding := range policy.SubjectBindings { if binding.Subject == claims.Subject { for _, resource := range binding.AllowedResources { - if resource == resources[0] { + if resource == requested { return resource, nil } } @@ -254,6 +253,71 @@ func validateJWTBearerPolicy(form url.Values, claims *ValidatedClaims, policy *J "The JWT bearer assertion subject is not configured for this grant.")) } +// resolveJWTBearerResource picks the resource to issue for. When the assertion +// carries a resource claim, the request must name one of its values. An +// omitted resource parameter falls back to a string claim; with an array claim +// it is ambiguous, and with no claim there is no default. +func resolveJWTBearerResource(form url.Values, claims *ValidatedClaims) (string, error) { + claimed, err := assertionResourceClaim(claims) + if err != nil { + return "", err + } + resources, present := form["resource"] + if !present { + if single, ok := claims.Extra["resource"].(string); ok { + return single, nil + } + if claimed == nil { + return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint( + "A resource parameter is required because the assertion carries no resource claim.")) + } + return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint( + "A resource parameter is required because the assertion's resource claim is an array.")) + } + if len(resources) != 1 || resources[0] == "" { + return "", errorsx.WithStack(fosite.ErrInvalidRequest.WithHint( + "Exactly one resource parameter is required for the JWT-bearer grant.")) + } + if claimed != nil && !slices.Contains(claimed, resources[0]) { + return "", errorsx.WithStack(server.ErrInvalidTarget.WithHint( + "The requested resource is not one the assertion was issued for.")) + } + return resources[0], nil +} + +// assertionResourceClaim returns the assertion's resource claim as a list, or +// nil when the claim is absent. The claim is a string or an array of strings; +// any other shape, or an empty value, is refused rather than treated as absent. +func assertionResourceClaim(claims *ValidatedClaims) ([]string, error) { + raw, ok := claims.Extra["resource"] + if !ok { + return nil, nil + } + malformed := errorsx.WithStack(fosite.ErrInvalidGrant.WithHint("The assertion's resource claim is malformed.")) + switch v := raw.(type) { + case string: + if v == "" { + return nil, malformed + } + return []string{v}, nil + case []any: + if len(v) == 0 { + return nil, malformed + } + values := make([]string, 0, len(v)) + for _, e := range v { + s, ok := e.(string) + if !ok || s == "" { + return nil, malformed + } + values = append(values, s) + } + return values, nil + default: + return nil, malformed + } +} + // PopulateTokenEndpointResponse issues only an access token. func (h *JWTBearerHandler) PopulateTokenEndpointResponse( ctx context.Context, requester fosite.AccessRequester, responder fosite.AccessResponder,