A JWT Is Signed, Not Encrypted, So Anyone Can Read Its Payload
A JSON Web Token has three Base64URL-encoded parts separated by dots: a header (algorithm and type), a payload (the claims) and a signature. The signature proves the token was issued by someone holding the key and hasn't been changed. It doesn't hide anything: decoding the payload takes one command, so a JWT must never carry secrets, and the server must actually verify the signature before trusting a single claim.
Anatomy
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0MiIsImV4cCI6MTcwMDAwMDAwMH0.<signature>
header payload
{"alg": "HS256", "typ": "JWT"}
{"sub": "42", "exp": 1700000000}
| Claim | Meaning |
|---|---|
sub |
subject: who the token is about |
exp |
expiry, in seconds since the epoch |
iat, nbf |
issued at, not valid before |
iss, aud |
issuer and intended audience |
HS256 signs with one shared secret; RS256 or ES256 sign with a private key and verify with a public one, so verifiers can't issue tokens.
Verify Properly
- Verify the signature with a maintained library, and pin the accepted algorithms (
algorithms=["HS256"]): never let the token's own header choose, and rejectalg: none. - Check
exp, andissandaudwhen several services share keys. - Keep lifetimes short: a JWT is valid until it expires, and the server can't revoke one without keeping state (a deny list, or short access tokens with refresh tokens).
References
- Web Application and Django Security MOC - hub for web security notes.
- CSRF Works Because Browsers Attach Cookies to Requests Other Sites Trigger - the trade-off when tokens travel in cookies.
- RFC 7519: JSON Web Token (JWT) - structure and registered claims.
- OWASP JSON Web Token Cheat Sheet for Java - algorithm confusion, storage and revocation (language-independent advice).
- jwt.io - decoder and library list.
Review cards
Flashcard Why must a JWT never carry secrets?
It's signed, not encrypted: the payload is Base64URL-encoded JSON that anyone can decode. Secret data needs JWE or stays server-side.
Flashcard What does the `exp` claim do, and why keep it short?
It's the expiry time in seconds since the epoch; after it, verification rejects the token. A JWT can't be revoked without server state, so a short exp limits how long a stolen token works.
Quiz What does `alg` in a JWT header name, and how should the server treat it?
- The signing algorithm; the server pins the algorithms it accepts
- The signing algorithm; the server verifies with whichever it names
- The encryption algorithm that hides the payload
- The encoding used for the payload, such as Base64URL
alg is a header parameter, not a claim, naming the signature algorithm (HS256, RS256). Letting the token choose opens algorithm confusion and alg: none, so pass algorithms=["HS256"].