temporal icon indicating copy to clipboard operation
temporal copied to clipboard

Default JWT key provider hangs on an unresponsive key endpoint and replaces working keys on JSON error responses

Open shinseongsu opened this issue 1 month ago • 0 comments

Expected Behavior

  • A key endpoint that does not answer should not block server startup, the periodic key refresh, or Close().
  • A failed refresh (for example an HTTP 503) should keep the previously loaded keys and report an error.

Actual Behavior

defaultTokenKeyProvider.openURI (common/authorization/default_token_key_provider.go) fetches http(s) key sources with http.Get(uri): no overall timeout, no request context, and the status code is never checked. This produces three failures. Numbers below are from main at ed5ad7b, measured against a local httptest server.

1. NewDefaultTokenKeyProvider blocks when the endpoint accepts the request and never answers

The constructor fetches keys synchronously, and GetClaimMapperFromConfig calls it while the server is configured (cmd/server/main.go:212).

Situation Result
Endpoint accepts the request, never responds constructor had not returned after 15s (measurement cap)

2. Close() blocks while a refresh request is stuck

The refresh goroutine only receives on stop between refreshes, so a.stop <- true waits for the stuck request.

Situation Result (3 runs)
First load succeeds, a later refresh request never responds, then Close() Close() had not returned after 5s in all 3 runs

3. A JSON error response replaces the working keys

A non-200 response with a JSON object body decodes into an empty jose.JSONWebKeySet without an error, and updateKeys swaps the loaded keys for the empty set. Nothing is logged because no error is returned.

Refresh response keys before -> after updateKeys() error RsaKey("RS256","key-1")
503 {"message":"Service Unavailable"} 1 -> 0 nil not found
404 {"error":"not found"} 1 -> 0 nil not found
500 empty body 1 -> 1 EOF found

Until the next successful refresh, every token signed with the removed key fails validation. Empty bodies and HTML error pages are not affected because they fail to decode.

Steps to Reproduce the Problem

Add this test to common/authorization/ and run go test ./common/authorization -run TestRepro -count=1 (case 3 shown; cases 1 and 2 use a handler that blocks on a channel).

func TestReproErrorStatusWipesKeys(t *testing.T) {
	privateKey, _ := rsa.GenerateKey(rand.Reader, 2048)
	body, _ := json.Marshal(jose.JSONWebKeySet{Keys: []jose.JSONWebKey{
		{Key: &privateKey.PublicKey, KeyID: "key-1", Algorithm: "RS256", Use: "sig"}}})

	var fail atomic.Bool
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		if fail.Load() {
			w.WriteHeader(http.StatusServiceUnavailable)
			_, _ = w.Write([]byte(`{"message":"Service Unavailable"}`))
			return
		}
		_, _ = w.Write(body)
	}))
	defer server.Close()

	provider := NewDefaultTokenKeyProvider(&config.Authorization{JWTKeyProvider: config.JWTKeyProvider{
		KeySourceURIs: []string{server.URL}}}, log.NewNoopLogger())
	_, err := provider.RsaKey("RS256", "key-1")
	require.NoError(t, err)

	fail.Store(true)
	require.NoError(t, provider.updateKeys()) // passes on main: the 503 is not reported
	_, err = provider.RsaKey("RS256", "key-1")
	require.NoError(t, err) // fails on main: RSA key not found for key ID: key-1
}

Cases 1 and 2 need a connection that is established and then stalls (a proxy, or a firewall dropping packets mid-request), because Go's default transport already bounds dialing and the TLS handshake. Case 3 only needs a transient upstream error that answers with a JSON body.

Specifications

  • Version: main at ed5ad7b
  • Only deployments that set authorization.jwtKeyProvider.keySourceURIs to http(s) URLs are affected

Possible fix

Use a client with a timeout, build requests with a context that Close() cancels, and treat any non-200 response as an error so the previous keys stay in place. I have this change and regression tests ready and am happy to open a PR if this looks right.

shinseongsu avatar Nov 02 '26 06:11 shinseongsu