- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
",t};e.buildCustomizationMenuUi=t;function n(e){let t='
",t}function s(e){let n=e.filter.currentValue||e.filter.defaultValue,t='${e.filter.label}
`,e.filter.options.forEach(s=>{let o=s.id===n;t+=``}),t+="${e.filter.label}
`,t+=`ID: swift-security/weak-random
Language: Unknown
Severity: Error
Category: Security
CWE: 338
This rule detects the use of random number generators that are not guaranteed to be cryptographically secure. Functions like arc4random()
and Int.random()
are suitable for general-purpose tasks like simulations or games, but they do not provide the strong guarantees of unpredictability required for security-sensitive operations.
Using a weak PRNG for cryptographic purposes (such as generating keys, initialization vectors, nonces, or salts) can expose an application to vulnerabilities where an attacker could predict the random values, compromising the security of the system. This corresponds to CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG).
For generating random values for security-sensitive contexts, Apple’s Security framework provides the SecRandomCopyBytes
function, which is the recommended API for obtaining cryptographically secure random data.
import Foundation
// This function generates a simple numeric "token" for a non-security purpose, like a game ID.
// While fine for that use case, it would be flagged if used for security.
func generateGameSessionID() -> UInt32 {
// VIOLATION: arc4random_uniform is not a cryptographically secure PRNG.
return arc4random_uniform(1000000)
}
// This function generates a random integer within a range.
func rollDice() -> Int {
// VIOLATION: Int.random(in:) is not guaranteed to be cryptographically secure.
// It is suitable for simulations or games, but not for generating secrets.
return Int.random(in: 1...6)
}
// This function attempts to generate a random key.
func generateWeakKey() -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// VIOLATION: String.randomElement() relies on the default non-secure PRNG.
let randomChar = letters.randomElement()!
return "key-\(randomChar)"
}
// Using the legacy C-style random function.
func legacyRandom() -> Int32 {
// VIOLATION: random() is a weak, predictable PRNG and should not be used.
return random()
}
import Foundation
import Security // Must import the Security framework
// This function generates a cryptographically secure token of a specified length.
func generateSecureToken(length: Int) -> String? {
// 1. Create a buffer of the desired size to hold the random bytes.
var randomBytes = [UInt8](repeating: 0, count: length)
// 2. Call SecRandomCopyBytes to fill the buffer with cryptographically secure random data.
// This is the recommended, compliant method.
let status = SecRandomCopyBytes(kSecRandomDefault, randomBytes.count, &randomBytes)
// 3. Check if the operation was successful.
if status == errSecSuccess {
// 4. Convert the raw bytes into a usable format, like a Base64 encoded string.
return Data(randomBytes).base64EncodedString()
} else {
// Handle the failure case. It is critical to not proceed if secure random data cannot be generated.
print("Error: Unable to generate secure random bytes. Status: \(status)")
return nil
}
}
// Example usage:
// let secureAPIToken = generateSecureToken(length: 32)
// let initializationVector = generateSecureToken(length: 16)