Static analysis (SAST) check · JavaScript

Weak cipher (JavaScript)

This cipher is no longer safe: DES, Triple DES and RC4 are broken, ECB mode reveals repeated blocks, and createCipher derives its key with one MD5 pass and no salt.

Why it matters

Data encrypted this way can be read or patterned by an attacker who captures enough of it, so the encryption no longer keeps it confidential.

How to fix it

Use crypto.createCipheriv("aes-256-gcm", key, iv) with a random 12-byte IV per message.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (JavaScript)
const crypto = require("crypto");

function seal(key, text) {
  const cipher = crypto.createCipheriv("aes-128-ecb", key, null);
  return Buffer.concat([cipher.update(text), cipher.final()]);
}
Fixed: not reported (JavaScript)
const crypto = require("crypto");

function seal(key, text) {
  const iv = crypto.randomBytes(12);
  const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
  return Buffer.concat([iv, cipher.update(text), cipher.final(), cipher.getAuthTag()]);
}

What TrustIvan reads

Runs on repository scans of JavaScript source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.