Security rules
NGB086: Static password-hashing salt
Reports password hashing / key derivation whose salt is a compile-time constant: Web Crypto PBKDF2 (deriveBits / deriveKey), Node crypto.pbkdf2(Sync) and crypto.scrypt(Sync), CryptoJS.PBKDF2, bcrypt.hash(password, "<constant salt>") and argon2.hash(password, { salt })..
| Property | Value |
|---|---|
| Rule ID | NGB086 |
| Category | Cryptography |
| Default severity | medium |
| CWE | CWE-760, CWE-916 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports password hashing / key derivation whose salt is a compile-time constant: Web Crypto PBKDF2 (deriveBits / deriveKey), Node crypto.pbkdf2(Sync) and crypto.scrypt(Sync), CryptoJS.PBKDF2, bcrypt.hash(password, "<constant salt>") and argon2.hash(password, { salt }).
Why it matters
A salt exists so that equal passwords produce different hashes and an attacker cannot reuse one precomputed (rainbow) table or one brute-force run for every user. A constant salt — the same string or byte array for every password — removes that: every user with the password "Summer2024!" gets the same hash, and one cracking run attacks all accounts at once. Keys derived from a password with a fixed salt are likewise identical across users and installations.
Detection behavior and supported patterns
crypto.subtle.deriveBits({ name: 'PBKDF2', salt, … })/deriveKey(…)onSubtleCryptopbkdf2(pw, salt, …),pbkdf2Sync(…),scrypt(pw, salt, …),scryptSync(…)fromcrypto/node:crypto(named, namespace, default orrequire()bindings)CryptoJS.PBKDF2(pw, salt, …),bcrypt.hash(pw, salt)/hashSync(bcrypt, bcryptjs) with a string salt,argon2.hash(pw, { salt })- Constant salts: string literals /
conststrings,new TextEncoder().encode('…'),Buffer.from('…'), literal byte arrays, zero-filled buffers (new Uint8Array(16)), andconst/ readonly / config-object bindings of those
Example
const hash = crypto.pbkdf2Sync(password, 'ngbastion-salt', 600000, 32, 'sha256');Safer alternative
const salt = crypto.randomBytes(16);const hash = crypto.pbkdf2Sync(password, salt, 600000, 32, 'sha256');Remediation
Generate a fresh random salt per password (crypto.getRandomValues(new Uint8Array(16)), crypto.randomBytes(16), bcrypt.genSalt()) and store it with the hash. Better: let the library manage salts (bcrypt.hash(pw, 12), argon2.hash(pw)).
False-positive considerations
- Deriving an application-wide key (not a per-user password hash) from a secret passphrase with a fixed salt is reported; such keys should still use a random salt stored next to the ciphertext.
Known limitations
- Salts derived deterministically from user data (e-mail, username) are predictable but not constant and are not reported.
- A random salt generated once at module level and reused for every user is not reported.
- Hashing done by other libraries (node-forge, libsodium wrappers) is not analyzed.
References
Run it
ngbastion explain NGB086