JWTs: readable by anyone, trusted only if verified
JSON Web Tokens carry claims like user ID and role, signed by the server. What the three parts are, the classic verification bugs (alg none, missing exp, weak secrets) and when a plain session is the better choice.
In one sentence
A JWT is base64, not encryption: anyone can read it. Its signature only proves the server issued it, and only if you verify it with a fixed algorithm and check the expiry.
The idea
A JWT is three base64url chunks joined by dots: a header (which algorithm), a payload (the claims: sub, role, exp) and a signature over the first two. Change one character of the payload and the signature no longer matches, as long as the server actually checks.
—
—
—
Classic JWT bugs
| Bug | What happens | Fix |
|---|---|---|
| alg: none accepted | Attacker removes the signature and sets any role | Pin the algorithm: verify(token, key, { algorithms: ['HS256'] }) |
| decode() instead of verify() | Claims are read but never checked | Only trust the result of verify |
| Weak HMAC secret | Secret is brute-forced offline from one token | 32+ random bytes, or asymmetric keys (RS256/EdDSA) |
| No exp / very long exp | A stolen token works for months | Short access tokens (5–15 min) + refresh tokens |
| Algorithm confusion | RS256 public key used as an HS256 secret | Separate keys per algorithm, pin the algorithm |
| Secrets in the payload | Anyone with the token can read them | Only IDs and non-sensitive claims |
import { jwtVerify } from "jose";
const { payload } = await jwtVerify(token, key, {
algorithms: ["HS256"],
issuer: "https://auth.shop.test",
audience: "shop-api",
}); // throws on bad signature, wrong alg or expired tokenJWT or session?
Server session (cookie with random ID)
- Revoke instantly by deleting the row
- Tiny cookie, nothing to leak
- Needs a session store lookup
JWT
- Stateless: any service can verify it
- Hard to revoke before it expires
- Good between services, or with short expiry + refresh
Key takeaways
- 01JWT payloads are readable by anyone; never put secrets in them.
- 02Always verify, with a pinned algorithm, issuer, audience and expiry.
- 03Use strong secrets or asymmetric keys, and short-lived access tokens.
- 04For a single web app, a classic server session is often simpler and safer.
Check your understanding
1 of 3
Can anyone read a JWT's payload?
Related concepts
Sessions & cookies
A session cookie is as good as a password while it's valid. Ship it as HttpOnly; Secure; SameSite=Lax, keep it short-lived, and rotate it on login and privilege changes.
7 min read →AuthN vs AuthZ
Authentication answers *who are you?* Authorization answers *are you allowed to do this, to this thing, right now?* Most breaches of web apps are the second one missing.
6 min read →XSS
XSS is injection into HTML instead of SQL. Escape output by default, never inject raw HTML, and add a Content Security Policy as the safety net.
8 min read →