weak-randomness
Bytes that must be secret read from a predictable generator.
- Group
- Code
- Runs
- Runs under ur scan:
ur scan --rule weak-randomness - Level
- warning
- Reads
- Every file
- Fix
- None: the safe form depends on what the value is meant to be
Why
The program fills bytes from a generator meant for simulations, such as Go's math/rand, whose output follows from its seed, so whoever learns a few values, or guesses the seed, can compute the keys, nonces, or tokens built from it. Read secret bytes from the operating system's generator, as Go's crypto/rand.Read.
The fix, as Ur suggests it: read the bytes from crypto/rand.
For agents
Never read keys, nonces, or tokens from math/rand; read them from crypto/rand.
Example
vault/key.gopackage vault
import "math/rand"
func Key() []byte {
key := make([]byte, 32)
rand.Read(key)
return key
}
$ ur scan --rule weak-randomness
warning[weak-randomness]: `rand.Read` uses math/rand, whose bytes are predictable
--> ./vault/key.go:7:2 module's top level
= hint: read the bytes from crypto/rand
= see: CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG), MITRE (2024-11-19), https://cwe.mitre.org/data/definitions/338.html
scanned 1 file (no framework recognized)
weak-randomness: 1 high, 0 medium
1 finding and 0 of medium confidence
Source
CWE-338: Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG), MITRE (2024-11-19).