Rules / Rust
SHIELD-RUST-010
Insecure randomness for security value
What it detects
rand::random / thread_rng is not a CSPRNG guarantee for tokens, keys, or nonces.
How to fix
Use a CSPRNG (rand::rngs::OsRng / getrandom) for security-sensitive values.
Vulnerable — Shield flags thissession.rs
pub fn new_session_id() -> String {
// Predictable PRNG seeds make these ids guessable.
let id: u64 = rand::random();
format!("sess-{:016x}", id)
}
Fixed — scans cleansession.rs
use rand::rngs::OsRng;
use rand::RngCore;
pub fn new_session_id() -> String {
let mut bytes = [0u8; 16];
OsRng.fill_bytes(&mut bytes);
format!("sess-{}", hex::encode(bytes))
}
Both snippets are verified against the shipped scanner: the vulnerable one triggers SHIELD-RUST-010, the fixed one does not.