Highlights

Contents

Part VIII
08

JWT attacks and mistakes

We go through the ways JWT verification fails in real code. Most JWT breaches come from verifiers that check too little, not from broken cryptography. We will cover missing verification and algorithm confusion, weak secrets and unchecked claims, and token leakage and oversharing payloads.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Session authentication2Logout, devices andrevocation3Password storage4Login attacks anddefences5Password reset6JSON Web Tokens7Access and refresh tokens8JWT attacks and mistakes9OAuth 2.010OpenID Connect and thefull login

#Section 23Missing verification and algorithm confusion

The simplest failure is not verifying at all. Many JWT libraries offer a decode function that parses a token without checking its signature, intended for debugging or for reading the kid before choosing a key. Code that calls decode and then trusts sub accepts any token anyone writes. An attacker takes a real token, changes "sub":"42" to "sub":"1", re-encodes it, and leaves the old signature attached. Nothing checks it. A related bug reads claims before verification, for example taking the iss claim from the unverified payload and using it to decide which key to verify with. The attacker then names an issuer they control and supplies the key too.

Algorithm confusion abuses libraries that let the token choose the algorithm. Wren signs with RS256, and its RSA public key is public by design, published in the JWKS. An attacker writes a token with header {"alg":"HS256"} and payload {"sub":"1","role":"admin"}. They compute an HMAC over it, using the text of Wren’s public key as the HMAC secret. A careless verifier calls verify(token, key), where key is Wren’s public key. It reads HS256 from the header and runs an HMAC with the key it was given, which is the same public key the attacker used, and the signature matches. The attacker is now user 1, with an admin role. This bug, published in 2015, affected several popular libraries at once.

ALGORITHM CONFUSION: THE PUBLIC KEY BECOMES AN HMAC SECRETALGORITHM CONFUSION: THE PUBLIC KEY BECOMES AN HMAC SECRETheader: {"alg":"HS256"}payload: {"sub":"1","role":"admin"}sig: HMAC(public_key_pem, ...)public key,anyone can fetch itnaive library:verify(token, key)trusts "HS256", passesfix: pin the algorithm per key: verify(token, key, algorithms=["RS256"])and reject "alg":"none" outright
Figure 3.35The attacker turns a public key into an HMAC secret. The fix is to never let the token choose.

The older variant is "alg":"none". The JWT specification defines an unsecured token with no signature, intended for contexts where integrity is guaranteed some other way. Some libraries accepted none from any token, so an attacker removed the signature, set the algorithm to none, and was trusted. The fix for both is the same. Configure the expected algorithm per key, as in verify(token, key, algorithms=["RS256"]), reject none outright, and use a library version that requires the algorithms argument, as current versions of PyJWT and jsonwebtoken do.

#Section 24Weak secrets, unchecked claims and long expiry

HS256 tokens are only as strong as their secret. A secret like secret, changeme or wren2026 can be cracked offline from a single captured token. The attacker needs no access to Wren at all. They take the header and payload, try candidate secrets, and check each against the signature, using the same GPU tools used for password cracking, at millions or billions of guesses per second. Once they find the secret, they can mint any token they like. HMAC secrets must be at least 256 random bits, generated by a machine, stored in a secret manager and never committed to a repository. Public scans of GitHub find JWT secrets in code every day.

A correct signature only proves who signed the token. The claims still need checking, and each unchecked claim lets a genuine token work somewhere it should not. Without an iss check, a token from Wren’s staging auth server is accepted in production if the staging key ever ends up in production’s JWKS, which happens more often than anyone admits. Without an aud check, a token issued for Wren’s analytics API, which many internal tools can obtain, opens the payments API. Without exp and nbf checks, old tokens live forever. Allow a little clock skew between servers, around 60 seconds, not hours.

A VALID SIGNATURE IS NOT A VALID TOKEN: FOUR CLAIM CHECKS AFTER ITA VALID SIGNATURE IS NOT A VALID TOKEN: FOUR CLAIM CHECKS AFTER ITissour auth server, not a test oneaudapi.wren.example, not another APIexpnot expired (about 60 s skew)nbfnot before its start timeanalytics APIpayments APIaud: analytics
Figure 3.36Four claim checks after the signature. Each blocks a token that is genuine but meant for something else.

Excessive expiry is the policy version of the same bug. An access token valid for 30 days turns any leak into a month of access, and cannot be revoked without exactly the state JWTs were chosen to avoid. Teams usually pick long expiry because refresh was hard to implement on some client, and then live with it for years. Keep access tokens to minutes and push longevity into refresh tokens, as Part VII described.

#Section 25Token leakage and sensitive payloads

A JWT is a bearer token, meaning whoever holds it can use it, with no further proof. So every place it might be copied matters. Tokens leak through URLs, for example ?access_token=... in a link or a websocket URL, which then lands in access logs, browser history and Referer headers sent to other sites. They leak through error trackers that capture request headers along with a stack trace, through debug log lines that print the Authorization header, through screenshots and screen recordings attached to support tickets, and through browser developer tools on shared machines. Send tokens only in the Authorization header or an HttpOnly cookie, and scrub them in logging middleware.

The payload is readable by anyone who holds the token, which includes every service it reaches, every proxy and gateway on the way, and every log that captured it. Running base64 -d on the middle part shows the JSON. Putting an email address, phone number, home address, date of birth or a detailed internal permission structure into a JWT publishes it to all of them. Under data protection laws such as GDPR, those logs then hold personal data you did not intend to keep. Keep payloads to identifiers and authorization data, and look up personal details from the user id when a service actually needs them. If a payload truly must stay confidential in transit, JWE (JSON Web Encryption, RFC 7516) encrypts it, at the cost of more complexity and key management.

ANYONE WHO SEES A JWT CAN DECODE ITS PAYLOAD; MANY PLACES SEE ITANYONE WHO SEES A JWT CAN DECODE ITS PAYLOAD; MANY PLACES SEE ITeyJ…eyJzdWIiOiI0MiIs…sigURL queryproxy and access logsRefererthird-party siteserror trackerrequest dumpshistoryshared devices{"sub":"42","email":"asha@…"}
Figure 3.37Decoding needs no key. Every place a token passes through can read its claims.

Some systems bind tokens to the client to reduce the value of a leaked copy. With DPoP (Demonstrating Proof of Possession, RFC 9449), the client holds a private key and signs a small proof for each request, and the token is only accepted together with a proof from that key. A token copied out of a log is then useless on its own.