Skip to content

Commit 7dc7d89

Browse files
committed
Avoid unsafe lazy-initialization for SSL sockets
This prevents calling HttpsURLConnection.getDefaultSSLSocketFactory() in an unsafe manner due to the poorly implemented lazy-initialization on the JDK. When multiple threads call that method concurrently (calling secureConnection()) the SSLSocketFactory is instantiated two times, making one thread fail the check and overriding the custom socket factory with the default one. Signed-off-by: Adrian Haasler García <adrian.haasler@amplia.es>
1 parent 839fb27 commit 7dc7d89

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

core-client/src/main/java/org/glassfish/jersey/client/internal/HttpUrlConnector.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public class HttpUrlConnector implements Connector {
7171

7272
private static final Logger LOGGER = Logger.getLogger(HttpUrlConnector.class.getName());
7373
private static final String ALLOW_RESTRICTED_HEADERS_SYSTEM_PROPERTY = "sun.net.http.allowRestrictedHeaders";
74+
// Avoid multi-thread uses of HttpsURLConnection.getDefaultSSLSocketFactory() because it does not implement a
75+
// proper lazy-initialization. See https://github.com/jersey/jersey/issues/3293
76+
private static final SSLSocketFactory DEFAULT_SSL_SOCKET_FACTORY = HttpsURLConnection.getDefaultSSLSocketFactory();
7477
// The list of restricted headers is extracted from sun.net.www.protocol.http.HttpURLConnection
7578
private static final String[] restrictedHeaders = {
7679
"Access-Control-Request-Headers",
@@ -299,7 +302,7 @@ protected void secureConnection(final JerseyClient client, final HttpURLConnecti
299302
suc.setHostnameVerifier(verifier);
300303
}
301304

302-
if (HttpsURLConnection.getDefaultSSLSocketFactory() == suc.getSSLSocketFactory()) {
305+
if (DEFAULT_SSL_SOCKET_FACTORY == suc.getSSLSocketFactory()) {
303306
// indicates that the custom socket factory was not set
304307
suc.setSSLSocketFactory(sslSocketFactory.get());
305308
}

0 commit comments

Comments
 (0)