CIDADAXSEC
← All topics
Expert

JWT

JWT attacks exploit weak signature verification, algorithm confusion, and header injection to forge tokens.

Material

A JWT is a base64url token holding a header, a payload, and a signature. The server verifies the signature and then trusts the claims in the payload. Because the format is uniform and not encrypted, anyone can read its contents. Attacks on JWT almost always target the trust boundary: does the server really check the signature, and does it trust a header supplied by the client?

Token structure and verification

Three dot-separated parts: the header carries the algorithm and a referenced key, the payload holds the claims, and the signature provides integrity. Ideal verification reads the algorithm from a fixed allowlist, fetches a trusted key, recomputes the signature, and compares it in constant time. Skip one step and the token can be crafted without the key.

http
GET /admin HTTP/1.1
Host: example.com
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbiIsInJvbGUiOiJhZG1pbiJ9.ZmFrZS1zaWduYXR1cmUtaGVyZQ

javascript
// Verifikasi JWT yang aman: algoritma dikunci ke allowlist
const jwt = require('jsonwebtoken');

function verifyToken(token) {
  const decoded = jwt.decode(token, { complete: true });
  if (!decoded) throw new Error('malformed token');
  if (decoded.header.alg !== 'RS256') throw new Error('bad algorithm');
  return jwt.verify(token, publicKey, { algorithms: ['RS256'] });
}

The simplest attack replaces the signature with an empty string and edits the payload. Some servers only check that the token has three parts and a non-empty signature, so an unsigned token still passes. Other variants ignore the signature entirely, and the most common one is algorithm confusion.

Attack forms

  • Unverified signature: the signature is dropped or replaced with an empty string and the payload is edited.
  • Weak signing key: the token is signed with a short or guessable secret, so it can be re-signed.
  • Algorithm none: the header is changed to alg=none and the server accepts the unsigned token.
  • Algorithm confusion: the header becomes HS256 while the server is asked to sign with an RSA public key.
  • jwk header injection: the attacker embeds their own public key in the header and the server verifies against it.
  • jku header injection: the server fetches the key from an attacker-controlled URL and verifies with it.
  • kid path traversal: the kid value is used as a filename, so path traversal points at an attacker-controlled key file.

Prevention

  • Always verify the signature; never accept a token without one.
  • Pin the algorithm to an allowlist during verification instead of following the token header.
  • Never reuse one key for two algorithms or two purposes, such as RSA and HMAC.
  • Use long random signing secrets, or asymmetric algorithms with key rotation.
  • Ignore jwk and jku headers from the token unless there is an explicit key id allowlist.
  • If kid is used, sanitise it so it cannot escape the key directory.
bash
# Uji cepat tanda tangan kosong / alg none
TOKEN=$(printf '{"alg":"none","typ":"JWT"}' | base64 -w0 | tr '+-' '-_' | tr -d '=').$(printf '{"sub":"admin","role":"admin"}' | base64 -w0 | tr '+-' '-_' | tr -d '=').
curl -s -H "Authorization: Bearer $TOKEN" https://example.com/admin
The algorithm lives inside the token, not in the server configuration. As long as the server reads it from the header to decide how to verify, the attacker controls the rules of the game.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.