Authentication

Manage Secure Cookies and Session Lifecycles

Set cookie attributes deliberately, rotate session identifiers, and expire sessions according to risk and user expectations.

Manage Secure Cookies and Session Lifecycles

Overview

Protect browser sessions with Secure, HttpOnly, SameSite, narrow scope, and server-side expiration controls.

Set cookie attributes explicitly

Session cookies should normally use Secure so they travel only over HTTPS, HttpOnly to prevent direct JavaScript access, and an intentional SameSite policy to reduce cross-site request exposure. Prefer a narrow path and avoid setting a broad domain unless subdomain sharing is required.

Set-Cookie: __Host-session=opaque-value; Path=/; Secure; HttpOnly; SameSite=Lax

The __Host- prefix has additional browser requirements: Secure, Path=/, and no Domain attribute. Confirm support and deployment behavior before adopting it.

Rotate identifiers at trust boundaries

Regenerate the session identifier after login, privilege elevation, and other important authentication transitions. Invalidate the previous identifier. This reduces the risk of session fixation. Do not place session identifiers in URLs, logs, analytics, or client-visible error messages.

Define expiration on the server

Use both idle and absolute expiration where appropriate. Cookie expiration alone is not enough because a copied identifier may still be accepted if the server-side session remains valid. Provide a reliable logout path that invalidates the server-side session and clears the browser cookie.

Test the lifecycle

Test HTTPS and HTTP behavior, subdomains, cross-site form flows, browser restart, idle timeout, logout, and concurrent sessions. Choose SameSite behavior based on actual sign-in and integration flows rather than copying a setting without testing.

Implementation checklist

  • Define the expected behavior and supported environments.
  • Keep security, accessibility, and failure states in the design.
  • Measure the result with representative user journeys.
  • Document operational assumptions and review them after changes.
Share this page Help other developers discover this resource.