A secret such as a token or salt comes from a generator whose output can be predicted
What it finds
A token, password, salt or key drawn from java.util.Random or Math.random() can be predicted: these generators have 48 bits of state, and a few outputs reveal all later ones (CWE-330). SecureRandom draws values that cannot be guessed from earlier ones.
Reported are values of Random and Math.random() stored in a variable, or returned from a method, whose name says it holds a secret: one with the word token, password, passphrase, secret, salt, nonce, otp or csrf, or api key, secret key or session id. ThreadLocalRandom counts as Random.
From the rule's source, predictable_secret.rs.
import java.util.Random;import java.util.concurrent.ThreadLocalRandom; class Tokens { private final Random random = new Random(); String resetToken() { long token = random.nextLong();tx0 reports: token gets a value that can be predicted return Long.toHexString(token); } long newSalt() { return random.nextLong();tx0 reports: newSalt gets a value that can be predicted } int dice() { return random.nextInt(6); } void session() { long csrfToken = ThreadLocalRandom.current().nextLong();tx0 reports: csrfToken gets a value that can be predicted int notPresent = random.nextInt(); double footprint = Math.random(); }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:predictable-secret" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:predictable-secret] reason="..."tx0 explain tx0/java:predictable-secret shows the same in the terminal. Found a false positive? Report it.