Security rules
NGB068: Hardcoded cryptographic key
Reports compile-time constant keys passed to cryptographic APIs: Web Crypto importKey('raw' | 'jwk', …), Node createCipheriv() / createDecipheriv() / createHmac() / createSecretKey(), CryptoJS ciphers and HMACs (CryptoJS.AES.encrypt(data, 'passphrase'), CryptoJS.HmacSHA256(msg, 'key')) and jsonwebtoken sign() / verify() secrets.
| Property | Value |
|---|---|
| Rule ID | NGB068 |
| Category | Cryptography |
| Default severity | high |
| CWE | CWE-321, CWE-798 |
| 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 compile-time constant keys passed to cryptographic APIs: Web Crypto importKey('raw' | 'jwk', …), Node createCipheriv() / createDecipheriv() / createHmac() / createSecretKey(), CryptoJS ciphers and HMACs (CryptoJS.AES.encrypt(data, 'passphrase'), CryptoJS.HmacSHA256(msg, 'key')) and jsonwebtoken sign() / verify() secrets. A key is constant when it is a string literal / const, TextEncoder().encode('…'), Buffer.from('…'), CryptoJS.enc.Utf8.parse('…'), a literal byte array, or a JWK literal with a constant k / d member.
Why it matters
A key in the source is a key everybody has: it ships in the JavaScript bundle (for browser code), lives forever in Git history, is shared by every environment, and cannot be rotated without a release. Anything "encrypted" with it — tokens in storage, signed JWTs, HMAC-protected webhooks — can be decrypted or forged by anyone who reads the bundle or the repository. NGB018 finds secret-looking literals by name and format; this rule finds keys by where they are used, regardless of their name.
Detection behavior and supported patterns
crypto.subtle.importKey('raw', new TextEncoder().encode('k'), …),importKey('jwk', { kty: 'oct', k: '…' }, …)createCipheriv(alg, 'key…', iv),createDecipheriv(…),createHmac('sha256', 'secret'),createSecretKey(Buffer.from('…'))fromcrypto/node:cryptoCryptoJS.AES.encrypt(data, 'passphrase')/decrypt(),CryptoJS.HmacSHA256(msg, 'key')and otherHmac*functionsjwt.sign(payload, 'secret')/jwt.verify(token, 'secret')(jsonwebtoken)
Example
const key = await crypto.subtle.importKey('raw', new TextEncoder().encode('0123456789abcdef'), 'AES-GCM', false, ['encrypt']);Safer alternative
const key = await crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);Remediation
Do not embed keys. In browser code, assume anything in the bundle is public: let the server hold keys and perform the operation, or derive per-user keys from the user's password with PBKDF2 / Argon2. In server code, load keys from a secret manager or environment variable at runtime and rotate them.
False-positive considerations
- Test vectors and demo code are reported unless excluded (
**/*.spec.tsis excluded by the presets). - Public keys imported as constants (
spki) are not reported; private keys and symmetric keys are.
Known limitations
- Keys loaded from bundled JSON / environment files at build time (
environment.ts) are not followed; NGB018 reports secret-looking values there. - Constant-ness is followed through
constvariables only, not through class fields or function returns.
References
Run it
ngbastion explain NGB068