← All lessons Stage 4 · Lesson 4
25 min

JWT: how tokens break

Stage 4 · Lesson 4 · 25 minutes · Requires Stage 1 Lesson 5 and Stage 0 Lesson 6


The one-line answer

A JWT (JSON Web Token) is a signed note that says "this is user 1043"; it's only safe if the server properly verifies the signature every time — and most JWT bugs come from servers that don't.

The boundary: decoding a token you were issued, to understand it, is fine. Modifying or forging tokens to gain access is testing — labs only.


Why you should care

JWTs are everywhere in modern APIs and single sign-on: the Authorization: Bearer eyJ... header you saw in Lesson 1 is very often a JWT. If the server checks them wrongly, an attacker can simply write a token saying they're the administrator.

You're well prepared: Stage 0 Lesson 6 taught you that encoding isn't encryption, and Stage 1 Lesson 5 taught you how sessions work. JWT sits right where those two meet.


How it actually works

The structure

A JWT is three parts separated by dots:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMDQzIiwicm9sZSI6InVzZXIiLCJleHAiOjE3OTAwMDAwMDB9.4Xk2...signature
└──────────── header ──────────────┘ └──────────────────── payload ─────────────────────┘ └── signature ──┘

Each of the first two parts is Base64URL-encoded JSON. Decode them (recall Stage 0 Lesson 6 and your decoding lab):

// header
{ "alg": "HS256", "typ": "JWT" }

// payload
{ "sub": "1043", "role": "user", "exp": 1790000000 }
  • Header — which signing algorithm was used (alg).
  • Payload — the claims: who the user is (sub), their role, when the token expires (exp).
  • Signature — a cryptographic value computed from the header, payload and a secret key only the server should know.

The key point: encoded, not encrypted

