createCipher derives key and IV from a password, without salt

tx0/ts:cipher-without-ivrecommendedwarningsecurity, cwe-329

What it finds

crypto.createCipher(algorithm, password) derives both the key and the initialization vector from the password with one round of MD5 and no salt. The same password always gives the same key stream, and the key is cheap to guess. Node deprecated the function for this reason and later removed it; createCipheriv with a random IV replaces it.

Reported are calls of createCipher and createDecipher on a receiver named crypto.

From the rule's source, cipher_without_iv.rs.

Example from the rule's tests
import * as crypto from "crypto";export function encrypt(password: string) {  const a = crypto.createCipher("aes-256-cbc", password);tx0 reports: derived from the password without salt  const b = other.createCipher("x", password);  return [a, b];}declare const other: { createCipher(a: string, b: string): unknown };
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/ts:cipher-without-iv" = "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/ts:cipher-without-iv] reason="..."

tx0 explain tx0/ts:cipher-without-iv shows the same in the terminal. Found a false positive? Report it.