Custom Verifier Returning True
Implementing HostnameVerifier that always returns true without checking hostname.
Hostname verification vulnerabilities occur when Java applications implement HostnameVerifier that always returns true or use NoopHostnameVerifier, skipping critical hostname validation checks. This allows man-in-the-middle attackers to present valid certificates for different domains and successfully intercept HTTPS traffic, exposing sensitive data and credentials.
import javax.net.ssl.*;
import java.net.URL;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
// VULNERABLE: Custom verifier that accepts any hostname
HostnameVerifier allHostsValid = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true; // DANGEROUS: Always returns true
}
};
// Apply permissive verifier to connection
URL url = new URL("https://api.example.com/data");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setHostnameVerifier(allHostsValid);
// Also vulnerable: Using NoopHostnameVerifier
// SSLConnectionSocketFactory socketFactory =
// new SSLConnectionSocketFactory(
// SSLContext.getDefault(),
// NoopHostnameVerifier.INSTANCE);import javax.net.ssl.*;
import java.net.URL;
import org.apache.http.conn.ssl.*;
// SECURE: Use default hostname verification
URL url = new URL("https://api.example.com/data");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
// Default verifier is used automatically - no custom verifier needed
// For Apache HttpClient - use DefaultHostnameVerifier
SSLConnectionSocketFactory socketFactory =
new SSLConnectionSocketFactory(
SSLContext.getDefault(),
new DefaultHostnameVerifier() // Strict verification
);
// If custom verification is absolutely necessary:
HostnameVerifier strictVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// Delegate to default verifier for actual validation
return HttpsURLConnection.getDefaultHostnameVerifier()
.verify(hostname, session);
}
};The vulnerable code implements HostnameVerifier that always returns true, accepting any certificate regardless of hostname mismatch. This allows MITM attackers to present certificates for different domains. The secure version uses default hostname verification which properly validates that the certificate matches the requested hostname.
Implementing HostnameVerifier that always returns true without checking hostname.
Sourcery automatically identifies insecure hostname verification in java https clients and many other security issues in your codebase.