Intro to SameSite Cookies (CSRF Protection)

A pretty common web attack involves hijacking a user’s session to get them to perform actions on your behalf.

An example attack:

Lets say Bob signs into his bank account at

Untitled drawing (2).png

From now on, whenever Bob interacts with the browser will send his cookies so that knows that the request was made by Bob.

Lets say Eve knows that he is logged into and sends him a message like:

Hey Bob,

Check out this sweet cat picture:

<img src=””&gt;

Sincerely :),


When Bob loads this message, his browser will make a web request to load the image.  The session cookies will be sent. But it’s not actually in image (the browser doesn’t know this), it’s a bank transfer. A bank transfer will be performed and Bob just lost all his money.

Normally you’d protect against this using csrf tokens, and use a http POST for this state changing request. See here for more information.

SameSite Cookie Flag

A new protection was recently introduced into Chrome that allows servers to instruct clients on when to send session cookies.

The problem in the CSRF attack is tricky. The browser did the right thing by trying to load the image, and it did the right thing by sending the cookies to ( told the browser to always send the cookies). But the bank still failed bob. Confused deputy.

SameSite cookie flag is a way for to protect their users against this attack. When Eve sent bob the message that had the embedded image, did cookies really need to be sent to load an image? Probably not.

Using SameSite, websites can tell clients to only send cookies if the request originated from the same site.

Once you’re on it makes sense to initiate transfers. But it doesn’t make sense to initiate transfers from or any other website. So in the previous attack example, since the transfer didn’t originate on, Bob will probably see a login page instead of a transaction receipt.

Using SameSite Cookie Flag

To set cookie, simply add the SameSite flag to the cookie like:

Set-Cookie: Session=6fa459ea-ee8a-3ca4-894e-db77e160355e; SameSite=Strict

You can specify how strict you want the SameSite restrictions to be. I’d recommend just using Strict and playing with the website for a bit, specially if you’re using a SPA.

The three options are None, Lax, and Strict. None is what a normal cookie uses. The different between Lax and Strict is mainly whether or not full page loads (like clicking on a link to will send the cookies. Lax will send the cookies.

To read more, checkout the RFC draft. Or google it.

Some Thoughts