Dev.to Security 🔐 Cybersecurity 👁 0 📖 2 min read

7 JWT mistakes that quietly break your authentication

JWTs are everywhere, and most breaches involving them come from a handful of repeat mistakes. Here are seven I check for first, with the fix for each. 1. Believing the payload is secret A JWT is signed, not e

JWTs are everywhere, and most breaches involving them come from a handful of repeat mistakes. Here are seven I check for first, with the fix for each.

1. Believing the payload is secret

A JWT is signed, not encrypted. Anyone holding the token can read the header and payload by Base64-decoding the middle part. Never put passwords, personal data or internal IDs you would not show a user in the payload.

2. Accepting any algorithm

The header says which algorithm to use, but the server must decide. Always pass an allow-list when verifying:

// Node, jsonwebtoken
const payload = jwt.verify(token, publicKeyOrSecret, {
  algorithms: ['RS256'],     // never trust the token's own "alg"
  issuer: 'https://auth.example.com',
  audience: 'my-api',
});

Old libraries accepted alg: "none" (no signature at all). Current ones reject it, but only if you do not enable it and you pin the algorithm.

3. Mixing HS256 and RS256 keys

If a verifier uses a public key but also accepts HS256, an attacker can sign a token with HS256 using the public key as the secret. Pin one algorithm per key and keep symmetric and asymmetric keys separate.

4. Weak or guessable HS256 secrets

An HS256 secret like secret or changeme can be brute-forced offline from a single token. Use at least 32 random bytes (for example openssl rand -base64 48) and load it from the environment, not source code.

5. Skipping claim checks

A valid signature only proves who issued the token. Also check:

  • exp (expiry) and nbf (not before), allowing a few seconds of clock skew
  • iss (issuer) and aud (audience), so a token for one service is not accepted by another
  • Any role or scope claim, on the server, for every protected route

6. Storing tokens where scripts can steal them

localStorage is readable by any script on the page, so one XSS bug leaks every session. Prefer an HttpOnly; Secure; SameSite=Lax cookie for browser apps, and add CSRF protection if you do.

7. No plan for revocation

JWTs are valid until they expire. Keep access tokens short (5 to 15 minutes), use refresh tokens you can revoke server-side, and rotate them on use. For logout or password change, invalidate the refresh token and any session record.

Quick audit checklist

  1. Algorithm pinned on every verify call.
  2. Secret or key length and storage reviewed.
  3. exp, iss, aud verified.
  4. Nothing sensitive in the payload.
  5. Tokens kept out of localStorage.
  6. Short expiry plus revocable refresh tokens.

When I debug these I keep a browser-only toolbox open so tokens never leave my machine: https://luxury-malabi-8a7447.netlify.app/toolbox.html (JSON, Base64 and URL are free; the full version adds a JWT decoder, hashes and a regex tester). Decoding only reads a token; it does not verify the signature, so treat it as inspection, not validation.

Written by Yogeshwaran Tech Tools. Corrections welcome.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.