What is a JSON Web Token (JWT)?

A JSON Web Token (JWT) is a compact, signed credential: three base64url parts, a header, a set of claims such as the user ID and expiry, and a signature over both. The server issues it at login and trusts its claims on every later request, but only if it verifies that signature.

Part of the roadmap: Stage 5, Deeper web and APIs

Why it matters

The format is defined in RFC 7519. The header and claims are encoded, not encrypted, so anyone holding a token can read them, and the only thing stopping a user from editing "role":"user" to "role":"admin" is the signature check. Every classic JWT vulnerability is a way to make a tampered or forged token pass that check, which MITRE files as CWE-347, improper verification of a cryptographic signature.

The bugs to test for are few and well known, and RFC 8725 lists the defences: a verifier that accepts alg: none, an HMAC secret that is weak or guessable, a verifier that lets the token choose its own algorithm (key confusion), and a server that never checks exp, so a stolen token lives forever.

Example: what a server must check on every token

CyberGym, the legal practice target (OWASP Juice Shop), has two challenges built on exactly these bugs, Unsigned JWT and Forged Signed JWT, so you can see what a forged token has to achieve on a site that permits it.

On your own app (https://your-app.example), the question is what your verifier accepts. With the Node jsonwebtoken library, the difference is one option:

// Risky: no algorithm pinned, so library defaults decide what passes
const claims = jwt.verify(token, key);

// Safe: one expected algorithm, a strong key, expiry enforced
const claims = jwt.verify(token, key, { algorithms: ["RS256"] });

Then test a build you run locally. Decode a token you were issued and change one claim without re-signing it, send a token whose exp has passed, and send one whose header says none. Each should be rejected with 401. If any is accepted, the server is trusting a claim it never verified.

Common mistakes

  • Decoding the token instead of verifying it. A decode function reads claims without checking the signature. Use the verify call, every time.
  • Putting secrets in the claims. Anyone with the token can read them. Keep only what the server needs to identify the user.
  • Signing with a short or default HMAC secret. It can be guessed offline from any token. Use a long random key, or an asymmetric key pair.
  • Issuing tokens that never expire. Set a short exp, check it, and have a way to revoke tokens when a user logs out or is removed.

Related terms

Go deeper

Sources

Test only sites you own or have written permission to test. To practise without one, use CyberGym, a legal, deliberately vulnerable target. Practise on CyberGym.