Skip to content

fix: bound the per-key fetch gates in UpstreamCredentials - #65

Merged
blakebauman merged 2 commits into
mainfrom
fix/upstream-fetch-gate-leak
Aug 14, 2026
Merged

blakebauman merged 2 commits into
mainfrom
fix/upstream-fetch-gate-leak

Conversation

@blakebauman

Copy link
Copy Markdown
Member

What

cachedFetch created a fetch mutex for every cache key, but pruneLocked only dropped the ones whose key was also present in the token cache:

for k, t := range c.tokens {
    if !now.Before(t.expires) {
        delete(c.tokens, k)
        delete(c.fetchMu, k)   // only ever reached for keys in c.tokens
    }
}

A token exchange that fails never reaches the token cache, so its gate was never pruned. Under token-exchange the key is (issuer, subject) — an identifier the gateway does not choose — and a caller with no grant on that upstream fails on every request. So the map grew with the caller population and never shrank, which is exactly what the bounded-cache rule in CLAUDE.md exists to prevent:

Per-instance affinity caches use internal/bounded — every one of them must be size-bounded, because all are keyed by identifiers the gateway does not choose.

How

Each gate now carries a waiter count (guarded by the existing UpstreamCredentials.mu) and is deleted when the last caller leaves, so the map is bounded by the fetches in flight rather than the callers ever seen. Lock ordering matches the existing cache write — gate, then c.mu — so the two cannot deadlock.

pruneLocked no longer touches the gate map at all, which removes the coupling that caused the bug. Single-flighting is unchanged: TestTokenExchangeSingleFlight still passes.

Tests

TestFetchGatesDoNotLeak drives 500 principals whose exchange is rejected and asserts zero surviving gates. On the previous code that map holds all 500 — which is the steady state for callers who have not connected the upstream.

TestTokenCachePruning asserted the old contract (that pruning bounds the gate map), so it now covers the token cache alone.

Not changed

auth/jwks.go:32 has the identical pattern, but its keys are JWKS URIs from the issuer config — identifiers the gateway does choose, bounded by the config document. Not a leak, so left alone.

Gate

gofmt, go mod tidy -diff, go vet, go build, go test -race ./..., and golangci-lint run (0 issues) all pass locally.

🤖 Generated with Claude Code

blakebauman and others added 2 commits August 13, 2026 17:48
cachedFetch created a fetch mutex for every cache key but pruneLocked only
dropped the ones whose key was also in the token cache. A token exchange
that *fails* never reaches the token cache, so its gate was never pruned —
and under token-exchange the key is (issuer, subject), an identifier the
gateway does not choose. A caller with no grant on the upstream fails on
every request, so the map grew with the caller population and never shrank.

Give each gate a waiter count and drop it when the last caller leaves, so
the map is bounded by the fetches in flight rather than by the callers ever
seen. pruneLocked no longer touches it, which removes the coupling that
caused the bug.

TestTokenCachePruning asserted the old contract — that pruning bounds the
gate map — so it moves to covering the token cache alone.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@blakebauman
blakebauman merged commit 4560398 into main Aug 14, 2026
7 checks passed
@blakebauman
blakebauman deleted the fix/upstream-fetch-gate-leak branch August 14, 2026 01:16
blakebauman added a commit that referenced this pull request Aug 14, 2026
Covers #65, #66 and #68. Filed under "Unreleased" rather than a dated
version: entries here are written as part of a release and land in the
release commit, so a dated heading on main before the tag is the exact
drift the release runbook warns about. It becomes the version heading when
these ship.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
blakebauman added a commit that referenced this pull request Aug 14, 2026
Dates the changelog entry for the three caller-derived credential fixes
(#65, #66, #68) and bumps the chart: appVersion to v1.12.1, which is what a
default helm install deploys, and the chart's own version to 0.2.2, since a
published OCI tag is immutable.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant