download

A user signed out. Why does their old JWT still work?

Security~10 minReviewed 2 Dec 2026Free · No sign-in

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.

  1. Why does deleting the browser cookie fail to revoke the session?
  2. How do you make logout and password reset revoke access right away?
  3. Which cookie flags protect session cookies in the browser?
"CODE · PYTHON"
def accept_token(claims, now):
    # Signature was already checked by middleware.
    return claims["exp"] > now
Show hints (3)
  1. "CLIENT VERSUS SERVER STATE"

    Clearing a cookie only asks one browser to delete its local copy. Does accept_token consult any server-side state?

  2. "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.

  3. "BROWSER COOKIE FLAGS"

    Think about XSS token theft, plain HTTP traffic, and cross-site requests: HttpOnly, Secure, and SameSite.

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.

"SOLUTION · PYTHON"
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))
"OUTPUT"
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:

designsingle-device logoutpassword reset (all devices)server lookup
7-day stateless JWTNoNoNone
Opaque server session (Redis / DB)Delete sid nowDelete all user sessionsEvery request
Short JWT (5–15 min) + refresh tokenRevoke refresh tokenRevoke refresh tokens or bump token_versionOn 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 (or Strict): 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=0 as logout. A copied token still works until exp passes.
  • Storing session tokens in localStorage. Any XSS bug can read localStorage directly, while an HttpOnly cookie 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_version directly.
  • "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=Lax eliminate the need for CSRF protection everywhere?" No. SameSite=Lax still sends cookies on top-level GET navigations, so GET routes must never change state. For cross-site POST flows or older clients, pair SameSite with anti-CSRF tokens or strict Origin header 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 →

Original OfferVibe question. OWASP Session Management Cheat Sheet · MDN Set-Cookie · RFC 6265 · RFC 7519.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.