A token, key, password or salt is made from math/rand, whose output can be predicted

tx0/go:predictable-random-secretrecommendedwarningsecurity, cwe-338

What it finds

token := rand.Int63() with math/rand: the generators of math/rand and math/rand/v2 are fast and statistically fine, but not built to resist prediction, so someone who sees some outputs can work out the others. Secrets come from crypto/rand, as in rand.Read(buf) or rand.Text().

Reported are calls of functions of math/rand or math/rand/v2, resolved through the imports, whose value is assigned to a variable or field, or returned from a function, whose name contains token, secret, password, passwd, nonce, salt, apikey or sessionid, or a name ending in key other than one of a few names such as cacheKey, in any case.

From the rule's source, predictable_random_secret.rs.

Example from the rule's tests
package p import (	"fmt"	"math/rand") type session struct{ token string } func newToken() string {	return fmt.Sprintf("%x", rand.Int63())tx0 reports: newToken is made from math/rand, whose values can be predicted} func f(s *session) int {	s.token = fmt.Sprint(rand.Int())tx0 reports: token is made from math/rand, whose values can be predicted	apiKey := rand.Int63()tx0 reports: apiKey is made from math/rand, whose values can be predicted	jitter := rand.Intn(100)	key := rand.Intn(10)	return jitter + key + int(apiKey)}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:predictable-random-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/go:predictable-random-secret] reason="..."

tx0 explain tx0/go:predictable-random-secret shows the same in the terminal. Found a false positive? Report it.