A user signed out. Why does their old JWT still work?
Question
A web app stores a 7-day signed JWT in a browser cookie. When user_101 clicks Sign out or resets a password, the server clears the cookie with Max-Age=0. A test client that copied the token before logout still gets HTTP 200 for 6 more days.
- Why does deleting the browser cookie fail to revoke the session?
- How do you make logout and password reset revoke access right away?
- Which cookie flags protect session cookies in the browser?
def accept_token(claims, now): # Signature was already checked by middleware. return claims["exp"] > now
Show hints (3)
- "CLIENT VERSUS SERVER STATE"
Clearing a cookie only asks one browser to delete its local copy. Does
accept_tokenconsult any server-side state? - "TWO REVOCATION DESIGNS"
Compare server-side opaque session IDs with short-lived access tokens paired with revocable refresh tokens or a per-user
token_version. - "BROWSER COOKIE FLAGS"
Think about XSS token theft, plain HTTP traffic, and cross-site requests:
HttpOnly,Secure, andSameSite.
Show checked explanation
A stateless JWT stays valid until exp because clearing a browser cookie never invalidates a copied token. Use server-side sessions you can delete right away, or pair short-lived access tokens with revocable refresh tokens and a token_version check for password resets. Protect session cookies with HttpOnly, Secure, and SameSite.
SESSIONS = {"s1", "s2"}
USER_VER = {"user_101": 2}
def is_valid(tok, now):
if tok["exp"] <= now or tok["sid"] not in SESSIONS:
return False
return tok["ver"] == USER_VER.get(tok["sub"])
samples = [
("active", {"sub": "user_101", "sid": "s1", "ver": 2, "exp": 900}),
("logout", {"sub": "user_101", "sid": "s9", "ver": 2, "exp": 900}),
("pw_reset", {"sub": "user_101", "sid": "s2", "ver": 1, "exp": 900}),
("expired", {"sub": "user_101", "sid": "s1", "ver": 2, "exp": 50}),
]
for name, tok in samples:
print(name, "->", is_valid(tok, now=100))active -> True logout -> False pw_reset -> False expired -> False
Code run on CPython 3.12.10 against synthetic active, logged-out, password-reset, and expired tokens; official OWASP, MDN, and RFC links checked. Checked by an editor other than the author.
Walkthrough
Sending Set-Cookie: session=; Max-Age=0 only asks a compliant browser to drop its own cookie. It does nothing to the signed JWT string itself. Because accept_token checks only the signature and exp, any copied token stays accepted for all 7 days.
To revoke sessions cleanly, the server must hold state that logout or password reset can change:
| design | single-device logout | password reset (all devices) | server lookup |
|---|---|---|---|
| 7-day stateless JWT | No | No | None |
| Opaque server session (Redis / DB) | Delete sid now | Delete all user sessions | Every request |
| Short JWT (5–15 min) + refresh token | Revoke refresh token | Revoke refresh tokens or bump token_version | On refresh (or cached ver) |
Server-side sessions are the simplest choice for most web apps. Store a random session ID (sid) in Redis or a database. On logout, delete that sid (s9 above fails right away). On password reset or suspected theft, delete every session for user_101 or bump USER_VER["user_101"] from 1 to 2 so s2 with ver: 1 fails on the next call.
Short access token + refresh token fits distributed APIs. Keep access tokens short (5 to 15 minutes) and store refresh tokens server-side so you can revoke them. For sensitive actions like changing a password or billing account, check token_version or session state right away instead of waiting out the access window.
Protect the cookie in the browser. Issue a fresh session ID after login to stop session fixation, and set three flags on Set-Cookie:
HttpOnly: blocks client-side JavaScript (document.cookie) from reading the cookie during XSS.Secure: sends the cookie only over HTTPS, never plain HTTP.SameSite=Lax(orStrict): keeps the browser from sending the cookie on cross-site state-changing requests, cutting CSRF risk.
Common mistakes
- Using a 7-day stateless JWT and treating
Max-Age=0as logout. A copied token still works untilexppasses. - Storing session tokens in
localStorage. Any XSS bug can readlocalStoragedirectly, while anHttpOnlycookie stays hidden from JavaScript. - Revoking only the current session after a password reset. Other signed-in devices stay logged in unless you clear all sessions or bump
token_version. - Checking a Redis denylist on every request while keeping large JWTs. If every call hits Redis anyway, a short opaque session ID is simpler.
Follow-up questions
- "With a 10-minute access token and a revocable refresh token, what happens right after logout?" Without an access-token store check, a copied access token works for up to 10 minutes until it expires and cannot refresh. For high-risk endpoints such as password change or payouts, check live session state or
token_versiondirectly. - "How does rotating a refresh token on every use help detect token theft?" Mark each refresh token as single-use inside a session family. If an already-used refresh token arrives again, treat it as a replay and revoke the whole family so the user signs in fresh.
- "Does
SameSite=Laxeliminate the need for CSRF protection everywhere?" No.SameSite=Laxstill sends cookies on top-levelGETnavigations, soGETroutes must never change state. For cross-site POST flows or older clients, pairSameSitewith anti-CSRF tokens or strictOriginheader checks.
What a strong answer covers
Explains why clearing a cookie leaves a stateless JWT valid. Compares server-side sessions with short access tokens, revocable refresh tokens, and token_version, covers password resets, and sets HttpOnly, Secure, and SameSite.
Open in interactive practice deck →