Secure Session Management: Cookies, SameSite, Rotation and Logout
Implement secure web sessions: HttpOnly, Secure and SameSite cookies, session ID rotation, timeouts, CSRF protection, logout everywhere and storage choices.
Published December 4, 2026
After a user logs in, every subsequent request must prove it comes from that user. Sessions do this, and they are a favourite target: steal a session and you skip the password, MFA and passkey entirely. Getting cookie attributes, rotation, expiry and logout right closes most of the doors attackers use.
The concept
A classic server-side session stores state on the server (memory, Redis, database) and gives the browser an unguessable session ID in a cookie. Cookie attributes do much of the security work: HttpOnly prevents JavaScript from reading the cookie (limiting XSS theft), Secure sends it only over HTTPS, SameSite=Lax or Strict stops it being sent on most cross-site requests (mitigating CSRF), and the __Host- prefix forces Secure, Path=/ and no Domain attribute.
Lifecycle rules matter as much as attributes. Rotate the session ID on login and privilege changes to prevent session fixation. Apply idle and absolute timeouts. On logout, destroy the server-side session, not just the cookie. Offer "log out of all devices" and invalidate sessions on password changes. For state-changing requests, combine SameSite with CSRF tokens or origin checks.
Cookie attributes
| Attribute | Value | Protects against |
|---|---|---|
| HttpOnly | set | Session theft via XSS |
| Secure | set | Interception over plain HTTP |
| SameSite | Lax (or Strict) | Most CSRF attacks |
| __Host- prefix | name prefix | Cookie injection from subdomains |
| Max-Age / Expires | bounded | Indefinitely valid sessions |
| Path=/ | set | Scope surprises |
Session lifecycle
Timeouts and re-authentication
| Control | Typical value |
|---|---|
| Idle timeout | 15-60 minutes (shorter for admin) |
| Absolute timeout | 8-24 hours, or days with re-auth for sensitive actions |
| Re-authentication | Required for password/email change, payments |
| Concurrent session listing | Show devices; allow revoke |
Practical examples
A hardened session cookie header
What the response should look like.
Set-Cookie: __Host-sid=8f2c1a9e...; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=28800Express with express-session and Redis
Regenerate on login, destroy on logout.
app.use(session({
store: new RedisStore({ client: redis }),
name: '__Host-sid', secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false,
cookie: { httpOnly: true, secure: true, sameSite: 'lax', maxAge: 8 * 3600 * 1000 },
}))
app.post('/login', async (req, res, next) => {
const user = await authenticate(req.body)
req.session.regenerate(err => { if (err) return next(err); req.session.userId = user.id; res.sendStatus(204) })
})
app.post('/logout', (req, res) => req.session.destroy(() => res.clearCookie('__Host-sid').sendStatus(204)))Laravel session settings
config/session.php equivalents via environment.
SESSION_DRIVER=redis
SESSION_LIFETIME=60
SESSION_SECURE_COOKIE=true
SESSION_SAME_SITE=lax
# and call $request->session()->regenerate() after loginCommon mistakes
- Not regenerating the session ID after login (session fixation).
- Logging out by deleting only the cookie while the server session stays valid.
- SameSite=None without a real cross-site need.
- Very long sessions without re-authentication for sensitive actions.
Key takeaways
- Use HttpOnly, Secure, SameSite cookies, ideally with the __Host- prefix.
- Rotate session IDs on login and privilege changes.
- Enforce idle and absolute timeouts; destroy sessions server-side on logout.
- Add CSRF protection and re-authentication for sensitive actions.
FAQ
Sessions or JWTs in cookies?
Either can be secure in HttpOnly cookies; server-side sessions are easier to revoke and inspect.
Does SameSite remove the need for CSRF tokens?
It blocks most CSRF, but tokens or origin checks still help for older browsers, same-site subdomains and edge cases.
Where should sessions be stored?
A shared store like Redis or the database, so any app instance can validate them and revoke them centrally.