Cryptographic Failures
Cryptographic Failures vulnerabilities at a glance
Overview
Cryptographic failures are mistakes in selecting, configuring, or applying cryptography. They often come from speed, legacy code, or copy paste from examples. Common patterns are fast password hashing, static or hardcoded keys, ECB mode, fixed IVs, accepting JWT alg=none, and turning off TLS validation.
The right fixes are simple but strict, for example using high level, battle tested libraries with safe defaults, pinning algorithms, and generating randomness from the OS CSPRNG.
Where it occurs
These issues often show up in home grown auth, token generators, encryption helpers, and HTTP client configuration.
Impact
Attackers can steal credentials, read or manipulate sensitive data, impersonate services, or escalate to full account or environment compromise. Because traffic and data appear valid, detection is difficult.
Prevention
Use secure random generators for secrets, store and rotate keys safely, apply strong salted password hashing, and enforce strict TLS certificate and hostname verification.
Specific Vulnerabilities
Explore specific vulnerability types within this category:
Examples
Switch tabs to view language/framework variants.
Express, JWT verification accepts alg=none
The server decodes a JWT but does not enforce a signature algorithm, so unsigned tokens are treated as valid.
const jwt = require('jsonwebtoken');
function auth(req, res, next){
const t = (req.headers.authorization||'').replace('Bearer ', '');
try {
// BUG: decode or verify without forcing algorithm and key
const payload = jwt.decode(t) || jwt.verify(t, 'secret');
req.user = payload;
next();
} catch { res.status(401).end(); }
}- Line 5: jwt.decode does not verify a signature
- Line 5: jwt.verify without restricting algorithms may misverify if used incorrectly
Decoding or loosely verifying JWTs accepts tokens that are not signed or signed with the wrong algorithm. Attackers forge admin claims and bypass auth.
const jwt = require('jsonwebtoken');
const PUB = Buffer.from(process.env.JWT_PUBLIC_KEY_PEM, 'utf8');
const ALGS = ['RS256'];
function auth(req, res, next){
const t = (req.headers.authorization||'').replace('Bearer ', '');
try {
const payload = jwt.verify(t, PUB, { algorithms: ALGS });
req.user = payload; next();
} catch { return res.status(401).end(); }
}- Line 3: Pin acceptable algorithms
- Line 7: Verify with the correct public key and algorithm
Always verify with an expected algorithm list and the correct key material. Reject alg=none.
Detect These Vulnerabilities in Your Code
Sourcery automatically identifies cryptographic failures and related vulnerabilities in your codebase.
Scan Your Code for Free