All posts
Internet Fundamentals

Cookies, Sessions, CORS and CSRF: What Each One Actually Protects

By

October 26, 2026

6 min read

CORS is not a firewall, SameSite is not a complete CSRF defence, and a token in localStorage is not safer than a cookie, just exposed differently. What each browser mechanism does and does not do, and the cookie settings we would start with.

Internet Fundamentals
Cookies
CORS
CSRF
Web Security

Most browser security mechanisms protect users from other websites, not your server from attackers. The server still has to check who is asking, and whether they may, on every request.

What each one does

Mechanism

Protects against

Does not protect against

Same-origin policy

Another site's JavaScript reading your responses

Requests being sent; only reading the response is blocked

CORS

Nothing on its own: it is how your server tells browsers which other origins may read its responses

curl, scripts, servers, anything that is not a browser

HttpOnly cookie

Injected JavaScript reading the session token

Injected JavaScript making requests as the user while the page is open

Secure cookie

The cookie travelling over plain HTTP

Anything else

SameSite=Lax or Strict

Most cross-site request forgery, by withholding the cookie on cross-site requests

Requests from your own subdomains, which count as same-site

CSRF token or Origin check

Forged state-changing requests from other sites

Cross-site scripting on your own site

Want a second opinion on this for your system?

Book a free 30-minute call. You'll leave knowing what we'd do first and roughly what it costs, whether or not you hire us.

Cookies or tokens in localStorage

A token in localStorage is readable by any script on the page, so one cross-site scripting bug leaks it and it works from anywhere. An HttpOnly cookie cannot be read by script, but the browser sends it automatically, which is why cookies need CSRF protection. For a web app on its own domain, an HttpOnly cookie with SameSite and an Origin check is the common choice. Native apps usually keep tokens in the platform's secure storage instead.

Cookie settings to start with

  • Set HttpOnly, Secure and SameSite=Lax explicitly. Some browsers default to Lax when SameSite is missing, not all do, and that default lets POSTs through for two minutes after the cookie is set.

  • Name the session cookie with the __Host- prefix. Browsers then require Secure, Path=/ and no Domain attribute, so a subdomain cannot set or overwrite it.

  • Use SameSite=None only for cookies that must work cross-site, such as embeds. It requires Secure.

  • Keep session lifetimes finite and let users sign out other devices.

CORS without guesswork

  • If the frontend and API share an origin, for example the API under /api behind the same CDN, you need no CORS at all.

  • Otherwise, allow the exact origins you serve. Do not echo back whatever Origin the request sends.

  • Browsers reject a wildcard (*) on requests with credentials.

  • Custom headers or JSON bodies trigger a preflight OPTIONS request. Answer it, and cache it with Access-Control-Max-Age.

hoolco Monthly Review

Once a month: what changed in tech that month and what it means for you, from cloud and AI to security and the tools you build with. Every item links to its source. No sales pitch, unsubscribe in one click.

As of October 26, 2026. Provided as is, for general information, and accurate to the best of our knowledge on the date shown. Not legal, compliance, tax, financial or other professional advice. Prices, limits and vendor services change; check current vendor documentation before relying on anything here. Terms of use.