logo

Database

Typescript Noble Predictable Kdf Salt

Description

This detector identifies the use of predictable or hardcoded salt values when using Noble cryptographic library's Key Derivation Functions (KDFs) in TypeScript code. Using a predictable or static salt in KDF operations (such as HKDF, PBKDF2, or similar) weakens the cryptographic strength of the derived keys, making them vulnerable to precomputation attacks and reducing the security guarantees of the key derivation process.

Weakness:

338 - Insecure service configuration - Salt

Category: Functionality Abuse

Detection Strategy

    • Scans TypeScript source files for invocations of Noble cryptography library KDF functions (e.g., hkdf, pbkdf2) where the salt parameter is supplied as a hardcoded, static, or otherwise predictable value (such as a string literal, empty value, or constant byte array) rather than a cryptographically random value generated at runtime.

    • A vulnerability is reported when a Noble KDF function call is detected and the argument corresponding to the salt position can be statically resolved to a fixed or empty value, indicating that the salt does not provide the necessary randomness for secure key derivation.

    • No vulnerability is reported when the salt argument is derived from a secure random source (e.g., crypto.getRandomValues, randomBytes) or is otherwise not resolvable to a static/predictable value at analysis time.

Vulnerable code example

import { argon2d, argon2i, argon2id } from "@noble/hashes/argon2.js";
import { pbkdf2, pbkdf2Async } from "@noble/hashes/pbkdf2.js";
import * as kdf from "@noble/hashes/scrypt";
import { scrypt } from "@noble/hashes/scrypt.js";
import { sha256 } from "@noble/hashes/sha2.js";
import { bytesToHex } from "@noble/hashes/utils.js";

const PASSWORD_SALT: string = "app-static-salt-2024";...

✅ Secure code example

import { argon2d, argon2i, argon2id } from "@noble/hashes/argon2.js";
import { pbkdf2, pbkdf2Async } from "@noble/hashes/pbkdf2.js";
import * as kdf from "@noble/hashes/scrypt";
import { scrypt } from "@noble/hashes/scrypt.js";
import { sha256 } from "@noble/hashes/sha2.js";
import { bytesToHex, randomBytes } from "@noble/hashes/utils.js"; // import randomBytes for CSPRNG salts

export function hashPassword(password: string): string {...