Skip to content

Lesson 9 of 20 · 20m

JWT and bearer tokens: signed, not encrypted, not magic

alg confusion, kid path traversal, claim checks nobody runs, and where tokens can actually live.

A JWT is base64url(header).base64url(payload).base64url(signature) — signed, not encrypted. Anyone who sees it can decode the claims in a browser console. If that surprises you, the wrong mental model is already loaded: "the token is encrypted because it looks unreadable." It looks unreadable because it's base64. Put nothing secret in the payload. And never put one in a URL — query strings land in access logs, Referer headers and browser history.

The second wrong model: "the library checks it for me." JWT libraries verify the signature you configure them to verify, and most JWT disasters come from trusting fields inside the attacker-controlled header.

The attacks that actually ship

alg: none. If the verifier accepts whatever algorithm the header claims, a token signed with "none" validates. CVE-2015-9235 and its siblings were exactly this. Modern libraries reject none unless you opt in — never opt in.

RS256 → HS256 confusion. Your public key is public. If the verifier honors an alg: HS256 header and treats the token as HMAC keyed with the RSA public key, the attacker signs a forged token with your public key. Documented by Auth0 in 2015 and still found in audits. Fix: pin the algorithm — jwt.verify(t, key, {algorithms: ['RS256']}) — and never let the token choose.

kid abuse. kid selects the verification key. If your lookup runs SELECT key FROM keys WHERE id = '<kid>', that's SQL injection; if it reads keys/<kid>.pem, that's path traversal to a file whose contents the attacker controls — keys/../../dev/null makes the verification key the empty string, which verifies anything signed with it. Resolve kid through a fixed keyring map and nothing else.

jku / x5u URL fetching. These headers point at a URL hosting the key set; fetch it blindly and the attacker hosts your keys. Allowlist issuers' JWKS endpoints or disable remote resolution — and cache the keys, because fetching on every verify is both slow and a live SSRF surface.

Claims nobody checked. exp, iss, aud, nbf. A token without exp never dies; an aud you don't verify means a token minted for another service gets honored here. jwt.decode(t, key, algorithms=['RS256'], issuer='…', audience='…') in PyJWT; the options object in node-jsonwebtoken. Read the header with get_unverified_header/decode(verify=False) only to pick a key after the algorithm is pinned — unverified decode is for choosing a key, never for trusting claims.

A weak HMAC secret is offline-brute-forceable — hashcat mode 16500 exists because a captured alg: HS256 token is a self-contained grinding target; JWT secrets need ≥256 bits of real entropy, not a word.

Where tokens live

localStorage is readable by any JavaScript that runs on your origin — one XSS and every token walks. HttpOnly cookies hide the value from JS but reintroduce CSRF, which the previous lesson covered. There is no free storage; Backend-For-Frontend keeps tokens off the browser entirely and is the honest option when you can afford it.

Lifetime and revocation

JWTs are stateless — that is the tradeoff, not a feature. A stolen access token works until exp. Keep access tokens at 5–15 minutes, rotate refresh tokens with reuse detection (a re-presented old refresh token means theft; kill the chain, per the OAuth 2.0 BCP), and keep a server-side revocation list for "log out everywhere" and account deletion (jti, a unique token ID, is what makes that list cheap to check). If your threat model needs instant revocation, opaque session IDs in a server store beat JWTs outright. That's a design choice, not a failure.

Practice

Pin algorithms, verify exp/iss/aud with a small clock skew, resolve kid through a map, never fetch jku blindly, short-lived access tokens with rotating refresh — and pick storage consciously: HttpOnly cookie plus CSRF defenses, or localStorage plus real XSS defenses.

Keep reading

Related articles from the SudoSecurity blog.