Session cookies: configure secure login sessions and verify logout
Learn what a session cookie carries, what Secure, HttpOnly, and SameSite protect, and why the server must manage expiry and logout.
Session cookies: configure secure login sessions and verify logout
A session cookie is a cookie the browser sends with matching requests so a web application can associate them with a server-side login session. Receiving a cookie does not mean login data is safely stored in the browser: if an attacker steals the session identifier, they may reuse that session. Protect both cookie transport and the server-side session lifecycle.
HTTP requests are independent. After authentication, the server commonly creates an unpredictable session identifier, sends it as a cookie, and maps it to account and permission state on the server. The cookie is a transport mechanism; it does not replace authorization checks or determine whether the session remains valid.
Set cookie attributes deliberately
An illustrative response is:
Set-Cookie: __Host-session=<random-session-id>; Path=/; Secure; HttpOnly; SameSite=LaxUse Secure so browsers send the cookie only over HTTPS. HttpOnly prevents page scripts from reading its value through APIs such as document.cookie, but does not stop XSS from making requests that automatically carry the cookie. SameSite limits sending on cross-site requests and helps defend against CSRF; it is defense in depth, not a replacement for a CSRF token or other request checks. Strict may affect some external navigation flows; Lax does not prevent every CSRF scenario. SameSite=None requires Secure.
Path controls request paths but is not a strong security boundary. Omit Domain unless sharing with subdomains is needed; a broad parent domain expands exposure. On supporting browsers, a __Host- cookie must use Secure, omit Domain, and set Path=/, binding it to the host. Max-Age or Expires makes a cookie persistent; a browser-session cookie may survive restart through session restore. Server expiry must be authoritative.
Protect server-side sessions too
Generate session IDs using a cryptographically secure random source; do not derive them from URLs or user-chosen values.
Rotate the identifier after login and privilege changes, and invalidate the previous identifier to reduce session fixation.
Enforce both idle and absolute expiration on the server. Require reauthentication for sensitive actions when appropriate.
On logout, expire the browser cookie and invalidate the server-side session record. Cookie deletion alone does not invalidate a stolen copy.
Add CSRF defenses appropriate to the application, such as CSRF tokens and Origin/Referer checks, in addition to SameSite.
Verify actual behavior: inspect `Set-Cookie`, ensure sessions do not switch to HTTP, confirm scripts cannot read an HttpOnly value, check ID rotation after login, and confirm the server rejects a logged-out session.
If document.cookie does not show an HttpOnly session cookie, that is expected. It does not mean an injected script cannot send authenticated requests. XSS prevention and server authorization remain necessary. If logout clears the browser cookie but the session still works elsewhere, the server record may not have been invalidated.
Sources
[RFC 6265: HTTP State Management Mechanism](https://www.rfc-editor.org/rfc/rfc6265.html)
[OWASP Session Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html)
[IETF Datatracker: RFC 6265bis draft status](https://datatracker.ietf.org/doc/draft-ietf-httpbis-rfc6265bis/)