logo

Database

Javascript Hardcoded Aead Nonce

Description

This vulnerability detector identifies hardcoded AEAD (Authenticated Encryption with Associated Data) nonces in JavaScript code. Using hardcoded nonces in AEAD encryption schemes completely breaks the security model since nonces must be unique and unpredictable - reusing nonces allows attackers to recover encryption keys or plaintext data.

Weakness:

395 - Insecure generation of random numbers - Static IV

Category: Functionality Abuse

Detection Strategy

    • Scans JavaScript source code for AEAD encryption operations (such as AES-GCM, ChaCha20-Poly1305, or similar authenticated encryption modes)

    • Identifies when nonce/IV parameters are set to static, hardcoded values instead of being randomly generated

    • Reports violations when AEAD functions use literal strings, fixed byte arrays, or other constant values as nonces

    • Triggers alerts for cryptographic library calls where the nonce parameter contains predictable or reused values

Vulnerable code example

const crypto = require('crypto');

function encryptData(key, data) {
  // VULNERABLE: hardcoded nonce creates identical ciphertext for same plaintext
  const cipher = crypto.createCipheriv('aes-256-gcm', key, '000000000000');
  return Buffer.concat([cipher.update(data), cipher.final()]);
}

✅ Secure code example

const crypto = require('crypto');

function encryptData(key, data) {
  const nonce = crypto.randomBytes(12); // SAFE: random nonce prevents identical ciphertext
  const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce);
  return Buffer.concat([nonce, cipher.update(data), cipher.final(), cipher.getAuthTag()]);
}