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
64 changes: 62 additions & 2 deletions .github/mutation/mutants.json
Original file line number Diff line number Diff line change
Expand Up @@ -2646,8 +2646,8 @@
{
"id": "fed-resolution-failure-remembered-before-hook",
"file": "federation/automatic_registration.go",
"old": "a.rememberFailure(id, err, a.clock.Now())\n\t\tif a.cfg.OnResolutionFailure != nil {\n\t\t\ta.cfg.OnResolutionFailure(ctx, id, err)\n\t\t}",
"new": "if a.cfg.OnResolutionFailure != nil {\n\t\t\ta.cfg.OnResolutionFailure(ctx, id, err)\n\t\t}\n\t\ta.rememberFailure(id, err, a.clock.Now())",
"old": "a.rememberFailure(id, err, a.clock.Now())\n\t\ta.publishFailure(call, err)\n\t\tif a.cfg.OnResolutionFailure != nil {\n\t\t\ta.cfg.OnResolutionFailure(ctx, id, err)\n\t\t}",
"new": "a.publishFailure(call, err)\n\t\tif a.cfg.OnResolutionFailure != nil {\n\t\t\ta.cfg.OnResolutionFailure(ctx, id, err)\n\t\t}\n\t\ta.rememberFailure(id, err, a.clock.Now())",
"module": "root",
"packages": [
"./federation"
Expand Down Expand Up @@ -5598,5 +5598,65 @@
],
"run": "^TestIssuersPrintNoKey$",
"why": "log/slog logs a TrustMarkIssuer's redacted shadow"
},
{
"id": "fed-chain-expiry-skew",
"file": "federation/resolver.go",
"old": "if now.After(expiresAt.Add(r.cfg.Limits.MaxClockSkew)) {",
"new": "if now.After(expiresAt) {",
"module": "root",
"packages": [
"./federation"
],
"run": "^(TestResolveRefusesChainExpiredWhileResolving|TestAutomaticRegistrationRefusesChainExpiredWhileRegistering|TestAutomaticRegistrationRefusesChainExpiredWhileResolving)$",
"why": "a chain still within MaxClockSkew of its exp when its resolution finishes is accepted, as each statement's exp check accepts it"
},
{
"id": "fed-resolve-end-expiry-check",
"file": "federation/resolver.go",
"old": "if err := r.checkChainUnexpired(subjectID, resolved.ExpiresAt, r.deps.Clock.Now()); err != nil {",
"new": "if err := r.checkChainUnexpired(subjectID, resolved.ExpiresAt, r.deps.Clock.Now()); err != nil && false {",
"module": "root",
"packages": [
"./federation"
],
"run": "^(TestResolveRefusesChainExpiredWhileResolving|TestAutomaticRegistrationRefusesChainExpiredWhileRegistering|TestAutomaticRegistrationRefusesChainExpiredWhileResolving)$",
"why": "Resolve refuses a Trust Chain that expired before its resolution finished"
},
{
"id": "fed-register-expiry-check",
"file": "federation/automatic_registration.go",
"old": "if err := a.resolver.checkChainUnexpired(string(id), resolved.ExpiresAt, now); err != nil {",
"new": "if err := a.resolver.checkChainUnexpired(string(id), resolved.ExpiresAt, now); err != nil && false {",
"module": "root",
"packages": [
"./federation"
],
"run": "^(TestResolveRefusesChainExpiredWhileResolving|TestAutomaticRegistrationRefusesChainExpiredWhileRegistering|TestAutomaticRegistrationRefusesChainExpiredWhileResolving)$",
"why": "automatic registration refuses a chain that expired while the registration was built (jwks_uri fetch)"
},
{
"id": "fed-reentrant-hook-published-failure",
"file": "federation/automatic_registration.go",
"old": "if ok && call.failure != nil {",
"new": "if ok && call.failure != nil && false {",
"module": "root",
"packages": [
"./federation"
],
"run": "^(TestReentrantResolutionFailureHookReturns|TestReentrantResolutionFailureHookReturnsWhenMemoryFull)$",
"why": "a request for a client_id whose resolution is reporting its failure gets it at once instead of waiting on the hook"
},
{
"id": "fed-reentrant-hook-publish",
"file": "federation/automatic_registration.go",
"old": "\t\ta.publishFailure(call, err)\n",
"new": "\t\t_ = call\n",
"module": "root",
"packages": [
"./federation"
],
"run": "^(TestReentrantResolutionFailureHookReturns|TestReentrantResolutionFailureHookReturnsWhenMemoryFull)$",
"why": "the failure is published on the in-flight resolution before OnResolutionFailure runs"
}
]
32 changes: 32 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ 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.85.1

### A Trust Chain that expires while it resolves is refused (federation)

**Affects:** a server using automatic registration
(`Config.AutomaticRegistration`), or code calling
`federation.Resolver.Resolve`, whose Relying Parties' Trust Chains can
expire while they are being resolved: a chain with little lifetime
left, fetched over slow federation endpoints.

