Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions .github/mutation/mutants.json
Original file line number Diff line number Diff line change
Expand Up @@ -5814,5 +5814,186 @@
],
"run": "^TestBackchannelDecisionsAndExpiry$",
"why": "sqlstore wraps ErrBackchannelAuthenticationNotFound for an unknown CIBA handle"
},
{
"id": "token-claims-refuse-duplicate-members",
"file": "internal/token/claims_object.go",
"old": "if err := strictjson.CheckConflictingMembers(payload); err != nil {",
"new": "if err := strictjson.CheckConflictingMembers(payload); err != nil && false {",
"module": "root",
"packages": [
"./internal/token",
"./resource"
],
"run": "^(TestParseClaimsRefuseDuplicateMembers|TestVerifyRefusesJWTAccessTokenRepeatingAClaim)$",
"why": "ID token and JWT access token claims sets that repeat a member with differing copies are refused, not read last-wins"
},
{
"id": "jarm-claims-refuse-duplicate-members",
"file": "internal/jarm/claims_object.go",
"old": "if err := strictjson.CheckConflictingMembers(payload); err != nil {",
"new": "if err := strictjson.CheckConflictingMembers(payload); err != nil && false {",
"module": "root",
"packages": [
"./internal/jarm"
],
"run": "^TestParseClaimsRefusesDuplicateMembers$",
"why": "a JARM claims set that repeats a member with differing copies is refused"
},
{
"id": "requestobject-claims-refuse-duplicate-members",
"file": "internal/requestobject/claims_object.go",
"old": "if err := strictjson.CheckConflictingMembers(payload); err != nil {",
"new": "if err := strictjson.CheckConflictingMembers(payload); err != nil && false {",
"module": "root",
"packages": [
"./internal/requestobject"
],
"run": "^TestParseClaimsRefusesDuplicateMembers$",
"why": "a request object claims set that repeats a member with differing copies is refused"
},
{
"id": "clientassertion-claims-refuse-duplicate-members",
"file": "internal/clientassertion/claims.go",
"old": "if err := strictjson.CheckConflictingMembers(payload); err != nil {",
"new": "if err := strictjson.CheckConflictingMembers(payload); err != nil && false {",
"module": "root",
"packages": [
"./internal/clientassertion"
],
"run": "^TestParseClaimsRefusesDuplicateMembers$",
"why": "a client assertion claims set that repeats a member with differing copies is refused"
},
{
"id": "dpop-claims-refuse-duplicate-members",
"file": "internal/dpop/proof.go",
"old": "strictjson.UnmarshalNoConflicts(payload, &c)",
"new": "strictjson.Unmarshal(payload, &c)",
"module": "root",
"packages": [
"./internal/dpop"
],
"run": "^TestParseClaimsRefusesDuplicateMembers$",
"why": "a DPoP proof claims set that repeats a member with differing copies is refused"
},
{
"id": "clientattestation-pop-refuse-duplicate-members",
"file": "internal/clientattestation/claims.go",
"old": "if err := strictjson.UnmarshalNoConflicts(payload, &c)",
"new": "if err := strictjson.Unmarshal(payload, &c)",
"module": "root",
"packages": [
"./internal/clientattestation"
],
"run": "^TestParsePoPClaimsRefusesDuplicateMembers$",
"why": "an attestation PoP claims set that repeats a member with differing copies is refused"
},
{
"id": "federation-jwt-claims-refuse-duplicate-members",
"file": "internal/federation/claims_validation.go",
"old": "if err := strictjson.CheckConflictingMembers(payload); err != nil {",
"new": "if err := strictjson.CheckConflictingMembers(payload); err != nil && false {",
"module": "root",
"packages": [
"./internal/federation"
],
"run": "^TestParseFederationJWTClaimsRefuseDuplicateMembers$",
"why": "Trust Marks, delegations, status, resolve and historical keys claims sets that repeat a member with differing copies are refused"
},
{
"id": "client-userinfo-refuse-duplicate-members",
"file": "client/userinfo.go",
"old": "if err := strictjson.CheckConflictingMembers(body); err != nil {",
"new": "if err := strictjson.CheckConflictingMembers(body); err != nil && false {",
"module": "root",
"packages": [
"./client"
],
"run": "^TestDecodeUserInfoJSONRefusesDuplicateMembers$",
"why": "a UserInfo response that repeats a member with differing copies is refused"
},
{
"id": "resource-subject-kind-grant-type-marks-client",
"file": "resource/verify.go",
"old": "raw, ok := claims[token.GrantTypeClaim]\n\tif !ok {",
"new": "raw, ok := claims[token.GrantTypeClaim]\n\tif !ok || true {",
"module": "root",
"packages": [
"./resource"
],
"run": "^(TestVerifySubjectKindOfThirdPartyJWTAccessTokens|TestVerifySubjectKind)$",
"why": "a token carrying grant_type client_credentials reads as SubjectClient"
},
{
"id": "strictjson-conflict-identical-copies-accepted",
"file": "internal/strictjson/members_conflict.go",
"old": "if !bytes.Equal(first.compact, c) {",
"new": "if !bytes.Equal(first.compact, c) || true {",
"module": "root",
"packages": [
"./internal/strictjson"
],
"run": "^(TestCheckConflictingMembersAcceptsIdenticalRepeats|TestCheckConflictingMembersRefusesConflicts|TestCheckConflictingMembersBoundsNesting|TestCheckConflictingMembersNamesTheMembers|TestUnmarshalNoConflicts|TestCheckConflictingMembersAcceptsNoDifferential|TestCheckDuplicateMembersClosesFirstWinsDifferential)$",
"why": "CheckConflictingMembers accepts a repeated member whose copies are identical"
},
{
"id": "strictjson-conflict-differing-copies-refused",
"file": "internal/strictjson/members_conflict.go",
"old": "if !bytes.Equal(first.compact, c) {",
"new": "if !bytes.Equal(first.compact, c) && false {",
"module": "root",
"packages": [
"./internal/strictjson"
],
"run": "^(TestCheckConflictingMembersAcceptsIdenticalRepeats|TestCheckConflictingMembersRefusesConflicts|TestCheckConflictingMembersBoundsNesting|TestCheckConflictingMembersNamesTheMembers|TestUnmarshalNoConflicts|TestCheckConflictingMembersAcceptsNoDifferential|TestCheckDuplicateMembersClosesFirstWinsDifferential)$",
"why": "CheckConflictingMembers refuses a repeated member whose copies differ"
},
{
"id": "strictjson-conflict-case-variant-refused",
"file": "internal/strictjson/members_conflict.go",
"old": "if first != nil && first.name != name {",
"new": "if first != nil && first.name != name && false {",
"module": "root",
"packages": [
"./internal/strictjson"
],
"run": "^(TestCheckConflictingMembersAcceptsIdenticalRepeats|TestCheckConflictingMembersRefusesConflicts|TestCheckConflictingMembersBoundsNesting|TestCheckConflictingMembersNamesTheMembers|TestUnmarshalNoConflicts|TestCheckConflictingMembersAcceptsNoDifferential|TestCheckDuplicateMembersClosesFirstWinsDifferential)$",
"why": "CheckConflictingMembers refuses two names that differ only in case, even with identical values"
},
{
"id": "strictjson-conflict-nested-objects-checked",
"file": "internal/strictjson/members_conflict.go",
"old": "\tif delim == '{' {\n\t\treturn w.object(depth)",
"new": "\tif delim == '{' && depth == 0 {\n\t\treturn w.object(depth)",
"module": "root",
"packages": [
"./internal/strictjson"
],
"run": "^(TestCheckConflictingMembersAcceptsIdenticalRepeats|TestCheckConflictingMembersRefusesConflicts|TestCheckConflictingMembersBoundsNesting|TestCheckConflictingMembersNamesTheMembers|TestUnmarshalNoConflicts|TestCheckConflictingMembersAcceptsNoDifferential|TestCheckDuplicateMembersClosesFirstWinsDifferential)$",
"why": "CheckConflictingMembers checks the members of nested objects, including inside identical copies"
},
{
"id": "strictjson-conflict-compares-compacted-copies",
"file": "internal/strictjson/members_conflict.go",
"old": "if err := json.Compact(&buf, bytes.TrimLeft(span, \" \\t\\r\\n:\")); err != nil {",
"new": "if _, err := buf.Write(bytes.TrimLeft(span, \" \\t\\r\\n:\")); err != nil {",
"module": "root",
"packages": [
"./internal/strictjson"
],
"run": "^(TestCheckConflictingMembersAcceptsIdenticalRepeats|TestCheckConflictingMembersRefusesConflicts|TestCheckConflictingMembersBoundsNesting|TestCheckConflictingMembersNamesTheMembers|TestUnmarshalNoConflicts|TestCheckConflictingMembersAcceptsNoDifferential|TestCheckDuplicateMembersClosesFirstWinsDifferential)$",
"why": "CheckConflictingMembers compares copies after json.Compact, so whitespace alone is no difference"
},
{
"id": "jarm-claims-accept-identical-repeat",
"file": "internal/jarm/claims_object.go",
"old": "strictjson.CheckConflictingMembers(payload)",
"new": "strictjson.CheckDuplicateMembers(payload)",
"module": "root",
"packages": [
"./internal/jarm"
],
"run": "^TestParseClaimsRefusesDuplicateMembers$",
"why": "a JARM response repeating iss with an identical value (claim and RFC 9207 parameter) is accepted"
}
]
2 changes: 2 additions & 0 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ jobs:
name: FuzzCheckDuplicateMembersThenDecode
- pkg: ./internal/strictjson
name: FuzzScanMembersMatchesWalker
- pkg: ./internal/strictjson
name: FuzzCheckConflictingMembersOneReading
- pkg: ./extension
name: FuzzExtensionValueReadersAgree
- pkg: ./internal/sealedcookie
Expand Down
9 changes: 8 additions & 1 deletion GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,14 @@ Authorize anything that belongs to a user by `EndUser`, never by
client's own `client_id` (`SubjectKind` is `SubjectClient`), and a client
can be registered with a `client_id` equal to a user's identifier
(automatically, through OpenID Federation, for one). `EndUser` reports
`ok` false for it:
`ok` false for it. It can only because FAPIgo's authorization server
marks a client credentials token with a `grant_type` claim: a token from
another authorization server carries no such mark (RFC 9068 makes its
`sub` the `client_id`, and leaves keeping client IDs and user
identifiers apart to the authorization server, §5), so it reads as
`SubjectEndUser`, and `EndUser` reports `ok` true for it. Behind such a
server, refuse a token whose `InternalSubject` equals its `ClientID`
yourself, unless the server guarantees no client ID names a user:

