Repository navigation
Conversation
The GitHub OIDC exchange fetched the JWKS on every request, inside the JWT keyfunc and before the signature, issuer or audience were checked, using http.DefaultClient with no timeout. Cache the parsed keys by kid for one hour, matching the max-age GitHub sends on the JWKS document. An unknown kid or an expired cache starts a refetch, at most once every 30 seconds, and concurrent callers share the fetch in progress. A failed fetch keeps the cached keys until they expire; after that the exchange fails closed. A response with no usable RSA keys counts as a failed fetch, so it cannot evict good keys. As before, only the first entry for a kid is used. The fetch has its own 10 second timeout and is not cancelled when the triggering caller goes away, so a client cannot keep the cache empty by hanging up. go-oidc's RemoteKeySet was not used because it refetches on every unknown kid with no rate limit.
rdimitrov
requested changes
Nov 28, 2026
rdimitrov
left a comment
Member
There was a problem hiding this comment.
Please address the cache replacement issue below before merging. The auth package tests passed locally with the race detector.
A JWKS entry with a missing or malformed modulus or exponent parsed into a key that can never verify a signature. If it was the only entry in a refresh, it replaced keys that still worked. Refuse such keys in parseRSAPublicKey, using the same bounds rsa.VerifyPKCS1v15 enforces, so the refresh counts as failed and the cached keys stay.
rdimitrov
reviewed
Nov 28, 2026
A token with "kid": "" can never match a cached key, but it still started a JWKS refresh and used up the 30 second window. Treat it like a missing kid. Read at most 1 MiB of a 200 JWKS response, as error bodies are already bounded. A larger document fails to decode, so the fetch counts as failed and the cached keys stay.
rdimitrov
approved these changes
Nov 28, 2026
rdimitrov
left a comment
Member
There was a problem hiding this comment.
The RSA parameter checks address my earlier change request. The regression tests confirm that a refresh containing only a malformed key preserves the working cached key. The auth package passed locally with the race detector, and go vet passed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Today every call to
/auth/github-oidcfetches GitHub's JWKS. The fetch runs inside the JWT keyfunc, so it happens before the signature, issuer or audience are checked, and any RS256 token with anykidcosts one request to GitHub. It also goes throughhttp.DefaultClient, which has no timeout of its own.This keeps the parsed keys in the validator, by
kid, for one hour. I picked one hour because that is themax-ageGitHub sends on the JWKS response. GitHub's OIDC docs don't say how long to cache the JWKS or how often the signing keys change. An unknownkidor an expired cache starts a refetch, at most one every 30 seconds, and concurrent requests wait on the same fetch. So a stream of made-up kids now costs a couple of requests a minute instead of one per request.Rotation: a new key is picked up the first time a token naming it arrives. If a fetch ran in the last 30 seconds, tokens with the new
kidare rejected until that window passes. A key GitHub drops is still accepted until the next successful fetch, at most an hour later.Outages: if GitHub is unreachable the cached keys keep working until the hour is up, then the exchange fails closed until a fetch succeeds. A 200 with no usable RSA keys counts as a failed fetch, so an empty or broken document can't wipe out good keys. Failed attempts count towards the 30 second limit too.
The fetch has its own 10 second timeout, the same budget as the HTTP key fetcher, and it is not cancelled when the request that started it goes away. Otherwise a client could start a fetch, hang up, and keep the cache empty for everyone else. The caller itself still stops waiting when its context ends.
I looked at go-oidc's
RemoteKeySet, which the generic OIDC handler already uses. It refetches on every unknownkidwith no rate limit, so it wouldn't remove the fetch per bad token.Not changed: claim validation, permissions, and
NewMockOIDCValidator's signature. As before, only the first JWKS entry for akidis used. One small difference: entries without akidare now skipped. GitHub's all have one.One thing I left alone:
/v0and/v0.1each register their own handler, so each has its own cache and the real ceiling is two fetches per 30 seconds per replica.Tests use an httptest JWKS server that counts requests, plus a fake clock: repeated exchanges, unknown kids, rotation, TTL expiry, fetch failure with a warm cache, with a cold or expired cache, a response with no usable keys, malformed and duplicate entries, 20 concurrent exchanges, and a cancelled caller.
go test ./internal/api/handlers/v0/auth/...passes with and without-race, and the GitHub OIDC tests pass 30 times in a row under-race. gofmt, go vet and golangci-lint v2.14.0 are clean on the package. I didn't runmake test-all.AI assistance was used during development and testing.