Skip to main content
JWT Vulnerabilities & Authentication Bypass Patterns
Lesson 10 of 18

JWT Vulnerabilities & Authentication Bypass Patterns

Understand how JSON Web Tokens represent identity claims, identify signature and verification flaws in controlled labs, and implement resilient token and authentication checks.

Intermediate 28 min Web security
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Explain the three-part structure of a JWT and why encoding does not protect integrity
  • Identify alg=none acceptance, weak HMAC secrets, algorithm confusion, and header parameter injection
  • Recognise common authentication bypass patterns across headers, multi-step flows, and claim validation
  • Configure strict server-side JWT verification and key management controls

Recommended first: HTTP Request/Response, Cookies & Session ManagementWeb Authentication & Access Control Fundamentals

#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.

json
// 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.

Stateless JWT verification flowStateless JWT verification flowBrowserAuthAPIAppAPIPOST /login (username, password)200 OK + JWT (signed with server key)GET /api/admin (Authorization: Bearer <JWT>)Verify signature with pinned key & check exp/iss/aud200 OK or 403 ForbiddenClaims must never be trusted before signature verification succeeds

#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.

Inspecting and testing a lab JWT safely
$ 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, or aud lets expired or staging tokens work on production.
  • Trusted proxy headers: Trusting client-supplied X-Forwarded-For or X-Original-URL headers 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 /dashboard directly.
  • Unchecked account status: Trusting sub or email without 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 header alg for verification choices, and reject none.
  • Use strong keys: Use at least 256-bit random secrets for HS256, keep asymmetric private keys on the auth server, and never trust jwk or jku headers.
  • Validate claims: Enforce exp, nbf, iss, and aud on 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 of verify().
  • Passing a public key to a verifier that accepts both RS256 and HS256.
  • 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.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge