A JWT is a container for claims. The server must verify who produced it and whether the claims are still allowed. Decoding is not verifying.
Some applications store base64url JSON without a signature. Others accept alg=none, use a guessable HMAC secret, or trust a role claim without checking it against the database. The attacker changes the claim and the server believes it.
We see a long encoded string and assume cryptography. Attackers decode it first and discover it is only a suggestion.
What is actually at stake?
Before touching a payload, map the assets, the actors and the boundary that is supposed to protect them. A finding only becomes a vulnerability when you can connect the failed boundary to a real asset.
Assets
- Role and tenant claims
- Session identity
- Administrative routes
- Signing keys and key identifiers
Actors
- Authenticated user who can decode their token
- Attacker with a stolen or forged token
- Malicious identity provider or key endpoint
Trust boundaries
- Token bytes to verified claims
- Header algorithm to verifier policy
- Key ID to trusted key set
- Claim role to server-side authorization
Abuse cases
- Change role=user to role=admin and re-sign with a weak key
- Use alg=none
- Exploit algorithm confusion between HMAC and RSA
- Point jku or kid at an attacker-controlled key
- Replay a token after logout or role removal
Follow the decision, not the payload
Every family has a moment where trusted data becomes an authorization, parsing or state decision. Learn to point at that moment in the request path.
- 01
Issue
The server creates a header, payload and signature, then sends the token to the client.
- 02
Store
The client keeps it in a cookie or storage. The server must protect it from theft and fixation.
- 03
Present
The token returns in a header or cookie on each request.
- 04
Verify
The server checks the signature, algorithm, issuer, audience, expiry and key before trusting any claim.
- 05
Authorize
The verified subject and claims are mapped to a policy. A claim is input to authorization, not a substitute for it.
The algorithm confusion era
JSON Web Token security research, 2015, public disclosuresPublic research in 2015 demonstrated that many JWT libraries accepted unexpected algorithms or allowed an attacker to choose how a token was verified.
Researchers forged tokens by switching the algorithm, using an empty signature, or exploiting a verifier that treated a public key as an HMAC secret.
Applications that trusted the token's role claim could be escalated to administrator. The issue affected libraries across multiple languages and frameworks.
The verifier trusted the token header to select the algorithm instead of enforcing a fixed, expected algorithm and key type.
Pin the accepted algorithm, verify with the correct key type, reject none, validate issuer and audience, and never let the token choose its own trust anchor.
Read it as a chain: discovery → failed decision → impact → fix. Then ask which link would have broken earliest.
A repeatable testing sequence
- Decode the token and inspect the header, payload and role fields.
- Check whether the token is signed, whether alg is enforced, and whether the secret is guessable.
- Change the role or user claim and replay the cookie.
- Test the admin route; a valid signature is not the same as valid authorization.
Where smart people get fooled
- HTTPS protects transport, not a forgeable token.
- HttpOnly prevents JavaScript theft, not manual tampering.
- A long random-looking string is not proof of cryptography.
What would the logs show?
A control is stronger when someone can see it fail. These signals are starting points for a detection rule, not a complete SIEM use case.
- Tokens with alg=none or an unexpected algorithm
- Signature verification failures followed by successful requests
- Role or tenant claims that differ from the server-side user record
- Tokens with unusual kid, jku or x5u values
- The same token used from multiple countries or after logout
index=web sourcetype=app_logs | eval jwt_bad=if(match(request_body,"(?i)(\"alg\"\s*:\s*\"none\"|jku|x5u)"),1,0) | where jwt_bad=1 | stats count values(path) by clientip, user_id | where count > 0Fix the decision, then prove it stays fixed
- Allowlist the accepted algorithm and verify the signature before reading claims.
- Use a strong secret or asymmetric keys, rotate them and keep expiry short.
- Authorize every sensitive action against server-side state, not only token claims.
const payload = jwt.decode(token);
if (payload.role === "admin") grantAdmin();const claims = jwt.verify(token, key, {
algorithms: ["RS256"],
issuer: "https://id.example.com",
audience: "academy-api"
});
const user = await loadUser(claims.sub);
if (policy.can(user, "admin.access")) grantAdmin();Verify before reading claims, pin the algorithm, validate issuer and audience, then reload the current user and authorize against server-side state.
Regression tests a reviewer would ask for
Algorithm pinning
GivenThe verifier expects RS256
WhenA token uses alg=none or HS256
ThenVerification fails and no claims are trusted
Claim tampering
GivenA normal user has role=user
WhenThe role is changed to admin without a valid signature
ThenThe request is rejected
Key endpoint
GivenThe token header contains jku or kid
WhenThe value points to an untrusted key
ThenThe server uses only its configured key set
Revocation
GivenA user is demoted or logs out
WhenAn old token is replayed
ThenThe server checks current status or a revocation list and denies the action
What this finding is not
Strong testers kill their own findings before someone else does. These are the shortcuts that turn a real bug into a weak report — or a false alarm into a wasted escalation.
- The payload is readable: JWT payloads are encoded, not encrypted. Confidentiality requires encryption or avoiding sensitive claims.
- Changing a claim breaks the signature: that is expected; look for endpoints that decode without verifying.
- The token uses HS256: HMAC is not inherently weak, but the secret must be long, random and rotated.
- The token expires in 24 hours: expiry helps but does not fix forgery, replay or missing revocation.
Terms you should be able to use precisely
- JWT
- JSON Web Token: a signed or encrypted set of claims used between parties.
- alg=none
- A JWT header value indicating no signature; secure verifiers must reject it.
- Algorithm confusion
- A verifier uses the token's algorithm or key type in an unintended way, enabling forgery.
- kid
- Key ID: a header that tells the verifier which key to use; it must not point to untrusted material.
- Audience
- The intended recipient of a token; verifying it prevents a token from one service being used at another.
- Revocation
- Invalidating a token or session before its natural expiry.
Answer these without reciting a payload
Why is decoding a JWT not verification?
Decoding only base64url-decodes the parts. Verification checks the signature and the claims against a trusted key and policy.
What is the risk of trusting the role claim directly?
A forged or stale token can carry a privileged role. The server should verify the token and then authorize against current server-side state.
How do you handle logout with stateless JWTs?
Use short-lived access tokens with refresh rotation, maintain a revocation or session-version check, and clear the cookie. Pure stateless tokens cannot be revoked without a state check.
Can you separate the mechanism from the payload?
Four questions, graded on the server. Answers and explanations are released only after you submit, and signed-in learners keep a best score.
JWT ليست صندوقًا سريًا؛ الحمولة مقروءة، والأمان كله في التحقق من التوقيع والادعاءات.
لا تسمح للتوكن باختيار الخوارزمية أو مصدر المفتاح، وارفض none، وتحقق من المصدر والجمهور والانتهاء. الأهم: لا تبنِ الصلاحية على role داخل التوكن فقط، بل أعد تحميل المستخدم وطبّق السياسة الحالية. التوكن الموقّع بشكل صحيح قد يكون قديمًا بعد تخفيض الصلاحية أو تسجيل الخروج.
Which part of the token does the server actually verify before trusting the role?