guides · 2026-11-14
JWT alg=none and Key Confusion: How Attackers Forge Any User's Token
Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team
The short answer
A JWT is only as strong as its verification. If your verifier accepts alg=none, lets the token choose the algorithm, treats an RSA public key as an HMAC secret, or fetches keys from token-supplied jku/kid values, anyone can mint a token that says they are your admin. Pin the algorithm and key source in server configuration — never in the token — and audit which of the four confusions your library defaults allow.
Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.
Attack one and two: none, and the RS/HS switch
The none downgrade is the oldest: the token declares "alg":"none" and carries no signature; a verifier that honours the token's own algorithm field accepts it. Libraries have patched this for years, but AI-generated auth code still ships verify options like algorithms=['none', 'HS256'] copied from a debugging session. Vibe-Code Audit rates JWT-accepts-none as CRITICAL — the only core-adjacent pattern with that weight — because it is unauthenticated login, full stop.
The subtler switch: your service signs with RS256 (private key signs, public key verifies). If the verifier allows HS256 and is handed the public key as its secret, an attacker HMAC-signs a forged token with that public key — which is public. The defence is identical in both cases: the server pins one algorithm; the token's preference is ignored.
Attack three and four: kid and jku steer the key
The kid header is meant to select among your keys. Implementations that use kid as a file path let ../../dev/null or a database value choose the key material; implementations that query WHERE kid = '
Legitimate key rotation does not need token-supplied URLs. Your verifier should resolve kid against a fixed, server-configured key set (a JWKS endpoint you chose, cached), and reject tokens pointing anywhere else.
The weak-secret sibling
HS256 with a human-chosen secret ('secret', 'changeme', your app name) is the fifth column: attackers crack it offline at billions of guesses per second against any captured token, then sign at leisure. The scanner's weak-JWT-secret and JWT-secret-assignment patterns exist because generated code loves jwt_secret = "secret". A real HMAC secret is 256 bits from a CSPRNG, stored in the environment — indistinguishable from random, because it is.
Also disable, explicitly, any 'verify_signature: False' debug option — the scanner flags that literal — and make sure staging's relaxed settings cannot be copied to production by an environment-variable default.
Verification checklist for your own auth
Forge tests against staging: (1) a none token with an admin payload; (2) an HS256 token HMAC-signed with your public key; (3) a token with jku pointing at a key you control on your own server; (4) a token with a path-traversal kid. All four must be rejected with the same boring 401. If any succeeds, the fix is configuration, not code volume: pin algorithms=['RS256'] (or your one choice), pin the key source, validate iss and aud while you are there, and rotate secrets if a weak one was ever live.
Worked example: CoachLoop, a fitness-coaching platform
CoachLoop (fictional), with 5,800 users, generated its auth with an AI assistant and 'temporarily' accepted both RS256 and HS256 during a migration that never ended. A scan's advanced JWT checks reported algorithm confusion: HS256 accepted with the RSA public key. The CTO reproduced it in staging — a self-signed HMAC token using the site's own public JWKS key logged in as user ID 1. The fix pinned RS256, removed the token-supplied key lookup (kid now resolves only against the cached JWKS), and added the four forge tests to CI. The one production token found in logs with an HS256 header predated the fix window and forced a precautionary signing-key rotation.
Five things to do before you ship
- List which algorithms your verifier accepts; if the answer is more than one, or includes none, treat it as a finding today.
- Pin the algorithm and the key source in server configuration — the token never chooses either.
- Test four forgeries against staging: none-alg, HS256-with-public-key, foreign jku, traversal kid. All must return 401.
- Replace any human-chosen JWT secret with 256 bits of CSPRNG output stored in the environment.
- Confirm no verify_signature=False (or equivalent debug bypass) survives outside local development, and re-scan the auth code.
What is JWT algorithm confusion?
a family of verification flaws where the attacker influences how a JSON Web Token's signature is checked: downgrading to 'none', switching RS256 to HS256 so the public key becomes an HMAC secret, or steering key lookup via kid/jku/x5u headers to attacker-controlled keys or files.
Sources
- OWASP Top 10, the OWASP Foundation's standard awareness list of the most critical web application security risks: owasp.org, 2021
- CWE-89: Improper Neutralization of Special Elements used in an SQL Command, MITRE's weakness entry for SQL injection and its mitigations: cwe.mitre.org, 2026
- OWASP Cheat Sheet Series — Secrets Management, OWASP's best-practice guide to storing, rotating and auditing secrets: cheatsheetseries.owasp.org, 2026
Frequently asked questions
My library is popular and current. Am I automatically safe?
Libraries ship safe defaults but expose dangerous options, and generated code sets them: algorithms lists copied from tutorials, verify disabled while debugging. The forge tests, not the library's reputation, are the evidence.
Why is the public key dangerous as an HMAC secret?
Because it is public. If the verifier can be talked into HS256, the 'secret' it checks against is a key anyone can download from your JWKS endpoint — signing a forgery becomes trivial.
Should I reject tokens with jku/x5u headers outright?
Unless you run a genuinely federated system with an allow-list of identity providers, yes. Self-hosted apps should resolve keys only from their own configured set.
What about token expiry and audience — do they matter here?
They matter for other attacks (replay, cross-service confusion). Set and validate exp, iss and aud — but note none of them compensate for a broken signature check.
Related guides
- How to Fix CORS Errors Without Opening Your API to Every Website
- Debug Mode in Production: What One Stack Trace Hands an Attacker
- Why AI-Generated Login and API Endpoints Ship Without Rate Limits
- GraphQL Introspection and Batching: The Two Settings AI Starters Leave On
- Prototype Pollution in JavaScript: How One JSON Body Poisons Every Object
- Path Traversal: How a Download Endpoint Serves Files It Never Meant to Share
- 7 Security Mistakes AI Coders Keep Making
- Supply-Chain Attacks in the AI-Coding Era
- Secret Hygiene: Stop API Key Leaks
- What Code Scanners Can't Find
- Scan Your Lovable Code Before Deploying
- LLM Threats in AI-Generated Apps
Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.
Informational explainer, not a security guarantee. Scan your code free before every deploy.