Skip to content
420 changes: 420 additions & 0 deletions .github/mutation/mutants.json

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,47 @@ withdraw the token, still answers `server_error`.
500: expect a 400 `invalid_grant` instead, and the client starts a new
authorization as for any other refused grant.

### Encoding a federation issuer's dependencies fails (federation)

**Affects:** code that encodes a `federation.HistoricalKeysIssueDependencies`,
`ResolveIssueDependencies`, `SelfIssueDependencies`,
`SubordinateIssueDependencies` or `TrustMarkIssueDependencies`, or a
struct holding one, with `encoding/json`, `encoding/xml` or another
encoder that uses `MarshalText` or `MarshalJSON`. Building and running
the issuers is unchanged.

**Why:** their `Signer` is often the private key itself, and an encoder
wrote it out: `encoding/json`, `encoding/xml` and `log/slog`'s JSON
handler encoded an ECDSA or RSA key's private exponent, into a config
dump or a log line, say. `keys.SignerSpec` already refuses encoding for
the same reason.

**What fails:** encoding returns an error wrapping
`federation.ErrIssueDependenciesSerialization`.

**What to change:** encode the entity's public keys instead (the
`JWKS` you pass `SelfIssueDependencies`, say), or the fields you need
without `Signer`.

### Key holders print `[REDACTED]` for their keys (keys, federation)

**Affects:** only code that compares the `fmt` or `log/slog` output of
the `KeyManager` `keys.NewKeyManagerFromSigners` returns, a
`keys/ephemeral.KeyManager`, the backends `keys.NewInMemoryECDH` and
`keys.NewInMemoryRSA` return, or a federation issuer or its
dependencies.

**Why:** they printed whatever `fmt` makes of their keys, which for an
`ed25519.PrivateKey`, a byte slice, is every byte of it, and for an
`*ecdh.PrivateKey` under `%s` is what it points to. An issuer printed
its dependencies field by field, its `Signer` included.

**What fails:** nothing fails. Each now prints its kids and the rest
of what it holds but its keys, with `[REDACTED]` for each private key
or signer, under every verb and in `log/slog`.

**What to change:** nothing, unless a test matched the old output.

## v0.85.0

### Codes, refresh tokens and `auth_req_id`s are stored under digests covering the issuer (server, storage)
Expand Down
74 changes: 59 additions & 15 deletions fapitest/key_rendering_guard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"slices"
"strings"
"testing"
"time"
"unsafe"

