Secure Cookies 2026: HttpOnly, Secure & SameSite Guide
Arnaud Fosse
Website security has never been more critical, and cookies remain one of the most vulnerable components in web applications. With cyber attacks increasing by 38% in 2025, implementing secure cookie practices is essential for protecting user data and maintaining website integrity.
Cookies store sensitive information like session tokens, user preferences, and authentication data. Without proper security attributes, they become easy targets for malicious actors. This comprehensive guide will show you how to implement HttpOnly, Secure, and SameSite attributes to bulletproof your cookies against common web vulnerabilities.
Understanding Cookie Security Attributes
Cookie security attributes are flags that control how browsers handle cookies, determining when and how they can be accessed or transmitted. These attributes act as security barriers, preventing unauthorized access and ensuring cookies behave according to your security requirements.
The three primary security attributes are:
- HttpOnly: Prevents client-side JavaScript access
- Secure: Ensures transmission only over HTTPS
- SameSite: Controls cross-site request behavior
When properly configured, these attributes work together to create multiple layers of protection against Cross-Site Scripting (XSS), Man-in-the-Middle attacks, and Cross-Site Request Forgery (CSRF).
The HttpOnly Attribute: Blocking JavaScript Access
The HttpOnly attribute prevents client-side scripts from accessing cookies through JavaScript's document.cookie property. This is crucial for protecting session tokens and authentication cookies from XSS attacks.
How HttpOnly Works
When a cookie has the HttpOnly flag, it becomes inaccessible to JavaScript running in the browser. The cookie is only sent in HTTP requests to the server, making it invisible to potentially malicious scripts injected through XSS vulnerabilities.
Implementation example:
Set-Cookie: sessionId=abc123; HttpOnly; Path=/In popular programming languages:
- PHP:
setcookie('sessionId', 'abc123', ['httponly' => true]); - Node.js:
res.cookie('sessionId', 'abc123', { httpOnly: true }); - Python/Django:
response.set_cookie('sessionId', 'abc123', httponly=True)
HttpOnly Best Practices
Apply HttpOnly to all cookies containing sensitive data, especially session identifiers and authentication tokens. However, avoid using HttpOnly for cookies that legitimate JavaScript needs to access, such as user preferences for UI customization.
The Secure Attribute: HTTPS-Only Transmission
The Secure attribute ensures cookies are only transmitted over encrypted HTTPS connections, preventing interception during transmission. This is fundamental for protecting sensitive data from Man-in-the-Middle attacks.
Implementing the Secure Attribute
Cookies with the Secure flag will never be sent over unencrypted HTTP connections. This means your website must use HTTPS for secure cookies to function properly.
Example implementation:
Set-Cookie: authToken=xyz789; Secure; HttpOnly; Path=/Mixed Content Considerations
When implementing Secure cookies, ensure your entire website operates over HTTPS. Mixed content (HTTP resources on HTTPS pages) can create security vulnerabilities and prevent secure cookies from working correctly.
Use tools like SiteRadar to audit your website for mixed content issues and verify that all security headers, including cookie attributes, are properly configured.
The SameSite Attribute: CSRF Protection
The SameSite attribute controls whether cookies are sent with cross-site requests, providing robust protection against CSRF attacks. This attribute has three possible values: Strict, Lax, and None.
SameSite=Strict
The strictest setting prevents cookies from being sent with any cross-site requests. While highly secure, this can break legitimate functionality like external payment processors or social media embeds.
Set-Cookie: sessionId=abc123; SameSite=Strict; Secure; HttpOnlySameSite=Lax
The default in modern browsers, Lax allows cookies to be sent with top-level navigation (like clicking a link) but blocks them from cross-site subrequests. This provides good security while maintaining usability.
Set-Cookie: sessionId=abc123; SameSite=Lax; Secure; HttpOnlySameSite=None
Allows cookies to be sent with all cross-site requests but requires the Secure attribute. Use this only when cross-site functionality is essential, such as embedded payment forms or third-party integrations.
Set-Cookie: trackingId=def456; SameSite=None; SecureCombining Security Attributes for Maximum Protection
The most secure approach combines all three attributes appropriately based on the cookie's purpose. Here are recommended configurations for different cookie types:
Session Cookies
Set-Cookie: sessionId=abc123; Secure; HttpOnly; SameSite=Lax; Path=/; Max-Age=3600Authentication Tokens
Set-Cookie: authToken=xyz789; Secure; HttpOnly; SameSite=Strict; Path=/; Max-Age=86400Tracking Cookies (when necessary)
Set-Cookie: analytics=uvw012; Secure; SameSite=Lax; Path=/; Max-Age=2592000Browser Compatibility and Legacy Support
Modern browsers widely support all three security attributes, but older versions may have limitations. As of 2026, over 95% of browsers support SameSite, making it safe to implement across all websites.
Progressive Enhancement Strategy
For maximum compatibility, implement a progressive enhancement approach:
- Set default secure configurations for modern browsers
- Include fallback handling for older browsers
- Monitor cookie behavior through browser analytics
- Update configurations as browser support evolves
Testing and Monitoring Cookie Security
Regular testing ensures your cookie security implementations remain effective. Use browser developer tools to inspect cookie attributes and verify they're set correctly.
Automated Security Testing
Implement automated tests to verify cookie security attributes in your deployment pipeline. This prevents security regressions and ensures consistent protection across all environments.
Security audit tools can help identify cookie misconfigurations and provide recommendations for improvement. Regular security assessments should include cookie attribute verification as part of comprehensive website security reviews.
What is the difference between HttpOnly and Secure cookie attributes?
HttpOnly prevents JavaScript access to cookies, protecting against XSS attacks where malicious scripts try to steal session data. Secure ensures cookies are only transmitted over HTTPS connections, protecting against Man-in-the-Middle attacks during data transmission. HttpOnly focuses on client-side protection, while Secure protects data in transit. Both attributes work independently and should be used together for comprehensive cookie security.
How does SameSite=Lax protect against CSRF attacks?
SameSite=Lax prevents cookies from being sent with cross-site POST requests, form submissions, and AJAX calls, which are common CSRF attack vectors. However, it allows cookies with top-level navigation (clicking links), maintaining legitimate user experience. This blocks 90% of CSRF attacks while preserving normal website functionality. For maximum CSRF protection, use SameSite=Strict, though this may break some legitimate cross-site interactions.
What happens when SameSite=None is used without Secure?
Modern browsers reject cookies with SameSite=None that don't include the Secure attribute, treating them as invalid. This requirement ensures that cross-site cookies can only be transmitted over encrypted HTTPS connections. If you set SameSite=None without Secure, the cookie will be blocked, potentially breaking third-party integrations or embedded content that relies on cross-site cookie functionality.
How can I test if my cookie security attributes are working correctly?
Use browser developer tools (F12) to inspect cookies in the Application/Storage tab, where security attributes are clearly displayed. For automated testing, tools like OWASP ZAP or custom scripts can verify cookie configurations across your entire application. Security scanners can detect missing security attributes and provide detailed reports on cookie vulnerabilities, helping ensure consistent protection across all website cookies.
When should I use SameSite=Strict vs SameSite=Lax?
Use SameSite=Strict for highly sensitive authentication cookies where maximum CSRF protection is required, accepting that some legitimate cross-site functionality may break. Choose SameSite=Lax for general session cookies where you need CSRF protection but want to maintain normal user experience when users arrive via external links. SameSite=Lax is the recommended default for most applications, providing 90% of CSRF protection while maintaining usability.
Discover SiteRadar
Analyze your website for free with our SEO, performance and security audit tool.
View pricing →