Skip to content

Add the option to generate a legacy cookie #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rails_same_site_cookie/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module RailsSameSiteCookie
class Configuration
attr_accessor :user_agent_regex
attr_accessor :user_agent_regex, :generate_legacy_cookie

def initialize
@user_agent_regex = nil
@generate_legacy_cookie = false
end
end
end
14 changes: 11 additions & 3 deletions lib/rails_same_site_cookie/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ def call(env)
status, headers, body = @app.call(env)

regex = RailsSameSiteCookie.configuration.user_agent_regex
generate_legacy = RailsSameSiteCookie.configuration.generate_legacy_cookie
set_cookie = headers['Set-Cookie']
if (regex.nil? or regex.match(env['HTTP_USER_AGENT'])) and not (set_cookie.nil? or set_cookie.strip == '')
parser = UserAgentChecker.new(env['HTTP_USER_AGENT'])
if parser.send_same_site_none?
if parser.send_same_site_none? || generate_legacy
cookies = set_cookie.split(COOKIE_SEPARATOR)
ssl = Rack::Request.new(env).ssl?

Expand All @@ -28,10 +29,17 @@ def call(env)
cookie << '; Secure'
end

unless cookie =~ /;\s*samesite=/i
cookie << '; SameSite=None'
if parser.send_same_site_none?
unless cookie =~ /;\s*samesite=/i
cookie << '; SameSite=None'
end
end

if generate_legacy
cookie_name, cookie_value = cookie.split('=', 2)
legacy_cookie = "#{COOKIE_SEPARATOR} #{cookie_name}-legacy=#{cookie_value}"
cookie << legacy_cookie
end
end

headers['Set-Cookie'] = cookies.join(COOKIE_SEPARATOR)
Expand Down
6 changes: 6 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@
config.user_agent_regex = /dsasdd/
expect(config.user_agent_regex).to eq(/dsasdd/)
end

it "sets legacy cookie generation" do
config = described_class.new
config.user_agent_regex = true
expect(config.user_agent_regex).to be_truthy
end
end
31 changes: 31 additions & 0 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,38 @@
response = request.post("/some/path", 'HTTP_USER_AGENT' => '')
expect(response['Set-Cookie']).to match(/;\s*samesite=none/i)
end
end

context "when configured to generate legacy cookie" do
let(:request) { Rack::MockRequest.new(subject) }
before(:each) do
RailsSameSiteCookie.configure do |config|
config.generate_legacy_cookie = true
end
end

context "when configured without same site support user agent" do
let(:response) { request.post("/some/path", 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Safari/605.1.15') }
it "adds legacy cookies for all requests" do
expect(response['Set-Cookie']).to match(/;\s*thiscookie-legacy=/i)
end

it "does not add SameSite=None to cookies for all requests" do
expect(response['Set-Cookie']).to_not match(/;\s*samesite=none/i)
end
end

context "when configured with same site support user agent" do
it "adds legacy cookies for all requests" do
response = request.post("/some/path", 'HTTP_USER_AGENT' => '')
expect(response['Set-Cookie']).to match(/\s*thiscookie-legacy=/i)
end

it "adds SameSite=None to cookies for all requests" do
response = request.post("/some/path", 'HTTP_USER_AGENT' => '')
expect(response['Set-Cookie']).to match(/;\s*samesite=none/i)
end
end
end

end
2 changes: 2 additions & 0 deletions spec/rails_same_site_cookie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
expect(config.is_a?(RailsSameSiteCookie::Configuration)).to be(true)
expect(config).to respond_to(:user_agent_regex)
expect(config).to respond_to("user_agent_regex=")
expect(config).to respond_to(:generate_legacy_cookie)
expect(config).to respond_to("generate_legacy_cookie=")
end
end
end