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 smallleeway(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.HttpOnlycookie — invisible to injected JS, but now CSRF applies: browsers attach cookies on cross-site requests.SameSite=Laxplus 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 tookallowed_algsas a decode argument — keep it pinned regardless of version; upgrades that drop the constraint are regressions.lcobucci/jwtv5 expresses this asSignedWith(new Sha256(), InMemory::plainText($secret))plusValidAt/IssuedBy/PermittedForconstraints — 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?
Keep reading
Related articles from the SudoSecurity blog.
PHP 8.6 Finally Turns On the Session Settings You Were Told to Set in 2006
Every PHP hardening guide for two decades has told you to flip the same three session settings. PHP 8.6, due November 19, 2026, flips them for you: strict mode on, HttpOnly on, SameSite set to Lax.
Money Is Not a Float: Decimals, Rounding and Cents in PHP, Python and JavaScript
I ran the same money math through PHP 8.6, Python 3.15 and Node 26. They round 2.675 differently, bcadd('0.1', '0.2') returns 0, PHP's BcMath\Number turns a float 0.1 into 0 with only a deprecation notice, and JSON.parse quietly changes 9007199254740993 to ...992.
PHP OPcache, Preloading and the JIT, Measured on Laravel: One Setting Is 18x, One Costs You 19%, and the JIT Does Nothing
I ran Laravel 13 on PHP 8.6 FPM under six OPcache configurations and timed every one. OPcache itself was worth 18x, revalidate_freq=0 quietly cost 19%, preloading added 9%, and the JIT that doubles Zend's CPU benchmark moved a real page by 1%.
One Slow Call, Three Runtimes: How PHP-FPM, Python ASGI and Node Fall Over When a Dependency Takes 200 ms
I put the same 200 ms wait behind PHP-FPM, FastAPI on uvicorn and a plain Node server, then hit each one with 50 concurrent requests. Handled correctly, the async runtimes served 205-211 req/s against FPM's 19.6. One blocking call in the wrong place took both of them down to 4.98, slower than...