Repository navigation
fix(auth): do not accept an empty HTTP key body as a fetched key - #1869
lanbass869-cell wants to merge 2 commits into
Conversation
fetchKeyOnce turned a 200 with a zero-length body into ("", nil). An
endpoint that answers 200 and then stalls races the client timeout: if the
empty 200 lands first, Do succeeds, io.ReadAll returns nothing, and the
empty string is handed back as the public key. The retry is skipped and the
caller only fails later with "no valid MCP public key found", which points
at the key rather than at the transport.
CI surfaced it twice as key="" err=<nil> requests=1 in
TestHTTPKeyFetcherRetryRecoversAfterAttemptTimeout and
TestHTTPKeyFetcherRetryProductionTimeBudget.
Treat an empty (or whitespace-only) body as a transient failure so the
existing second attempt runs and the error names the real problem.
JosephDoUrden
left a comment
There was a problem hiding this comment.
Tested this locally. The three new tests pass on the head and all fail on the base without the fix. After merging current main, go test -race on the auth package (skipping OIDC) is 63/63. Also tried CRLF only, Content-Length 0 plus chunked, an always-empty endpoint (2 requests, ~0.5s) and cancelling in the backoff, all fine. One thing: a timeout mid-body already comes back as a read error, not "". The CI flake was the test handler returning on r.Context().Done(), fixed on main in #1865, so I'd reword the comment at http.go:290. Otherwise LGTM.
Review feedback (JosephDoUrden): a timeout mid-body already surfaces as a read error, not as ""; the CI flake was the test handler returning on r.Context().Done(), fixed on main in modelcontextprotocol#1865. The comment claimed a case this branch does not handle, so it now scopes itself to a 200 with zero bytes and says the mid-read case is a different path.
|
Reworded in f6b4940. The comment now scopes itself to a 200 carrying zero bytes, and states explicitly that a body cut off mid-read - including when the client timeout fires - already surfaces as a read error from Verified locally on the branch: |
|
Re-ran on f6b4940: auth tests green under -race (OIDC ones skipped), and dropping the empty check fails all three new tests. The doc comment on TestHTTPKeyFetcherEmptyBodyIsNotTreatedAsSuccess still describes the timeout race, could use the same rewording. |
fetchKeyOncereturns("", nil)when the well-known endpoint answers200with a zero-length body. It is reachable in production, and it showed up in CI as a flake.What goes wrong
If an endpoint sends the response headers and then stalls, the empty
200can land just as the client timeout fires.Dothen succeeds,io.ReadAllyields nothing, andstrings.TrimSpace("")is handed back as the public key withretryable=false. Consequences:no valid MCP public key found— an error pointing at the key, not at the transport;""is returned toKeyFetcher, where it is only rejected further down the stack.This is not hypothetical. While reviewing the
Testsjob of #1861 it failed twice in the same file, with the same signature, on two different tests:TestHTTPKeyFetcherRetryProductionTimeBudgeterr=<nil> requests=1 elapsed=10.006368148sTestHTTPKeyFetcherRetryRecoversAfterAttemptTimeoutkey="" err=<nil> requests=1requests=1witherr=<nil>is only reachable through the empty-body path: a genuine timeout is an error and triggers the retry.Reproduction
Against the pre-patch code, a server that returns
200with no body:Deterministic, no timing needed.
Fix
After the size check, treat an empty (or whitespace-only) body as a transient failure:
retryable=truemeans the existing second attempt now runs, and if both attempts are empty the returned error names the actual problem instead of surfacing as a key-parsing failure. The success path, the permanent-failure classification and the timeout budget are unchanged.Tests
New
http_empty_body_internal_test.go:requests == httpKeyFetchAttempts)Mutation check: reverting the one condition makes all three fail with
key="" err=<nil>.Locally:
go vetclean,gofmtclean,go test ./internal/api/handlers/v0/auth/ -skip OIDCpasses. The OIDC tests in this package needaccounts.google.comfor provider discovery and cannot run without egress from this machine; that is unrelated to this change and they pass in CI.