Session management
HTTP is stateless. Each request arrives on its own, and the server needs a value from the browser to tie it to an earlier sign-in.1 Session management is the part of a web application that keeps that link: it creates a session when the user signs in, sends proof of it with each request, decides how long it stays valid and ends it. While a session is valid, the value that represents it is worth as much as the strongest method used to sign in, password and second factor included, so a copied value lets another person act as the user (Session hijacking).1
Session identifiers
In the classic design the server keeps the session record, with the user ID, role and timestamps, in memory or a database, and the browser holds only a random identifier that points to it. The Express middleware express-session works this way: its cookie carries the session ID and the data stays on the server.2
The identifier needs at least 64 bits of entropy from a CSPRNG, which is at least 16 hexadecimal characters when every character is random. At that size, an attacker trying 10,000 guesses per second against an application with 100,000 live sessions would need about 585 years on average to hit one. An application that generates its own IDs should use at least 128 bits. The value itself carries no meaning, so decoding it reveals nothing about the user.1 Default cookie names such as PHPSESSID, JSESSIONID and connect.sid name the framework behind the site.12
Stateless sessions and tokens
In a stateless design the browser holds the session data itself, signed or encrypted so that it cannot be altered, usually as a JSON Web Token. Next.js supports both designs: a stateless session kept in a cookie, and a database session in which the browser receives only an encrypted session ID. Its guide rates the first as simpler and the second as more secure, at the price of more complexity and server work.3
Supabase Auth combines the two. A session is a short-lived access token, a JWT valid for between 5 minutes and 1 hour, plus a refresh token that never expires but can be exchanged only once for a new pair. Each access token holds a session_id claim that matches a row in the auth.sessions table, and signing out deletes that row.4 Supabase chose this design so that APIs can check a session from the access token without asking a central server; a server that needs to know whether the user has since signed out looks up the session_id row.4
Cookie attributes
The server sets the cookie with the Set-Cookie header. Its attributes decide where the browser sends it and who can read it:5
| Attribute | Effect |
|---|---|
HttpOnly | Scripts cannot read the cookie through document.cookie; it is still sent with fetch() requests. |
Secure | Sent only over https, except on localhost. |
SameSite=Strict or Lax | Strict withholds the cookie from every request started by another site; Lax sends it only on top-level navigation with a safe method such as GET. |
Domain | Shares the cookie with every subdomain. Leaving it out keeps the cookie on the host that set it. |
Max-Age, Expires | Make the cookie persistent. Without them it is a session cookie, deleted when the browser closes, unless session restore brings it back. Max-Age wins when both are set. |
__Host- name prefix | The browser accepts the cookie only with Secure, Path=/ and no Domain. |
A session cookie without HttpOnly can be stolen through cross-site scripting, so the attribute is mandatory, and the __Host- prefix suits session IDs. SameSite is set explicitly, Strict where possible, because browser defaults differ; it adds a layer on top of a CSRF token.1 The Path attribute is not a security boundary.5 A session cookie with all of these set:
Set-Cookie: __Host-SessionID=<value>; Path=/; Secure; HttpOnly; SameSite=StrictTokens kept in localStorage or sessionStorage lack all of these protections. Any script running on the page can read them, so one cross-site scripting flaw exposes every token, and localStorage survives browser restarts.1
Lifetime
Two limits bound a session. The idle timeout ends it after a period with no requests; the absolute timeout ends it a fixed time after sign-in, however active it is. The idle timeout alone does not stop an attacker who holds a stolen session, because periodic requests keep it alive; the absolute timeout caps how long a stolen session works. Both have to be checked on the server, since a time kept on the client can be changed.1
Common idle timeouts are 2 to 5 minutes for high-value applications and 15 to 30 minutes for low-risk ones; an application used through a working day typically gets an absolute timeout of 4 to 8 hours.1 NIST SP 800-63B-4 sets ceilings by assurance level: an overall timeout of at most 30 days at the basic level, at most 24 hours with a 1-hour inactivity timeout at the level that requires two factors, and 12 hours with 15 minutes at the highest.6
Supabase sessions last indefinitely by default. On Pro plans and above, Time-box user sessions, Inactivity timeout and Single session per user in the Auth settings limit them; the check runs at the next refresh, so the real lifetime is the timeout plus the access token's expiry. The default access token expiry of 1 hour is set under Auth settings > Sessions.4
Renewal and sign-out
The session ID is replaced whenever the user's privilege changes: at sign-in, after a password change, and when switching to an administrator role. Keeping the pre-sign-in ID allows session fixation, in which the attacker plants a known ID in the victim's browser before the victim signs in.1 At sign-out and at expiry the server deletes its record. Clearing the cookie alone leaves any copied ID working.1
// Express with express-session
app.set("trust proxy", 1);
app.use(session({
secret: process.env.SESSION_SECRET,
cookie: { httpOnly: true, secure: true, sameSite: "strict" },
store: productionStore, // the default MemoryStore is for development only
}));
// after the password is checked
req.session.regenerate((err) => { /* store the user ID, then save */ });
// sign-out
req.session.destroy((err) => { res.redirect("/"); });express-session sets HttpOnly by default but not Secure, and its default MemoryStore leaks memory and does not scale past one process.2 In Next.js, the session cookie is set on the server with cookies().set() and the options httpOnly: true, secure: true, sameSite: 'lax', path: '/' and an expiry; a database session also makes it possible to sign a user out on all devices.3 In PHP, session_regenerate_id(true) replaces the ID at sign-in.1
In SecFlash
SecFlash reports a session cookie without HttpOnly, Secure or SameSite as SF-SEC-501 on the Security page, and the attack scan reports each one as SF-SEC-628, SF-SEC-629 or SF-SEC-630. A login token in localStorage or sessionStorage shows as SF-SEC-503, a session ID in the address as SF-SEC-654, and a sign-in token in unencrypted storage of a mobile app as SF-SEC-730, all on the Security page.
See also
- Authentication
- Session hijacking
- JSON Web Token
- Cross-site request forgery
- Cross-site scripting
- Multi-factor authentication
- Authorization
References
- a b c d e f g h i j k "Session Management Cheat Sheet". OWASP Cheat Sheet Series. OWASP Foundation.
- a b c "express-session". GitHub. Express.js project.
- a b "How to implement authentication in Next.js". Next.js Docs. Vercel.
- a b c "User sessions". Supabase Docs. Supabase.
- a b "Set-Cookie header". MDN Web Docs. Mozilla.
- back National Institute of Standards and Technology (August 2025). "NIST Special Publication 800-63B-4: Authentication and Authenticator Management". Digital Identity Guidelines. NIST.
External links
- Session Management Cheat Sheet, OWASP Cheat Sheet Series
- User sessions, Supabase Docs