Skip to content

Cryptography rules

Finds crypto.createCipheriv calls whose algorithm uses the CBC or ECB mode, or names a block cipher without a mode, also when a constant holds the algorithm.

On in the recommended and all configs.

ECB shows the patterns of the data. CBC without a message check lets attackers change or decrypt the data through padding errors.

Use a mode that authenticates the data, such as GCM: aes-256-gcm.

The call crypto.createCipheriv("aes-128-cbc", key, iv) reports insecure. Use crypto.createCipheriv("aes-128-gcm", key, iv).

Finds a string written in the code that a call passes as a secret, such as the key of crypto.createHmac or jsonwebtoken.sign, the secret of express-session, or a database password, except short values and placeholders.

On in the recommended and all configs.

A secret in the code reaches everyone who can read the repository or the built files, so it protects nothing.

Revoke the secret, and read its replacement from the environment or a secret store at run time.

The call jwt.sign(claims, "s3cr3t") reports hardcoded. Use jwt.sign(claims, process.env.JWT_SECRET).

Finds jsonwebtoken calls that sign a token with the none algorithm, verify a token with none among the algorithms, or verify a token with a null key and no algorithms option.

On in the recommended and all configs.

A token without a signature proves nothing, so anyone can forge it.

Sign with a strong algorithm such as RS256 or HS256, and list only strong algorithms in the algorithms option of verify.

The call jwt.sign(claims, key, { algorithm: "none" }) reports unsignedSign. Pass { algorithm: "RS256" }.

Reports Math.random calls, including aliases and destructured references.

On in the recommended and all configs.

Predictable random values cannot protect tokens, identifiers, or other security decisions.

Use crypto.getRandomValues, crypto.randomUUID, or the cryptographic random API of the runtime.

const token = crypto.randomUUID(); uses cryptographic randomness for an identifier.

Finds crypto.createCipheriv calls whose algorithm is Blowfish, DES, RC2, or RC4, also when a constant holds the algorithm.

On in the recommended and all configs.

Attackers can break these ciphers, so the data that they encrypt is not protected.

Use AES with a mode that authenticates the data, such as aes-256-gcm.

The call crypto.createCipheriv("des-ede3-cbc", key, iv) reports weak. Use crypto.createCipheriv("aes-256-gcm", key, iv).

Finds crypto.createHash calls with MD5, SHA-1, RIPEMD, or another weak algorithm, and crypto.subtle.digest calls with SHA-1. A digest that the code only shortens with slice or substring passes, because a short digest serves as an identifier, not as a protection.

On in the recommended and all configs.

Attackers can find two inputs with the same weak hash, so the hash does not protect passwords, signatures, or file integrity.

Use SHA-256 or a stronger hash. Store passwords with a password hash such as scrypt or Argon2.

The call crypto.createHash("md5").update(password).digest("hex") reports weak. Use crypto.createHash("sha256").

Finds generateKeyPair and generateKeyPairSync calls that make an RSA or DSA key with a modulus shorter than 2048 bits or a divisor shorter than 224 bits, or an elliptic curve key on a weak curve.

On in the recommended and all configs.

Attackers can break short keys and weak curves, so the signatures and the encryption that use them protect nothing.

Use a modulus of at least 2048 bits, a divisor of at least 224 bits, and a curve such as prime256v1 or secp384r1.

The call crypto.generateKeyPairSync("rsa", { modulusLength: 1024 }) reports shortKey. Pass { modulusLength: 2048 }.

In a file that imports ethers, viem/accounts, or tronweb, finds fromPhrase, mnemonicToAccount, and fromMnemonic calls whose seed phrase is a string written in the code.

On in the recommended and all configs.

A seed phrase gives full control of a wallet, so everyone who reads the code can take its funds.

Move the funds to a new wallet, and read the new seed phrase from a secret store at run time.

The call Wallet.fromPhrase("test test test ...") reports mnemonic. Use Wallet.fromPhrase(process.env.WALLET_PHRASE).