Skip to content

SimpleHttpResponse: connection pool leak on invalid charset and case-sensitive Content-Encoding check #53494

Description

@ahus1

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:

  1. Connection pool leak on invalid charset: ContentType.getOrDefault(entity) is called before entity.getContent() opens the entity stream. If the remote server returns an invalid or unsupported charset in the Content-Type header, ContentType.getOrDefault() throws UnsupportedCharsetException before the entity stream is opened. Since statusCode is already set at that point, subsequent calls to readResponse() (including via close()) return immediately without consuming the entity. The pooled HTTP connection is never returned, leading to connection pool exhaustion over time.

  2. Case-sensitive Content-Encoding comparison: The header name and value are compared using equals() instead of equalsIgnoreCase(). Per RFC 9110, HTTP header names are case-insensitive, and x-gzip is a valid alias for gzip (§8.4.1.3). A proxy or CDN that sends content-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 via VertxHttpClientBridge, Apache HttpClient via ResponseContentEncoding) already perform automatic gzip decompression and strip the Content-Encoding header before the response reaches application code. All production callers obtain their HttpClient from HttpClientProvider, which uses these auto-decompressing clients.
  • Both Apache HttpClient and Vert.x/Netty already handle x-gzip as an alias for gzip in their automatic decompression.
  • InputStream.close() is idempotent for Apache HttpClient's EofSensorInputStream, so the double-close that occurs when gzip is false (where decoded == entityStream) is harmless.

Decisions

  • Fix the entity stream lifecycle: open entity.getContent() in a try-with-resources before calling ContentType.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's HttpHeaders.CONTENT_ENCODING constant and performs case-insensitive header name matching internally.
  • Add x-gzip as a recognized alias for gzip per 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 SimpleHttpResponse and the deprecated SimpleHttp.Response — both share the identical code pattern.

Expected behavior

  1. 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.
  2. Content-Encoding header comparison should be case-insensitive and recognize both gzip and x-gzip per RFC 9110 §8.4.1.3.

Actual behavior

  1. An UnsupportedCharsetException from a malformed Content-Type header leaks the pooled HTTP connection.
  2. Content-Encoding values in non-standard casing (e.g., lowercase from a proxy) are silently ignored, and the x-gzip alias is not recognized.

How to Reproduce?

  1. Have a remote server (e.g., an identity provider endpoint) return Content-Type: text/html; charset=INVALID_CHARSET in its HTTP responses.
  2. Trigger an outbound HTTP request from Keycloak to that server (e.g., during identity brokering).
  3. Observe that the HTTP connection is not returned to the pool.
  4. 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.

Activity

  1. added theissue type on Oct 3, 2026
  2. added
    area/core
    on Oct 3, 2026
  3. added
    priority/important
    on Oct 5, 2026
  4. added a commit that references this issue on Oct 7, 2026
    7976013
  5. self-assigned this
    on Oct 7, 2026
  6. added a commit that references this issue on Oct 8, 2026
    e3a0a45
  7. added a commit that references this issue on Oct 9, 2026
    e17a115
  8. added 2 commits that reference this issue on Oct 22, 2026
    2514c1b
    7762200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Fields

No fields configured for weakness.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions