security, , updated

A Practical Guide to JWT Security

JWTs let any service check identity without calling home. That only works if verification is done right. What a JWT really guarantees, why it suits decentralized systems, and the handful of flaws that keep turning into token forgery.

A JSON Web Token (JWT, RFC 7519) is a digital passport: a set of claims about a subject (“user 4821, role admin, valid until 14:15”), stamped by an issuer that everyone trusts. Like a passport, its value depends entirely on how carefully the border officer checks the stamp. Most JWT incidents are not broken cryptography. They are verifiers that accept a forged stamp.

Want to look inside a real token while you read? Paste it into the JWT Decoder. It decodes and verifies in your browser, nothing is sent to a server.

Signed, not encrypted

A JWT has three Base64url-encoded parts separated by dots:

eyJhbGciOiJSUzI1NiIsImtpZCI6ImsxIn0.eyJzdWIiOiI0ODIxIiwicm9sZSI6InVzZXIiLCJleHAiOjE3NjE2NjI1MDB9.kX9…Qw

header     {"alg":"RS256","kid":"k1"}
payload    {"sub":"4821","role":"user","exp":1761662500}
signature  RS256 signature over "header.payload"

The header names the algorithm, the payload carries the claims, and the signature is computed over both. Base64url is an encoding, not a cipher: anyone who holds the token can read every claim.

JWT as JWS (what almost everyone uses)
header payload: sub, role, exp … signature
JWT as JWE (encrypted)
header key IV payload tag
A normal JWT (a JWS) only adds a signature; header and payload stay readable. Hiding the claims needs a JWE, which encrypts the payload.

So a standard JWT (technically a JWS, RFC 7515) gives you:

PropertyProvided?Why
IntegrityYesAny change to header or payload breaks the signature
AuthenticityYesOnly the holder of the signing key can produce a valid signature
ConfidentialityNoThe payload is just encoded; use JWE (RFC 7516) or keep sensitive data out
Freshness / revocationOnly via expA valid token stays valid until it expires

Two consequences follow. First, anything in the payload should be treated as public: tokens end up in logs, proxies, browser storage and support tickets. Second, a token is a bearer credential. Whoever holds it is the subject until exp, so theft is as good as forgery.

Why it works for decentralized systems

The point of signing instead of storing is that verification needs no shared state. With classic sessions, every service has to ask a central store “is this session ID valid?”. With an asymmetrically signed JWT, the issuer signs once with its private key and publishes the public key (usually as a JWKS endpoint). Every service can then verify any token on its own, offline, without calling back to the issuer.

This is why JWTs underpin OpenID Connect, OAuth 2.0 access tokens, service meshes and cross-organization federation: trust is anchored in a key, not in a database.

The same property is the main trade-off. Because no one is asked, no one can say “no” before exp. Revocation needs either short lifetimes (minutes) with refresh tokens checked at the issuer, or a deny list, which brings back the shared state the design avoided.

Symmetric vs. asymmetric

HS256 (HMAC)RS256 / ES256 / EdDSA
KeysOne shared secretPrivate key signs, public key verifies
Who can verifyEveryone holding the secretAnyone with the public key
Who can forgeEveryone who can verifyOnly the issuer
FitsOne service issuing and checking its own tokensAnything distributed or multi-party

In a decentralized setup, HMAC turns every verifier into a potential issuer. Asymmetric signing keeps the ability to mint tokens in one place.

Where things go wrong

Nearly every JWT flaw comes down to one mistake: the verifier trusts the token to tell it how to verify the token. The header is attacker-controlled input.

Algorithm confusion is the most instructive example. A server issues RS256 tokens, and its public key is, by design, public. The attacker sets "alg": "HS256" and computes an HMAC using that public key as the secret. A library that picks the algorithm from the header now verifies an HMAC with the same public key bytes, and the forged token passes.

flowchart LR
    P["Public RSA key
(from JWKS)"] --> X["Attacker signs
alg: HS256
secret = public key"] X --> S{"Verifier reads alg
from header"} S -- "HS256: HMAC with
configured key" --> OK(["Signature matches
forged token accepted"])

The other recurring flaws, in short:

FlawWhat the attacker doesControl
alg: noneRemoves the signature, sometimes with case tricks (nOnE)Allow-list of algorithms, fixed per issuer
Algorithm confusionSigns HS256 with the RS256 public keyPin algorithm and key type server-side
Header-supplied keys (jwk, jku, x5u, kid)Points the verifier at their own key, or injects paths/SQL via kidResolve keys only from a trusted, pre-configured source
Weak HMAC secretCracks the secret offline from one captured token≥ 256-bit random secret, or move to asymmetric
Decode without verifyEdits the payload freelyUse the library’s verify call, never only decode
Claims not checkedReplays expired tokens, or uses a token issued for another serviceValidate exp, nbf, iss, aud on every request

None of these require a cryptographic break. They are configuration and library defaults, which is also why they keep reappearing as CVEs in widely used JWT libraries. RFC 8725 (JWT Best Current Practices) covers all of them and is the right reference for an assessment.

Key takeaways

  • A JWT is signed, not encrypted. It proves who issued it and that it was not changed. It hides nothing.
  • Signing is what makes JWTs suitable for decentralized systems: any party with the public key can verify without contacting the issuer.
  • The cost is revocation: keep lifetimes short and treat stolen tokens as valid credentials.
  • Prefer asymmetric algorithms whenever more than one party verifies.
  • Never let the token choose the algorithm or the key. Pin both on the verifier and validate the claims.

To inspect a token’s header and claims or check its signature against a key, use the JWT Decoder.

§ home
~/knubertus/home/
1,1 Top