JWT Vulnerabilities: alg=none & Key Confusion
Exploit and fix the most common JWT security mistakes.
JWT Vulnerabilities: alg=none & Key Confusion is a free Cryptology Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
JWT Attack Surface
JWTs are cryptographically signed but many libraries had severe vulnerabilities. The most exploited: the alg=none attack, the RS256→HS256 confusion attack, and weak secret brute-forcing. All stem from trusting attacker-controlled header fields.
alg=none Attack
The JWT spec allows alg:"none" for unsecured tokens. Buggy libraries accepted this and skipped signature verification entirely. An attacker modifies the payload (e.g., "role":"admin"), sets alg:"none", strips the signature, and the server accepts it.
alg=none Exploit Example
Original: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIn0.SIG Attack: eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiJ9. (empty signature, trailing dot). A vulnerable server verifies successfully and grants admin access.
alg=none Fix
Never accept alg:"none" in production. Explicitly whitelist allowed algorithms: jwt.decode(token, key, algorithms=["HS256"]). Most modern libraries (PyJWT 2+, jsonwebtoken 9+) reject alg=none by default.
RS256 → HS256 Key Confusion
If a server uses RS256 and a client changes alg to HS256, the server might verify using its RSA public key as the HMAC secret. Since the public key is... public, the attacker signs arbitrary payloads with it and the server verifies them.
Key Confusion Exploit
Steps: 1) Fetch public key from JWKS endpoint. 2) Build malicious payload. 3) Sign with HMAC-SHA256 using the raw public key PEM bytes as the HMAC secret. 4) Set alg:"HS256" in header. 5) Server's HS256 path verifies successfully.
Key Confusion Fix
Always specify the expected algorithm explicitly in the verify call. Never derive the algorithm from the token header. Use separate code paths for RS256 and HS256 that cannot be switched by the caller.
Weak Secret Brute Force
HS256 with weak secrets (less than 256 bits of entropy) can be brute-forced offline. Tools like hashcat support JWT cracking: hashcat -a 0 -m 16500 token.txt wordlist.txt. Use cryptographically random secrets of at least 32 bytes.
Header Injection via kid
The kid (key ID) header selects which key to use. If the server uses kid as a SQL query parameter or filesystem path without sanitization: kid="../../dev/null" → HMAC with empty key → forged token accepted. Always sanitize kid.
jwk Header Injection
The jwk header embeds a public key for verification. A vulnerable server might use the embedded key directly, letting the attacker supply their own key pair, sign the token with the private key, and embed the public key. Verify only against registered keys.
Expired Token Bypass
Some implementations forgot to check the exp claim, or used server time inconsistently. Always explicitly validate exp, nbf, and iat. Use a library that checks these by default and never disable claim validation in production.
Quick Check
In the RS256→HS256 key confusion attack, what does the attacker use as the HMAC secret?
Recap
JWT vulnerabilities arise from trusting the alg header, accepting alg=none, and weak secrets. Fix: pin algorithms at the verifier, use strong secrets, and sanitize kid. Next: secure JWT implementation practices.
Frequently asked questions
Is the “JWT Vulnerabilities: alg=none & Key Confusion” lesson free?
Yes — the full text of “JWT Vulnerabilities: alg=none & Key Confusion” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.
What will I learn in “JWT Vulnerabilities: alg=none & Key Confusion”?
Exploit and fix the most common JWT security mistakes. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cryptology Academy?
No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “JWT Vulnerabilities: alg=none & Key Confusion” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cryptology Academy lesson?
Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- JWT Anatomy: Header, Payload, Signature
- HS256 vs RS256: Symmetric vs Asymmetric JWTs
- JWT Vulnerabilities: alg=none & Key Confusion
- Secure JWT Implementation Best Practices