Why Your Bank Logs You Out in 5 Minutes but Other Sites Keep You Signed In for Weeks
You've noticed this your whole life as a user: your banking app signs you out after a few minutes of inactivity, while the forum you registered on three years ago still greets you by name. That's not an accident or a ban
You've noticed this your whole life as a user: your banking app signs you out after a few minutes of inactivity, while the forum you registered on three years ago still greets you by name. That's not an accident or a bank being paranoid for fun. It's two different, deliberate configurations of the same mechanism: session expiration.
This post breaks down how session timeouts actually work, what the OWASP Session Management Cheat Sheet recommends, and how to pick values for your own app.
The cookie underneath
Most web sessions are just a cookie holding a long random ID. The server keeps the real state (who you are, what you're allowed to do) and looks it up by that ID on every request.
Cookies come in two flavors, and the difference matters:
-
Session (non-persistent) cookies have no
ExpiresorMax-Ageattribute. Per MDN's Set-Cookie reference, the browser removes them when the "session" ends, meaning when the browser shuts down. -
Persistent cookies carry
ExpiresorMax-Age(if both are set,Max-Agewins) and get written to disk until that time.
One trap worth knowing: MDN warns that browsers with a session-restore feature will restore session cookies too, as if the browser never closed. So "the cookie dies when the browser closes" is not a security guarantee. OWASP says the same thing and draws the conclusion: never rely on browser behavior to end a session. Expire it on the server.
The two timeouts that decide everything
OWASP recommends every session have both of these:
1. Idle (inactivity) timeout. How long a session stays alive with no requests. This is what your bank is using. The cheat sheet's common ranges:
- 2-5 minutes for high-value applications (banking, admin panels)
- 15-30 minutes for low-risk applications
2. Absolute timeout. The maximum lifetime of a session, no matter how active it is. When it hits, the user must log in again. OWASP's example: if an office worker uses your app all day, a 4-8 hour absolute timeout is reasonable.
The bank is running a short idle timeout. The forum that remembers you for weeks is running a long absolute timeout plus a persistent cookie, and probably refreshes things quietly in the background.
There's also a third, optional mechanism: a renewal timeout, where the session ID itself is swapped for a fresh value periodically while the session stays alive. This limits how long a stolen session ID remains useful, even while the legitimate user keeps working. OWASP suggests it especially when your absolute timeout is long.
Why the server has to enforce it
Two rules the cheat sheet is blunt about:
- Timeout enforcement must be server-side. If the client tracks "minutes since login" (in the token or any client-readable value), an attacker can just edit it and extend the session.
-
When a session ends, invalidate it on both sides. Clearing the cookie client-side is not enough: anyone holding a copy of the session ID can keep using it while the server-side session is still active. On logout or expiry, destroy the server-side session (
session_destroy()in PHP,HttpSession.invalidate()in J2EE,Session.Abandon()in ASP.NET) and send an expired cookie.
The whole point of short timeouts is shrinking the window in which a stolen session ID works. An idle timeout alone doesn't stop an active attacker (they can generate activity to stay alive), which is exactly why the absolute timeout exists as a backstop.
A practical checklist
If you're building session handling today:
- Pick an idle timeout that matches your data's sensitivity: 2-5 min for money and admin tools, 15-30 min for everything else.
- Always set an absolute timeout, even a generous one. "Never expires" is a bug, not a feature.
- Use a session cookie when login shouldn't survive a browser restart, a persistent cookie with
Max-Agewhen you offer "remember me". - Mark session cookies
SecureandHttpOnly(so JavaScript can't read them, mitigating XSS token theft) and setSameSitedeliberately. Note from MDN:SameSite=NonerequiresSecure, and several browsers default toLaxwhen the attribute is missing. - Invalidate server-side on logout and on expiry. Deleting the cookie is cosmetic.
- Consider a renewal timeout if sessions live long.
Sources
- OWASP Cheat Sheet Series, "Session Management Cheat Sheet": https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html
- MDN Web Docs, "Set-Cookie" header reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Set-Cookie
What idle and absolute timeouts have you shipped in production, and did users push back on the short ones or the long ones? Curious what real-world values people ended up with.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.