Description
An X509TrustManager checks certificate chains. A HostnameVerifier checks whether the server certificate is valid for the intended host. Accepting every result in either check can authenticate the wrong server.
Potential impact
Another server may receive API requests or session tokens, or alter the responses.
Remediation
Use the default trust store and hostname checks. If a test uses a self-signed certificate, trust it in a separate test trust store and keep production verification enabled.
Examples
Before
import javax.net.ssl.HttpsURLConnection;
public class UnsafeHostnameVerifier {
void configure(HttpsURLConnection conn) {
conn.setHostnameVerifier((hostname, session) -> true);
}
}
After
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
public class SafeTlsVerifier {
void configure(HttpsURLConnection conn) {
HostnameVerifier verifier = HttpsURLConnection.getDefaultHostnameVerifier();
conn.setHostnameVerifier(verifier);
}
}
The example uses the current default from HttpsURLConnection.getDefaultHostnameVerifier(). Check that the application has not replaced that default with a verifier that accepts every host.
Other verification bypasses
checkServerTrusted must validate the server chain, and checkClientTrusted the chain used for client authentication. The same checks apply to named or anonymous Java classes and to Kotlin lambdas or objects.