Skip to content

perf(auth/github-oidc): cache GitHub's JWKS between token exchanges - #1924

Merged
rdimitrov merged 3 commits into
modelcontextprotocol:mainfrom
JosephDoUrden:perf/github-oidc-jwks-cache
Nov 28, 2026
Merged

rdimitrov merged 3 commits into
modelcontextprotocol:mainfrom
JosephDoUrden:perf/github-oidc-jwks-cache

Conversation

@JosephDoUrden

Copy link
Copy Markdown
Contributor

Today every call to /auth/github-oidc fetches 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 any kid costs one request to GitHub. It also goes through http.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 the max-age GitHub 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 unknown kid or 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 kid are 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 unknown kid with 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 a kid is used. One small difference: entries without a kid are now skipped. GitHub's all have one.

One thing I left alone: /v0 and /v0.1 each 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 run make test-all.

AI assistance was used during development and testing.

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 rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
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 rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rdimitrov
rdimitrov merged commit 56e4b06 into modelcontextprotocol:main Nov 28, 2026
9 checks passed
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.

2 participants