Keeping a signed-in session safe
After someone signs in, a website or system has to keep recognising them on every later click without asking for the password again. That continuing recognition is a session, and it rests on a short secret held by the user's device. No background is assumed. A security professional has to know how sessions are stolen, how long they may last and how they are ended, because a stolen session skips sign-in entirely.
By the end of this unit you will be able to:
- Explain how a session token keeps a user signed in after authentication
- Tell apart an idle timeout and an absolute timeout, and give the NIST limits for each level
- Tell apart session hijacking, session fixation and replay
- Choose the cookie attributes and server actions that protect a session token
- Explain why closing a browser window does not end a session
A closed tab that stayed signed in
An illustrative example
A school office assistant uses a computer in the public library to update a pupil's address in the school's records system. When she has finished, she closes the tab and walks away.
Twenty minutes later a teenager sits down at the same computer. He opens the browser's history, clicks the records system, and the page opens on the pupil list. He is signed in as the assistant, and nobody has typed a password.
She closed the page. What was still alive that let the next person carry on as her?
A session is the system remembering a sign-in
A browser is the program a person uses to view websites, such as Chrome, Safari or Edge. Each click sends a fresh request to the website's server, and nothing in the request itself says who sent it.
Asking for the password on every click would be unbearable. So after a successful sign-in the server creates a session: a period during which it treats a run of requests as coming from one authenticated user. It hands the browser a session token, also called a session identifier or session ID. This is a long random value that the browser sends back with every request.
The token usually travels in a cookie, a small named value that a website asks the browser to store and to return on each later visit. The server looks the token up, finds the session and knows who is asking.
A cloakroom ticket
At a theatre cloakroom a guest hands over a coat once and gets a numbered ticket. Later, whoever holds the ticket collects the coat. The attendant checks the ticket, not the face.
A session token works the same way. Anyone who presents it is treated as the user who signed in.
That is the whole . A session token is a bearer secret: possession alone is enough. An attacker who obtains it does not need the password or the second factor, because sign-in has already happened.
Two clocks: the idle timeout and the absolute timeout
A session should not last for ever. Two clocks limit it, and the session ends when either runs out.
- Idle timeout
- Also called an inactivity timeout. The session ends after a set time with no activity. Each request restarts the clock. It protects a device left unattended.
- Absolute timeout
- Also called an overall timeout. The session ends a set time after sign-in, however busy the user is. It limits how long a stolen token stays useful.
When a clock runs out, the user must authenticate again. A successful reauthentication restarts both clocks. NIST sets the limits by (AAL), the strength of sign-in that a system needs.
| Level | Absolute timeout | Idle timeout |
|---|---|---|
| AAL1 | No more than 30 days | Not required |
| AAL2 | No more than 24 hours (revision 3: 12 hours) | No more than 1 hour (revision 3: 30 minutes) |
| AAL3 | No more than 12 hours | No more than 15 minutes |
Revision 4 lets a system that reaches the idle limit, before the absolute limit, accept the password or biometric alone with the existing session secret, instead of full sign-in.
A screen lock is the same idea applied to the whole computer. After a set idle time, or when the user locks it on leaving, the screen hides its contents and stays locked until the user authenticates again. A screen lock is not a substitute for signing out at the end of the day.
Three ways a session is stolen: hijacking, fixation and replay
Session hijacking is taking over a user's session by obtaining a valid token. The attacker then acts as the user. Tokens are captured from an unencrypted connection, read by a malicious script planted in a page, guessed when they are not random enough, or copied from the device.
Session fixation works the other way round. The attacker first obtains a token from the website, then tricks the victim into signing in while using that same token, for example through a crafted link. If the website keeps the token after sign-in, the attacker's copy now belongs to an authenticated session.
Replay is capturing a valid message or token and sending it again later, so that the receiver accepts it a second time.
| Attack | What the attacker does | Main defence |
|---|---|---|
| Hijacking by capture | Reads the token on its way across a network | Send the token only over connections |
| Hijacking by script | Plants code in a page that reads the cookie | Forbid scripts from reading the session cookie |
| Hijacking by guessing | Predicts a token from a pattern | At least 64 bits of randomness in every token |
| Fixation | Plants a known token before the victim signs in | Issue a new token at sign-in and discard the old |
| Replay | Resends a captured token or message later | Short lifetimes, and invalidation at sign-out |
Protecting the session token in the browser
A website protects its session cookie by setting cookie attributes: instructions, sent with the cookie, that tell the browser when it may return or reveal the cookie.
- Secure
- The browser sends the cookie only over an encrypted connection (HTTPS, the encrypted form of the web's protocol). An eavesdropper on the network cannot read it.
- HttpOnly
- Scripts running in the page cannot read the cookie. Code planted in a page cannot copy the token, though it may still act within the page.
- SameSite
- The browser withholds the cookie from requests that another website starts. This hinders a hostile site from making requests that ride on the user's session. A site here means the scheme and the registered domain, so shop.example.com and blog.example.com count as one site. A request started from any address under that domain still carries the cookie.
- Domain and Path
- Limit which addresses on the site receive the cookie. Narrow is safer.
- No expiry date
- A cookie without one is held in memory and discarded when the browser fully closes. One with an expiry date is written to disk and survives a restart.
The server's duties matter as much. Tokens come from a proper random generator and are at least 64 bits long. A new token is issued at sign-in and whenever the user's privilege level changes, which defeats fixation. Tokens are not kept in places that page scripts can read.
Before a sensitive change, such as a new password, a new email address or a new payee, a well-built system asks the user to authenticate again. A stolen session then cannot complete the change that would lock the real user out.
Ending a session happens at the server
The token is only a reference. The session itself is a record on the server. Closing a tab, or even deleting the cookie, leaves that record valid, and any copy of the token still works.
Signing out properly means the server invalidates the session: it marks the token as dead so that no copy is accepted again. A sign-out button that only clears the browser has done half the job. The library computer in the opening case kept a live token, and the server had not reached its idle timeout.
Concurrent sessions are several sessions open at once for one account, for example on a laptop and a phone. An organisation can cap the number, especially for administrators. A website can end the older session when a new one starts, or show the user a list of active sessions with a way to end any of them.
Common misconception
“Closing the browser window signs the user out.”
The session lives on the server until a timeout ends it or the server invalidates it. A token kept in browser history, on disk or in an attacker's hands keeps working. Only sign-out at the server, or a timeout, ends it.
A session ends by timeout or by server sign-out
Read this figure as text
A session is a record on the server, and the session token is a bearer secret that stands for it. The figure follows one session from sign-in to the only three things that end it.
- The user signs in. The server issues a new random session token, discarding any token the browser held before, which defeats session fixation. The token travels in a cookie marked Secure, HttpOnly and SameSite.
- The browser returns the token with every request. Each request restarts the idle clock but not the absolute clock.
- A copy of the token, taken from the network, by a script in the page or from the device, is accepted like the real one. Session hijacking works until the session ends.
- Three things end the session. The idle timeout ends it after a period with no activity, no more than 15 minutes at AAL3. The absolute timeout ends it a fixed time after sign-in, no more than 12 hours at AAL3, however active the user is. Sign-out at the server marks the token dead so that no copy is accepted again.
- When a timeout ends the session the user must authenticate again, and a successful reauthentication restarts both clocks. Closing the window or deleting the cookie does not end the session.
A question, worked through
An online pharmacy finds that several customer accounts placed orders their owners never made. Investigators show that a script hidden in a product review read each visitor's session cookie and sent it to the attacker, who used the cookies within minutes. The site already uses encrypted connections everywhere. The security manager must recommend one change.
Which change would BEST stop this attack?
Decide on your answer, then open the reasoning
- The question asks for the change that stops this theft. The token was read by a script inside the page, then reused. That is session hijacking.
- Rule out a: multi-factor authentication protects sign-in. The attacker never signed in. He presented a token issued after the real customer had already passed both factors.
- Rule out c: the cookies were used within minutes. A shorter absolute timeout narrows the window and leaves the theft working.
- Between b and d: the Secure attribute stops a cookie being read on the network. The site is already encrypted, and the theft happened inside the page, not on the network.
- The HttpOnly attribute stops scripts in the page reading the cookie, which is the exact path used. Option b is the best answer. Removing the planted script is the other half of the repair.
Answer: B. Mark the session cookie so page scripts cannot read it
Choosing between good answers
- A token is copied and used without the password
- the attack is session hijacking. Look for answers that protect the token, not answers that strengthen sign-in.
- The attacker supplies the token before the victim signs in
- it is session fixation. The fix is a new token issued at sign-in.
- A question asks what limits the damage from a token stolen while the user is busy
- choose the absolute timeout. An idle timeout never fires while the session is in use.
- An answer says the user closed the browser and was therefore signed out
- reject it. Only invalidation at the server, or a timeout, ends a session.
Check your understanding
5 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.
These questions are drafts that no named reviewer has approved yet.
Question 1 of 5
What to remember
- A session token is a bearer secret: whoever presents it is treated as the user, with no password and no second factor.
- An idle timeout ends a session after inactivity. An absolute timeout ends it a fixed time after sign-in, however active the user is.
- NIST revision 4 limits AAL2 sessions to 24 hours with a 1-hour idle limit, and AAL3 sessions to 12 hours with a 15-minute idle limit.
- Hijacking steals a token, fixation plants one before sign-in, and replay reuses a captured one. A new token at sign-in defeats fixation.
- Secure keeps the session cookie off unencrypted connections, HttpOnly keeps it from page scripts, and SameSite withholds it from requests other sites start.
- Sign-out must invalidate the session at the server. Closing the window does not end it.
Say it from memory
How is a signed-in session kept safe from start to finish?
The server issues a random session token of at least 64 bits and replaces it at sign-in, which defeats fixation. The cookie carrying it is marked Secure, HttpOnly and SameSite against hijacking. An idle timeout and an absolute timeout end the session, with sensitive changes needing reauthentication. Sign-out invalidates the token at the server, and concurrent sessions can be limited.
What is a session token, how does it usually travel, and why is it a bearer secret?
A long random value the server hands the browser after sign-in, sent back with every request so the server knows who is asking. It usually travels in a cookie. It is a bearer secret because possession alone is enough: an attacker who obtains it needs neither the password nor the second factor.
How does an idle timeout differ from an absolute timeout, and what are the AAL2 limits in NIST SP 800-63B revision 4?
An idle timeout ends the session after a set time with no activity, and each request restarts it; it protects an unattended device. An absolute timeout ends the session a set time after sign-in, however busy the user is; it limits how long a stolen token stays useful. At AAL2: absolute no more than 24 hours, idle no more than 1 hour.
How do session hijacking, session fixation and replay differ, and what defeats fixation?
Hijacking takes over a session by obtaining a valid token. Fixation plants a token the attacker already holds, then tricks the victim into signing in with it. Replay captures a valid message or token and sends it again later. Issuing a new token at sign-in and discarding the old defeats fixation.
Why does closing the browser not sign a user out, and what does proper sign-out do?
The session is a record on the server, and the token is only a reference to it. Closing a tab or deleting the cookie leaves the record valid, so any copy of the token still works. Proper sign-out has the server invalidate the session, marking the token as dead so no copy is accepted again.
Sources
- NIST SP 800-63B-4: Authentication and Authenticator Management, NISTThe revision 4 reauthentication limits for AAL1, AAL2 and AAL3, the rule that either timeout ends the session and reauthentication resets both, reauthentication by password or biometric after an idle timeout, session secrets of at least 64 bits, Secure and HttpOnly session cookies, and erasing or invalidating secrets at sign-out.
- NIST SP 800-63B: Digital Identity Guidelines, Authentication and Lifecycle Management (revision 3), National Institute of Standards and TechnologyThe revision 3 reauthentication limits: 12 hours and 30 minutes of inactivity at AAL2, 12 hours and 15 minutes at AAL3, and 30 days at AAL1.
- OWASP Cheat Sheet Series: Session Management, OWASP FoundationSession hijacking and fixation, at least 64 bits of entropy in session identifiers, the Secure, HttpOnly, SameSite, Domain, Path and expiry attributes, renewing the identifier after any privilege change, invalidating the session at the server on sign-out, and choices for simultaneous sessions.
- OWASP Cheat Sheet Series: Authentication, OWASP FoundationRequiring the current credentials again before sensitive account changes such as a new password or email address.
- Cookies: HTTP State Management Mechanism (draft-ietf-httpbis-rfc6265bis-22), IETF HTTP Working GroupThe SameSite attribute attaches a cookie only to same-site requests (section 4.1.2.7), where a site is the scheme plus the registrable domain, the public suffix and the label to its left (section 2.3), so addresses under one registrable domain are same-site.
- RFC 6265: HTTP State Management Mechanism, IETFCookies as name and value pairs that a server asks the browser to return, and the Secure and HttpOnly attributes.
- SP 800-53 Rev. 5, Security and Privacy Controls for Information Systems and Organizations, NISTConcurrent session limits (AC-10), device lock after inactivity that is not a substitute for logging out (AC-11), session termination (AC-12), and invalidating session identifiers at logout against hijacking (SC-23).