**Why:** `Resolve` verified every statement at the time it started,
so a resolution whose fetches outlasted the chain's earliest `exp`
returned a chain that had already expired, and automatic registration
handed the requests waiting on it a client registered from that chain.
`Resolve` now checks the chain's expiry (`ResolvedEntity.ExpiresAt`,
with `Limits.MaxClockSkew`'s tolerance) again when it finishes, and
automatic registration checks it once more after building the
registration (which can fetch the RP's `jwks_uri`).

**What fails:** such a resolution returns an error ("the trust chain
for ... expired at ..., before its resolution finished"); for automatic
registration the client gets `invalid_client`, the failure is reported
to `OnResolutionFailure` and remembered for `FailureCacheAge`, as any
other registration failure. A resolution started after the chain
expired was already refused, so the only new refusals are of chains
that expire mid-resolution.

**What to change:** nothing in code. If it shows up in
`OnResolutionFailure`, the federation's statements are issued with too
little lifetime for how long its endpoints take to answer: ask the
superior or RP to issue them for longer.

## v0.85.0

### Codes, refresh tokens and `auth_req_id`s are stored under digests covering the issuer (server, storage)
Expand Down
3 changes: 2 additions & 1 deletion docs/guides/openid-federation.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ your operator why a client was refused, once per resolution attempted; the
client itself only sees `invalid_client`. The hook runs on a goroutine of
the library's own, shared by the requests for that client_id made while it
resolves, and can run after the request that triggered the resolution has
returned. It must not block, panic, or resolve the same client_id. A panic
returned. It must not block or panic; resolving the same client_id from it
gets the failure it reports at once, rather than waiting on itself. A panic
in it is recovered rather than ending the process: the requests waiting on
that resolution get `server_error`, and the failure is remembered as if the
hook had returned.
Expand Down
62 changes: 49 additions & 13 deletions federation/automatic_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,18 @@ type AutomaticRegistrationConfig struct {
// that started the resolution has returned, while another still
// waits. ctx keeps that first request's values but not its
// cancellation, and is cancelled once no request waits on the
// resolution any more. So it must not block, must not panic, and
// must not call ResolveClient for the same client_id, which could
// wait on the very resolution calling it. The failure is remembered,
// as FailureCacheAge sets out, before it is called, so a request
// arriving while it runs is answered from that memory instead of
// joining the resolution. A panic in it is recovered rather than
// ending the process: every request waiting on the resolution gets
// resolution any more. So it must not block and must not panic. The
// failure is remembered, as FailureCacheAge sets out, before it is
// called, and a request for that client_id arriving while it runs
// gets that failure at once instead of joining the resolution, even
// once FailureCacheAge has passed or when the failure couldn't be
// remembered. That includes a ResolveClient (or
// AutomaticClientKeySource lookup) for the same client_id made from
// the hook itself, which so never waits on the very resolution
// calling it; resolving another client_id from it needs a place
// under MaxConcurrentResolutions of its own while this resolution
// still holds one. A panic in it is recovered rather than ending
// the process: every request waiting on the resolution gets
// an error wrapping storage.ErrStoreUnavailable (the server answers
// 500 server_error), and the failure it was called for is remembered
// as if it had returned. A failure is remembered for FailureCacheAge,
Expand Down Expand Up @@ -486,14 +491,18 @@ type AutomaticClientRepository struct {
// inflightResolution is one in-progress resolution that concurrent
// callers for the same client_id wait on; done is closed once entry and
// err are set. waiters counts the callers waiting on it, and cancel
// stops it once the last of them has gone. Both are guarded by
// stops it once the last of them has gone. failure, once set, is the
// failure the resolution has ended in, while OnResolutionFailure is
// called with it: a caller for its client_id gets that at once instead
// of waiting (see resolveUncached). waiters and failure are guarded by
// AutomaticClientRepository.mu.
type inflightResolution struct {
done chan struct{}
entry cachedClient
err error
waiters int
cancel context.CancelFunc
failure error
}

// NewAutomaticClientRepository validates cfg and returns an
Expand Down Expand Up @@ -693,6 +702,10 @@ func (a *AutomaticClientRepository) resolve(ctx context.Context, id fapi.ClientI
return cachedClient{}, err
}
call, ok := a.inflight[id]
if ok && call.failure != nil {
a.mu.Unlock()
return cachedClient{}, call.failure
}
if !ok {
if a.running >= a.maxConcurrentResolutions() {
a.mu.Unlock()
Expand Down Expand Up @@ -793,7 +806,7 @@ func (a *AutomaticClientRepository) startResolution(ctx context.Context, id fapi
func (a *AutomaticClientRepository) runResolution(ctx context.Context, id fapi.ClientID, call *inflightResolution) {
entry, err := cachedClient{}, errResolutionUnfinished
defer func() { a.finishResolution(id, call, entry, err, recover()) }()
entry, err = a.resolveUncached(ctx, id)
entry, err = a.resolveUncached(ctx, id, call)
}

// finishResolution records call's outcome for every caller waiting on
Expand Down Expand Up @@ -872,8 +885,8 @@ func (a *AutomaticClientRepository) leaveResolution(id fapi.ClientID, call *infl
// resolveUncached resolves id's Trust Chain and builds its registration,
// caching a success and reporting a failure to
// AutomaticRegistrationConfig.OnResolutionFailure. resolve runs at most
// one at a time per client_id.
func (a *AutomaticClientRepository) resolveUncached(ctx context.Context, id fapi.ClientID) (cachedClient, error) {
// one at a time per client_id, as call.
func (a *AutomaticClientRepository) resolveUncached(ctx context.Context, id fapi.ClientID, call *inflightResolution) (cachedClient, error) {
if err := ValidEntityID(string(id)); err != nil {
// Not a federation client_id at all — an ordinary unknown
// client, so nothing for OnResolutionFailure to report.
Expand All @@ -891,8 +904,14 @@ func (a *AutomaticClientRepository) resolveUncached(ctx context.Context, id fapi
return cachedClient{}, err
}
// Remembered before the hook runs, so a hook that panics (see
// runResolution) still leaves the failure remembered.
// runResolution) still leaves the failure remembered. And
// published on call, so a request for id while the hook runs,
// the hook's own included, gets it at once rather than waiting
// on call, which the hook would keep from finishing: memory
// alone doesn't do that when it is full or the hook outlasts
// FailureCacheAge.
a.rememberFailure(id, err, a.clock.Now())
a.publishFailure(call, err)
if a.cfg.OnResolutionFailure != nil {
a.cfg.OnResolutionFailure(ctx, id, err)
}
Expand Down Expand Up @@ -920,6 +939,15 @@ func (a *AutomaticClientRepository) rememberRegistration(id fapi.ClientID, entry
delete(a.failures, id)
}

// publishFailure records err, bounded as rememberFailure bounds it, as
// the failure call has ended in, for the callers resolve answers from
// it while OnResolutionFailure runs.
func (a *AutomaticClientRepository) publishFailure(call *inflightResolution, err error) {
a.mu.Lock()
defer a.mu.Unlock()
call.failure = newRememberedFailure(err)
}

// rememberFailure records err as id's registration failure until
// FailureCacheAge after now, within maxFailedResolutions, keeping only
// its bounded message (see rememberedFailure) and its own copy of id
Expand Down Expand Up @@ -962,8 +990,16 @@ func (a *AutomaticClientRepository) register(ctx context.Context, id fapi.Client
return cachedClient{}, fmt.Errorf("federation: %q: %w", id, err)
}

// Building the registration can fetch (its jwks_uri), so the chain
// can expire after Resolve has checked it: a registration whose
// chain has expired by now is never handed out, not even to the
// requests waiting on this resolution.
now := a.clock.Now()
if err := a.resolver.checkChainUnexpired(string(id), resolved.ExpiresAt, now); err != nil {
return cachedClient{}, err
}
expiresAt := resolved.ExpiresAt
if maxAge := a.clock.Now().Add(a.cfg.MaxCacheAge); maxAge.Before(expiresAt) {
if maxAge := now.Add(a.cfg.MaxCacheAge); maxAge.Before(expiresAt) {
expiresAt = maxAge
}
superior, branch := chainPosition(resolved.Chain)
Expand Down
6 changes: 5 additions & 1 deletion federation/automatic_registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type automaticRegistrationFixture struct {
rpConfigCalls *int32
rpMux *http.ServeMux

// pool trusts both servers' certificates, for a test building a
// fetcher of its own over them.
pool *x509.CertPool

// setTAPolicy, set by setupAutomaticRegistrationFixtureUnderIntermediate
// only, has the Trust Anchor sign and serve its Subordinate Statement
// about the Intermediate with another metadata policy from then on.
Expand Down Expand Up @@ -140,7 +144,7 @@ func setupAutomaticRegistrationFixtureWithPolicy(t *testing.T, metadataFn func(r
taID: taID, rpID: rpID, taKey: taKey, taJWKS: taJWKS,
rpFedKey: rpFedKey, rpOIDCKey: rpOIDCKey,
fetcher: fetcher, now: now, rpConfigCalls: &calls,
rpMux: rpMux,
rpMux: rpMux, pool: pool,
}
}

Expand Down
4 changes: 4 additions & 0 deletions federation/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func CachedChainPosition(r *AutomaticClientRepository, id fapi.ClientID) (superi
return slot.entry.superior, slot.entry.branch, true
}

// MaxFailedResolutions is maxFailedResolutions, for tests in package
// federation_test.
const MaxFailedResolutions = maxFailedResolutions

// MaxEntityIDBytes is maxEntityIDBytes, for tests in package
// federation_test.
const MaxEntityIDBytes = maxEntityIDBytes
Expand Down
Loading
Loading