Explaining SameSite and CSRF
SameSite=Strict prevents CSRF by blocking cookies on all cross-site requests, so attackers can’t send authenticated requests silently. SameSite=Lax allows cookies on top-level GET navigations, so if a sensitive action is triggered via a GET request (like clicking a link), an attacker can bypass CSRF protections by tricking the user to follow that link, causing their browser to send cookies and perform the action.
Cross‑site request forgery works because the browser will happily attach ambient authentication state (session cookies, bearer tokens in cookies, etc.) to a request regardless of where that request originated. If you can trick Alice’s browser into issuing POST /transfer to her bank while she’s logged in, the bank will see a valid Cookie: session=… header and carry out the transfer.
The SameSite attribute on cookies changes when the browser is willing to attach those cookies. With SameSite=Strict the cookie is only sent on “same‑site” traffic – requests whose initiator and target have the same site according to the public suffix list. Any request initiated from evil.example to bank.example will be treated as cross‑site, so the browser won’t include the bank’s session cookie. The forged request reaches the bank unauthenticated and is rejected, which is why Strict is an effective CSRF mitigation.
SameSite=Lax relaxes that slightly: cookies are withheld on most cross‑site requests, but they are still sent on top‑level navigations using a “safe” method such as GET. That makes it possible to bypass if your application does something sensitive in response to a GET.
For example, suppose https://bank.example/closeAccount closes the current user’s account when requested with GET. An attacker could publish a page at https://evil.example/ with a link or automatic redirect:
<!doctype html>
<html>
<body>
<p>Click here to see a funny video!</p>
<a href="https://bank.example/closeAccount">Funny video</a>
<!-- or an automatic redirect -->
<meta http-equiv="refresh" content="0; url=https://bank.example/closeAccount">
</body>
</html>
If Alice is logged into bank.example and visits the attacker’s page, her browser will perform a top‑level navigation to https://bank.example/closeAccount. Under SameSite=Lax, the bank’s session cookie is included on that navigation because it’s a top‑level GET, so the bank thinks Alice herself requested the action and closes her account. Had the cookie been marked SameSite=Strict, the cookie would have been omitted on that cross‑site navigation and the operation would fail unless the bank prompted for login again.