Security Playbook
12 · Authentication & sessionsOWASP A07:20258 min read

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.

JWT lab: decode, tamper, verify (real HMAC, runs locally)
Server verification
Header
—
Payload
—
Signature
—
Anyone can read a JWT. The signature only proves the server issued it. Verify with a fixed algorithm, check exp, and never put secrets in the payload.
Real HMAC-SHA256 computed in your browser with a demo secret. Paste any JWT to decode it; nothing is sent anywhere.

Classic JWT bugs

BugWhat happensFix
alg: none acceptedAttacker removes the signature and sets any rolePin the algorithm: verify(token, key, { algorithms: ['HS256'] })
decode() instead of verify()Claims are read but never checkedOnly trust the result of verify
Weak HMAC secretSecret is brute-forced offline from one token32+ random bytes, or asymmetric keys (RS256/EdDSA)
No exp / very long expA stolen token works for monthsShort access tokens (5–15 min) + refresh tokens
Algorithm confusionRS256 public key used as an HS256 secretSeparate keys per algorithm, pin the algorithm
Secrets in the payloadAnyone with the token can read themOnly 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 token

JWT 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