Anyone holding a JWT can read the payload — it's just Base64URL. So:

  • Never put secrets in a JWT payload (passwords, personal data you wouldn't want visible).
  • The payload is not protected by being hidden. It's protected only by the signature.

How the signature protects it

When the server receives a token it should:

  1. Recompute the signature from the header and payload using its secret key.
  2. Compare it with the signature in the token.
  3. Reject the token if they don't match, or if it has expired.

If a user changes "role": "user" to "role": "admin", the signature no longer matches — so a correct server rejects it. Everything depends on step 1–3 actually happening, correctly.

Common flaws (concept level)

1. Signature not verified. The server decodes the token and reads the claims, but never checks the signature. Then anyone can edit the payload and be whoever they like. This happens when developers use a library's "decode" function instead of its "verify" function.

2. Accepting alg: none. The JWT standard includes an "unsigned" option. If the server lets the token's own header decide the algorithm, an attacker can set "alg": "none", remove the signature, and a badly configured server accepts it. The flaw: the server trusts the client to say how the token should be checked.

3. Weak secret keys. With HS256, the same secret signs and verifies. If the secret is short or common (secret, password123, the company name), an attacker who has one valid token can guess the secret offline — trying candidate keys until one reproduces the signature — and then sign any token they want.

4. Algorithm confusion. Some servers accept more than one algorithm family and can be tricked into verifying a token with the wrong kind of key. PortSwigger covers this at practitioner level; the root cause is the same as flaw 2 — letting the token choose.

5. No expiry or revocation. Tokens that never expire, or that stay valid after logout or password change, give a stolen token a long life (recall broken logout, Stage 3 Lesson 5).

Impact

Forged or tampered tokens mean authentication bypass and privilege escalation: logging in as any user, or as an admin, without a password.


See it yourself

  1. Log in to OWASP Juice Shop on your own machine. Find the JWT in requests (an Authorization header or cookie).
  2. Decode the header and payload yourself — with your decoding skills from Stage 0, Burp's Decoder tab, or a Base64URL decode in Python. What claims does it carry? What's the alg?
  3. Read PortSwigger's JWT attacks topic introduction (link below). Install the free JWT Editor extension from Burp's BApp Store (Extensions tab), which works in Community.
  4. Do the apprentice labs on unverified signature and flawed signature verification. For each, note which check the server skipped.

How attackers abuse this

Attackers collect a valid token from their own account, decode it, and check: does the server verify the signature? Does it accept none? Is the secret guessable? If any answer is yes, they edit sub or role and become another user or an admin. Stolen tokens (via XSS, logs, or insecure storage) are also replayed for as long as they stay valid.

The defensive fix:

  • Always verify — use the library's verify function, never decode-only, on every request.
  • Fix the algorithm on the server. Configure the exact algorithm(s) you accept; never let the token's alg header decide. Reject none.
  • Strong keys. For HS256, a long, random secret (at least 256 bits) stored securely — not in source code. For larger systems, asymmetric algorithms (e.g. RS256/ES256) so only the auth server holds the signing key.
  • Short expiry + refresh. Short-lived access tokens (minutes), with refresh tokens that can be revoked.
  • Revocation for logout, password change and compromise (e.g. a denylist of token IDs, or rotating keys).
  • Validate the standard claims: exp, and where used, iss (issuer) and aud (audience).
  • Keep payloads minimal and free of sensitive data.
  • Use a maintained, well-known JWT library — never hand-roll token checking.

Common mistakes

  • Thinking the payload is secret. It's only encoded.
  • Using decode instead of verify.
  • Trusting the alg header.
  • Weak, short or hard-coded secrets.
  • Tokens that never expire or survive logout.
  • Storing JWTs where scripts can read them (e.g. localStorage) on sites with XSS risk — combine with Stage 3 Lesson 3's defences.

Check yourself

  1. Why can anyone read a JWT's payload, and what does that mean for what you put in it?
  2. What does a correct server do with a token whose payload has been edited?
  3. Why is accepting "alg": "none" dangerous?
Answers
  1. The payload is only Base64URL-encoded, not encrypted. Never put secrets or sensitive personal data in it.
  2. It recomputes the signature, finds it doesn't match, and rejects the token.
  3. It lets the client decide that no signature is needed. An attacker can then write any payload with no signature and a misconfigured server will trust it. The server must fix the allowed algorithm itself.

Your lab

  • PortSwigger JWT attacks: https://portswigger.net/web-security/jwt
  • Decode the JWT issued by OWASP Juice Shop on your own machine and document every claim.

Writeup: the token's structure, the flaw in the lab, the check the server skipped, and the configuration fix.


Summary in your language

தமிழில் சுருக்கம்

JWT என்பது "இவர் user 1043" என்று சொல்லும் கையொப்பமிட்ட குறிப்பு — header, payload, signature என மூன்று பகுதிகள். Payload encoded மட்டுமே, encrypted அல்ல — யார் வேண்டுமானாலும் படிக்கலாம்; அதைப் பாதுகாப்பது signature மட்டுமே. பிழைகள்: signature சரிபார்க்காமை, alg: none-ஐ ஏற்பது, பலவீனமான secret, காலாவதியாகாத tokens. தீர்வு: எப்போதும் verify செய்யுங்கள், algorithm-ஐ server-இல் நிர்ணயியுங்கள், வலுவான key, குறுகிய காலாவதி, revocation, payload-இல் ரகசியங்கள் வேண்டாம்.

हिंदी में सारांश

JWT एक signed note है जो कहता है "यह user 1043 है" — इसके तीन हिस्से हैं: header, payload और signature। Payload सिर्फ़ encoded है, encrypted नहीं — कोई भी पढ़ सकता है; उसे सिर्फ़ signature बचाता है। गलतियाँ: signature verify न करना, alg: none मान लेना, कमज़ोर secret, कभी expire न होने वाले tokens। इलाज: हमेशा verify करें, algorithm server पर तय करें, मज़बूत key, छोटी expiry, revocation, और payload में कोई secret नहीं।

Want this summary in Malayalam, Telugu, Kannada or another language? Tell us — the most requested languages get added next.


What's next

What happens when an API never says "slow down"? Rate limits, enumeration and abuse