🌐 English

Home › Guides

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.

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.

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.

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.

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.