```go
user, ok := authCtx.EndUser()
Expand Down
47 changes: 47 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,53 @@ Most production-assurance changes only affect `Config.Assurance =
AssuranceProduction`. A development-assurance setup built on `memstore`
and `keys/ephemeral` needs only the steps not marked *production only*.

## v0.87.0

### A JWT or UserInfo response may not repeat a claim with differing values (all packages)

**Affects:** a deployment whose peer repeats a member name, in any
object at any depth, of a JWT's claims set, with copies whose values
differ, or writes two names that differ only in case there: an
authorization server's ID tokens, JARM responses, UserInfo responses or
JWT access tokens; a client's request objects, signed CIBA requests,
client assertions, DPoP proofs or attestation PoPs; or a federation
entity's Trust Marks, Trust Mark Delegations, Trust Mark Status,
Resolve or Historical Keys responses. A member repeated with identical
copies (the same JSON text, ignoring whitespace), such as a JARM
response carrying `iss` both as its JWT claim and as the RFC 9207
response parameter, is still accepted. v0.81.0 refused any repeat in
JOSE headers, client attestations, metadata and Entity Statements, and
still does; these claims sets kept encoding/json's last copy of a
repeated claim.

**Why:** RFC 7519 §4 requires a JWT's claim names to be unique, and
lets a recipient reject a repeat or keep the last copy. Keeping the
last copy left a JWT with differing copies two readings: another reader
of the same token (a gateway in front of a resource server, a log
processor, a second verifier) that keeps the first copy would see a
different `sub`, `aud` or `scope` than FAPIgo acted on. Identical
copies read the same to every reader, so they are accepted. Two names
differing only in case are refused whatever their values, since
encoding/json reads them as one member where another reader reads two.
Copies are compared as text: values another reader might take as equal
but written differently (`"a"` and `"\u0061"`, `1` and `1.0`) are
refused.

**What fails:** nothing at startup. The server answers such a request
object with `invalid_request_object`, a signed CIBA request with
`invalid_request`, a client assertion or attestation PoP with
`invalid_client`, and a DPoP proof with `invalid_dpop_proof`; a
resource server answers such a JWT access token with `invalid_token`,
and a DPoP proof with `invalid_dpop_proof`. The client refuses such an
ID token, JARM response or UserInfo response, signed, encrypted or
plain JSON, with `invalid_response`. A federation JWT that repeats a
claim with differing values fails to parse, with the error its parser
gives any malformed one.

**What to change:** nothing in your code. A peer producing such JWTs
must write each claim once, spelled one way, or repeat it only with an
identical value.

## v0.86.0

### A Trust Chain that expires while it resolves is refused (federation)
Expand Down
12 changes: 11 additions & 1 deletion client/userinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,21 @@ func (c *Client) decodeUserInfoResponse(ctx context.Context, contentType string,
// since two different sub values would then compare equal in
// checkUserInfoSubject's exact match against the ID token's sub. A
// signed response's payload already passed the same check in
// jose.ParseCompact; a plain-JSON one gets it here.
// jose.ParseCompact; a plain-JSON one gets it here. It refuses, too, a
// body, plain JSON or a signed response's payload, that repeats a member
// name, at any depth, with copies that differ, or holds two names that
// differ only in case (strictjson.CheckConflictingMembers):
// encoding/json keeps the last copy, where another reader of the same
// response may keep the first, and so see a different sub. RFC 7519 §4
// requires a JWT's claim names to be unique. A repeat whose copies are
// identical is accepted: every reader takes the same value from it.
func decodeUserInfoJSON(body []byte) (map[string]json.RawMessage, *Error) {
if !strictjson.DecodesLosslessly(body) {
return nil, newError(ErrorInvalidResponse, "malformed UserInfo response: not valid UTF-8, or an unpaired surrogate escape", nil)
}
if err := strictjson.CheckConflictingMembers(body); err != nil {
return nil, newError(ErrorInvalidResponse, "malformed UserInfo response", err)
}
var claims map[string]json.RawMessage
if err := json.Unmarshal(body, &claims); err != nil {
return nil, newError(ErrorInvalidResponse, "malformed UserInfo response", err)
Expand Down
38 changes: 38 additions & 0 deletions client/userinfo_duplicate_members_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package client

import (
"errors"
"testing"

"github.com/idfoundry/fapigo/internal/strictjson"
)

// TestDecodeUserInfoJSONRefusesDuplicateMembers: a UserInfo response,
// plain JSON or a signed JWT's payload, whose sub is compared with the
// ID token's, may not repeat a member: encoding/json keeps the last
// copy, where another reader of the same response (a gateway, a log
// processor) may keep the first, so the two would see different users.
// RFC 7519 §4 requires unique claim names in a JWT, and RFC 8259 §4 has
// a JSON object's names unique. A member repeated with identical copies
// reads alike to every reader, so it is accepted.
func TestDecodeUserInfoJSONRefusesDuplicateMembers(t *testing.T) {
for _, body := range []string{
`{"sub":"alice","email":"a@example.com"}`,
`{"sub":"alice","email":"a@example.com","sub":"alice"}`,
`{"sub":"alice","address":{"country":"A","country":"A"}}`,
} {
if _, err := decodeUserInfoJSON([]byte(body)); err != nil {
t.Fatalf("decodeUserInfoJSON(%s) = %v, want nil", body, err)
}
}
for _, body := range []string{
`{"sub":"mallory","sub":"alice"}`,
`{"sub":"alice","address":{"country":"A","country":"B"}}`,
`{"sub":"alice","SUB":"mallory"}`,
} {
_, err := decodeUserInfoJSON([]byte(body))
if err == nil || err.Code() != ErrorInvalidResponse || !errors.Is(err, strictjson.ErrDuplicateMember) {
t.Errorf("decodeUserInfoJSON(%s) = %v, want invalid_response for a duplicate member", body, err)
}
}
}
6 changes: 5 additions & 1 deletion docs/PRODUCTION_CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ are things no constructor can check.
- [ ] *(yours)* **Authorize anything a user owns by
`AuthorizationContext.EndUser()`**, never by `InternalSubject` alone (a
client credentials token's subject is a client), and use
`ClientSubject` for anything returned to a client. [Pairwise subjects at a resource server](../GETTING_STARTED.md#pairwise-subjects-at-a-resource-server)
`ClientSubject` for anything returned to a client. `EndUser` tells a
client's token apart only for FAPIgo's authorization server, which
marks one with `grant_type`; behind another, check that it keeps
client IDs and user identifiers apart (RFC 9068 §5), or refuse a
token whose `InternalSubject` equals its `ClientID`. [Pairwise subjects at a resource server](../GETTING_STARTED.md#pairwise-subjects-at-a-resource-server)
- [ ] *(yours)* **`PeerCertificate` behind a proxy:** the same rule as
the authorization server's, with `fapihttp.ForwardedClientCertificate`.
[Verify a request](../GETTING_STARTED.md#verify-a-request)
Expand Down
8 changes: 8 additions & 0 deletions internal/clientassertion/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ type claims struct {
func parseClaims(payload []byte) (claims, error) {
dec := json.NewDecoder(bytes.NewReader(payload))
var c claims
// RFC 7519 §4 requires unique claim names: encoding/json keeps the
// last copy of a repeated one, where another reader of the same
// assertion may keep the first. A repeat with identical copies reads
// the same either way, so only differing copies (and case variants)
// are refused.
if err := strictjson.CheckConflictingMembers(payload); err != nil {
return claims{}, fmt.Errorf("%w: %v", ErrMalformedClaims, err)
}
if err := strictjson.CheckFieldCase(payload, &c); err != nil {
return claims{}, fmt.Errorf("%w: %v", ErrMalformedClaims, err)
}
Expand Down
34 changes: 34 additions & 0 deletions internal/clientassertion/duplicate_claims_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package clientassertion

import (
"errors"
"strings"
"testing"
)

// TestParseClaimsRefusesDuplicateMembers: RFC 7519 §4 requires a JWT's
// claim names to be unique. encoding/json keeps the last copy of a
// repeated one, where a first-wins reader of the same JWT keeps the
// first, so the two would act on different claims. A claims set that
// repeats a member with differing copies, at any depth, is refused; one
// whose copies are identical, which every reader reads alike, is
// accepted.
func TestParseClaimsRefusesDuplicateMembers(t *testing.T) {
for _, payload := range []string{
`{"iss":"c","sub":"c","aud":"https://as.example.com","exp":2,"iat":1,"jti":"j"}`,
`{"iss":"c","sub":"c","aud":"https://as.example.com","exp":2,"iat":1,"jti":"j","aud":"https://as.example.com"}`,
} {
if _, err := parseClaims([]byte(payload)); err != nil {
t.Fatalf("parseClaims(%s) = %v, want nil", payload, err)
}
}
for _, payload := range []string{
`{"iss":"c","sub":"c","aud":"https://other.example","exp":2,"iat":1,"jti":"j","aud":"https://as.example.com"}`,
`{"iss":"c","sub":"c","aud":"https://as.example.com","exp":2,"iat":1,"jti":"a","jti":"b"}`,
} {
_, err := parseClaims([]byte(payload))
if !errors.Is(err, ErrMalformedClaims) || !strings.Contains(err.Error(), "duplicate member name") {
t.Errorf("parseClaims(%s) = %v, want ErrMalformedClaims for a duplicate member", payload, err)
}
}
}
Loading
Loading