Skip to content

Lesson 6 of 14 · 20m

API Tokens and JWTs: Verification That Actually Verifies

Why alg is attacker input, how RS256 becomes forgeable HS256, and the claims you must check every time.

A JWT is three base64url segments joined by dots: header, payload, signature. The header names the algorithm. The payload carries claims — sub, exp, iss, aud, iat. The signature covers the first two. Everything that goes wrong with JWTs goes wrong in the same place: the receiver decides how to verify from data the attacker wrote.

Decoding is not verifying

base64_decode on the payload is not authentication — anyone can write a payload. Verification is a decision: check the signature against a specific key under a specific algorithm, then validate the claims. The alg field in the header is attacker input. If your verifier reads it and adapts, you handed the attacker the choice of what "valid" means.

The classic is alg: none. In 2015 Tim McLean published a run of JWT library CVEs — multiple popular libraries, when a token arrived claiming alg: none, skipped signature checking entirely. Token valid, no key needed. Libraries fixed it; the design lesson survived: the algorithm must come from your configuration, not the token.

The sibling bug is algorithm confusion. You verify RSA tokens (RS256) with a public key. An attacker takes a real token, re-signs it with HMAC (HS256) using your public key as the HMAC secret — public keys are public, so they have it — and a verifier that accepts HS256 for that key verifies the forged signature. The fix is the same shape: alg is pinned per key, and key type and algorithm never mix. In firebase/php-jwt this is new Key($keyMaterial, 'RS256') — the allowed algorithm is bound to the key object since version 6.0. If you ever see JWT::decode($token, $key) without an algorithm constraint, that line is a finding.

Third leg: kid. The kid header picks which key from a set verifies the token. Resolve it against a hardcoded allowlist — kid has been used for path traversal (../../dev/null, giving the verifier an empty "key") and for SQL injection in key-lookup queries. It's an identifier from the attacker; treat it like one.

Claims are a contract, not a garnish

A valid signature on an expired token is still worthless. Check, every time:

  • exp — reject expired, with a small leeway (60s) for clock skew.
  • iss — the issuer you expected, exactly.
  • aud — this service's name, not another tenant's.
  • iat/nbf — reject tokens from the future beyond skew.

Tokens that don't die are tokens that leak forever. Access tokens live minutes (5–15 is sane); the long-lived thing is the refresh token, which rotates on every use and is stored hashed. A stolen access token with a 10-minute life is an incident; a stolen refresh with rotation detects itself — the first reuse invalidates the family.

Replay is real even on valid tokens: an intercepted token re-presents perfectly. Short exp is the primary control; if you need stronger, keep a jti denylist in Redis until natural expiry.

Storage is a security decision

Where the client keeps the token decides which attack you inherit:

  • localStorage — readable by any JavaScript you run, which means readable by any XSS. Every npm install is now in your auth boundary.
  • HttpOnly cookie — invisible to injected JS, but now CSRF applies: browsers attach cookies on cross-site requests. SameSite=Lax plus a CSRF token on state changes is the usual answer.

There is no safe choice, only a chosen exposure. Session-shaped cookies for same-site apps; short-lived bearer tokens for third-party clients; never tokens in URLs, where they land in access logs and referer headers.

Try this

Take a token your system issued. Flip alg in the header to none, strip the signature, send it. Then take a valid HS256 token, decode the payload (just base64url_decode), change role to admin, re-sign with garbage, send. Both must 401 — if either passes, the verifier trusts the token more than it trusts you.

Pitfalls

  • firebase/php-jwt < 6.0 took allowed_algs as a decode argument — keep it pinned regardless of version; upgrades that drop the constraint are regressions.
  • lcobucci/jwt v5 expresses this as SignedWith(new Sha256(), InMemory::plainText($secret)) plus ValidAt/IssuedBy/PermittedFor constraints — the constraint objects exist precisely so you can't forget the checks.
  • JWT payloads are base64, not encryption. Nothing secret goes in a claim — everyone can read it.
  • Logging raw tokens turns your log store into a credential store. Log iss/sub/jti, never the token.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.