Five ways JWT validation fails, and how to test for them
A JSON Web Token carries, in its own header, the name of the algorithm used to sign it. The original sin of many JWT libraries is trusting that header, letting the token choose how it gets checked. From that one mistake grows a whole family of attacks. I built a tool that automates five of them against a target you're authorized to test, each paired with its fix.
The setup
To test a scanner you need something broken to point it at. So the project ships a small local API with two modes: a vulnerable build that makes the classic mistakes, and a secure build that fixes them. It binds to 127.0.0.1 and is a practice target in the spirit of OWASP Juice Shop. The auditor logs in as a low-privilege user and checks whether any token manipulation lets that user reach an admin-only endpoint. A flaw is only reported when the forged token actually reaches /admin, not merely when it's accepted, which keeps false positives down.
Vulnerable build: 3 vulnerabilities found across 5 checks
Secure build: 0 vulnerabilities found across 5 checks
1. alg: none
The token header says "alg": "none" and carries no signature. A verifier that honours it accepts unsigned tokens, so a normal user can mint one that says role: admin. The fix is to pin the accepted algorithm server-side and never let the header decide.
2. Weak HMAC secret
HS256 tokens are signed with a shared secret. If that secret is something like secret or changeme, it can be guessed offline against a token you already hold, no server interaction needed. Once guessed, you can forge anything. The auditor brute-forces the real token against a common-secret list, and if it wins, signs its own admin token. The fix: a long random secret from a secret manager, or move to RS256/ES256 so there's no shared secret at all.
3. RS/HS algorithm confusion
This is the subtle one. A service that issues RS256 tokens verifies them with its RSA public key, which is not secret and is often published at a JWKS endpoint. If the verifier trusts the header, an attacker can send an HS256 token signed with that public key as the HMAC secret. The server, following the header, uses the same public key to verify the HMAC, and it matches.
Building this token is itself instructive: PyJWT refuses to use an RSA key as an HMAC secret, exactly the mistake being tested, so the tool constructs the token by hand.
def _hs256_with_key(payload, key_bytes):
header = b64url(b'{"alg":"HS256","typ":"JWT"}')
body = b64url(json.dumps(payload).encode())
sig = hmac.new(key_bytes, f"{header}.{body}".encode(), hashlib.sha256).digest()
return f"{header}.{body}.{b64url(sig)}"
The fix is to pin the algorithm to the asymmetric one you issue and reject HMAC algorithms on verification.
4. Missing or unenforced expiry
A token with no exp claim is valid forever, so a single stolen token never stops working. Even with an exp, the verifier has to actually check it. The auditor flags a missing expiry, and where it can, forges an already-expired token to see if the server still accepts it. Against an RS256 service it can't re-sign without the private key, so it reports that limitation honestly rather than guessing.
5. Signature not verified
The bluntest flaw: the server reads the claims but never checks the signature. The auditor changes the payload (role to admin), keeps the original signature, and sends it. If that's accepted, the signature is decorative. Always verify the signature before trusting any claim.
What I took from it
Every one of these reduces to the same root cause: the verifier trusting input it should have pinned. The defender's version of JWT handling is short and boring, pin the algorithm, verify with the right key, require exp, iss and aud, and that's most of the battle. Writing the attacks made the defenses concrete in a way that reading the RFC didn't. The project has 8 automated tests and a CI pipeline, so the before/after (3 vulnerabilities to 0) is checked on every push.
More writing
- The row ID that scored 100%: data leakage in the Bot-IoT dataset
- Hiding instructions in a document: indirect prompt injection in a RAG chatbot
- Access control belongs in the retrieval layer, not the prompt
- Quarantining a compromised EC2 instance in seconds with GuardDuty and Lambda
- Working the OWASP Top 10 by hand: PortSwigger and TryHackMe
- TryHackMe Vulnversity: enumeration to root