fapi "github.com/idfoundry/fapigo"
Expand All @@ -43,7 +44,10 @@ import (
//
// A case whose renderings print its key today is skipped, with the
// reason in knownLeak, so the matrix records the gap without failing
// the build; removing knownLeak is how a fix proves itself.
// the build; removing knownLeak is how a fix proves itself. A case
// whose key sits in an exported field the caller fills, which fmt's
// reflection fallback prints as it prints any exported field, accepts
// that fallback, with the reason in inputField.

// keyHolder is one case of the key-material matrix.
type keyHolder struct {
Expand All @@ -54,14 +58,15 @@ type keyHolder struct {
build func(t *testing.T) (value any, secrets []string)
// knownLeak, when set, skips the case: some rendering prints the key.
knownLeak string
// inputField, when set, accepts what fmt's reflection fallback
// prints, which calls none of the value's methods: the key is in an
// exported field of an input type the caller fills.
inputField string
}

// The known leaks the skipped cases record.
const (
leaksEd25519 = "fmt's %v prints an ed25519.PrivateKey held behind crypto.Signer: it is a byte slice, which fmt prints at any depth"
leaksSignerJSON = "encoding/json, encoding/xml and slog's JSON handler encode an *ecdsa.PrivateKey held in the exported Signer field, D included"
leaksECDHBadVerb = "fmt's %s and %q print the *ecdh.PrivateKey's bytes: a verb a pointer field doesn't take makes fmt print what it points to"
)
// signerSpecEd25519 is why the keys.SignerSpec EdDSA case accepts fmt's
// reflection fallback.
const signerSpecEd25519 = "an ed25519.PrivateKey in SignerSpec's exported Signer field is a byte slice, which fmt's reflection fallback (%w outside Errorf, %p, a SignerSpec in an unexported field) prints byte by byte; an *ecdsa.PrivateKey or *rsa.PrivateKey sits behind a pointer it prints as an address"

// keyHolders are the key-material matrix's cases, by name.
var keyHolders = map[string]keyHolder{
Expand All @@ -79,7 +84,7 @@ var keyHolders = map[string]keyHolder{
build: func(t *testing.T) (any, []string) { return signerKeyManager(t, fapi.ES256, ecdsaKey(t)) }},
"keys.NewKeyManagerFromSigners PS256": {covers: []string{"keys.signerBackend.signer"},
build: func(t *testing.T) (any, []string) { return signerKeyManager(t, fapi.PS256, rsaKey(t)) }},
"keys.NewKeyManagerFromSigners EdDSA": {covers: []string{"keys.signerBackend.signer"}, knownLeak: leaksEd25519,
"keys.NewKeyManagerFromSigners EdDSA": {covers: []string{"keys.signerBackend.signer"},
build: func(t *testing.T) (any, []string) { return signerKeyManager(t, fapi.EdDSA, ed25519Key(t)) }},
"keys.SignerSpec ES256 and PS256": {covers: []string{"keys.SignerSpec.Signer"},
build: func(t *testing.T) (any, []string) {
Expand All @@ -89,12 +94,12 @@ var keyHolders = map[string]keyHolder{
{Purpose: keys.IDTokenSigning, Algorithm: fapi.PS256, Signer: rsa},
}, append(keySecrets(ec), keySecrets(rsa)...)
}},
"keys.SignerSpec EdDSA": {covers: []string{"keys.SignerSpec.Signer"}, knownLeak: leaksEd25519,
"keys.SignerSpec EdDSA": {covers: []string{"keys.SignerSpec.Signer"}, inputField: signerSpecEd25519,
build: func(t *testing.T) (any, []string) {
key := ed25519Key(t)
return keys.SignerSpec{Purpose: keys.JARMSigning, Algorithm: fapi.EdDSA, Signer: key}, keySecrets(key)
}},
"federation issuers' Dependencies": {knownLeak: leaksSignerJSON,
"federation issuers' Dependencies": {
covers: []string{
"federation.HistoricalKeysIssueDependencies.Signer", "federation.ResolveIssueDependencies.Signer",
"federation.SelfIssueDependencies.Signer", "federation.SubordinateIssueDependencies.Signer",
Expand All @@ -116,21 +121,26 @@ var keyHolders = map[string]keyHolder{
federation.TrustMarkIssueDependencies{Signer: key, Algorithm: fapi.ES256},
}, keySecrets(key)
}},
"federation issuers": {
build: func(t *testing.T) (any, []string) {
key := ed25519Key(t)
return federationIssuers(t, key), keySecrets(key)
}},
"keys.NewInMemoryRSA": {covers: []string{"keys.inMemoryRSA.priv"},
build: func(t *testing.T) (any, []string) {
key := rsaKey(t)
return must(keys.NewInMemoryRSA(key, "k"))(t), keySecrets(key)
}},
"keys.NewInMemoryECDH": {covers: []string{"keys.inMemoryECDH.priv"}, knownLeak: leaksECDHBadVerb,
"keys.NewInMemoryECDH": {covers: []string{"keys.inMemoryECDH.priv"},
build: func(t *testing.T) (any, []string) {
key := must(ecdh.P256().GenerateKey(rand.Reader))(t)
return must(keys.NewInMemoryECDH(key, "k"))(t), keySecrets(key)
}},
"ephemeral.NewKeyManager ES256 and PS256": {covers: []string{"ephemeral.KeyManager.keys"},
"ephemeral.NewKeyManager ES256 and PS256": {covers: []string{"ephemeral.signingKey.signer"},
build: func(t *testing.T) (any, []string) {
return ephemeralKeyManager(t, map[keys.SigningPurpose]fapi.SignatureAlgorithm{keys.JARMSigning: fapi.ES256, keys.IDTokenSigning: fapi.PS256}, nil)
}},
"ephemeral.NewKeyManager EdDSA": {covers: []string{"ephemeral.KeyManager.keys"}, knownLeak: leaksEd25519,
"ephemeral.NewKeyManager EdDSA": {covers: []string{"ephemeral.signingKey.signer"},
build: func(t *testing.T) (any, []string) {
return ephemeralKeyManager(t, map[keys.SigningPurpose]fapi.SignatureAlgorithm{keys.JARMSigning: fapi.EdDSA}, nil)
}},
Expand Down Expand Up @@ -166,7 +176,11 @@ func TestKeyHoldersDontPrintTheirKeys(t *testing.T) {
t.Skip("known leak, awaiting a decision: " + tc.knownLeak)
}
value, secrets := tc.build(t)
rendertest.Check(t, value, secrets, rendertest.Quick())
opts := []rendertest.Option{rendertest.Quick()}
if tc.inputField != "" {
opts = append(opts, rendertest.Allow(rendertest.Fallback))
}
rendertest.Check(t, value, secrets, opts...)
})
}
}
Expand Down Expand Up @@ -326,6 +340,32 @@ func ed25519Key(t *testing.T) ed25519.PrivateKey {
return key
}

// federationIssuers is one of each federation issuer, signing with key.
func federationIssuers(t *testing.T, key ed25519.PrivateKey) any {
t.Helper()
const id = "https://entity.example"
clock := federation.SystemClock{}
jwks := []byte(`{"keys":[]}`)
return struct {
Historical *federation.HistoricalKeysIssuer
Resolve *federation.ResolveIssuer
Self *federation.SelfIssuer
Subordinate *federation.SubordinateIssuer
TrustMark *federation.TrustMarkIssuer
}{
must(federation.NewHistoricalKeysIssuer(federation.HistoricalKeysIssueConfig{EntityID: id},
federation.HistoricalKeysIssueDependencies{Signer: key, Algorithm: fapi.EdDSA, KeyID: "k", Clock: clock}))(t),
must(federation.NewResolveIssuer(federation.ResolveIssueConfig{EntityID: id},
federation.ResolveIssueDependencies{Signer: key, Algorithm: fapi.EdDSA, KeyID: "k", Clock: clock}))(t),
must(federation.NewSelfIssuer(federation.SelfIssueConfig{EntityID: id, Lifetime: time.Hour},
federation.SelfIssueDependencies{Signer: key, Algorithm: fapi.EdDSA, KeyID: "k", JWKS: jwks, Clock: clock}))(t),
must(federation.NewSubordinateIssuer(federation.SubordinateIssueConfig{EntityID: id, Lifetime: time.Hour},
federation.SubordinateIssueDependencies{Signer: key, Algorithm: fapi.EdDSA, KeyID: "k", Clock: clock}))(t),
must(federation.NewTrustMarkIssuer(federation.TrustMarkIssueConfig{EntityID: id},
federation.TrustMarkIssueDependencies{Signer: key, Algorithm: fapi.EdDSA, KeyID: "k", Clock: clock}))(t),
}
}

// signerKeyManager is keys.NewKeyManagerFromSigners over signer, with
// the key material it holds.
func signerKeyManager(t *testing.T, alg fapi.SignatureAlgorithm, signer crypto.Signer) (any, []string) {
Expand Down Expand Up @@ -356,10 +396,14 @@ func unexportedField(v reflect.Value, name string) reflect.Value {
return reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Elem()
}

// mapKeySecrets is the key material of m's values.
// mapKeySecrets is the key material of m's values: a key, or a pointer
// to a struct holding one in its signer field.
func mapKeySecrets(m reflect.Value) []string {
var secrets []string
for _, v := range m.Seq2() {
if v.Kind() == reflect.Pointer && v.Elem().Kind() == reflect.Struct {
v = unexportedField(v.Elem(), "signer")
}
secrets = append(secrets, keySecrets(v.Interface())...)
}
return secrets
Expand Down
Loading
Loading