Skip to content

Migrate all SDK HTTP requests to HttpClient 5 - #90

Merged
franciscbalint merged 5 commits into
zerobounce:masterfrom
mikebell90:migrate-httpclient5
Nov 13, 2026
Merged

franciscbalint merged 5 commits into
zerobounce:masterfrom
mikebell90:migrate-httpclient5

Conversation

@mikebell90

@mikebell90 mikebell90 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Moves the SDK from Apache HttpClient 4.5 to HttpClient 5.6. Apache only ships security and major-defect fixes for 4.x and recommends moving to 5.x (status page).

No public API changes.

What changed

  • All requests (JSON, forms, multipart uploads, downloads, Warmup) go through one HttpClient 5 helper in SdkHttp.
  • HttpClient 4, HttpCore 4, HttpMime and the Commons Codec pin are removed.
  • Redirects and automatic retries stay off, including for 429 and 503.
  • Connections are reused across calls: one shared client per timeout value, at most eight. Each runs HttpClient's idle-connection evictor, a daemon thread that closes connections after 5 seconds idle. Callers still have nothing to close.
  • JVM proxy and TLS system properties are honoured.

Wire changes worth checking against the live API

  • JSON POST bodies (e.g. validateBatch) send Content-Type: application/json; charset=UTF-8. HttpURLConnection used to send application/x-www-form-urlencoded by default.
  • Multipart text fields are UTF-8 instead of ISO-8859-1.
  • User-Agent is Apache-HttpClient/5.6.5 (Java/…) instead of Java/….
  • HttpClient 5 logs through SLF4J, so apps without a binding get SLF4J's one-time warning on stderr.
  • Apps that used org.apache.http classes through this SDK now need to declare HttpClient 4 themselves.

All of these are in the CHANGELOG.

Testing

  • 219 offline tests pass on Java 25. The first commit (215 tests) also passed on Java 21.
  • Loopback HTTP tests hit every public request path: truncated bodies, redirects, timeouts, 429/503 without retry, multipart bytes, downloads, auth headers, proxy settings and User-Agent.
  • Against a local keep-alive server, repeated calls reused one connection, a stale pooled connection didn't break the next call, and idle connections were closed about 6 seconds after a burst.

Not verified

The live API: I didn't have keys. Running live/LiveApi.java with ZEROBOUNCE_LIVE_SPEND=1 and ZEROBOUNCE_LIVE_CHANGES=1 would cover the wire changes above.

mikebell90 and others added 3 commits November 12, 2026 19:03
- Share one HttpClient 5 client per timeout across calls and instances
  (at most eight), so repeated calls reuse connections as HttpURLConnection
  did. Idle connections are checked before reuse after 1 s, since automatic
  retries are off, and kept 5 s unless the server says otherwise. The pool
  has no per-host limit, so concurrent calls never queue behind it.
- Use the JVM proxy and TLS system properties on the client and the
  connection manager; with the previous builder, https.proxyHost was ignored.
- Keep HttpClient's default User-Agent instead of sending none.
- Pin httpcore5 and httpcore5-h2 to one version property.
- CHANGELOG: list the removed HttpClient 4 dependencies under Removed, and
  the Content-Type, multipart charset, SLF4J, proxy, User-Agent and
  connection-reuse changes under Changed.
- Tests open HttpClients mocks through HttpClientTestSupport.mockClients(),
  which drops the shared clients around each mock, and pin the proxy,
  User-Agent and client sharing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Shared clients run HttpClient's idle-connection evictor (one daemon
  thread each), closing connections idle for 5 s or past their keep-alive.
  The pool only closes an idle connection when it is next used, so after a
  burst of concurrent calls those sockets stayed open indefinitely.
  Per-call fallback clients skip the evictor; they are closed after the call.
- Admit a new timeout value under a lock, re-checking the size there.
  SHARED.size() and computeIfAbsent() were separate steps: 64 concurrent
  callers with distinct timeouts got 64 permanent clients instead of 8.
- CHANGELOG and AGENTS.md describe the evictor and the eight-value limit
  instead of saying idle connections are dropped.
- Test: 64 callers released together with distinct timeouts get exactly
  eight shared clients. It fails on the previous commit (64).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mikebell90
mikebell90 marked this pull request as ready for review November 13, 2026 04:10
@franciscbalint

Copy link
Copy Markdown
Collaborator

Security review: one finding (Medium)

Reviewed at aee8feb.

HttpClient 5 debug logging writes the API key in plain text, outside the SDK's redaction (CWE-532)

