A password hash is derived with a salt fixed in the code

tx0/csharp:constant-password-saltstrictwarningsecurity/cryptodepth fastsecurity, cwe-760

What it finds

A salt makes the hash of one password differ between users and between systems, so that one table of precomputed hashes cannot crack them all and equal passwords do not show as equal hashes. A salt written in the code is the same for everyone and defeats both. Each password needs its own random salt, from RandomNumberGenerator, stored next to the hash.

Reported is the salt argument of new Rfc2898DeriveBytes(...), Rfc2898DeriveBytes.Pbkdf2 and KeyDerivation.Pbkdf2 when it is fixed: an array of literal bytes, a zeroed new byte[n], the bytes of a string literal, or a variable or field of the file initialized with literal bytes and used for nothing else. The constructors that take a salt size make a random salt and are fine, and so is a zeroed array in a variable, which is usually filled with random bytes before use. Tests, which use fixed salts to check known results, are left alone.

From the rule's source, constant_password_salt.rs.

Example from the rule's tests
using System.Security.Cryptography;using System.Text; class Hasher{    private static readonly byte[] Pepper = { 1, 2, 3, 4, 5, 6, 7, 8 };     byte[] Fixed(string password)    {        using var kdf = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes("salty"), 100000, HashAlgorithmName.SHA256);tx0 reports: the salt of this password hash is fixed in the code        return kdf.GetBytes(32);    }     byte[] Zeroed(string password) => Rfc2898DeriveBytes.Pbkdf2(password, new byte[16], 100000, HashAlgorithmName.SHA256, 32);tx0 reports: the salt of this password hash is fixed in the code     byte[] Field(string password) => Rfc2898DeriveBytes.Pbkdf2(password, Pepper, 100000, HashAlgorithmName.SHA256, 32);tx0 reports: the salt of this password hash is fixed in the code     byte[] Random(string password)    {        byte[] salt = new byte[16];        RandomNumberGenerator.Fill(salt);        return Rfc2898DeriveBytes.Pbkdf2(password, salt, 100000, HashAlgorithmName.SHA256, 32);    }     byte[] Sized(string password)    {        using var kdf = new Rfc2898DeriveBytes(password, 16, 100000, HashAlgorithmName.SHA256);        return kdf.GetBytes(32);    }     byte[] Given(string password, byte[] salt) => Rfc2898DeriveBytes.Pbkdf2(password, salt, 100000, HashAlgorithmName.SHA256, 32);}
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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:constant-password-salt" = "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:constant-password-salt] reason="..."

tx0 explain tx0/csharp:constant-password-salt shows the same in the terminal. Found a false positive? Report it.