Anatomy of a JSON Web Token
A JSON Web Token (JWT), defined in RFC 7519, is a compact way to carry claims between two parties. The common signed form uses JSON Web Signature (RFC 7515) and has three Base64URL parts joined by dots.
base64url(header) . base64url(payload) . base64url(signature) | Part | Contents | Trust it? |
|---|---|---|
| Header | Algorithm (alg), type and key hints such as kid | Never before verification, and treat key hints as hostile input |
| Payload | Claims such as sub, iss, aud, exp | Only after the signature and claims are checked |
| Signature | HMAC or digital signature over the first two parts as transmitted | It is the proof, if you verify it with the right key |
The signature covers the encoded text of the first two parts and not the decoded JSON. Changing one character of either part, even in a way that decodes to the same data, invalidates the signature.
Base64URL is encoding, not encryption
Base64URL turns bytes into characters that are safe in URLs and headers. It replaces + and / with
- and _, and JWTs omit the = padding. There is no key, and reversing it takes no
effort. Anything in a JWT payload can be read by the user who holds it, by every proxy that logs it, and by any script that
can reach it.
That makes claims such as password, api_key or ssn a data exposure even when the
signature is perfect. The auditor looks for names like these at every level of the payload. Keep secrets on the server and
put an opaque identifier in the token instead.
Common JWT attacks
Unsigned tokens with alg: none
The specification defines none for tokens protected by some other means. Early libraries accepted it by
default, so an attacker could change the header to none, edit any claim and delete the signature. Filters that
blocked only the lowercase word were bypassed with None or nOnE. The fix is to allow a fixed list
of algorithms instead of blocking bad ones.
RS256 to HS256 algorithm confusion
RS256 verifies with a public key, and HS256 verifies with a shared secret. If one verify function accepts either kind of key and takes the algorithm from the token, an attacker can sign with HS256 and use the public key file as the secret. During an authorized test you can check for this here: if an HS256 token verifies when you paste a PEM public key as the secret, the tool warns that it looks like an algorithm confusion attack.
Key ID (kid) injection
The kid header tells the server which key to use, and it arrives before anything has been verified. Where it
reaches a file path, ../../dev/null can select an empty file as the HMAC key. Where it reaches a SQL query,
' UNION SELECT can return a key of the attacker's choice. Look up key IDs in a fixed map and reject anything
that is not in it.
Attacker-supplied keys: jwk, jku and x5u
These headers embed a key or point to one. A verifier that trusts them lets the attacker bring their own key. The URL forms also make the server fetch an address of the attacker's choosing, which is server-side request forgery.
Weak HMAC secrets
One captured HS256 token is enough to test guesses offline at high speed, with no rate limit and no log entry. RFC 7518 requires a key at least as long as the hash output, which is 32 bytes for HS256, 48 for HS384 and 64 for HS512. Length alone is not enough. The key should come from a secure random generator and not from a person.
What the auditor checks
| Severity | Examples |
|---|---|
| Critical | alg: none, missing signature, embedded jwk, path traversal or SQL injection in kid |
| High | No exp, jku or x5u present, sensitive-looking claims, missing alg |
| Medium | Expired, lifetime over 24 hours, times in milliseconds, x5c, crit |
| Info | Time remaining, notes on the algorithm, missing iss or aud |
Findings describe the token and not your server. A token with jku is safe if the verifier ignores it, and a
token with no findings can still be accepted by a server that never checks signatures. An expired token is listed as Medium
because it is worth noticing, although expiry is normal. ID tokens and refresh tokens may have a longer lifetime by design.
Best practices for developers
- Pin the algorithm in the verifier's configuration, and bind each key to one algorithm.
- Check every claim you rely on:
exp,nbf,issandaud, with a small allowance for clock drift. - Keep access tokens short-lived, from minutes to an hour, and use rotating refresh tokens for longer sessions.
- Prefer asymmetric signatures when several services verify, so that verifiers cannot forge tokens.
- Plan revocation. A signed token stays valid until it expires unless you track a
jtior session version. - Protect storage. A token in local storage can be read by any injected script. A cookie with HttpOnly, Secure and SameSite is harder to steal.
Workflow for pentesters and responders
- Decode the token and read the findings from the most severe down.
- Note the algorithm, key ID and lifetime, and whether the issuer and audience match the target.
- With permission, test whether the server rejects an altered signature,
none, and an expired token. - Verify with the published public key to confirm which key signed the token.
- In an incident, compare
iatandexpwith your timeline to learn how long a stolen token was usable. - Redact the signature before you put a token in a ticket. Without it, the token cannot be replayed.
Verification here supports HS256, HS384, HS512, RS256 and ES256. Other algorithms are decoded and audited without it. Large numbers and duplicate keys are displayed exactly as they appear in the token, because many tools round or drop them and the difference can matter in an investigation.
Why does my correct secret show as not matching?
The usual cause is encoding. Many systems store the secret as Base64, so switch between Text and Base64. Also check for trailing spaces or line breaks, confirm that the header names the algorithm you expect, and make sure that the whole token was copied. For RS256 and ES256, paste the public key and not a certificate.