Skip to content

Commit 6d4b9eb

Browse files
committed
fix(oidcjwt): report a canceled fetch for an unknown key ID as unavailable
A fetch for an unknown key ID starts a one-minute backoff even when the caller cancels it. Within that backoff, Verify returned a plain "unknown key ID", which the exchange maps to UNAUTHENTICATED, so a valid token with a newly rotated key ID failed without a retry. Until a fetch completes, wrap ErrSigningKeysUnavailable instead. Signed-off-by: Anish Ramasekar <[email protected]>
1 parent 5feb97d commit 6d4b9eb

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎internal/oidcjwt/oidcjwt.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ var (
145145

146146
// ErrSigningKeysUnavailable is wrapped by Verify errors that come from not
147147
// having the issuer's keys, rather than from the token: a failed fetch with no
148-
// cached key for the token, or the backoff that follows one.
148+
// cached key for the token, the backoff that follows one, or the backoff after
149+
// a fetch for an unknown key ID that did not complete.
149150
var ErrSigningKeysUnavailable = errors.New("could not fetch signing keys")
150151

151152
// KeySource supplies the public keys a Verifier checks signatures with. The
@@ -186,6 +187,9 @@ type Verifier struct {
186187
lastRefresh time.Time
187188
lastFailedRefresh time.Time
188189
lastUnknownKeyRefresh time.Time
190+
// unknownKeyFetchFailed says the fetch that set lastUnknownKeyRefresh
191+
// did not complete. Any successful fetch clears it.
192+
unknownKeyFetchFailed bool
189193
}
190194

191195
// Option configures a Verifier.
@@ -220,6 +224,7 @@ func (v *Verifier) Refresh(ctx context.Context, now time.Time) error {
220224
v.keys = keys
221225
v.lastRefresh = now
222226
v.lastFailedRefresh = time.Time{}
227+
v.unknownKeyFetchFailed = false
223228
return nil
224229
}
225230

@@ -414,6 +419,7 @@ func (v *Verifier) key(ctx context.Context, keyID string, now time.Time) (crypto
414419
// Set before the fetch so a caller that cancels it still counts, and
415420
// made-up key IDs cannot each trigger a fetch.
416421
v.lastUnknownKeyRefresh = now
422+
v.unknownKeyFetchFailed = true
417423
}
418424
keys, err := v.source.Keys(ctx)
419425
if err != nil {
@@ -433,6 +439,7 @@ func (v *Verifier) key(ctx context.Context, keyID string, now time.Time) (crypto
433439
v.keys = keys
434440
v.lastRefresh = now
435441
v.lastFailedRefresh = time.Time{}
442+
v.unknownKeyFetchFailed = false
436443
k := findKey(keys, keyID)
437444
if k == nil {
438445
return nil, fmt.Errorf("unknown key ID %q", keyID)
@@ -458,6 +465,11 @@ func (v *Verifier) cachedKey(keyID string, now time.Time) (key crypto.PublicKey,
458465
return nil, true, fmt.Errorf("%w from %s: the last attempt failed less than %v ago", ErrSigningKeysUnavailable, v.issuer, jwksRefreshRetryInterval)
459466
}
460467
if k == nil && len(v.keys) > 0 && !v.lastUnknownKeyRefresh.IsZero() && now.Sub(v.lastUnknownKeyRefresh) < jwksRefreshRetryInterval {
468+
if v.unknownKeyFetchFailed {
469+
// The issuer may have rotated this key in, and no fetch since
470+
// says otherwise.
471+
return nil, true, fmt.Errorf("%w from %s: a fetch for an unknown key ID did not complete less than %v ago", ErrSigningKeysUnavailable, v.issuer, jwksRefreshRetryInterval)
472+
}
461473
return nil, true, fmt.Errorf("unknown key ID %q", keyID)
462474
}
463475
return nil, false, nil

‎internal/oidcjwt/oidcjwt_test.go‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,16 @@ func TestVerifierCallerCancelStartsNoBackoff(t *testing.T) {
207207
}
208208
}
209209

210-
// cancelingKeySource returns keys on the first call. Later calls cancel the
211-
// caller's context as the fetch starts and return its error.
210+
// cancelingKeySource returns keys, except that the second call cancels the
211+
// caller's context as the fetch starts and returns its error.
212212
type cancelingKeySource struct {
213213
keys []*KeyAndID
214214
cancel context.CancelFunc
215215
calls atomic.Int32
216216
}
217217

218218
func (c *cancelingKeySource) Keys(ctx context.Context) ([]*KeyAndID, error) {
219-
if c.calls.Add(1) == 1 {
219+
if c.calls.Add(1) != 2 {
220220
return c.keys, nil
221221
}
222222
c.cancel()
@@ -226,7 +226,8 @@ func (c *cancelingKeySource) Keys(ctx context.Context) ([]*KeyAndID, error) {
226226
// TestVerifierCanceledUnknownKeyFetchIsRateLimited checks that a fetch for an
227227
// unknown key ID that the caller cancels still counts against the unknown key
228228
// ID rate limit, so repeated made-up key IDs with short deadlines do not each
229-
// fetch.
229+
// fetch. Until a fetch completes, the key ID may be one the issuer rotated in,
230+
// so Verify reports the keys as unavailable rather than the token as invalid.
230231
func TestVerifierCanceledUnknownKeyFetchIsRateLimited(t *testing.T) {
231232
key := testRSAKey(t)
232233
ctx, cancel := context.WithCancel(t.Context())
@@ -245,12 +246,22 @@ func TestVerifierCanceledUnknownKeyFetchIsRateLimited(t *testing.T) {
245246
if _, err := verifier.Verify(ctx, unknown, now); !errors.Is(err, context.Canceled) {
246247
t.Fatalf("Verify() with a fetch canceled by the caller error = %v, want context.Canceled", err)
247248
}
248-
if _, err := verifier.Verify(t.Context(), unknown, now.Add(time.Second)); err == nil || !strings.Contains(err.Error(), "unknown key ID") {
249-
t.Fatalf("Verify() after a canceled fetch error = %v, want unknown key ID", err)
249+
if _, err := verifier.Verify(t.Context(), unknown, now.Add(time.Second)); !errors.Is(err, ErrSigningKeysUnavailable) {
250+
t.Fatalf("Verify() after a canceled fetch error = %v, want ErrSigningKeysUnavailable", err)
250251
}
251252
if got := source.calls.Load(); got != 2 {
252253
t.Fatalf("key fetches = %d, want 2: the canceled fetch must start the unknown key ID backoff", got)
253254
}
255+
256+
later := now.Add(jwksRefreshRetryInterval)
257+
for _, at := range []time.Time{later, later.Add(time.Second)} {
258+
if _, err := verifier.Verify(t.Context(), unknown, at); err == nil || errors.Is(err, ErrSigningKeysUnavailable) || !strings.Contains(err.Error(), "unknown key ID") {
259+
t.Fatalf("Verify() at %v after a fetch without the key error = %v, want unknown key ID", at.Sub(now), err)
260+
}
261+
}
262+
if got := source.calls.Load(); got != 3 {
263+
t.Fatalf("key fetches = %d, want 3: a completed fetch must start the unknown key ID backoff", got)
264+
}
254265
}
255266

256267
// TestVerifierCallerCancelWithCachedKeyBacksOff checks that a canceled refresh

0 commit comments

Comments
 (0)