If you have ever pasted a JSON Web Token into a decoder and watched your user's email appear in plain text, you have already seen the main point of this post. A JWT is signed, not secret. Anyone who holds the token can read what is inside it. The signature only tells the server whether someone changed it.
That difference decides what you can safely put in a token and which checks your server has to run. Everything below comes from two documents: RFC 7519, which defines the JWT, and RFC 8725, the best current practices for using them. Where I give a judgment of my own, I label it as advice.
What is actually inside a token
A JWT is three parts joined by periods. RFC 7519 says each part is a base64url-encoded value. The first part is the header, which names the signing algorithm in a field called alg. The second is the payload, a JSON object of claims such as iss (issuer), sub (subject), aud (audience) and exp (expiration time). The third is the signature.
Base64url is an encoding, not encryption. There is no key involved in turning those first two parts back into JSON. Any person, script or browser extension that sees the token can read the header and the claims.
What the signature gives you
A valid signature means the header and payload have not been changed since someone with the signing key produced them. That is integrity. It is worth having, because it lets your server trust a claim like sub without a database lookup.
It does not give you secrecy. It also does not tell you the token is meant for your service, that it is still fresh, or that the algorithm in the header is one you wanted to accept. Those are separate checks, and your code has to make them.
What to keep out of the claims
RFC 7519 has a section on privacy. It says a JWT may contain privacy-sensitive information, that measures must be taken to keep it from unintended parties, and that omitting privacy-sensitive information from a JWT is the simplest way to avoid the problem. It also names two other routes: use an encrypted JWT, or make sure the token only travels over TLS.
My advice, which is a judgment and not a rule from the RFC: put an opaque user ID in the token and look up the rest on the server. Leave out email addresses, phone numbers, internal notes, role names you would not want a stranger to read, and anything that would be awkward in a screenshot. Treat every claim as public to whoever holds the token.
Checks your server still has to make
RFC 8725 lists the attacks that came from skipping these checks. Four are worth knowing.
Pin the algorithm. The header says which algorithm signed the token, and a careless library may believe it. RFC 8725 section 2.1 describes two real failure modes. An attacker changes alg to none and some libraries accept the token without checking any signature. Or an attacker changes an RSA token to an HMAC one, and a library tries to verify it using the RSA public key as the shared secret. The fix in section 3.1 is to tell the library exactly which algorithms you accept, and to use each key with exactly one algorithm.
Use a strong key. Section 2.2 warns that if you sign with HS256 and a weak, human-memorable secret, an attacker who gets one token can try guesses offline until the signature matches. Use a random secret with enough entropy, not a password.
Check who issued it and who it is for. Section 3.8 says that when a token has an iss claim, you must confirm the keys used belong to that issuer. Section 3.9 says that if one issuer can create tokens for more than one application, the token must carry an aud claim and each application must check that its own identifier is in it. Without that, a token meant for one service can be replayed at another.
Check expiry. RFC 7519 section 4.1.4 says the current time must be before exp, and that a token must not be accepted on or after it. Make sure your library enforces it. My advice: keep lifetimes short, because a signed token cannot be recalled by the signature alone once it has leaked.
One more from section 3.10: values in the header are untrusted input too. A kid used for key lookup can open the door to SQL or LDAP injection, and a jku or x5u URL that you follow blindly can lead to server-side request forgery. Validate them against an allowlist.
A short checklist
- Assume anyone with the token can read every claim.
- Keep sensitive data out of the payload. Use an opaque ID.
- Tell your library which algorithms to accept. Never let the header decide.
- Sign with a strong key.
- Check
iss,audandexpon every request. - Treat
kid,jkuandx5uas untrusted input.
If you remember one line, make it the first one. A signed token proves nobody edited it. It says nothing about who else has read it.
Sources: RFC 7519, JSON Web Token and RFC 8725, JWT Best Current Practices.
More writing on startups and AI: https://startupwiki.substack.com
Cover photo by Mick Haupt on Unsplash.

Top comments (0)