Using JWT safely: what to keep out and how to verify
A JWT is like a signed identity card, but it is easy to mistake it for something encrypted. Starting from the fact that anyone can open it makes it clear what to put inside and what must always be checked. This guide covers the fundamentals of handling it safely.
Anyone can read the contents
A JWT consists of three parts separated by dots (header, payload and signature), and the first two are merely written in the URL-safe form of Base64. It is a readable signed document, not an encrypted one. Whoever holds the token can see its contents in a decoder.
The signature only proves that the contents were not changed on the way; it does not hide them. Decide what to include on the assumption that the token may be exposed as it passes through browsers, logs, address bars and proxies.
Paste a token into the JWT decoder on this site and the header and payload are readable at once. That is how easy it is for anyone.
A token pasted into an issue tracker or chat can be used until it expires, so do not share live tokens.
If you must check a token with an outside service, make sure first that it is expired or only for testing.
What not to put inside
Never include passwords, payment details or national identification numbers. Email addresses and phone numbers are better left out too unless truly needed. A bigger token travels with every request, so size has a running cost as well.
Identity of the user (an identifier), a rough permission level such as a role, and an expiry time are usually enough. Look up everything else on the server using the identifier.
In the payload, sub is the subject, exp is the expiry time and iat is the issue time.
If permissions change often, check the latest values on the server instead of writing them all into the token.
Avoid values that reveal internal system names or structure; they give attackers clues.
Always verify the signature
Decoding and verifying are different things. Being able to read the contents does not mean you can trust them. The server must compute the signature of the received token with its known key and compare, and until then it must not trust the contents.
The header names the signing method, and trusting that value blindly is dangerous. Decide in advance which algorithms the server accepts, and reject everything else, especially none, which means no signature at all.
With the symmetric method the same secret key signs and verifies, so never let that key leave the server side.
With the public key method the private key signs and the public key verifies, so the verifying side needs no secret.
If a key leaks, anyone can forge tokens, so prepare a procedure for rotating keys.
Expiry and revocation
A short exp, on the order of minutes, limits the damage window if a token leaks. To keep users signed in longer, a short access token paired with a longer refresh token is common. Use the server clock for expiry checks and allow a small tolerance if needed.
Once issued, a JWT is not remembered by the server, so cancelling it midway is hard. If logout or account suspension must take effect immediately, add a revocation list or a session check.
exp is Unix time in seconds. Do not confuse it with milliseconds.
Checking aud (audience) and iss (issuer) stops tokens meant for another service from slipping in.
You can design the system so that changing a password invalidates earlier tokens.
Storage and transport
Send tokens only over an encrypted connection (HTTPS) and never in the query string of an address, since addresses are easily stored in histories and logs. Putting the token in a request header is the usual way.
In a browser, each storage place has a different exposure risk. Storage readable by page scripts can be stolen if a malicious script runs, while cookies can switch on protections such as blocking script access and limiting cross-site sending.
For cookies, enable the options for no script access, encrypted connections only, and same-site restriction together.
Configure logging so full request headers are not recorded.
Decide in advance on a response procedure for suspected leaks: rotate the key and invalidate all tokens.