#Why this matters
Modern web apps and APIs often replace server-side session lookups with stateless JSON Web Tokens (JWTs). After login, the server signs claims such as sub (subject) and role, and the client sends that token back in an Authorization: Bearer <token> header.
Because the server trusts the token instead of querying a session table on every request, any verification flaw lets an attacker forge a privileged identity and hijack administrator access.
#Anatomy of a JWT
A signed JWT (technically a JWS, defined alongside JWT in RFC 7519) consists of three Base64URL-encoded segments separated by dots: header.payload.signature.
// Decoded Header
{
"alg": "HS256",
"typ": "JWT"
}
// Decoded Payload
{
"sub": "1042",
"username": "student",
"role": "user",
"iat": 1795000000,
"exp": 1795003600
}The first two parts are simply encoded, not encrypted. Anyone who intercepts or holds the token can decode the header and payload with standard tools. Integrity relies entirely on the third segment: an HMAC (such as HS256) computed with a server secret, or a digital signature (such as RS256 or ES256) computed with a private key.
#Signature and header vulnerabilities
Most JWT compromises happen because the verification library trusts attacker-controlled metadata inside the header before establishing cryptographic trust.
| Vulnerability | Mechanism | Root cause |
|---|---|---|
alg: "none" bypass |
Header algorithm changed to none and signature stripped |
Library accepts unsigned tokens in production |
| Weak HMAC secret | Offline dictionary attack recovers the HS256 key |
Short, predictable, or default signing secret |
| Algorithm confusion | Token switched from RS256 to HS256 and signed with the RSA public key |
Server passes the same key variable to both RSA and HMAC verifiers |
kid path or SQL injection |
kid header points to /dev/null or injects a controlled key value |
Key ID is concatenated into a file path or database query |
Untrusted jwk / jku |
Attacker embeds a public key or hosts a JWK Set URL | Server fetches or trusts keys referenced by the token itself |
Consider testing an alg: "none" flaw in a controlled lab. If the server inspects the header's alg field directly and skips signature checks when alg equals none, modifying "role": "user" to "role": "admin" and keeping the trailing dot (header.payload.) grants admin access.
$ TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMDQyIiwicm9sZSI6InVzZXIifQ.sig"
$ echo "$TOKEN" | cut -d. -f2 | base64 -d 2>/dev/null
{"sub":"1042","role":"user"}
$ python3 -c 'import base64, json; h=base64.urlsafe_b64encode(b"{\"alg\":\"none\",\"typ\":\"JWT\"}").rstrip(b"=").decode(); p=base64.urlsafe_b64encode(b"{\"sub\":\"1042\",\"role\":\"admin\"}").rstrip(b"=").decode(); print(f"{h}.{p}.")'
eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJzdWIiOiIxMDQyIiwicm9sZSI6ImFkbWluIn0.Tools such as Burp Suite (via JWT extensions) and jwt_tool automate decoding, tampering, and offline wordlist checks against weak HS256 secrets; check jwt_tool --help in your lab VM for available test flags.
#Authentication bypass patterns beyond signatures
Even with valid signatures, authentication logic often fails around the token:
- Unvalidated claims: Skipping checks on
exp,iss, oraudlets expired or staging tokens work on production. - Trusted proxy headers: Trusting client-supplied
X-Forwarded-FororX-Original-URLheaders bypasses IP allow-lists or route checks. - Multi-step state skipping: Issuing a session after password login before MFA finishes lets a caller skip MFA by requesting
/dashboarddirectly. - Unchecked account status: Trusting
suboremailwithout checking whether the account is locked or disabled allows post-suspension access.
#Defending JWT and authentication flows
Treat every field inside an incoming token as untrusted until verification succeeds:
- Pin algorithms server-side: Configure an explicit allow-list (for example
["RS256"]or["EdDSA"]), ignore the headeralgfor verification choices, and rejectnone. - Use strong keys: Use at least 256-bit random secrets for
HS256, keep asymmetric private keys on the auth server, and never trustjwkorjkuheaders. - Validate claims: Enforce
exp,nbf,iss, andaudon every protected route with short token lifetimes. - Enforce full auth state: Verify MFA completion and account status in server middleware, not UI redirects.
#Common mistakes
- Calling
decode()(Base64 decoding only) instead ofverify(). - Passing a public key to a verifier that accepts both
RS256andHS256. - Storing secrets or sensitive personal data in an unencrypted JWT payload.
- Relying on client-side token deletion for logout when immediate revocation is needed.
#Summary
JWTs guarantee stateless integrity only when the server enforces strict cryptographic verification and claim checks. Attackers target alg: "none", weak HMAC secrets, algorithm confusion, kid injection, and surrounding logic flaws such as skipped MFA steps or spoofed headers. Pin allowed algorithms server-side, map key IDs via allow-lists, validate exp/iss/aud, and never trust claims before verification passes.