Securing Cookies with HttpOnly, Secure, and SameSite

Think of cookie attributes as security guards for your session data. They prevent common attacks by telling the browser strict rules for sending the cookie, mitigating risks like cross-site scripting (XSS) and cross-site request forgery (CSRF).
Why it exists
By default, cookies are insecure. They can be read by client-side scripts, sent over unencrypted HTTP, and attached to requests initiated by other websites. This exposes sensitive data like session IDs to Cross-Site Scripting (XSS) and Cross-Site Request Forgery (CSRF) attacks. Secure cookie attributes were created to give developers control to mitigate these specific vulnerabilities.
The mental model
Treat a standard cookie like a postcard: anyone who intercepts it can read it, and it can be sent from anywhere. Secure attributes are like putting that postcard in a locked, armored box that can only be delivered to a specific address under specific conditions. HttpOnly is the lock, Secure is the armored transport, and SameSite is the strict delivery instruction.
How it works
These are flags set in the Set-Cookie HTTP response header from the server. There are three main attributes for security. First, HttpOnly: This tells the browser to prevent client-side scripts from accessing the cookie. It's a critical defense against XSS attacks where an attacker injects script to steal session cookies. Second, Secure: This ensures the browser only sends the cookie with requests made over an encrypted HTTPS connection. It prevents attackers from sniffing the cookie data on an insecure network. Third, SameSite: This attribute controls whether a cookie is sent with cross-site requests, providing CSRF protection. Strict prevents the cookie from being sent on any cross-site request. Lax is a good default, allowing cookies on top-level navigation (e.g., clicking a link to your site from another). None allows cross-site usage but requires the Secure attribute.
When to use it
Always use these attributes for any cookie containing sensitive information, especially session identifiers. A good default for session cookies is HttpOnly, Secure, and SameSite=Lax. This provides a strong security baseline with good usability. Use SameSite=Strict for actions on highly sensitive pages, like changing a password.
When not to use it
Avoid these attributes only in specific scenarios. Don't use HttpOnly if your own client-side JavaScript genuinely needs to read a cookie's value. Avoid Secure only during local development without HTTPS. Use SameSite=None only for cookies explicitly designed for cross-site contexts, like those used by some third-party authentication or ad-tech services.
One canonical example
In an Express.js application, setting a secure session cookie looks like this: res.cookie('sessionId', 'abc123xyz', { httpOnly: true, secure: true, sameSite: 'lax' });. This command tells the browser to create a cookie named sessionId that cannot be read by JavaScript, will only be sent over HTTPS, and will only be sent on same-site requests or top-level cross-site navigations.
Interview question
Which combination of cookie attributes provides a strong security baseline for a session identifier, mitigating client-side script access, insecure transmission, and common cross-site request forgery?
- a.Secure, SameSite=None, Expires
- b.Path, Domain, SameSite=Lax
- c.HttpOnly, Secure, SameSite=LaxCorrect
- d.HttpOnly, SameSite=Strict, Max-Age
Why? this is the answer
HttpOnly prevents client-side script access (XSS), Secure ensures transmission over HTTPS, and SameSite=Lax protects against common CSRF while maintaining usability for top-level navigations. Other options either include non-security attributes or use SameSite values that are too restrictive (Strict) or too permissive (None) for a general security baseline.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on security — each one lists the topics its interview covers.
See open roles