rails_same_site_cookie 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.0
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at p.schinis@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails_same_site_cookie.gemspec
4
+ gemspec
@@ -0,0 +1,144 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_same_site_cookie (0.1.0)
5
+ rails (>= 4.1)
6
+ user_agent_parser
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.2.3)
12
+ actionpack (= 5.2.3)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailer (5.2.3)
16
+ actionpack (= 5.2.3)
17
+ actionview (= 5.2.3)
18
+ activejob (= 5.2.3)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.2.3)
22
+ actionview (= 5.2.3)
23
+ activesupport (= 5.2.3)
24
+ rack (~> 2.0)
25
+ rack-test (>= 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.2.3)
29
+ activesupport (= 5.2.3)
30
+ builder (~> 3.1)
31
+ erubi (~> 1.4)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
+ activejob (5.2.3)
35
+ activesupport (= 5.2.3)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.2.3)
38
+ activesupport (= 5.2.3)
39
+ activerecord (5.2.3)
40
+ activemodel (= 5.2.3)
41
+ activesupport (= 5.2.3)
42
+ arel (>= 9.0)
43
+ activestorage (5.2.3)
44
+ actionpack (= 5.2.3)
45
+ activerecord (= 5.2.3)
46
+ marcel (~> 0.3.1)
47
+ activesupport (5.2.3)
48
+ concurrent-ruby (~> 1.0, >= 1.0.2)
49
+ i18n (>= 0.7, < 2)
50
+ minitest (~> 5.1)
51
+ tzinfo (~> 1.1)
52
+ arel (9.0.0)
53
+ builder (3.2.3)
54
+ concurrent-ruby (1.1.5)
55
+ crass (1.0.5)
56
+ diff-lcs (1.3)
57
+ erubi (1.9.0)
58
+ globalid (0.4.2)
59
+ activesupport (>= 4.2.0)
60
+ i18n (1.7.0)
61
+ concurrent-ruby (~> 1.0)
62
+ loofah (2.3.1)
63
+ crass (~> 1.0.2)
64
+ nokogiri (>= 1.5.9)
65
+ mail (2.7.1)
66
+ mini_mime (>= 0.1.1)
67
+ marcel (0.3.3)
68
+ mimemagic (~> 0.3.2)
69
+ method_source (0.9.2)
70
+ mimemagic (0.3.3)
71
+ mini_mime (1.0.2)
72
+ mini_portile2 (2.4.0)
73
+ minitest (5.13.0)
74
+ nio4r (2.5.2)
75
+ nokogiri (1.10.5)
76
+ mini_portile2 (~> 2.4.0)
77
+ rack (2.0.7)
78
+ rack-test (1.1.0)
79
+ rack (>= 1.0, < 3)
80
+ rails (5.2.3)
81
+ actioncable (= 5.2.3)
82
+ actionmailer (= 5.2.3)
83
+ actionpack (= 5.2.3)
84
+ actionview (= 5.2.3)
85
+ activejob (= 5.2.3)
86
+ activemodel (= 5.2.3)
87
+ activerecord (= 5.2.3)
88
+ activestorage (= 5.2.3)
89
+ activesupport (= 5.2.3)
90
+ bundler (>= 1.3.0)
91
+ railties (= 5.2.3)
92
+ sprockets-rails (>= 2.0.0)
93
+ rails-dom-testing (2.0.3)
94
+ activesupport (>= 4.2.0)
95
+ nokogiri (>= 1.6)
96
+ rails-html-sanitizer (1.3.0)
97
+ loofah (~> 2.3)
98
+ railties (5.2.3)
99
+ actionpack (= 5.2.3)
100
+ activesupport (= 5.2.3)
101
+ method_source
102
+ rake (>= 0.8.7)
103
+ thor (>= 0.19.0, < 2.0)
104
+ rake (10.5.0)
105
+ rspec (3.9.0)
106
+ rspec-core (~> 3.9.0)
107
+ rspec-expectations (~> 3.9.0)
108
+ rspec-mocks (~> 3.9.0)
109
+ rspec-core (3.9.0)
110
+ rspec-support (~> 3.9.0)
111
+ rspec-expectations (3.9.0)
112
+ diff-lcs (>= 1.2.0, < 2.0)
113
+ rspec-support (~> 3.9.0)
114
+ rspec-mocks (3.9.0)
115
+ diff-lcs (>= 1.2.0, < 2.0)
116
+ rspec-support (~> 3.9.0)
117
+ rspec-support (3.9.0)
118
+ sprockets (3.7.2)
119
+ concurrent-ruby (~> 1.0)
120
+ rack (> 1, < 3)
121
+ sprockets-rails (3.2.1)
122
+ actionpack (>= 4.0)
123
+ activesupport (>= 4.0)
124
+ sprockets (>= 3.0.0)
125
+ thor (0.20.3)
126
+ thread_safe (0.3.6)
127
+ tzinfo (1.2.5)
128
+ thread_safe (~> 0.1)
129
+ user_agent_parser (2.5.1)
130
+ websocket-driver (0.7.1)
131
+ websocket-extensions (>= 0.1.0)
132
+ websocket-extensions (0.1.4)
133
+
134
+ PLATFORMS
135
+ ruby
136
+
137
+ DEPENDENCIES
138
+ bundler
139
+ rails_same_site_cookie!
140
+ rake (~> 10.0)
141
+ rspec (~> 3.0)
142
+
143
+ BUNDLED WITH
144
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Philip Schinis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # RailsSameSiteCookie
2
+
3
+ This gem sets the SameSite=None directive on all cookies coming from your Rails app that are missing the SameSite directive. This behavior can also be limited to only requests coming from a specific user agent.
4
+
5
+ This is useful because in February 2020 Chrome will start treating any cookies without the SameSite directive set as though they are SameSite=Lax(https://www.chromestatus.com/feature/5088147346030592). This is a breaking change from the previous default behavior which was to treat those cookies as SameSite=None. See [this explanation](https://web.dev/samesite-cookies-explained/) for more information on the SameSite directive and the reasons for this change.
6
+
7
+ This new behavior shouldn't be a problem for most apps but if your Rails app provides an API that uses cookies for authentication (which itself may or may not be ill-advised), the new behavior means cookie authenticated requests to your API from third-party domains will no longer work in Chrome. In addition, fixing the problem isn't as simple as just setting SameSite=None on your app's cookies because there are a number of user agents that will either (a) ignore cookies with SameSite=None or (b) treat SameSite=None as SameSite=Strict. In other words, if a cookie-authenticated API sets SameSite=None it will break for some users, and if it doesn't set SameSite=None, it will also break for many users.
8
+
9
+ This gem fixes the above problems by explicity setting SameSite=None for all cookies where the SameSite directive is missing and the requesting user agent is not in Chrome's [provided list of known incompatible clients](https://www.chromium.org/updates/same-site/incompatible-clients).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'rails_same_site_cookie'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install rails_same_site_cookie
26
+
27
+ ## Usage
28
+
29
+ Once you've installed the gem that's basically it unless you want to limit the SameSite=None behavior to specific user agents. This can be useful, for example, if you have a cordova app (or other client) that accesses your API using a custom user agent string and you know in those situations that the cookie will not be accessible to third party sites because the containing browser will never be allowed to navigate to other domains.
30
+
31
+ To set this up:
32
+ ```ruby
33
+ #config/initializers/rails_same_site_cookie.rb
34
+ RailsSameSiteCookie.configuration do |config|
35
+ config.user_agent_regex = /MyCustomUserAgentString/
36
+ end
37
+ ```
38
+
39
+ Now only user agents that support SameSite=None and match the given regex string will have the directive set.
40
+
41
+ ## Development
42
+
43
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
44
+
45
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_same_site_cookie. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
54
+
55
+ ## Code of Conduct
56
+
57
+ Everyone interacting in the RailsSameSiteCookie project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rails_same_site_cookie/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_same_site_cookie"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,20 @@
1
+ require "rails_same_site_cookie/version"
2
+ require "rails_same_site_cookie/configuration"
3
+ require "rails_same_site_cookie/railtie" if defined?(Rails)
4
+
5
+ module RailsSameSiteCookie
6
+ class Error < StandardError; end
7
+
8
+ class << self
9
+ attr_writer :configuration
10
+ end
11
+
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def self.configure
17
+ yield(configuration)
18
+ end
19
+
20
+ end
@@ -0,0 +1,9 @@
1
+ module RailsSameSiteCookie
2
+ class Configuration
3
+ attr_accessor :user_agent_regex
4
+
5
+ def initialize
6
+ @user_agent_regex = nil
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ require 'rails_same_site_cookie/user_agent_checker'
2
+
3
+ module RailsSameSiteCookie
4
+ class Middleware
5
+
6
+ COOKIE_SEPARATOR = "\n".freeze
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ status, headers, body = @app.call(env)
14
+
15
+ regex = RailsSameSiteCookie.configuration.user_agent_regex
16
+ if headers['Set-Cookie'].present? and (regex.nil? or regex.match(env['HTTP_USER_AGENT']))
17
+ parser = UserAgentChecker.new(env['HTTP_USER_AGENT'])
18
+ if parser.send_same_site_none?
19
+ cookies = headers['Set-Cookie'].split(COOKIE_SEPARATOR)
20
+ ssl = Rack::Request.new(env).ssl?
21
+
22
+ cookies.each do |cookie|
23
+ next if cookie.blank?
24
+ if ssl and not cookie =~ /;\s*secure/i
25
+ cookie << '; Secure'
26
+ end
27
+
28
+ unless cookie =~ /;\s*samesite=/i
29
+ cookie << '; SameSite=None'
30
+ end
31
+
32
+ end
33
+
34
+ headers['Set-Cookie'] = cookies.join(COOKIE_SEPARATOR)
35
+ end
36
+ end
37
+
38
+ [status, headers, body]
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails_same_site_cookie/middleware'
2
+
3
+ module RailsSameSiteCookie
4
+ class Railtie < Rails::Railtie
5
+ initializer "rails_same_site_cookie.setup_middleware" do
6
+ Rails.application.middleware.insert_after ActionDispatch::Static, Middleware
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,83 @@
1
+ require 'user_agent_parser'
2
+
3
+ module RailsSameSiteCookie
4
+ class UserAgentChecker
5
+ attr_reader :user_agent
6
+
7
+ def user_agent=(user_agent)
8
+ @user_agent_str = user_agent
9
+ @user_agent = user_agent ? UserAgentParser.parse(user_agent) : nil
10
+ end
11
+
12
+ def initialize(user_agent=nil)
13
+ @user_agent_str = user_agent
14
+ @user_agent = UserAgentParser.parse(user_agent) if user_agent
15
+ end
16
+
17
+ def send_same_site_none?
18
+ return true if user_agent.nil? or @user_agent_str == ''
19
+ return !missing_same_site_none_support?
20
+ end
21
+
22
+ private
23
+ def missing_same_site_none_support?
24
+ has_webkit_ss_bug? or drops_unrecognized_ss_cookies?
25
+ end
26
+
27
+ def has_webkit_ss_bug?
28
+ is_ios_version?('12') or (is_mac_osx_version?('10','14') and is_safari?)
29
+ end
30
+
31
+ def drops_unrecognized_ss_cookies?
32
+ is_buggy_chrome? or is_buggy_uc?
33
+ end
34
+
35
+ def is_ios_version?(major)
36
+ user_agent.os&.family == 'iOS' and user_agent.os&.version&.major == major
37
+ end
38
+
39
+ def is_mac_osx_version?(major,minor)
40
+ user_agent.os&.family == 'Mac OS X' and user_agent.os&.version&.major == major and user_agent.os&.version&.minor == minor
41
+ end
42
+
43
+ def is_safari?
44
+ /Safari/.match(user_agent.family)
45
+ end
46
+
47
+ def is_buggy_chrome?
48
+ is_chromium_based? and is_chromium_version_between?((51...67))
49
+ end
50
+
51
+ def is_buggy_uc?
52
+ is_uc_browser? and not is_uc_version_at_least?(12,13,2)
53
+ end
54
+
55
+ def is_chromium_based?
56
+ /Chrom(e|ium)/.match(@user_agent_str)
57
+ end
58
+
59
+ def is_chromium_version_between?(range)
60
+ match = /Chrom[^\/]+\/(\d+)[\.\d]*/.match(@user_agent_str)
61
+ return false unless match
62
+ version = match[1].to_i
63
+ return range.include?(version)
64
+ end
65
+
66
+ def is_uc_browser?
67
+ user_agent.family == 'UC Browser'
68
+ end
69
+
70
+ def is_uc_version_at_least?(major,minor,build)
71
+ if user_agent.version&.major&.to_i == major
72
+ if user_agent.version&.minor&.to_i == minor
73
+ return user_agent.version&.patch&.to_i >= build
74
+ else
75
+ return user_agent.version&.minor&.to_i > minor
76
+ end
77
+ else
78
+ return user_agent.version&.major&.to_i > major
79
+ end
80
+ end
81
+
82
+ end
83
+ end