Posted over 1 year ago. Visible to the public. Repeats.

SameSite cookies

TL;DR Most web applications do not require action on this. SameSite=None (old browser default) will continue to work, and SameSite=Lax (new Chrome default, gradually rolled out) is an even better default for cookies. Set SameSite=Strict only for extra security in special cases (see below). If your application is rendered in an iframe (e.g. a video player or some news stream), you need to configure its relevant cookies as SameSite=None.


The SameSite cookie attribute targets cross-origin requests. It defines under which circumstances a cookie should be sent to the server, putting cookies into three different classes:

SameSite=None

Send the cookie whenever a request is made to the cookie domain, be it cross-origin or on the same site, from the page or from an iframe. This is how cookies have behaved the last decades.

SameSite=Lax

Only send the cookie in a first-party context (meaning the URL in the address bar matches the cookie domain). Do not send it with the following cross-origin requests: non-GET, AJAX, iframe, image requests etc. It saves the user from cross-site request forgery.

SameSite=Strict

Only send the cookie if the request was initiated from the cookie domain. The cookie will not be sent if the user e.g. opens a link from an email. Strict is a good idea e.g. for an CSRF cookie.

A cookie without the SameSite attribute will currently be handled as if it was sent with SameSite=None. However, Google announced to start enforcing usage of the SameSite attribute in Chrome in February 2020, meaning it will handle cookies without the SameSite attribute as if they were sent with SameSite=Lax.

SameSite variants

What this means for web development

If your application is running on a single domain without any cross-origin communication, you're fine: nothing to do.

Chrome moving to SameSite=Lax as default forces web developers to handle this change. Considering its market share, other browser vendors will move along.

A good default is SameSite=Lax. In fact, you can add SameSite=Lax to all set cookies and it will run just fine in the most cases. Some breaking use cases to watch out for:

Rendering in an iframe

When your application (or parts of it) are rendered inside an iframe, SameSite=Lax cookies will not be sent along with requests of the iframe (unless the iframe is embedded on its own domain). If you need cookies in an iframe context, set SameSite=None to disable the protective behavior.

Cross-domain API

When you're offering an API that is queried from browsers on various domains, SameSite=Lax will prevent your cookies to be sent along. Set SameSite=None on cookies you need to receive on the API.

Cross-domain non-GET requests

A Lax cookie will not be sent with POST, DELETE, OPTIONS or any other request. If you need an authentication cookie on these, you must either make the cookie SameSite=None or SameSite=Strict.

Incompatible clients
: A few web browsers have issues:

  • Chrome 51-66: blocks cookies with SameSite=None
  • UC browser < 12.13.2 on Android: blocks cookies with SameSite=None
  • iOS 12 (all browsers): treat cookies with SameSite=None like SameSite=Strict
  • Safari on Mac OS 10.14 Mojave: : treats cookies with SameSite=None like SameSite=Strict

How to set a SameSite cookie

Rails (session configuration)

In config/initializers/session_store.rb, add the options secure: true, same_site: :strict|:lax. To set :none you need Rack 2 (i.e. Rails 5).

Rails

Custom cookies are set with cookies[:cookie_name] = 'value' or = { value: 'value', path: '/path' }. Use the latter version and add

Javascript (using js-cookie)

Make sure you're using at least 2.2.0. Set a cookie with options secure: true, samesite: 'strict|lax|none'.

Testing in advance

To test the effect of the new Chrome behavior on your site or cookies you manage, you can go to chrome://flags in Chrome 76+ and enable the “SameSite by default cookies” and “Cookies without SameSite must be secure” experiments.

Selected Resources

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
7 months ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more