How to set SameSite attribute to 'None; Secure' in Rails3.1.12 and Ruby1.9.3

前端 未结 5 647
逝去的感伤
逝去的感伤 2021-01-05 02:31

A cookie associated with a cross-site resource at https://example.com/ was set without the SameSite attribute. It has been blocked, as Chrome now only delivers

5条回答
  •  青春惊慌失措
    2021-01-05 02:58

    Action dispatch cookies is responsible for writing cookies to browser set in application, this uses Rack::Utils.set_cookie_header!.

    Support for SameSite has been added after rack version 1.6, you need to check your rack version in Gemfile and if it is < 1.6 you need to add the following code in config/initializers

    require 'rack/utils'
    module Rack
      module Utils
        def self.set_cookie_header!(header, key, value)
          case value
          when Hash
            domain  = "; domain="  + value[:domain] if value[:domain]
            path    = "; path="    + value[:path]   if value[:path]
            max_age = "; max-age=" + value[:max_age] if value[:max_age]
            expires = "; expires=" +
              rfc2822(value[:expires].clone.gmtime) if value[:expires]
            secure = "; secure"  if value[:secure]
            httponly = "; HttpOnly" if value[:httponly]
            same_site =
              case value[:same_site]
              when false, nil
                nil
              when :none, 'None', :None
                '; SameSite=None'
              when :lax, 'Lax', :Lax
                '; SameSite=Lax'
              when true, :strict, 'Strict', :Strict
                '; SameSite=Strict'
              else
                raise ArgumentError, "Invalid SameSite value: #{value[:same_site].inspect}"
              end
            value = value[:value]
          end
          value = [value] unless Array === value
          cookie = escape(key) + "=" +
            value.map { |v| escape v }.join("&") +
            "#{domain}#{path}#{max_age}#{expires}#{secure}#{httponly}#{same_site}"
    
          case header["Set-Cookie"]
          when nil, ''
            header["Set-Cookie"] = cookie
          when String
            header["Set-Cookie"] = [header["Set-Cookie"], cookie].join("\n")
          when Array
            header["Set-Cookie"] = (header["Set-Cookie"] + [cookie]).join("\n")
          end
    
          nil
        end
      end
    end
    

    Once done you can set SameSite attribute while creating a new cookie, for ex:

    cookies['testing'] = {
      value: 'test',
      path: '/',
      expiry: 1.weeks.from_now,
      same_site: :none
    }
    

    you can also add the same_site: to your session store as well.

    Hope this helps!

提交回复
热议问题