Where: SdkHttp.newClient / exchange / execute, and every call site moved onto them in ZeroBounceSDK, ZeroBounceWarmup and Priority3Api.

Before this PR, the JSON GET/POST paths and all Warmup calls used HttpURLConnection, which doesn't log through SLF4J. Now every request goes through HttpClient 5, which logs straight to SLF4J, bypassing ZBLogger and redact(). From the 5.6.5 source:

  • org.apache.hc.client5.http.impl.classic.MainClientExec logs "{} executing {} {}" with request.getRequestUri() at DEBUG. Most endpoints send the key as a query parameter, so this logs ...?api_key=<key>.
  • org.apache.hc.client5.http.headers (DefaultManagedHttpClientConnection) logs every request header at DEBUG. That includes the Warmup API-Key: <key> and the inbox-placement Authorization: Bearer <key>.
  • org.apache.hc.client5.http.wire logs raw request bodies at DEBUG: form and multipart api_key fields, and the SMTP password from testSmtpConfiguration.

When it happens: any app whose SLF4J backend has org.apache.hc at DEBUG. That includes logback with no config file, where the root logger defaults to DEBUG on the console. The app doesn't need to call setLogPayloads or setLogger. This breaks the CHANGELOG's promise that SDK logging "never carries the API key".

The old HttpClient 4 multipart and getfile paths had the same exposure through commons-logging. This PR spreads it to every endpoint and Warmup, and routes it through SLF4J, where DEBUG is more often on.

Suggested fixes:

  • Keep the key out of what HttpClient logs. For example, use a connection factory that doesn't bind the header/wire loggers, and move the key out of query strings where the API allows it.
  • At minimum, add a CHANGELOG/README warning next to the SLF4J note: never run org.apache.hc at DEBUG in production.
  • Add a regression test with a capturing SLF4J binding at DEBUG that asserts the key and SMTP password appear in no log line.

Checked and fine

  • TLS: the system-default context is used, hostname verification stays on, and there's no trust-all.
  • Redirects, retries, cookies: all disabled.
  • Shared connection pool across SDK instances: no cookie or auth state is carried between API keys.
  • Proxy and Authenticator support behaves the same as HttpURLConnection did.
  • Header injection: header values are constants or the API key, which is already rejected if it contains control characters.
  • Truncated responses: a Content-Length shortfall still throws, so a cut-off body isn't treated as complete.

At DEBUG, HttpClient 5 logs the request line (with api_key), every
request header (Authorization, API-Key) and the raw request bytes (form
fields such as the SMTP password). This is outside the SDK's ZBLogger
redaction and can't be turned off from the builder.

- README Security: keep org.apache.hc at INFO or above in production;
  logback with no configuration file logs at DEBUG.
- README logging section: "silent by default" covers the SDK's own
  logging, not HttpClient's.
- CHANGELOG: the SLF4J entry names the loggers, and notes that up to
  3.0.1 HttpClient 4 and HttpURLConnection logged the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mikebell90

Copy link
Copy Markdown
Contributor Author

Thanks, confirmed. With an SLF4J backend at DEBUG for org.apache.hc, HttpClient 5 logs the request line, the request headers and the raw request bytes, so api_key, Authorization/API-Key and form fields end up in the log. At INFO it logs none of it.

This isn't new to HC5. The HC4 multipart and getfile paths did the same through commons-logging, and HttpURLConnection logs the request line and headers to java.util.logging at FINE, ?api_key= and Authorization: Bearer included (checked on JDK 25). What changes is that SLF4J backends are more often left at DEBUG.

HttpClient logs the request line regardless of builder settings, so a test asserting no key at DEBUG can't pass while the key is in the query string. I've documented it instead in bced013: a README Security note next to the existing api_key warning, and the CHANGELOG's SLF4J entry, both saying not to run org.apache.hc at DEBUG in production. I can also switch off the header and wire loggers with a custom connection class if you want it, but that only covers part of it and relies on HttpClient internals.

The section opened with "silent by default so it never emits PII",
which only holds for the SDK's own ZBLogger. It now says up front that
two loggers can write during a request: the SDK's (off until a ZBLogger
is registered, key redacted) and HttpClient's, through the app's SLF4J
backend, which writes keys and the SMTP password at DEBUG.

Adds a complete logback.xml that keeps org.apache.hc at INFO, for apps
running Logback with no configuration file (which logs at DEBUG). The
Security bullet links to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@franciscbalint
franciscbalint merged commit 54c44ae into zerobounce:master Nov 13, 2026
1 check passed
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