Lesson 2 of 9 · 22m
Algorithm Confusion: The `alg` Field Is an Attack Surface
alg=none, RS256-to-HS256 downgrade, and why pinning the algorithm server-side is non-negotiable.
The most famous JWT vulnerability class doesn't exploit crypto — it exploits the fact that early libraries trusted the token to tell them how to verify the token. Two attacks defined the genre, and libraries still ship footguns that re-enable them.
Attack 1: alg=none
The JWT spec includes a none algorithm for unsigned tokens. In 2015-era libraries, this flow worked:
// attacker takes any valid token, rewrites the header
{"alg": "none"}
// forges any payload
{"sub": "admin", "role": "admin", "exp": 9999999999}
// signature segment: empty stringEncode, concatenate with a trailing dot, submit. Libraries that treated alg as a trusted instruction happily accepted an unsigned token. The fix in the spec world: none is for a specific use case and every modern library rejects it by default — but I've still seen it accepted in 2023 in a hand-rolled verifier inside a Go microservice. Nobody hand-rolls crypto on purpose; they hand-roll JWT parsing because "it's just base64."
Attack 2: RS256 → HS256 confusion
This one is subtler and still catches teams today. RS256 is asymmetric: the issuer signs with a private key, services verify with a public key — which is, by design, forums. HS256 is symmetric: one secret signs and verifies.
The attack: if the verifier uses the algorithm from the token to decide how to verify, the attacker rewrites alg to HS256 and signs the forged token using the public key as the HMAC secret. Verification passes, because the library did exactly what it was told:
# the vulnerable pattern — alg from token controls verification
alg = header["alg"]
jwt.decode(token, public_key, algorithms=[alg]) # attacker sets alg=HS256The public key is fetchable from the JWKS endpoint — that's its job. So the attacker needs zero secrets to become admin.
The fix is structural, not clever
# correct: the algorithm set is pinned in server config, not negotiated
jwt.decode(token, key, algorithms=["RS256"], ...)One algorithm, pinned, full stop. Keys should also be typed — an RSA public key object should refuse to be used as an HMAC secret. Good libraries (pyjwt ≥ 2.x, golang-jwt v4+, jsonwebtoken in Rust) enforce key-type matching; older or minimal ones don't.
Also: never configure algorithms=["RS256", "HS256"] "for flexibility." That reintroduces the downgrade in one line. If you genuinely support both (you don't), route by kid to a key that already knows its own algorithm.
Field notes
- Test your own verifier: forge an alg=none token and an HS256-confused token in staging. Both must fail with the same generic error as a bad signature — no oracle behavior.
- Watch for verification inside API gateways and service meshes separately from app code — the gateway passing means nothing if the app re-verifies wrong (or doesn't re-verify at all behind it).
- EdDSA (Ed25519) is the modern pick: fast, small keys, no HMAC-confusion cousin, no padding oracles. RS256 survives because of inertia and JWKS tooling.
kidinjection is the same genre:kid: "../../dev/null"or SQL-fragmentkidvalues attacking naive key lookups. Treat the whole header as hostile input.
Was this lesson helpful?