JSON Web Tokens (JWT), standardized in RFC 7519, are the de facto standard for stateless authorization in modern microservices, single-page applications (SPAs), and OAuth 2.0 / OpenID Connect flows. However, subtle implementation mistakes can lead to total authentication bypass.
1. Anatomy of a JSON Web Token
A compact JWT consists of three distinct URL-safe Base64-encoded strings joined by period (.) delimiters:
[Base64URL Header] . [Base64URL Payload] . [Base64URL Signature]
- Header: Specifies the token type (
typ: "JWT") and the cryptographic signing algorithm (alg: "RS256","ES256", or"HS256"). - Payload: Contains JSON claims asserting identity, permissions, and session lifecycle metadata.
- Signature: Computed by hashing the header and payload with a secret key or private key. Ensures data integrity and authenticity.
2. The Infamous "none" Algorithm Flaw
RFC 7519 permits an unsecured token by setting {"alg": "none"} in the header, in which case the signature component is empty. Vulnerable JWT libraries in early implementations blindly trusted the header's alg field.
An attacker could take an authentic token, modify the payload to set "role": "admin", change the header to {"alg": "none"}, drop the signature, and submit it to the server. If the server used an unpatched library, it would accept the forged claims as valid without checking any signature.
algorithms = ["RS256"]). Never permit runtime algorithm switching from client-controlled headers.
3. Key Confusion Attacks (RS256 vs HS256)
The Key Confusion (or Algorithm Confusion) attack is one of the most elegant and dangerous vulnerabilities in token verification. It occurs when a backend system accepts both asymmetric (RS256) and symmetric (HS256) tokens:
- The backend issues tokens signed with its RSA private key (RS256). The corresponding RSA public key is publicly accessible (e.g., via
/.well-known/jwks.json). - The verification handler uses a single function like
jwt.verify(token, key)wherekeyis loaded as the RSA public key. - An attacker crafts a malicious token, sets the header algorithm to
"HS256", and signs the token using the server's public key text as the HMAC secret! - When the backend verifies the token, it reads
alg: HS256from the header, assumes the supplied key is an HMAC symmetric secret, and recalculatesHMAC-SHA256(header.payload, rsa_public_key). - The signature matches perfectly, granting the attacker arbitrary forged privileges!
Prevention: Strictly enforce algorithm verification in your code: if a route expects an asymmetric signature, never allow an HMAC algorithm to execute against an asymmetric public key string.
4. Standard Registered Claims Checklist
To ensure token safety, your verification pipeline must validate all registered claims:
exp(Expiration Time): Reject tokens where current timestamp ≥exp. Allow a tiny clock skew tolerance (e.g., 30–60 seconds).nbf(Not Before): Ensure the token is not being processed prior to its activation window.iat(Issued At): Verify the issuance date is not in the future.iss(Issuer): Match the issuer URI against your expected authentication authority.aud(Audience): Ensure your microservice or application client ID is listed in the audience array.
Put This Guide into Practice
Test your certificates, keys, or tokens in-memory with our zero-disk developer tool.