Repository navigation
Description
Before reporting an issue
- I have read and understood the above terms for submitting issues, and I understand that my issue may be closed without action if I do not follow them.
Area
core
Describe the bug
SimpleHttpResponse.readResponse() and SimpleHttp.Response.readResponse() have two bugs in HTTP response handling:
-
Connection pool leak on invalid charset:
ContentType.getOrDefault(entity)is called beforeentity.getContent()opens the entity stream. If the remote server returns an invalid or unsupported charset in theContent-Typeheader,ContentType.getOrDefault()throwsUnsupportedCharsetExceptionbefore the entity stream is opened. SincestatusCodeis already set at that point, subsequent calls toreadResponse()(including viaclose()) return immediately without consuming the entity. The pooled HTTP connection is never returned, leading to connection pool exhaustion over time. -
Case-sensitive Content-Encoding comparison: The header name and value are compared using
equals()instead ofequalsIgnoreCase(). Per RFC 9110, HTTP header names are case-insensitive, andx-gzipis a valid alias forgzip(§8.4.1.3). A proxy or CDN that sendscontent-encoding: gzip(lowercase) would not be recognized.
Note: In practice, the manual gzip handling is a safety net — the underlying HTTP client (Vert.x/Netty or Apache HttpClient) performs automatic decompression before the response reaches this code. However, the connection pool leak (#1) can be triggered regardless of gzip handling.
Version
26.0
Rationale
These issues were flagged by Copilot during review of PR #52996 but were not addressed there as they were outside the original change set. The connection pool leak (#1) is a security hardening concern (CVSS 3.7 Low) — while the attack surface is constrained (requires a remote server returning an invalid charset), connection pool exhaustion can degrade all outbound HTTP functionality over time.
Assumptions
- The manual gzip decompression in
readResponse()is effectively dead code in production: the underlying HTTP clients (Vert.x/Netty viaVertxHttpClientBridge, Apache HttpClient viaResponseContentEncoding) already perform automatic gzip decompression and strip theContent-Encodingheader before the response reaches application code. All production callers obtain theirHttpClientfromHttpClientProvider, which uses these auto-decompressing clients. - Both Apache HttpClient and Vert.x/Netty already handle
x-gzipas an alias forgzipin their automatic decompression. InputStream.close()is idempotent for Apache HttpClient'sEofSensorInputStream, so the double-close that occurs whengzipis false (wheredecoded == entityStream) is harmless.
Decisions
- Fix the entity stream lifecycle: open
entity.getContent()in a try-with-resources before callingContentType.getOrDefault(entity), so that even if charset parsing throws, the entity stream is properly closed and the pooled connection is returned. - Use
response.getFirstHeader(HttpHeaders.CONTENT_ENCODING)instead of manually iterating headers — this uses Apache'sHttpHeaders.CONTENT_ENCODINGconstant and performs case-insensitive header name matching internally. - Add
x-gzipas a recognized alias forgzipper RFC 9110 §8.4.1.3, consistent with what both Apache HttpClient and Vert.x/Netty do. - Simplify stream reading by replacing the manual buffer loop with
StreamUtil.readString(), which is already used elsewhere in the Keycloak codebase and does the same thing. - Add a TODO comment noting that the manual gzip handling should be removed in a follow-up PR, since decompression is already performed by the underlying HTTP clients.
- Do not remove the manual gzip handling yet — while it is dead code in production, removing it is a larger change that should be done separately.
- Apply the same fixes to both
SimpleHttpResponseand the deprecatedSimpleHttp.Response— both share the identical code pattern.
Expected behavior
- The entity stream should be opened before parsing
ContentType, so that the try-with-resources guarantees the connection is returned to the pool even if charset parsing fails. - Content-Encoding header comparison should be case-insensitive and recognize both
gzipandx-gzipper RFC 9110 §8.4.1.3.
Actual behavior
- An
UnsupportedCharsetExceptionfrom a malformedContent-Typeheader leaks the pooled HTTP connection. Content-Encodingvalues in non-standard casing (e.g., lowercase from a proxy) are silently ignored, and thex-gzipalias is not recognized.
How to Reproduce?
- Have a remote server (e.g., an identity provider endpoint) return
Content-Type: text/html; charset=INVALID_CHARSETin its HTTP responses. - Trigger an outbound HTTP request from Keycloak to that server (e.g., during identity brokering).
- Observe that the HTTP connection is not returned to the pool.
- Repeat to exhaust the connection pool.
Anything else?
Both SimpleHttpResponse (org.keycloak.http.simple) and the deprecated SimpleHttp.Response (org.keycloak.broker.provider.util) share the same code pattern and are affected.
The fix also simplifies the stream reading by using the existing StreamUtil.readString() utility instead of a manual buffer loop.
Copilot flagged these issues during review of PR #52996 but they were not addressed there as they were outside the original change set.
Metadata
Metadata
Assignees
Labels
Type
Fields
Projects
- StatusShow more project fieldsDone
Activity
Close HTTP responses with illegal charsets
Close HTTP responses with illegal charsets
Several SimpleHttp fixes to always close connections (#53692)
Close HTTP responses with illegal charsets
Close HTTP responses with illegal charsets