Skip to content

fix(auth): do not accept an empty HTTP key body as a fetched key - #1869

Open
lanbass869-cell wants to merge 2 commits into
modelcontextprotocol:mainfrom
lanbass869-cell:fix/empty-http-key-body
Open

lanbass869-cell wants to merge 2 commits into
modelcontextprotocol:mainfrom
lanbass869-cell:fix/empty-http-key-body

Conversation

@lanbass869-cell

Copy link
Copy Markdown

fetchKeyOnce returns ("", nil) when the well-known endpoint answers 200 with 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 200 can land just as the client timeout fires. Do then succeeds, io.ReadAll yields nothing, and strings.TrimSpace("") is handed back as the public key with retryable=false. Consequences:

  • the second attempt never runs, so a transient edge/deploy glitch fails the whole HTTP login;
  • the caller fails later with no valid MCP public key found — an error pointing at the key, not at the transport;
  • a public key of "" is returned to KeyFetcher, where it is only rejected further down the stack.

This is not hypothetical. While reviewing the Tests job of #1861 it failed twice in the same file, with the same signature, on two different tests:

run failing test observed
1 TestHTTPKeyFetcherRetryProductionTimeBudget err=<nil> requests=1 elapsed=10.006368148s
2 TestHTTPKeyFetcherRetryRecoversAfterAttemptTimeout key="" err=<nil> requests=1

requests=1 with err=<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 200 with no body:

count=1 key="" err=<nil>
ACCEPTED empty key as success

Deterministic, no timing needed.

Fix

After the size check, treat an empty (or whitespace-only) body as a transient failure:

key := strings.TrimSpace(string(body))
if key == "" {
    return "", true, fmt.Errorf("HTTP auth key response was empty")
}
return key, false, nil

retryable=true means 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:

  • empty body → error, and both attempts are made (asserts requests == httpKeyFetchAttempts)
  • empty body on attempt 1, real key on attempt 2 → recovers and returns the key
  • whitespace-only body → treated as empty, not accepted

Mutation check: reverting the one condition makes all three fail with key="" err=<nil>.

Locally: go vet clean, gofmt clean, go test ./internal/api/handlers/v0/auth/ -skip OIDC passes. The OIDC tests in this package need accounts.google.com for provider discovery and cannot run without egress from this machine; that is unrelated to this change and they pass in CI.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.
@lanbass869-cell

Copy link
Copy Markdown
Author

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 Do/ReadAll rather than as an empty string. You are right that my original wording claimed a case this branch does not catch, and that #1865 was the flake.

Verified locally on the branch: go test -run TestHTTPKeyFetcher ./internal/api/handlers/v0/auth/ passes, including TestHTTPKeyFetcherRetryProductionTimeBudget, which is the timeout path in question.

@JosephDoUrden

Copy link
Copy Markdown
Contributor

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.

This branch has not been deployed

No deployments
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