This rule enforces the use of secure and unpredictable random numbers in Kotlin applications. Using pseudo-random numbers can make your code vulnerable to attacks because pseudo-random numbers follow a deterministic sequence that can be predicted if the initial seed is known. This is especially crucial in contexts such as generating encryption keys, generating random identifiers, or performing any other security-related functionalities.
To adhere to this rule, avoid using SecureRandom with a fixed seed using the setSeed() method or passing a byte array to the SecureRandom constructor. Both of these methods produce pseudo-random numbers, which can lead to vulnerabilities in your code. Also, avoid reseeding a SecureRandom instance with a predictable value, such as the current time.
Instead, create a SecureRandom instance without a set seed, or use SecureRandom.getInstanceStrong(). Following these best practices helps you generate secure and unpredictable random numbers in your Kotlin applications.
Non-Compliant Code Examples
importjava.security.SecureRandom// Setting a fixed numeric seed
valrandom1=SecureRandom()random1.setSeed(123456L)// Noncompliant
// Setting a fixed string seed
valrandom2=SecureRandom("myseed".toByteArray())// Noncompliant
// Reseeding with predictable value
valrandom3=SecureRandom()valtime=System.currentTimeMillis()random3.setSeed(time)// Noncompliant: timestamp is predictable
Compliant Code Examples
importjava.security.SecureRandom// Let SecureRandom choose its own seed
valrandom1=SecureRandom()valbytes=random1.nextBytes(32)// Use strong instance (preferred)
valrandom2=SecureRandom.getInstanceStrong()valnumber=random2.nextInt()
원활한 통합. 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 검사를 추가합니다