Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 6.0.2.1 - “sameSite” attribute set to “none” “secure” attribute

Firefox error:

Cookie “_myapp_session” will be soon rejected because it has the “sameSite” attribute set to “none” or an invalid value, without the “secure” attribute. To know more about the “sameSite“ attribute, read https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie/SameSite

"To fix this, you will have to add the Secure attribute to your SameSite=None cookies."

How do I add the secure attribute into my SameSite=None cookie, when using Rails 6?

I do not want to add a separate gem to accomplish this.This error randomly appeared, I assume there was a browser change. Does rails 6 have a native way to fix this? I read this post,

Thank you

like image 944
user2012677 Avatar asked Jun 20 '20 15:06

user2012677


People also ask

How do I make SameSite none secure?

SameSite=None requires Secure The warning appears because any cookie that requests SameSite=None but is not marked Secure will be rejected. To fix this, you will have to add the Secure attribute to your SameSite=None cookies. A Secure cookie is only sent to the server with an encrypted request over the HTTPS protocol.

How do I set strict on SameSite?

To prepare, Android allows native apps to set cookies directly through the CookieManager API. You must declare first party cookies as SameSite=Lax or SameSite=Strict , as appropriate. You must declare third party cookies as SameSite=None; Secure .

Where do you set the SameSite attribute?

You can add SameSite cookie attributes in the set-cookie HTTP response header to restricts browser behavior. It may prevent the browser from sending the cookie's key=value pair based on the type of interaction that triggered the HTTP request.

How do you fix some cookies are misusing the recommended SameSite attribute?

The warnings will look something like this: Resolve this issue by updating the attributes of the cookie: Specify SameSite=None and Secure if the cookie is intended to be set in cross-site contexts. Note that only cookies sent over HTTPS may use the Secure attribute.

What happens if I set the SameSite=none attribute to none?

If SameSite=None is set, the cookie Secure attribute must also be set (or the cookie will be blocked). Warnings like the ones below might appear in your console: Cookie "myCookie" rejected because it has the "SameSite=None" attribute but is missing the "secure" attribute.

What is the difference between SameSite=none and Secure attribute?

Note: Using SameSite=None requires Secure attribute in some latest browser versions. Secure attribute is more straight-forward to understand. A Secure cookie is only sent to the server with an encrypted request over the HTTPS protocol.

What is the SameSite attribute?

The SameSite attribute allows developers to specify cookie security for each particular case. SameSite can take 3 possible values: Strict, Lax or None. Lax —Default value in modern browsers. Cookies are allowed to be sent with top-level navigations and will be sent along with GET requests initiated by a third party website.

How to add SameSite=none to cookies in rails?

For Rails 5.x and lower, the rails_same_site_cookie gem is a good option for adding SameSite=None; to all your app's cookies. It uses middleware to do it. Show activity on this post. The way to set custom headers is to add the line below to your controller action: response.headers ['Set-Cookie'] = 'Secure;SameSite=None'.


2 Answers

You can configure your session store to use secure cookies in production, just add this to an initializer:

MyApp::Application.config.session_store :cookie_store, key: '_my_app_session', secure: Rails.env.production?

You may already have it on config/initializers/session_store.rb.

Documentation and pertinent issue. This will be fixed in Rails 6.1.

like image 118
Felipe Zavan Avatar answered Oct 19 '22 12:10

Felipe Zavan


You need this line in your Rails config file:

 # Specify cookies SameSite protection level: either :none, :lax, or :strict. 
 # 
 # This change is not backwards compatible with earlier Rails versions. 
 # It's best enabled when your entire app is migrated and stable on 6.1. 
 Rails.application.config.action_dispatch.cookies_same_site_protection = :lax 
like image 37
rapidror Avatar answered Oct 19 '22 14:10

rapidror