8 JWT security mistakes developers still make
JSON Web Tokens are not insecure by design, but they give you a lot of rope. The specification leaves many decisions to you — which algorithms to accept, how long tokens live, where the browser keeps them — and most real-world JWT vulnerabilities come from getting one of those decisions wrong. Here are eight mistakes that still show up in code reviews and bug-bounty reports, why each one matters, and what to do instead. If you are new to the format, start with how to decode a JWT.
1. Accepting alg: "none"
The JWT standard defines an “unsecured” token whose header says {"alg":"none"} and whose signature is empty. Some early libraries honoured that header, so an attacker could take any token, set the algorithm to none, edit the payload and delete the signature:
eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.<edited payload>.
// header = {"alg":"none","typ":"JWT"}, note the empty last part
Fix: never let the token choose its own algorithm. Configure the verifier with an explicit allow-list and reject everything else. Current versions of mainstream libraries reject none by default, but only if you don’t turn it back on or roll your own parser.
// JavaScript (jose)
await jwtVerify(token, key, { algorithms: ['RS256'], issuer, audience });
# Python (PyJWT)
jwt.decode(token, key, algorithms=["RS256"], audience=AUD, issuer=ISS)
2. Algorithm confusion (RS256 key used as an HS256 secret)
Your API expects RS256 tokens and verifies them with the provider’s public key — which, being public, the attacker also has. If the verification call is generic (“verify this token with this key”) and the library trusts the header’s alg, the attacker can send a token marked HS256 and sign it with HMAC, using the public key’s PEM text as the “secret”. The server runs HMAC with the same bytes and the signature matches.
Fix: pin the algorithm (see mistake 1), and keep keys typed: an RSA or EC public key should only ever be passed to an RSA or ECDSA verifier. Modern libraries refuse to use a PEM public key as an HMAC secret, which is another reason to keep them updated.
3. Weak or guessable HMAC secrets
With HS256 the whole security of every token rests on one secret. An attacker who has a single valid token can try billions of guesses per second offline — password-cracking tools have a JWT mode built in — without ever touching your server. Secrets like secret, changeme, the app name, or the example value from a tutorial fall in minutes.
Fix: use at least 256 bits of randomness (RFC 7518 requires a key at least as long as the hash output), keep it in a secret manager or environment variable, not in Git, and rotate it if it leaks.
node -e "console.log(require('crypto').randomBytes(32).toString('base64url'))"
python -c "import secrets; print(secrets.token_urlsafe(32))"
If several independent services need to verify tokens, switch to RS256 or ES256 so that only the issuer holds a signing key.
4. Verifying the signature but not the claims
A valid signature only proves who issued the token. It does not prove the token is still valid or meant for you. Skipping exp means stolen tokens work forever; skipping nbf accepts tokens before their start time; skipping aud lets a token issued for your billing service be replayed against your admin API; skipping iss may accept tokens from a different tenant of the same identity provider.
Fix: pass the expected issuer and audience to the library and make exp required. Allow a small clock skew (30–60 seconds), not hours.
5. Putting sensitive data in the payload
A signed JWT is encoded, not encrypted. Anyone who sees it — the browser, a proxy, a log file, an analytics script — can read every claim. We regularly see national ID numbers, phone numbers, internal pricing flags and even password hashes in payloads.
Fix: keep the payload to what the receiving API needs to make a decision: a user ID, roles or scopes, expiry. Look everything else up on the server. If a token genuinely must carry confidential data, use encrypted JWE instead of a plain signed JWT.
6. Long-lived tokens with no way to revoke them
A JWT is valid until exp, even after the user logs out, changes their password or is banned — the server doesn’t look anything up. A 30-day access token is therefore a 30-day skeleton key if it leaks.
Fix: keep access tokens short (often 5–15 minutes) and pair them with a refresh token that is stored server-side, rotated on every use and revoked on logout. Reuse of an already-rotated refresh token is a strong signal of theft; revoke the whole family. For immediate revocation of access tokens, keep a small deny-list of jti values or a per-user “token version” until their natural expiry.
7. Storing tokens where XSS can read them
Tokens in localStorage or sessionStorage are readable by any JavaScript on the page. One cross-site scripting bug, or one compromised npm package or third-party script, can copy the token to an attacker’s server, where it keeps working until it expires.
An HttpOnly cookie can’t be read by JavaScript at all:
Set-Cookie: session=<token>; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=900
This is not a free win, and it’s worth being honest about the trade-off:
| localStorage + header | HttpOnly cookie | |
|---|---|---|
| XSS can steal the token | Yes | No (but XSS can still make requests as the user while the page is open) |
| CSRF risk | None — the browser never attaches it automatically | Yes — the browser sends it automatically; mitigate with SameSite and CSRF tokens for state-changing requests |
| Works across domains | Easy | Needs careful CORS and cookie settings |
Fix: for browser apps, prefer HttpOnly; Secure; SameSite cookies (or a backend-for-frontend that keeps tokens server-side entirely), add CSRF protection, and treat XSS prevention — output escaping and a Content Security Policy — as the real defence either way.
8. Pasting production tokens into online tools that upload them
A live access token is a credential. Many “JWT decoder” websites send what you paste to their server, where it may be logged, cached or analysed. If that token hasn’t expired, whoever reads those logs can call your API as that user.
Fix: debug with test tokens where possible; use tools that run entirely in your browser (you can confirm in the browser’s Network tab that nothing is sent); and if a real token was exposed, revoke the session or wait for it to expire rather than assuming nobody saw it.
Rule of thumb: the verifier decides the algorithm, the key and the expected claims. The token only brings data to be checked against those expectations.
none tokens and verifies signatures locally with Web Crypto — nothing is uploaded.
Try it now
FAQ
Are JWTs less secure than traditional sessions?
Not inherently. Server-side sessions are simpler to revoke and harder to misconfigure, so for a single web app they are often the better default. JWTs shine when several services must verify identity without a shared session store. Either way, the mistakes above are what make the difference.
Is HS256 unsafe? Should I always use RS256?
HS256 with a long random secret is secure. Its limitation is that every verifier can also sign. Use RS256 or ES256 when the verifiers are separate services or third parties; ES256 gives much shorter signatures.
Does HttpOnly protect me from XSS completely?
No. It stops a script from reading the token, but a script running in your page can still send requests that the browser authenticates with the cookie. It limits the damage to the time the page is open; it does not replace fixing XSS.
What should I do if a token leaked?
Revoke the related session or refresh token, add the jti to a deny-list if you have one, and if the signing secret itself may have leaked, rotate it — which invalidates every token signed with it.