Cookies for site owners: what they do and the security settings that matter
Learn how cookies support sessions and which security settings matter when a site stores and sends browser state.
A cookie is, per MDN's guide (https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies), a small piece of data a server sends to the browser, which may store it and send it back with later requests. Because HTTP is stateless by default, cookies let sites remember state.
What cookies are used for
MDN names three main purposes: session management (sign-in status, shopping cart), personalization (language, theme) and tracking (recording and analysing behaviour). In a basic sign-in system, the server sets a cookie containing a session ID after correct credentials, the browser sends it with later requests, and the server checks the ID to decide what to show.
Security attributes
MDN warns that cookie values are by default visible to, and changeable by, the end user, and that a stolen session ID could let someone appear logged in as another person. Settings it describes:
- Secure: the cookie is only sent over HTTPS. MDN adds that this does not prevent all access to sensitive information in cookies.
- HttpOnly: JavaScript cannot read the cookie. MDN says cookies that persist user sessions should have it, which helps mitigate cross-site scripting (XSS).
- SameSite: controls whether cookies are sent with cross-site requests. Strict sends only for requests from the same site, and MDN suggests it for things like authentication or cart cookies; Lax also allows some cross-site top-level navigations, subject to method and browser rules; None permits cross-site sending subject to other browser restrictions and requires Secure, and MDN says it is for cases like ad-tech or analytics widgets. If no SameSite is set, MDN says browsers treat the cookie as Lax.
- Domain and Path: define where the cookie is sent. MDN says that setting Domain makes the cookie available to subdomains too, so it is less restrictive than omitting it, and that Path is not a security measure.
- Prefixes: names starting with __Host- or __Secure- make supporting browsers enforce certain attributes, as a defence-in-depth measure against session fixation. MDN notes the server must check the full cookie name including the prefix.
MDN also suggests using an opaque identifier that the server looks up, instead of putting sensitive information in a cookie.
Third-party cookies and privacy
MDN says third-party cookies have legitimate uses such as counting ad impressions or analytics across related domains, but can also build profiles of browsing habits. It reports that browser vendors have started blocking third-party cookies by default or planned to, and that this can break components like social widgets. Developers should look at reducing reliance on them. This matters if you plan analytics or advertising: check each vendor's current behaviour and the browsers you care about.
Regulations
MDN lists the GDPR, the ePrivacy Directive and the California Consumer Privacy Act as laws covering cookies, says they can reach sites accessed from those jurisdictions, and says the burden of knowing and complying is on you. We have not researched Indian rules or any of these laws, and this article is not legal advice. Ask qualified counsel what applies to your site.
Checklist for your developer
- Do session cookies have Secure, HttpOnly and a deliberate SameSite value?
- Is anything sensitive stored directly in a cookie?
- Which cookies do third-party scripts set, and do we need them?
- Have cookie consent and notice requirements been checked with counsel?
The checklist is ours, based on the guide.
Limits
We did not read MDN's Set-Cookie reference or its third-party cookies article.
By Omnistu.