System.Random produces a token, password, key, salt or nonce, which an attacker can predict
What it finds
System.Random is a fast generator for simulations; its output can be reconstructed from a few observed values. Secrets need RandomNumberGenerator. The rule reports new Random(...) and Random.Shared used in a method, or stored in a variable, whose name mentions a token, password, secret, salt, nonce, key or one-time code.
From the rule's source, predictable_random_secret.rs.
using System; class Accounts{ string ResetToken() { var rng = new Random();tx0 reports: System.Random is predictable, but ResetToken suggests a secret return rng.Next().ToString(); } int Dice() => new Random().Next(6);} 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/csharp: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/csharp:predictable-random-secret] reason="..."tx0 explain tx0/csharp:predictable-random-secret shows the same in the terminal. Found a false positive? Report it.