Skip to the page

Cookies, CSRF and cross-window attacks · core

Cookies in depth: attributes, SameSite, prefixes, partitioning

A session cookie should be HttpOnly (no JS access), Secure (HTTPS only), SameSite=Lax or Strict (not sent on most cross-site requests), host-only with the __Host- prefix, and as short-lived as the product allows.

The notes

What you must know

  • HttpOnly: JS cannot read it (document.cookie), so XSS cannot steal it, but XSS can still USE it by making requests from the page.
  • Secure: sent only over HTTPS. SameSite=None requires Secure.
  • SameSite=Strict: never sent on cross-site requests, even when the user clicks a link from another site. Lax: also sent on top-level GET navigations from other sites (clicking a link). None: sent on all requests, including cross-site iframes and fetches.
  • Default without SameSite: Chromium treats it as Lax; Firefox and Safari do not. Always set SameSite explicitly.
  • Domain: without it the cookie is host-only; Domain=example.com sends it to all subdomains (a takeover of any subdomain can then read or set it). Path is not a security boundary.
  • Prefixes: __Secure- needs Secure; __Host- needs Secure, no Domain and Path=/ (host-only, cannot be overwritten by subdomains). Newer __Http- and __Host-Http- prefixes also require HttpOnly (Chrome 140+; not in every browser yet, so check support).
  • Third-party cookies: Safari and Firefox block them by default; Chrome kept them (it dropped the removal plan in 2025). Partitioned (CHIPS) cookies are keyed by the top-level site and work in Chrome 114+, Firefox 141+, Safari 26.2+.

The red pen

Common traps: mistake → fix

  • Mistake: "HttpOnly cookies stop XSS"

    Fix: they stop cookie theft, not actions; injected script can still call your API with the cookie.

  • Mistake: "SameSite=Lax is the default everywhere"

    Fix: only in Chromium; set it explicitly.

  • Mistake: "Cookies are always insecure, use localStorage"

    Fix: HttpOnly + Secure + SameSite cookies are usually the safer place for sessions.

  • Mistake: "Chrome is removing third-party cookies"

    Fix: Google dropped that plan in 2025; Safari and Firefox still block them.

Easy to hard

Questions interviewers ask, easy to hard

  1. Cookies vs localStorage vs sessionStorage: differences?
  2. Explain HttpOnly, Secure and each SameSite value.
  3. What does the __Host- prefix guarantee?
  4. Your app's widget runs in an iframe on partner sites and needs its own session. What cookie setup do you use in 2026?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview