JWT pitfalls
alg=none, missing expiry, leaked secrets.
JWTs are easy to issue and hard to take back. Most of the trouble comes from that one sentence. This lesson covers the mistakes that show up in real audits, and what Grit already does about each.
A JWT is not encrypted
It is signed. Anyone holding one can read every claim in it, because the payload is base64, not ciphertext. Paste one into any decoder and it reads back in full.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 header.eyJ1c2VyX2lkIjoiMDFhIiwicm9sZSI6IkFETUlOIn0 payload, plain base64.zSRqtZM3OSBQYP8aqKdkWDX8I_nP7WErllS57DfPa8 signature
So a claim is public. An email address in a token is an email address in the browser's storage, in your logs, and in every proxy the request passed through. Put an id in and look the rest up.
The access token cannot be revoked, so make it short
Signature verification is the whole check. The server does not ask a database whether the token is still good, which is exactly why JWTs scale and exactly why "log this user out everywhere" does not work on its own. A stolen access token is valid until it expires, and nothing you do in the meantime changes that.
The answer is not a longer-lived token with a revocation list, which is a session with extra steps. It is a short access token and a refresh token that is backed by a row:
// TokenHash is the SHA-256 of the current refresh token. The token itself// is never persisted, so a dump of this table cannot be replayed as a// session.TokenHash string `gorm:"size:64;uniqueIndex;not null"`// PrevTokenHash is the hash of the token this one replaced. If a client// ever presents THAT token again, the token was captured and replayed// after rotation: the session is killed rather than refreshed.PrevTokenHash string `gorm:"size:64;index"`
Two decisions worth copying even outside Grit:
- Only the hash is stored. Somebody who reads the sessions table gets 64 hex characters, not a key to the account.
- The previous hash is kept on purpose. A refresh token is used once and replaced. If the old one turns up again, two parties hold it, which means it was stolen. The right response is to end the session, not to issue a fresh token to whoever asked last.
That is the difference between detecting theft and merely surviving it. Rotation alone means the thief and the user take turns. Replay detection means the second use gives the theft away.
Where the token lives
If JavaScript can read it, any script on the page can take it, and that includes whatever arrives through a dependency you did not audit. Grit puts tokens in HttpOnly cookies, so there is no Authorization header and nothing for a script to steal.
That choice buys an XSS defence and brings a CSRF obligation with it: a cookie is sent automatically, including on a request another site made. That is the next lesson, and it is why middleware.CSRF() exists.
alg: none, and why the key is named
The original JWT disaster was libraries that trusted the alg header. Send alg: none, drop the signature, and a naive verifier accepts it. Modern libraries refuse, but the lesson underneath still applies: never let the token decide how it is verified.
Grit decides from configuration rather than from the token. HS256 on the shared secret by default, RS256 when a private key is configured, with the public half derived from the private key rather than configured separately, so the two cannot disagree. Tokens name their key so a rotation can verify the previous one while new tokens are signed with the current.
On HS256 the signing key and the verifying key are the same string. Anything that can verify a token can mint one. If a second service needs to verify your tokens, that is the argument for RS256, where it gets the public half only.
The checklist
Claims are readable put an id in, look the rest upAccess tokens are short minutes, not daysRefresh is a row hashed, rotated, replay-detectedTokens are HttpOnly and therefore CSRF is now your problemalg comes from config never from the tokenSecret is 32+ random bytes and not in the repository
Try it
One. Sign in to your project and copy the access token out of the cookie jar. Decode the payload with base64 -d. Write down every field in it and ask of each one: would I publish this?
Two. Refresh the session, then replay the old refresh token. Record the status and what happened to the session.
Three. In one sentence, say why answering that replay with a fresh token would be worse than failing.
Quick check
Spot a typo? Have an idea?
Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled, suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.
Suggest an improvement on GitHub