JWT traps
A typical JWT (JWS) is signed but not encrypted: the signature guarantees integrity, while anyone can read the payload. The server must first verify the signature with its own algorithm and key, then the claims exp, iss and aud, and only then trust role. The traps are alg: none, algorithm confusion, a weak HS256 secret, unchecked claims and a stateless token that stays valid until exp even after logout.
API · Security
A token with alg: none and an empty signature got into the API. Which other JWT traps should you check?
A tester changes "role": "user" to "admin" in the payload, keeps the old signature, and the API answers 200. Or sets "alg": "none" in the header, removes the signature, and the server accepts the token. This is not exotic: they are error classes described in RFC 8725 (JWT Best Current Practices). The lesson covers the JWT weaknesses asked about at middle and senior level, and how to check them without special tools.
How it is built
A JWT (RFC 7519) has three base64url parts separated by dots. The signature covers the header and the payload:
header { "alg": "HS256", "typ": "JWT" }
payload { "sub": "42", "role": "user", "exp": 1790000000 }
signature HMAC-SHA256(base64url(header) + "." + base64url(payload), secret)
The signature guarantees integrity, not secrecy. The payload is only encoded and anyone can read it without a key. The server has to do three things in this order: verify the signature with its own expected algorithm and key, check the claims (exp, nbf, iss, aud), and only then trust sub and role. All the traps appear when one of these steps is skipped or done in another order.
What the server must check, and what breaks
Example: seven traps and how to check each
- Algorithm
none. The attacker sets"alg": "none"in the header and removes the signature. A server that trusts thealgfield from the token will accept it. Expected: 401. - Algorithm confusion. A server with an asymmetric key (RS256) has a public key, which is not a secret. If the library takes the algorithm from the header, the attacker signs the token with HS256 using the public key as a symmetric secret. Correct: the server hard-codes the expected algorithm.
- A weak HS256 secret. A short secret is brute-forced offline from a dictionary, after which any payload can be signed. A QA check: the secret must not be
secretor the project name, and its length meets the algorithm's requirements (at least 256 bits for HS256). - Sensitive data in the payload. An email, a phone or an account number is visible to anyone who has the token.
exp,aud,issare not checked. A token from another service or environment (stage on dev, a token for another API) must not be accepted.- Revocation. A JWT is stateless, so after logout or blocking a user the access token lives until
exp. Protection: a short access token lifetime, refresh token rotation and a denylist for critical cases. - Trusting the
kidandjkuheaders. If the server takes the key from a URL or a path in the header, an attacker can plant their own. This is harder to check from the QA side, but the question "where does the server get the key from" is worth asking.
To check alg none and the payload edit, a few functions are enough: they decode a token and build a forged one (a token without re-signing):
const b64 = (o: object) => Buffer.from(JSON.stringify(o)).toString('base64url');
function decode(token: string) {
const [h, p] = token.split('.');
return {
header: JSON.parse(Buffer.from(h, 'base64url').toString()),
payload: JSON.parse(Buffer.from(p, 'base64url').toString()),
};
}
// 1. alg none: header says "none", the signature part is empty
function algNone(token: string) {
const { payload } = decode(token);
return b64({ alg: 'none', typ: 'JWT' }) + '.' + b64({ ...payload, role: 'admin' }) + '.';
}
// 2. payload edited, the old signature kept
function tamperedPayload(token: string) {
const [h, , s] = token.split('.');
const { payload } = decode(token);
return h + '.' + b64({ ...payload, role: 'admin' }) + '.' + s;
}
Then these tokens are sent to a protected endpoint, and all of them must give 401 (neither 200 nor 500):
for (const [name, token] of [
['alg none', algNone(userToken)],
['edited payload', tamperedPayload(userToken)],
['empty signature', userToken.split('.').slice(0, 2).join('.') + '.'],
]) {
test('API rejects a forged token: ' + name, async ({ request }) => {
const res = await request.get('/v1/users/me', {
headers: { Authorization: 'Bearer ' + token },
});
expect(res.status()).toBe(401);
});
}
A 200 on "edited payload" would mean the server does not verify the signature at all and only reads the claims. A 500 is also a defect: the server is not ready for corrupted input.
Limits and alternatives
- A signed token can be read. If the payload needs confidential data, use JWE (an encrypted token) or an opaque token with the data read on the server by key.
- Opaque (reference) tokens are revoked instantly because the server checks them in a store, but every request needs a trip there. A JWT is cheaper per request but harder to revoke. It is a trade-off, not a "right" choice.
- Algorithm confusion and
kidare hard to reproduce without knowing the server's library. Here QA rather asks the developers ("which library, is the algorithm pinned") and reads the configuration than attacks blindly. - Tools (jwt.io for reading,
jwt_toolor Burp extensions for forgeries) speed up the check. A production token is never pasted into an online decoder: it could be valid.
Common mistakes
- Checking only an expired and a corrupted token. These are the simplest cases, while
alg: noneand a foreignaudgo unnoticed. - Assuming the payload is "encrypted". It is encoded, and everyone can read it.
- Relying on logout as revocation. After logout the JWT is still valid until
expunless there is a denylist. - Testing only a token of the same environment. It is a stage token accepted on dev that shows
issandaudare not checked.
Check yourself
A test changes "role": "user" to "admin" in the token, leaves the signature unchanged, and the API answers 401. Is that enough to consider signature verification correct?
Answer
It is a good sign but not proof. Also check alg: none and a token signed with a different key: the server could have rejected it because of the signature mismatch while still accepting a token without a signature.
A user was blocked, and his access token with exp in 15 minutes is still valid. What is wrong and what would you suggest?
Answer
A JWT is stateless and the server does not consult a store, so blocking has no effect until exp. Options: a shorter token lifetime and refresh token rotation, a user status check on sensitive operations, or a denylist for critical cases. It is a compromise between speed and controllability.
alg: none, a token from another environment and sensitive data in the payload. Middle/Senior: A correct server sets the algorithm and the key itself, then checks exp, iss, aud, and only then trusts role. A signature gives integrity but not secrecy. I also ask how the team revokes tokens: a JWT is stateless, so after logout it lives until exp, and the answer depends on the acceptable risk: a short access token, refresh token rotation or a denylist.