The Attack

Server expects RS256 and uses public key for RSA verify. Attacker sets alg=HS256 and signs with public key as HMAC secret. If server switches verify mode based on header, attack succeeds.

Real-World Impact

CVE-class vulnerability in multiple libraries over the years. Always pinned algorithm lists.

Prevention

jwt.verify(token, getKey, { algorithms: ['RS256'] }); // never ['RS256','HS256'] from header

Related

Invalid Signature, Security Hub.

Understanding JWT Algorithm Confusion Attack Explained in Production

Developers search for JWT Algorithm Confusion Attack Explained when building API authentication with JSON Web Tokens. JWTs are used by OAuth 2.0, OpenID Connect, Auth0, Firebase, AWS Cognito, and Keycloak. Always validate exp, iss, and aud server-side — decoding alone proves nothing about authenticity.

JWT Structure Recap

Every JWT has three dot-separated segments: header (algorithm), payload (claims), signature (proof). Use JWT Decoder to inspect and JWT Validator to verify before trusting any claim value in production code.

Common Pitfalls

  • Algorithm confusion (none attack) — whitelist allowed algorithms
  • Secrets in the payload — payload is only Base64-encoded, not encrypted
  • Ignoring clock skew on exp and nbf
  • Weak HMAC secrets — use 256-bit random keys
  • Skipping signature verification — always call verify(), not decode()
  • Storing tokens in localStorage — XSS can steal them

Further Reading

Browse related resources: JWT Decoder, JWT Validator, JWT Basics, JWT Authentication, JWT Errors, Algorithms, Glossary, and Learning Path.

Try It Now

FAQ

What is algorithm confusion?

Attacker changes alg to HS256 and signs with the RSA public key as HMAC secret. Server verifies with same public key string as secret.

How do I prevent it?

Whitelist allowed algorithms explicitly. Never derive verification algorithm from token header alone.