Skip to content

Lesson 10 of 13 · 20m

JWTs and Signed Tokens: alg Confusion, kid Injection, and Claims That Lie

A JWT is a signed assertion, not an encrypted blob — pin the algorithm, fix the key, validate the claims, or ship a bypass.

Your API takes a header, Authorization: Bearer eyJhbGciOi..., base64-decodes it, checks a signature, and trusts {user_id, role} for the rest of the request. That three-part blob is a JWT — and every layer of it has shipped a production bypass: the alg field attackers rewrite, the kid they point at their own key, the claims nobody validated. The token format is fine. The verification story is where bodies get buried.

What a JWT actually is

Three base64url parts: header.payload.signature. The header says {"alg":"ES256","typ":"JWT"}. The payload is a JSON blob of claims — iss, aud, sub, exp, iat, your custom fields. The signature is over header + "." + payload, keyed or asymmetric depending on alg.

A JWT is a signed assertion, not an encrypted container — jwt.io and any base64 decoder read the payload in cleartext. The wrong mental model is "the token is protected." It's attested. Everything in it is public-readable; the signature only proves who wrote it.

The failure catalog that keeps shipping

  • alg: none. The header can literally declare no signature, and early library versions accepted it — strip the signature, flip alg to none, become admin. Every modern library bans it; the check that matters is your code rejecting unsigned tokens, not the library's marketing.
  • Algorithm confusion (RS256 → HS256). Your verifier holds the RSA public key for signature checks. If the library lets the token's alg pick the algorithm, an attacker flips alg to HS256 and signs with... your public key, which is public. The fix is pinning the expected algorithm in the verifier (jwt.verify(token, key, algorithms=["ES256"])), never trusting the header.
  • kid injection. The kid header says which key verified it — and if your code does keys[token.kid] against a filesystem or a fetch, an attacker sets kid: "../../dev/null" or kid: "http://evil/k" and supplies the key themselves. kid selects from a fixed allowlist of keys you control; it never resolves.
  • Unvalidated claims. Signature valid, exp unchecked → a token from 2019 works forever. aud unchecked → a token minted for your staging API works on prod. iss unchecked → any issuer's token works. Verification = signature and claims.

What correct verification looks like

import jwt   # PyJWT

token = request.headers["Authorization"].removeprefix("Bearer ")

claims = jwt.decode(
    token,
    key=JWKS["ES256"],                  # the key *you* chose for ES256
    algorithms=["ES256"],               # pinned — not read from the token
    audience="api://prod",              # aud must match, or it fails
    issuer="https://auth.example.com",  # iss must match
    options={"require": ["exp", "iat", "iss", "aud", "sub"]},
)
# claims is now trustworthy — exp was already checked by decode()

Four decisions in that call are security decisions: the pinned algorithm, the fixed key, the required audience, the required issuer. Miss one and the signature check is decoration.

When a JWT is the wrong tool

JWTs sell statelessness — no lookup to verify. The flip side: you can't revoke one. A stolen token works until exp; logout can't kill it; banning the user doesn't help until the token ages out. That's why sessions still use opaque tokens (secrets.token_urlsafe(32) → Authorization header → server-side lookup → row exists or it doesn't): revocable, no claims to validate, no algorithm to confuse.

Use JWTs when stateless verification is genuinely load-bearing — short-lived service-to-service calls, third-party OIDC login where Google/GitHub is the issuer, signed URLs. Keep exp short (minutes, not days) and prefer asymmetric (ES256, EdDSA) over HS256 for anything crossing trust boundaries — a shared HMAC key means every verifier can also mint.

Field notes

  • kid, jku, x5u headers all point at key material — treat them as attacker input that selects from your pinned set, never as resolvable references.

  • jwt.decode without algorithms= is the bug. Most CVE-era JWT bypasses were exactly this, one parameter wide.

  • Refresh tokens and access tokens are different animals: access = short-lived JWT, refresh = long-lived opaque, revocable, rotated on use.

  • Storing JWTs in localStorage hands them to any XSS. httpOnly, Secure, SameSite cookies are the harder target — and CSRF becomes the thing to solve instead.

  • kid used correctly looks like JWKS: pin the issuer's /.well-known/jwks.json, cache the keyset, and let kid choose within that pinned set. The bug is resolving attacker-supplied locations — not the existence of kid.

  • Clock skew is real, and libraries take a leeway on exp/nbf. Two minutes is sane; disabling expiry checks is not a leeway setting.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.