The alg:none attack

JWT Authentication Course · lesson 4 of 13 · 4 min read

The classic JWT vulnerability, and why it is a library-configuration problem.

Open this lesson in the learning hub

Key points

  • The header declares the algorithm - and the header is supplied by whoever sends the token.
  • An attacker sets alg: "none", strips the signature, and edits the payload freely.
  • A naive library reads the header, sees "none", and reports the token as valid.
  • The fix: the verifier must decide the expected algorithm, never take it from the token.
  • A related attack swaps RS256 for HS256 and signs with the public key as the HMAC secret.

Example

// WRONG - trusts the algorithm the attacker chose
Jwts.parser().setSigningKey(key).parse(token);

// RIGHT - the verifier fixes the algorithm
Jwts.parser()
    .verifyWith(publicKey)
    .sig().add(Jwts.SIG.RS256).and()   // only RS256 accepted
    .build()
    .parseSignedClaims(token);

Never let the token tell you how to verify it - pin the algorithm on the server.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the JWT Authentication Course course, and every lesson in it is listed on the JWT Authentication Course contents page.