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.
So a standard JWT (technically a JWS, RFC 7515) gives you:
| Property | Provided? | Why |
|---|---|---|
| Integrity | Yes | Any change to header or payload breaks the signature |
| Authenticity | Yes | Only the holder of the signing key can produce a valid signature |
| Confidentiality | No | The payload is just encoded; use JWE (RFC 7516) or keep sensitive data out |
| Freshness / revocation | Only via exp | A 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 | |
|---|---|---|
| Keys | One shared secret | Private key signs, public key verifies |
| Who can verify | Everyone holding the secret | Anyone with the public key |
| Who can forge | Everyone who can verify | Only the issuer |
| Fits | One service issuing and checking its own tokens | Anything 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:
| Flaw | What the attacker does | Control |
|---|---|---|
alg: none | Removes the signature, sometimes with case tricks (nOnE) | Allow-list of algorithms, fixed per issuer |
| Algorithm confusion | Signs HS256 with the RS256 public key | Pin 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 kid | Resolve keys only from a trusted, pre-configured source |
| Weak HMAC secret | Cracks the secret offline from one captured token | ≥ 256-bit random secret, or move to asymmetric |
| Decode without verify | Edits the payload freely | Use the library’s verify call, never only decode |
| Claims not checked | Replays expired tokens, or uses a token issued for another service | Validate 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.