Sessions and tokens on the web: cookie session or JWT
A cookie session keeps its state on the server and a JWT inside a signed token, so a session can be revoked at once and a token stays valid until exp. After logout the old identifier must not grant access, and this is checked by replaying a request. For a JWT the expected result is set by the requirement: a short lifetime, a revocable refresh token or a denylist.
QA Fundamentals · Web and client-server
The user logged out, yet the old token in a request still returns data. Is that a bug?
A tester clicks "Log out", then copies a previously saved request in DevTools and sends it again. The response is 200 with profile data. In one product that is a critical bug, in another it is documented behaviour. The difference is the session model, and that is what an interview asks about: how a cookie session differs from a JWT and what you check at logout and when the session expires. The lesson is about the session lifecycle. Storage threats (XSS, CSRF) are covered in the lesson about cookies and localStorage and only mentioned here.
How it works
Cookie session. After login the server creates a session record (user, creation time, last activity, expiry) in its own store and hands the browser a random identifier in a cookie. Every request carries the identifier and the server looks the record up. The state lives on the server.
JWT. After login the server signs a token with claims: who the user is, when it was issued, when it expires (exp, in seconds since the epoch per RFC 7519). Every request carries the token and the server checks the signature and exp without touching a store. The state lives in the token itself. A token is usually only signed (JWS), not encrypted (JWE): anyone can read its content, so secrets are not kept in it.
This gives the main difference for a tester. A session record can be deleted and the identifier stops working at once. A signed token without help from the server is valid until exp, and revoking it earlier needs extra state: a denylist, a token version counter in the database, or a short-lived access token together with a revocable refresh token. Where the token lives (a cookie or an Authorization header) is a separate question: a JWT can perfectly well sit in an HttpOnly cookie.
| What we check | Cookie session | JWT (access token) |
|---|---|---|
| Logout | The record is deleted on the server and the cookie cleared; the old identifier gives 401 | The client drops the token; the old token works until exp unless there is revocation. The expected result is set by the requirement |
| Expiry | The server enforces idle and absolute limits; Max-Age on the cookie is only a hint to the browser | The server rejects the token after exp; then either a refresh or a new login |
| Forced end (password change, ban) | Delete the user’s sessions, effective at once | Needs a revocation mechanism, otherwise valid until exp |
| Tampering | A made-up identifier: 401 | A modified payload or removed signature: 401 |
Example: replaying a request after logout
The simplest check does not need the UI: save a request before logging out and replay it afterwards.
GET /api/profile HTTP/1.1
Cookie: sid=9f3c71
HTTP/1.1 200 OK
POST /api/logout HTTP/1.1
Cookie: sid=9f3c71
HTTP/1.1 204 No Content
Set-Cookie: sid=; Max-Age=0; Path=/; HttpOnly; Secure
GET /api/profile HTTP/1.1
Cookie: sid=9f3c71
HTTP/1.1 401 Unauthorized
If the third response is 200, the server only cleared the cookie in the browser and did not destroy the session: an identifier someone has already copied stays valid. That is a bug. The same replay with a JWT and a 200 is not yet a bug: compare it with the requirement. If the access token lives a few minutes and the refresh token is revoked, that is an acceptable residual window. If the requirement says that logout closes access at once, or the token lives a day and there is no revocation, it is a question for the team, and the decision is recorded in the ticket.
Expiry is not checked by waiting. Ask for a short session or token lifetime in the test environment (a minute), or change the time on the test server, or issue an expired token with a test key. After it lapses, check that the API answers 401, that the UI sends the user to the login and afterwards back to the right page, and that an unsaved form does not vanish silently and there is no endless "401, refresh, 401" loop.
Limits and alternatives
- A JWT is not "less safe" or "better" than a session: it is a trade-off. It removes a store lookup on every request but gives up instant revocation.
- There are in-between models too: opaque tokens verified at the authorization server, a pair of "short access token plus a refresh token with rotation". Their rules come from the product documentation, not from a handbook.
- The codes differ: 401 means there is no valid authentication (no token, expired or invalid), 403 means the user is authenticated but not allowed to act (RFC 9110).
Common mistakes
- Checking logout only in the UI. The button hides the token but does not destroy the session; consequence: the bug goes unnoticed.
- Counting a cleared localStorage as a logout. A token that has already been copied stays valid.
- Not checking a second tab and another device. After a password change the old sessions must end, which shows only by a replay from a second client.
- Mixing up 401 and 403. The client reacts differently to these codes, so the mistake breaks the token refresh.
- Trusting the token content as proof of rights. The server checks the rights, and the token can be forged if the signature is not verified.
Check yourself
The access token lives 15 minutes and logout revokes the refresh token. Ten minutes after logout the old access token still returns 200. Is it a bug?
Answer
Not under this model: it is a documented residual window until exp. Check that the refresh no longer works, that the token gives 401 after exp and that the requirement really allows such a window. For payments the window may be unacceptable, and then it is a question about revocation, not about code.
A user changed the password on device A, and the cookie session on device B stays valid. What do you do?
Answer
Check against the requirement: normally a password change must end the other sessions. Replay a request with the cookie of device B. If the response is 200, the server does not delete sessions on a password change, and that is a bug of high severity, because a stolen identifier survives the password change.
exp unless there is revocation, so the expected result after logout depends on the model and the requirement. I check logout and expiry by replaying a request, a password change from a second device, rejection of a forged token and the UI behaviour after a 401, and I record the accepted token window in the ticket.