A SecureRandom gets a seed written in the source

tx0/java:seeded-secure-randomdefaultwarningsecurity, cwe-336

What it finds

new SecureRandom("seed".getBytes()) or random.setSeed(42) with a constant seed can make a SecureRandom produce the same sequence on every run, depending on the provider: the values are then as predictable as the seed in the source (CWE-336). Without a seed, the generator seeds itself from the operating system.

Reported are constant seeds, integer literals, array literals and the bytes of string literals, given to the SecureRandom constructor or to setSeed on a SecureRandom.

From the rule's source, seeded_secure_random.rs.

Example from the rule's tests
import java.security.SecureRandom; class Seeds {  void make() {    SecureRandom a = new SecureRandom("fixed".getBytes());tx0 reports: this seed is written in the source    SecureRandom b = new SecureRandom();    b.setSeed(42L);tx0 reports: this seed is written in the source    b.setSeed(System.nanoTime());  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:seeded-secure-random" = "off"

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:seeded-secure-random] reason="..."

tx0 explain tx0/java:seeded-secure-random shows the same in the terminal. Found a false positive? Report it.