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
72 changes: 72 additions & 0 deletions .github/mutation/mutants.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
33 changes: 33 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions client/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions client/issuer_keys_binding_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
25 changes: 25 additions & 0 deletions keys/issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
62 changes: 62 additions & 0 deletions keys/issuer_bound_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 10 additions & 1 deletion keys/jwksissuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions keys/localissuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading
Loading