Offensive Security

JWT Security: The Mistakes That Keep Appearing in Real Applications

JSON Web Tokens replaced server-side sessions in a lot of applications because they are simple to issue and need no shared session store. Both of those properties are also why they are so frequently insecure.

JWT Security: The Mistakes That Keep Appearing in Real Applications

A JWT is three base64url segments: header, payload, signature. Anyone holding the token can read the payload. That is the first thing teams get wrong.

Treating the payload as secret

It is encoded, not encrypted. Decoding it requires no key and no tooling beyond a browser console. Yet tokens routinely carry internal user identifiers, role structures, email addresses and occasionally far worse.

Assume anything in the payload is visible to the user and to anyone who obtains the token. If it must be confidential, it does not belong there.

Trusting the algorithm the token declares

The header names the signing algorithm, and the token is attacker-supplied data. Two classic attacks follow.

The none algorithm. The specification includes an "unsecured" mode. Change the header to {"alg":"none"}, strip the signature, and a library that honours the header accepts the token. Modern libraries refuse by default, but applications that pass the header value into the verification call reintroduce it.

Algorithm confusion. An application verifying RS256 holds a public key, which is by definition not secret. An attacker switches the header to HS256 and signs the token using that public key as the HMAC secret. A naive verifier uses its configured key material with the attacker's chosen algorithm and the signature validates.

The fix for both is the same: specify the expected algorithm in the verification call and reject anything else. Never let the token decide how it is verified.

Weak signing secrets

HMAC-signed tokens are only as strong as the secret. Secrets like secret, changeme or the framework default are crackable offline in moments, and tooling to do it against a captured token is widely available.

Use a long random secret from a cryptographic source, store it outside the repository, and rotate it with support for a previous key during the overlap. A secret committed to version control is compromised permanently, because history persists after deletion.

Not validating the claims

Verifying the signature proves the token was issued by someone holding the key. It proves nothing else. Applications regularly stop there.

  • exp — expiry. Not all libraries enforce it automatically.
  • iss — issuer. Without it, a valid token from another system sharing the key is accepted.
  • aud — audience. This is what stops a token issued for one service being replayed against another.
  • sub — the subject. Verify the user still exists and is still active, because the token will not know they were disabled.

The revocation problem

This is the structural weakness, not an implementation bug. A stateless token stays valid until it expires. There is no logout that invalidates it, and no way to cut off an account that has just been compromised.

The practical answers are all compromises. Short-lived access tokens with refresh tokens you can revoke centrally. A denylist of revoked token identifiers, which reintroduces server state. A per-user token version incremented on password change or logout, checked at verification — cheap and effective, and also server state.

Choose deliberately. An access token valid for twenty-four hours with no revocation path means a stolen token is good for a day no matter what you do.

Where the token is stored

In localStorage, any cross-site scripting flaw reads it directly. In a cookie with HttpOnly, Secure and SameSite, script cannot read it — but you take on cross-site request forgery, which needs its own defence.

Neither is free. The cookie approach is generally the safer default, with CSRF handled explicitly.

The short version

Pin the algorithm. Use a long random secret kept out of the repository. Validate issuer, audience and expiry, not just the signature. Keep access tokens short and have a revocation path. Put nothing secret in the payload.

Frequently asked questions

Are JWTs less secure than sessions?
They are not inherently weaker, but they move responsibility to the application. Server-side sessions get expiry and revocation for free; with JWTs you must build both deliberately.
Where should a JWT be stored in a browser?
Usually a cookie with HttpOnly, Secure and SameSite set, because that keeps it out of reach of injected scripts. You then have to defend against cross-site request forgery separately.
Is the alg=none attack still relevant?
Current libraries reject it by default, but it still appears where application code passes the token's own header into the verification call. Always pin the expected algorithm server-side.

Related reading

0 comments

Leave a comment

Comments are moderated before appearing.