Freeman Yue Fang created CXF-9266:
-------------------------------------
Summary: HttpClientHTTPConduit: a shared HttpClient uses another
client's proxy and TLS settings
Key: CXF-9266
URL: https://issues.apache.org/jira/browse/CXF-9266
Project: CXF
Issue Type: Bug
Reporter: Freeman Yue Fang
By default, HttpClientHTTPConduit shares one java.net.http.HttpClient between
conduits with equal settings. When the settings of a conduit change after its
first call, for example with conduit.getClient().setProxyServer(...) or
conduit.getTlsClientParameters().setTrustManagers(...), the shared HttpClient
applies one client's settings to other clients:
* *Proxy:* a client is routed through the proxy of another client that shares
its HttpClient, and its own proxy settings are ignored.
* *TLS:* a client configured with a strict trust store can get an HttpClient
whose SSLContext was built from another client's trust-all trust manager, and
then accepts a server certificate it does not trust.
* *Built-in settings* such as the connection timeout: a client can get an
HttpClient built with a different timeout than the one it was configured with.
With share.httpclient.http.conduit=false, or with the URLConnection based
conduit, each client uses its own settings.
h3. Reproducer
Attached: a Maven project with two JUnit 5 test classes. Each test creates two
WebClients with the same settings, makes a first call with each, and then
changes the settings of one of them. Every test runs with sharing on (the
default) and off. JDK 21, CXF 4.2.4 and 4.1.9, 3 of 3 runs:
||Test||Sharing on||Sharing off||
|Only client 1 is given a proxy|*client 2's request goes through client 1's
proxy*|client 2 calls the backend directly|
|Only client 2 is given a proxy|*client 2 ignores it and calls the backend
directly*|client 2 goes through its proxy|
|Client 1 starts with a trust-all trust manager and is switched to the JDK's
default trust managers; client 2 uses the default trust managers from the
start, against a server with a self-signed certificate|*client 2 accepts the
server*|client 2 fails with "PKIX path building failed"|
Run with {{mvn test}} (CXF 4.2.4) or {{mvn test -Dcxf.version=4.1.9}}.
h3. Cause
HttpClientHTTPConduit keeps the shared clients in a static cache
(CLIENTS_CACHE). It builds each HttpClient from the settings of the conduit
that first needs it, and hands that client to later conduits whose settings are
equal.
* *The cache key is the creating conduit's own, mutable settings.* An entry
keeps a reference to that conduit's HTTPClientPolicy and TLSClientParameters,
not a copy, and compares them with equals(). When the conduit's settings
change, the entry's key changes with them, but the HttpClient stays the one
built from the old settings. A conduit created later with the new settings
matches the entry and gets that HttpClient, including its SSLContext and
connection timeout.
* *The proxy of a shared client comes from the creating conduit.* The
HttpClient's ProxySelector (ProxyFactoryProxySelector) holds the creating
conduit's HTTPClientPolicy and reads it on every request, so every conduit
sharing the client is routed by that conduit's proxy settings.
h3. Possible fix
* The cache keeps a copy of the settings a client was built from, as its key,
and builds the ProxySelector from that copy.
HttpClientHTTPConduit keeps the shared clients in a static cache
(CLIENTS_CACHE). It builds each HttpClient from the settings of the conduit
that first needs it, and hands that client to later conduits whose settings are
equal.
* *The cache key is the creating conduit's own, mutable settings.* An entry
keeps a reference to that conduit's HTTPClientPolicy and TLSClientParameters,
not a copy, and compares them with equals(). When the conduit's settings
change, the entry's key changes with them, but the HttpClient stays the one
built from the old settings. A conduit created later with the new settings
matches the entry and gets that HttpClient, including its SSLContext and
connection timeout.
* *The proxy of a shared client comes from the creating conduit.* The
HttpClient's ProxySelector (ProxyFactoryProxySelector) holds the creating
conduit's HTTPClientPolicy and reads it on every request, so every conduit
sharing the client is routed by that conduit's proxy settings.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)