Security rules
NGB047: Static IV in Web Crypto encryption
Reports crypto.subtle.encrypt() with AES-GCM, AES-CTR or AES-CBC whose iv / counter is a constant (string, TextEncoder().encode('…'), literal byte array, zero-filled new Uint8Array(12)) or a random value created once at module / class level and reused for every call..
| Property | Value |
|---|---|
| Rule ID | NGB047 |
| Category | Cryptography |
| Default severity | high |
| CWE | CWE-329, CWE-323 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports crypto.subtle.encrypt() with AES-GCM, AES-CTR or AES-CBC whose iv / counter is a constant (string, TextEncoder().encode('…'), literal byte array, zero-filled new Uint8Array(12)) or a random value created once at module / class level and reused for every call.
Why it matters
AES-GCM and AES-CTR are only secure when every encryption under a key uses a fresh IV/counter: one repetition reveals the XOR of two plaintexts and, for GCM, lets an attacker forge authenticated messages. AES-CBC with a fixed IV leaks which messages share a prefix. Static IVs are a common copy-paste mistake in client-side "encrypted storage" helpers.
Detection behavior and supported patterns
crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, data)on the DOMSubtleCrypto(algorithm object inline or in aconst)- Constant IVs: string / template constants,
new TextEncoder().encode('…'),Uint8Array.from([...]),new Uint8Array([1, 2, …]),new Uint8Array(12)(all zeros) - Reused IVs:
const IV = crypto.getRandomValues(new Uint8Array(12))at module level or as a class field, used inside the encrypting function - AES-GCM / AES-CTR: high severity; AES-CBC: medium
Example
const IV = new TextEncoder().encode('0123456789ab');await crypto.subtle.encrypt({ name: 'AES-GCM', iv: IV }, key, data);Safer alternative
const iv = crypto.getRandomValues(new Uint8Array(12));const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, data);return { iv, ciphertext };Remediation
Generate a fresh IV for every encryption (crypto.getRandomValues(new Uint8Array(12)) for AES-GCM), and store or send it next to the ciphertext; it does not need to be secret.
False-positive considerations
- A key that is itself used only once (fresh random key per message) makes a fixed IV harmless; such designs are rare and are still reported.
Known limitations
- IVs derived deterministically from data (counters, hashes of the plaintext) are not recognized as reused.
- Encryption through libraries (crypto-js, node-forge, libsodium wrappers) is not analyzed.
References
Run it
ngbastion explain NGB047