Fix broken cryptographic primitive protecting a secret in NestJS

A hash, cipher, or random source that cannot carry the weight it has been given: MD5 or SHA-1 over a password, a DES or ECB cipher, or Math.random() producing a token. Each has a drop-in replacement in the standard library, so the fix is small - the cost of not making it is that the protection is decorative.

high likely NestJS CWE-327 / OWASP A02:2021

The vulnerable pattern in NestJS

HIGH likely Broken cryptographic primitive protecting a secret A02:2021 src/crypto.ts:5:35 3 │ 4 │ export function hashPassword(password: string): string { 5 │ const passwordHash = createHash('md5').update(password).digest('hex') │ ~~~~~ fast hash protecting a credential 6 │ return passwordHash 7 │ }

This finding comes from the NestJS fixture in the owlwarden test suite. This hash is fast, and speed is the attacker's advantage: a commodity GPU tries billions of candidates a second, so a leaked table of these hashes is a leaked table of the values behind them. Password hashing needs a deliberately slow algorithm with a per-value salt.

The corrected handler

Put the hashing behind a provider so every caller gets the same algorithm, rather than each service choosing one.

@Injectable()
export class PasswordService {
  async hash(plain: string) {
    return await argon2.hash(plain)
  }
  async verify(hash: string, plain: string) {
    return await argon2.verify(hash, plain)
  }
}

If you are not using NestJS

Use a slow, salted hash for passwords and a cryptographic random source for tokens. Both are in the Node standard library; neither needs a dependency.

Check your own repository

npx owlwarden scan
npx owlwarden explain weak-crypto

Runs on your machine. No account, no telemetry, no network unless you ask. In CI, SARIF uploads to code scanning and the exit code is the gate.

Other NestJS checks

Rules with a tested NestJS example.

weak-crypto for every framework / All rules / owlwarden