Skip to content
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

Add user_filter to config to allow having lazy checks #15

Open
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion lib/rails_same_site_cookie/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module RailsSameSiteCookie
class Configuration
attr_accessor :user_agent_regex
attr_accessor :user_filter

def initialize
@user_agent_regex = nil
@user_filter = nil
end
end
end
end
16 changes: 13 additions & 3 deletions lib/rails_same_site_cookie/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def initialize(app)
def call(env)
status, headers, body = @app.call(env)

regex = RailsSameSiteCookie.configuration.user_agent_regex
if headers['Set-Cookie'].present? and (regex.nil? or regex.match(env['HTTP_USER_AGENT']))
if should_intercept?(env, headers)
parser = UserAgentChecker.new(env['HTTP_USER_AGENT'])
if parser.send_same_site_none?
cookies = headers['Set-Cookie'].split(COOKIE_SEPARATOR)
Expand All @@ -38,5 +37,16 @@ def call(env)
[status, headers, body]
end

private

def should_intercept?(env, headers)
regex = RailsSameSiteCookie.configuration.user_agent_regex
user_filter = RailsSameSiteCookie.configuration.user_filter
result = headers['Set-Cookie'].present? && (regex.nil? || regex.match(env['HTTP_USER_AGENT']))

return result unless user_filter.respond_to?(:call)
result && user_filter.call(env)
end

end
end
end
2 changes: 1 addition & 1 deletion lib/rails_same_site_cookie/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RailsSameSiteCookie
VERSION = "0.1.5"
VERSION = "0.2.0"
end
26 changes: 25 additions & 1 deletion spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,35 @@
end
end

context "when configured with user filter" do

before(:each) do
RailsSameSiteCookie.configure do |config|
config.user_agent_regex = nil
config.user_filter = lambda { |env| !env['IS-MOBILE-APPLICATION'].present? }
end
end

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

context "when user filter returns false" do
it "doesn't add SameSite=None if request is missing regex" do
response = request.post("/some/path", 'HTTP_USER_AGENT' => '', 'IS-MOBILE-APPLICATION' => true)
expect(response['Set-Cookie']).not_to match(/;\s*samesite=none/i)
end
end
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