Skip to content

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

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

lanbass869-cell wants to merge 1 commit 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.

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.

1 participant