This rule enforces the secure verification of SSL/TLS hostnames when validating certificates. In secure communication, this step is crucial to prevent Man-In-The-Middle (MITM) attacks. In such a case, an attacker could intercept the communication, present a fraudulent certificate, and if hostname verification is not implemented or is improperly implemented, the client might accept it.
Developers sometimes disable hostname verification for testing purposes or to bypass certain network restrictions. This practice opens up a serious security vulnerability when used in production code. You must ensure that you do not override the hostname verifier to return true for all hostnames, because this accepts any certificate, even if it’s not valid for the server you’re connecting to.
To avoid violating this rule, always use the default hostname verification provided by the SSL/TLS library (such as OkHttpClient, HttpsURLConnection, or SSLContext). These libraries have secure hostname verification enabled by default; do not disable or modify it. If you need to handle exceptions for certain hostnames, you can use a custom hostname verifier that checks the hostname against a list of allowed exceptions instead of accepting all hostnames.
Non-Compliant Code Examples
importjavax.net.ssl.*importjava.net.URLimportokhttp3.OkHttpClientimportjava.security.cert.X509CertificateclassInsecureConnections{// Pattern 1: OkHttpClient with disabled verification
funcreateInsecureOkHttpClient():OkHttpClient{returnOkHttpClient.Builder().hostnameVerifier{_,_->true}// Insecure: accepts any hostname
.build()}// Pattern 2: OkHttpClient with disabled verification
funcreateInsecureOkHttpClient2():OkHttpClient{valbuilder2=OkHttpClient.Builder()builder2.hostnameVerifier(object: HostnameVerifier{overridefunverify(hostname:String?,session:SSLSession?):Boolean{// Insecure: accepts any hostname
returntrue}})}// Pattern 3: HttpsURLConnection with disabled verification
funcreateInsecureUrlConnection(urlString:String):HttpsURLConnection{valurl=URL(urlString)valconnection=url.openConnection()asHttpsURLConnectionconnection.hostnameVerifier=HostnameVerifier{_,_->true}// Insecure
returnconnection}// Pattern 4: SSLSocketFactory with disabled verification
funcreateInsecureSSLContext():SSLContext{valtrustAllCerts=arrayOf<TrustManager>(object: X509TrustManager{overridefungetAcceptedIssuers():Array<X509Certificate>=arrayOf()overridefuncheckClientTrusted(chain:Array<X509Certificate>,authType:String){}overridefuncheckServerTrusted(chain:Array<X509Certificate>,authType:String){}})returnSSLContext.getInstance("TLS").apply{init(null,trustAllCerts,java.security.SecureRandom())}.also{context->HttpsURLConnection.setDefaultSSLSocketFactory(context.socketFactory)// Insecure: Disables hostname verification globally
HttpsURLConnection.setDefaultHostnameVerifier{_,_->true}}}}
Compliant Code Examples
classSecureConnections{// Pattern 1: OkHttpClient with default verification
funcreateSecureOkHttpClient():OkHttpClient{returnOkHttpClient.Builder()// No custom hostname verifier = uses default secure verification
.build()}// Pattern 2: HttpsURLConnection with default verification
funcreateSecureUrlConnection(urlString:String):HttpsURLConnection{valurl=URL(urlString)returnurl.openConnection()asHttpsURLConnection// Default hostname verifier is secure
}// Pattern 3: SSLSocketFactory with default verification
funcreateSecureSSLContext():SSLContext{returnSSLContext.getInstance("TLS").apply{init(null,null,null)// Uses system default security providers
}// Default hostname verification remains intact
}}
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- kotlin-security # Rules to enforce Kotlin security.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다