- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: kotlin-security/verify-ssl-certificates
Language: Kotlin
Severity: Error
Category: Security
CWE: 295
This rule mandates that SSL/TLS certificates always be validated. Certificate validation is an essential part of the SSL/TLS protocol that ensures the server you are communicating with is indeed who it claims to be. This prevents man-in-the-middle attacks, where an attacker intercepts and possibly alters the communication between two parties without their knowledge.
Ignoring or bypassing certificate validation severely undermines the security of your application and should be avoided.
To adhere to this rule, always use the system’s default SSLSocketFactory
and TrustManager
for SSL/TLS connections. These default settings perform certificate validation automatically. Never attempt to bypass or disable certificate validation. If you need to trust a self-signed certificate for testing purposes, add it to a custom trust store and use that instead of bypassing all certificate validation.
import javax.net.ssl.*
import okhttp3.OkHttpClient
import java.security.cert.X509Certificate
import java.security.KeyStore
class InsecureTlsConfigurations {
// Pattern 1: Bypass certificate validation in OkHttpClient
fun createInsecureOkHttpClient(): OkHttpClient {
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
})
val sslContext = SSLContext.getInstance("TLS").apply {
init(null, trustAllCerts, java.security.SecureRandom())
}
return OkHttpClient.Builder()
.sslSocketFactory(sslContext.socketFactory, trustAllCerts[0] as X509TrustManager)
.build()
}
// Pattern 2: Bypass in HttpsURLConnection
fun disableUrlConnectionValidation() {
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
})
val sslContext = SSLContext.getInstance("TLS").apply {
init(null, trustAllCerts, java.security.SecureRandom())
}
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.socketFactory)
}
}
import javax.net.ssl.*
import okhttp3.OkHttpClient
import java.security.cert.X509Certificate
import java.security.KeyStore
class SecureTlsConfigurations {
// Pattern 1: OkHttpClient with proper validation
fun createSecureOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder()
// Uses system default SSLSocketFactory and TrustManager
.build()
}
// Pattern 2: HttpsURLConnection with proper validation
fun createSecureUrlConnection(urlString: String): HttpsURLConnection {
val url = URL(urlString)
val connection = url.openConnection() as HttpsURLConnection
// Uses system default SSLSocketFactory and trust manager
// No need to override any SSL settings
return connection
}
}