diff --git a/.github/mutation/mutants.json b/.github/mutation/mutants.json index f24617c4..3018dc37 100644 --- a/.github/mutation/mutants.json +++ b/.github/mutation/mutants.json @@ -5730,5 +5730,77 @@ ], "run": "^TestBackchannelAuthenticationStoreRefusesDecisionOnUnknownHandle$", "why": "memstore wraps ErrBackchannelAuthenticationNotFound for a decision on an unknown handle" + }, + { + "id": "client-issuer-keys-binding-mismatch", + "file": "client/client.go", + "old": "iss != \"\" && iss != cfg.Issuer.String()", + "new": "iss != \"\" && iss != cfg.Issuer.String() && false", + "module": "root", + "packages": [ + "./client" + ], + "run": "^(TestNewRefusesIssuerKeysBoundToAnotherIssuer|TestNewAcceptsMatchingOrUnboundIssuerKeys)$", + "why": "client.New refuses issuer keys bound to an issuer other than Config.Issuer" + }, + { + "id": "client-issuer-keys-binding-unbound", + "file": "client/client.go", + "old": "iss != \"\" && iss != cfg.Issuer.String()", + "new": "iss != cfg.Issuer.String()", + "module": "root", + "packages": [ + "./client" + ], + "run": "^(TestNewRefusesIssuerKeysBoundToAnotherIssuer|TestNewAcceptsMatchingOrUnboundIssuerKeys)$", + "why": "client.New accepts an unbound issuer key source" + }, + { + "id": "resource-issuer-keys-binding-mismatch", + "file": "resource/accesstoken.go", + "old": "iss != \"\" && iss != j.Issuer.String()", + "new": "iss != \"\" && iss != j.Issuer.String() && false", + "module": "root", + "packages": [ + "./resource" + ], + "run": "^(TestJWTAccessTokensRefuseIssuerKeysBoundToAnotherIssuer|TestJWTAccessTokensAcceptMatchingOrUnboundIssuerKeys)$", + "why": "JWTAccessTokens validation refuses issuer keys bound to an issuer other than Issuer" + }, + { + "id": "resource-issuer-keys-binding-unbound", + "file": "resource/accesstoken.go", + "old": "iss != \"\" && iss != j.Issuer.String()", + "new": "iss != j.Issuer.String()", + "module": "root", + "packages": [ + "./resource" + ], + "run": "^(TestJWTAccessTokensRefuseIssuerKeysBoundToAnotherIssuer|TestJWTAccessTokensAcceptMatchingOrUnboundIssuerKeys)$", + "why": "JWTAccessTokens validation accepts an unbound issuer key source" + }, + { + "id": "keys-jwks-bound-issuer", + "file": "keys/jwksissuer.go", + "old": "return s.issuer\n", + "new": "return \"\"\n", + "module": "root", + "packages": [ + "./keys" + ], + "run": "^TestJWKSIssuerKeySourceBoundIssuer$", + "why": "JWKSIssuerKeySource reports the issuer WithIssuer bound it to" + }, + { + "id": "keys-local-bound-issuer", + "file": "keys/localissuer.go", + "old": "return l.issuer\n", + "new": "return \"\"\n", + "module": "root", + "packages": [ + "./keys" + ], + "run": "^TestLocalIssuerKeysBoundIssuer$", + "why": "LocalIssuerKeys reports its own issuer as its binding" } ] diff --git a/UPGRADING.md b/UPGRADING.md index ccb9d779..ef7c9f00 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -18,6 +18,39 @@ 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 + +### An issuer key source bound to another issuer is refused at construction (client, resource) + +**Affects:** a client whose `Dependencies.IssuerKeys`, or a resource +server whose `JWTAccessTokens.IssuerKeys`, is a `keys.LocalIssuerKeys` +or a `keys.JWKSIssuerKeySource` built with `keys.WithIssuer` for an +issuer other than the configured one (`client.Config.Issuer`, +`JWTAccessTokens.Issuer`), compared exactly, so a trailing slash or a +different scheme or host counts as different. A source built by +`DiscoveredMetadata.IssuerKeySource` is bound to the discovered issuer +and so matches a client built from the same metadata. + +**Why:** a bound source refuses every lookup for any issuer but its +own, so such a client or verifier could never verify anything its +issuer signs: every JARM response, ID token, signed UserInfo response +or JWT access token failed. The new optional `keys.IssuerBound` +interface lets a source report its binding, and construction now +checks it. + +**What fails:** `client.New` (and `client.NewFromDiscovery`), +`resource.NewJWTAccessTokens` and `resource.NewVerifier` return an +error naming both issuers ("issuer keys is bound to issuer ..., not +..."). A deployment that hit this was already failing every +verification; it now fails at startup instead. + +**What to change:** build the key source for the issuer the client or +verifier is configured for. An unbound source, or one of your own that +doesn't implement `keys.IssuerBound`, is accepted as before; if one +source is shared between clients or verifiers of different issuers, +bind it (`keys.WithIssuer`), or build one per issuer, so none of them +can be handed another issuer's keys. + ## v0.86.0 ### A Trust Chain that expires while it resolves is refused (federation) diff --git a/client/client.go b/client/client.go index 5ccd7c3e..f4ae943b 100644 --- a/client/client.go +++ b/client/client.go @@ -12,6 +12,7 @@ import ( "github.com/idfoundry/fapigo/internal/depcheck" "github.com/idfoundry/fapigo/internal/nofollow" "github.com/idfoundry/fapigo/internal/querystring" + "github.com/idfoundry/fapigo/keys" "github.com/idfoundry/fapigo/storage" ) @@ -716,6 +717,22 @@ func validateIssuerKeysDependency(cfg Config, deps Dependencies) error { if deps.IssuerKeys == nil && issuerKeysNeeded(cfg) { return fmt.Errorf("client: dependencies: issuer keys is required (it may be left nil only when oauth_only is set and neither a JARM response nor a signed UserInfo response is verified)") } + return checkIssuerKeysBinding(cfg, deps.IssuerKeys) +} + +// checkIssuerKeysBinding refuses an issuer key source bound (see +// keys.IssuerBound) to an issuer other than cfg.Issuer: it would refuse +// every lookup this client makes, so nothing the issuer signs could +// ever verify. Issuers are compared exactly, as a response's "iss" is. +// An unbound source, or one not implementing keys.IssuerBound, passes. +func checkIssuerKeysBinding(cfg Config, src keys.IssuerKeySource) error { + bound, ok := src.(keys.IssuerBound) + if !ok { + return nil + } + if iss := bound.BoundIssuer(); iss != "" && iss != cfg.Issuer.String() { + return fmt.Errorf("client: dependencies: issuer keys is bound to issuer %q, not this client's issuer %q", iss, cfg.Issuer.String()) + } return nil } diff --git a/client/dependencies.go b/client/dependencies.go index f7937e89..786522fb 100644 --- a/client/dependencies.go +++ b/client/dependencies.go @@ -47,6 +47,14 @@ type Dependencies struct { // keys.IssuerKeySet). Required, except that a Config.OAuthOnly client // verifying neither JARM nor signed UserInfo leaves it nil — see // Config.OAuthOnly. + // + // New refuses a source bound to an issuer other than Config.Issuer + // (keys.IssuerBound: a keys.LocalIssuerKeys, or a + // keys.JWKSIssuerKeySource built with keys.WithIssuer), since it + // would refuse every lookup this client makes. A source shared + // between clients of different issuers must be bound: an unbound one + // serves its keys for any issuer, so each client would accept + // another issuer's signatures as its own issuer's. IssuerKeys keys.IssuerKeySource // HTTP performs this client's PAR, token-endpoint and other POST diff --git a/client/issuer_keys_binding_test.go b/client/issuer_keys_binding_test.go new file mode 100644 index 00000000..a8e6fe0b --- /dev/null +++ b/client/issuer_keys_binding_test.go @@ -0,0 +1,84 @@ +package client_test + +import ( + "context" + "strings" + "testing" + + fapi "github.com/idfoundry/fapigo" + "github.com/idfoundry/fapigo/client" + "github.com/idfoundry/fapigo/keys" + "github.com/idfoundry/fapigo/keys/ephemeral" +) + +// boundIssuerKeys is an IssuerKeySource reporting a binding through +// keys.IssuerBound, without resolving anything. +type boundIssuerKeys struct{ issuer string } + +func (boundIssuerKeys) ResolveIssuerKeys(context.Context, keys.IssuerKeyRequest) (keys.IssuerKeySet, error) { + return keys.IssuerKeySet{}, nil +} + +func (b boundIssuerKeys) BoundIssuer() string { return b.issuer } + +func localIssuerKeysFor(t *testing.T, issuer string) *keys.LocalIssuerKeys { + t.Helper() + iss, err := fapi.ParseIssuerURL(issuer) + if err != nil { + t.Fatalf("ParseIssuerURL: %v", err) + } + km, err := ephemeral.NewKeyManager(map[keys.SigningPurpose]fapi.SignatureAlgorithm{keys.IDTokenSigning: fapi.ES256}) + if err != nil { + t.Fatalf("NewKeyManager: %v", err) + } + l, err := keys.NewLocalIssuerKeys(iss, km) + if err != nil { + t.Fatalf("NewLocalIssuerKeys: %v", err) + } + return l +} + +// TestNewRefusesIssuerKeysBoundToAnotherIssuer: an issuer key source +// bound to a different issuer would refuse every lookup the client +// makes, so New refuses it, naming both issuers. +func TestNewRefusesIssuerKeysBoundToAnotherIssuer(t *testing.T) { + const other = "https://other-as.example.com" + for name, src := range map[string]keys.IssuerKeySource{ + "stub": boundIssuerKeys{issuer: other}, + "LocalIssuerKeys": localIssuerKeysFor(t, other), + "trailing slash": boundIssuerKeys{issuer: testIssuer + "/"}, + } { + t.Run(name, func(t *testing.T) { + deps := validDependencies(t) + deps.IssuerKeys = src + _, err := client.New(validConfig(t), deps) + if err == nil { + t.Fatal("New = nil error, want refusal of a source bound to another issuer") + } + want := src.(keys.IssuerBound).BoundIssuer() + if !strings.Contains(err.Error(), want) || !strings.Contains(err.Error(), `"`+testIssuer+`"`) { + t.Errorf("New error = %q, want it to name %q and %q", err, want, testIssuer) + } + }) + } +} + +// TestNewAcceptsMatchingOrUnboundIssuerKeys: a source bound to the +// client's own issuer, an unbound one, and one not implementing +// keys.IssuerBound are all accepted. +func TestNewAcceptsMatchingOrUnboundIssuerKeys(t *testing.T) { + for name, src := range map[string]keys.IssuerKeySource{ + "bound to own issuer": boundIssuerKeys{issuer: testIssuer}, + "LocalIssuerKeys": localIssuerKeysFor(t, testIssuer), + "unbound": boundIssuerKeys{}, + "no IssuerBound": validDependencies(t).IssuerKeys, + } { + t.Run(name, func(t *testing.T) { + deps := validDependencies(t) + deps.IssuerKeys = src + if _, err := client.New(validConfig(t), deps); err != nil { + t.Fatalf("New: %v", err) + } + }) + } +} diff --git a/keys/issuer.go b/keys/issuer.go index b2d107b3..ecc7fefa 100644 --- a/keys/issuer.go +++ b/keys/issuer.go @@ -84,3 +84,28 @@ type IssuerKeySet struct { type IssuerKeySource interface { ResolveIssuerKeys(ctx context.Context, req IssuerKeyRequest) (IssuerKeySet, error) } + +// IssuerBound is the optional interface an IssuerKeySource implements +// when it serves one issuer's keys only. BoundIssuer returns that +// issuer identifier, or "" when the source isn't bound to one and serves +// any issuer that asks. +// +// It lets a verifier refuse a misconfiguration up front: +// client.New and resource.NewVerifier (and resource.NewJWTAccessTokens) +// refuse a source whose BoundIssuer is set and isn't, exactly, the +// issuer they are configured for, since such a source refuses every one +// of that issuer's lookups and so could never verify anything it signed. +// A source that doesn't implement IssuerBound, or returns "", is +// accepted as before, and is then trusted to serve the right issuer's +// keys: a source shared between verifiers of different issuers must be +// bound, or it hands one issuer's keys to a check of another's. +// LocalIssuerKeys is always bound; JWKSIssuerKeySource is bound when +// built WithIssuer. +// +// Like KeySourceAssurance, a declaration describes the declaring type's +// own behaviour, and a type embedding a bound source inherits it. A +// wrapper that changes which issuers it answers for must override +// BoundIssuer to match. +type IssuerBound interface { + BoundIssuer() string +} diff --git a/keys/issuer_bound_test.go b/keys/issuer_bound_test.go new file mode 100644 index 00000000..5b9b1990 --- /dev/null +++ b/keys/issuer_bound_test.go @@ -0,0 +1,62 @@ +package keys_test + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + fapi "github.com/idfoundry/fapigo" + "github.com/idfoundry/fapigo/keys" + "github.com/idfoundry/fapigo/keys/ephemeral" +) + +var ( + _ keys.IssuerBound = (*keys.JWKSIssuerKeySource)(nil) + _ keys.IssuerBound = (*keys.LocalIssuerKeys)(nil) +) + +// TestJWKSIssuerKeySourceBoundIssuer: a JWKSIssuerKeySource reports the +// issuer WithIssuer bound it to, and "" when built without it. +func TestJWKSIssuerKeySourceBoundIssuer(t *testing.T) { + ts := httptest.NewTLSServer(http.NotFoundHandler()) + defer ts.Close() + jwksURI, err := fapi.ParseEndpointURL(ts.URL + "/jwks") + if err != nil { + t.Fatalf("ParseEndpointURL: %v", err) + } + issuer, err := fapi.ParseIssuerURL("https://as.example.com") + if err != nil { + t.Fatalf("ParseIssuerURL: %v", err) + } + unbound, err := keys.NewJWKSIssuerKeySource(newTestFetcherTLS(t, ts), jwksURI, time.Minute) + if err != nil { + t.Fatalf("NewJWKSIssuerKeySource: %v", err) + } + if got := unbound.BoundIssuer(); got != "" { + t.Errorf("unbound BoundIssuer() = %q, want \"\"", got) + } + bound, err := keys.NewJWKSIssuerKeySource(newTestFetcherTLS(t, ts), jwksURI, time.Minute, keys.WithIssuer(issuer)) + if err != nil { + t.Fatalf("NewJWKSIssuerKeySource: %v", err) + } + if got := bound.BoundIssuer(); got != issuer.String() { + t.Errorf("bound BoundIssuer() = %q, want %q", got, issuer.String()) + } +} + +// TestLocalIssuerKeysBoundIssuer: a LocalIssuerKeys is always bound to +// the issuer it was built for. +func TestLocalIssuerKeysBoundIssuer(t *testing.T) { + km, err := ephemeral.NewKeyManager(map[keys.SigningPurpose]fapi.SignatureAlgorithm{keys.AccessTokenSigning: fapi.ES256}) + if err != nil { + t.Fatalf("NewKeyManager: %v", err) + } + l, err := keys.NewLocalIssuerKeys(mustIssuer(t), km) + if err != nil { + t.Fatalf("NewLocalIssuerKeys: %v", err) + } + if got := l.BoundIssuer(); got != localIssuer { + t.Errorf("BoundIssuer() = %q, want %q", got, localIssuer) + } +} diff --git a/keys/jwksissuer.go b/keys/jwksissuer.go index 37ce1de9..1b9ada33 100644 --- a/keys/jwksissuer.go +++ b/keys/jwksissuer.go @@ -78,7 +78,10 @@ func WithRefreshBackoff(d time.Duration) JWKSOption { // mistake between clients (or verifiers) of different issuers can't // hand one issuer's keys to a check of another's. Without it the source // can't tell, and serves whatever its JWKS URI holds for any issuer. -// DiscoveredMetadata.IssuerKeySource in package client sets it. +// DiscoveredMetadata.IssuerKeySource in package client sets it. A bound +// source reports its issuer through IssuerBound, so client.New and +// resource.NewVerifier refuse one bound to an issuer other than their +// own at construction, rather than failing every verification. func WithIssuer(issuer fapi.URL) JWKSOption { return func(s *JWKSIssuerKeySource) { s.issuer = issuer.String() } } @@ -124,6 +127,12 @@ func (s *JWKSIssuerKeySource) Capabilities() KeySourceCapabilities { return KeySourceCapabilities{LiveFetchHardened: s.fetcher.HardenedForProduction()} } +// BoundIssuer implements IssuerBound: the issuer WithIssuer bound the +// source to, or "" when it was built without WithIssuer. +func (s *JWKSIssuerKeySource) BoundIssuer() string { + return s.issuer +} + // ResolveIssuerKeys returns every cached key matching req.Algorithm // (and req.KeyID, if set). If a specific KeyID was requested and isn't // found in the cache, it forces one refresh before giving up — the diff --git a/keys/localissuer.go b/keys/localissuer.go index 9e23c0b1..aab6d77b 100644 --- a/keys/localissuer.go +++ b/keys/localissuer.go @@ -75,6 +75,12 @@ func (l *LocalIssuerKeys) Capabilities() KeySourceCapabilities { return KeySourceCapabilities{LiveFetchHardened: true} } +// BoundIssuer implements IssuerBound: a LocalIssuerKeys answers for +// the issuer it was built for only. +func (l *LocalIssuerKeys) BoundIssuer() string { + return l.issuer +} + // signingPurposeFor maps what a verifier checks to the purpose the // issuer signed it under. func signingPurposeFor(p IssuerVerificationPurpose) (SigningPurpose, error) { diff --git a/resource/accesstoken.go b/resource/accesstoken.go index f9499fd9..0bf42c30 100644 --- a/resource/accesstoken.go +++ b/resource/accesstoken.go @@ -150,7 +150,13 @@ type AccessTokenResolver interface { // this package. type JWTAccessTokens struct { // IssuerKeys resolves the authorization server's access-token - // verification keys. Required. + // verification keys. Required. NewJWTAccessTokens and NewVerifier + // refuse a source bound to an issuer other than Issuer + // (keys.IssuerBound: a keys.LocalIssuerKeys, or a + // keys.JWKSIssuerKeySource built with keys.WithIssuer), since it + // would refuse every lookup. A source shared between verifiers of + // different issuers must be bound: an unbound one serves its keys + // for any issuer. IssuerKeys keys.IssuerKeySource // Issuer is the authorization server's issuer identifier; a token's @@ -261,6 +267,22 @@ func (j JWTAccessTokens) validate() error { case j.MaxTokenBytes < 0: return errors.New("max token bytes must not be negative") } + return j.checkIssuerKeysBinding() +} + +// checkIssuerKeysBinding refuses an IssuerKeys bound (see +// keys.IssuerBound) to an issuer other than Issuer: it would refuse +// every lookup ResolveAccessToken makes, so no token could ever verify. +// Issuers are compared exactly, as a token's "iss" is. An unbound +// source, or one not implementing keys.IssuerBound, passes. +func (j JWTAccessTokens) checkIssuerKeysBinding() error { + bound, ok := j.IssuerKeys.(keys.IssuerBound) + if !ok { + return nil + } + if iss := bound.BoundIssuer(); iss != "" && iss != j.Issuer.String() { + return fmt.Errorf("issuer keys is bound to issuer %q, not the configured issuer %q", iss, j.Issuer.String()) + } return nil } diff --git a/resource/issuer_keys_binding_test.go b/resource/issuer_keys_binding_test.go new file mode 100644 index 00000000..fb5f99dd --- /dev/null +++ b/resource/issuer_keys_binding_test.go @@ -0,0 +1,84 @@ +package resource_test + +import ( + "context" + "strings" + "testing" + "time" + + fapi "github.com/idfoundry/fapigo" + "github.com/idfoundry/fapigo/keys" + "github.com/idfoundry/fapigo/resource" +) + +// boundIssuerKeys is an IssuerKeySource reporting a binding through +// keys.IssuerBound (or none, when issuer is ""), without resolving +// anything. +type boundIssuerKeys struct{ issuer string } + +func (boundIssuerKeys) ResolveIssuerKeys(context.Context, keys.IssuerKeyRequest) (keys.IssuerKeySet, error) { + return keys.IssuerKeySet{}, nil +} + +func (b boundIssuerKeys) BoundIssuer() string { return b.issuer } + +// unmarkedIssuerKeys is an IssuerKeySource not implementing +// keys.IssuerBound. +type unmarkedIssuerKeys struct{} + +func (unmarkedIssuerKeys) ResolveIssuerKeys(context.Context, keys.IssuerKeyRequest) (keys.IssuerKeySet, error) { + return keys.IssuerKeySet{}, nil +} + +// TestJWTAccessTokensRefuseIssuerKeysBoundToAnotherIssuer: a source +// bound to another issuer would refuse every lookup, so both +// NewJWTAccessTokens and NewVerifier (for a literal) refuse it, naming +// both issuers. +func TestJWTAccessTokensRefuseIssuerKeysBoundToAnotherIssuer(t *testing.T) { + issuer, err := fapi.ParseIssuerURL(testIssuer) + if err != nil { + t.Fatal(err) + } + const other = "https://other-as.example.com" + src := boundIssuerKeys{issuer: other} + wantErr := func(t *testing.T, err error) { + t.Helper() + if err == nil { + t.Fatal("error = nil, want refusal of a source bound to another issuer") + } + if !strings.Contains(err.Error(), `"`+other+`"`) || !strings.Contains(err.Error(), `"`+testIssuer+`"`) { + t.Errorf("error = %q, want it to name %q and %q", err, other, testIssuer) + } + } + _, err = resource.NewJWTAccessTokens(src, issuer, testIssuer, fapi.ES256, 5*time.Minute, 8) + wantErr(t, err) + + deps := validDependencies(t) + deps.AccessTokens = resource.JWTAccessTokens{ + IssuerKeys: src, Issuer: issuer, Audience: testIssuer, + Algorithm: fapi.ES256, MaxTokenLifetime: 5 * time.Minute, MaxKeyCandidates: 8, + } + _, err = resource.NewVerifier(validConfig(t), deps) + wantErr(t, err) +} + +// TestJWTAccessTokensAcceptMatchingOrUnboundIssuerKeys: a source bound +// to the configured issuer, an unbound one, and one not implementing +// keys.IssuerBound are all accepted. +func TestJWTAccessTokensAcceptMatchingOrUnboundIssuerKeys(t *testing.T) { + issuer, err := fapi.ParseIssuerURL(testIssuer) + if err != nil { + t.Fatal(err) + } + for name, src := range map[string]keys.IssuerKeySource{ + "bound to own issuer": boundIssuerKeys{issuer: testIssuer}, + "unbound": boundIssuerKeys{}, + "no IssuerBound": unmarkedIssuerKeys{}, + } { + t.Run(name, func(t *testing.T) { + if _, err := resource.NewJWTAccessTokens(src, issuer, testIssuer, fapi.ES256, 5*time.Minute, 8); err != nil { + t.Fatalf("NewJWTAccessTokens: %v", err) + } + }) + } +}