ESLint InterlaceESLint Interlace
Plugin: node-securityRules

no-static-iv

ESLint rule documentation for no-static-iv

📡 Live from GitHub — This documentation is fetched directly from no-static-iv.md and cached for 6 hours.

Keywords: IV, initialization vector, hardcoded, static, nonce, CWE-329, security, ESLint rule, LLM-optimized CWE: CWE-329
OWASP: A02:2021-Cryptographic Failures

Disallow static or hardcoded initialization vectors (IVs)

Detects usage of hardcoded or static initialization vectors (IVs) in encryption operations. This rule is part of eslint-plugin-node-security and provides LLM-optimized error messages.

🚨 Security rule | ⚠️ Set to error in recommended

Quick Summary

AspectDetails
CWE ReferenceCWE-329 (Static IV)
SeverityHigh (security vulnerability)
Auto-Fix❌ No auto-fix (requires randomBytes)
CategorySecurity
ESLint MCP✅ Optimized for ESLint MCP integration
Best ForNode.js applications using symmetric encryption

Vulnerability and Risk

Vulnerability: Using a static or hardcoded IV makes encryption deterministic. When the same key and IV are used, encrypting the same plaintext produces the same ciphertext every time, allowing attackers to detect repeated messages.

Risk: Static IVs enable pattern analysis attacks. In CBC mode, identical first blocks produce identical first ciphertext blocks. In GCM mode, reusing a nonce with the same key completely breaks the authentication guarantees.

Rule Details

This rule detects hardcoded IVs passed to crypto.createCipheriv() including:

  • String literal IVs
  • Buffer.from('static-string')
  • Buffer.alloc() with literal values
  • Hardcoded hex or base64 strings

Why This Matters

RiskImpactSolution
🔄 DeterministicSame plaintext = same ciphertextUse crypto.randomBytes(16)
🔓 GCM Nonce ReuseComplete authentication bypassGenerate unique IV per encryption
🔒 Pattern DetectionAttackers detect repeated messagesNever hardcode IVs

Configuration

OptionTypeDefaultDescription
allowInTestsbooleanfalseAllow static IVs in test files
{
  rules: {
    'node-security/no-static-iv': ['error', {
      allowInTests: true // Only for unit tests
    }]
  }
}

Examples

❌ Incorrect

import crypto from 'crypto';

// Hardcoded string IV - HIGH risk
const cipher = crypto.createCipheriv('aes-256-gcm', key, '1234567890123456');

// Hardcoded hex IV - HIGH risk
const iv = Buffer.from('00112233445566778899aabbccddeeff', 'hex');
const cipher2 = crypto.createCipheriv('aes-256-cbc', key, iv);

// Static buffer - HIGH risk
const staticIv = Buffer.alloc(16, 0);
const cipher3 = crypto.createCipheriv('aes-256-gcm', key, staticIv);

✅ Correct

import crypto from 'crypto';

// Generate random IV for each encryption
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);

// Store IV with ciphertext (it's not secret)
const encrypted = Buffer.concat([iv, cipher.update(data), cipher.final()]);

// For GCM, use 12-byte IV (nonce)
const gcmNonce = crypto.randomBytes(12);
const gcmCipher = crypto.createCipheriv('aes-256-gcm', key, gcmNonce);

Security Impact

VulnerabilityCWEOWASPCVSSImpact
Static IV329A02:20215.9 MediumDeterministic encryption
Nonce Reuse323A02:20217.5 HighAuthentication bypass in GCM

Migration Guide

Phase 1: Discovery

{
  rules: {
    'node-security/no-static-iv': 'warn'
  }
}

Phase 2: Replacement

// Replace static IV with randomBytes
const iv = '0000000000000000'; // ❌ Before
const iv = crypto.randomBytes(16); // ✅ After

// Prepend IV to ciphertext for decryption
const encrypted = Buffer.concat([iv, encryptedData]);

Phase 3: Enforcement

{
  rules: {
    'node-security/no-static-iv': 'error'
  }
}

Known False Negatives

Variable IVs

Why: Variables are not tracked across scopes.

// ❌ NOT DETECTED - Variable IV
const config = { iv: '1234567890123456' };
crypto.createCipheriv('aes-256-gcm', key, config.iv);

Mitigation: Ensure IV generation uses randomBytes at generation site.

Dynamic Buffer Creation

Why: Complex expressions not analyzed.

// ❌ NOT DETECTED
const iv = getStaticIv();
crypto.createCipheriv('aes-256-gcm', key, iv);

Mitigation: Code review for IV generation patterns.

Further Reading

On this page

No Headings