Skip to content

Commit 390fc00

Browse files
authored
Merge pull request #414 from twitter/add-same-site-none-support
Add support for SameSite=None
2 parents 1fa2083 + 17a5958 commit 390fc00

File tree

7 files changed

+44
-38
lines changed

7 files changed

+44
-38
lines changed

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.6.1
1+
2.6.5

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ language: ruby
22

33
rvm:
44
- ruby-head
5-
- 2.6.1
6-
- 2.5.0
7-
- 2.4.3
5+
- 2.5
6+
- 2.6
7+
- 2.7
88
- jruby-head
99

1010
env:

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ group :test do
99
gem "pry-nav"
1010
gem "rack"
1111
gem "rspec"
12-
gem "rubocop"
12+
gem "rubocop", "< 0.68"
1313
gem "rubocop-github"
1414
gem "term-ansicolor"
1515
gem "tins"
1616
end
1717

1818
group :guard do
1919
gem "growl"
20-
gem "guard-rspec", platforms: [:ruby_19, :ruby_20, :ruby_21, :ruby_22, :ruby_23, :ruby_24]
20+
gem "guard-rspec", platforms: [:ruby]
2121
gem "rb-fsevent"
2222
gem "terminal-notifier-guard"
2323
end

docs/cookies.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ config.cookies = {
5252
}
5353
```
5454

55-
`Strict` and `Lax` enforcement modes can also be specified using a Hash.
55+
`Strict`, `Lax`, and `None` enforcement modes can also be specified using a Hash.
5656

5757
```ruby
5858
config.cookies = {
5959
samesite: {
6060
strict: { only: ['_rails_session'] },
61-
lax: { only: ['_guest'] }
61+
lax: { only: ['_guest'] },
62+
none: { only: ['_tracking'] },
6263
}
6364
}
6465
```

lib/secure_headers/headers/cookie.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ def samesite_cookie
9494
"SameSite=Lax"
9595
elsif flag_samesite_strict?
9696
"SameSite=Strict"
97+
elsif flag_samesite_none?
98+
"SameSite=None"
9799
end
98100
end
99101

100102
def flag_samesite?
101103
return false if config == OPT_OUT || config[:samesite] == OPT_OUT
102-
flag_samesite_lax? || flag_samesite_strict?
104+
flag_samesite_lax? || flag_samesite_strict? || flag_samesite_none?
103105
end
104106

105107
def flag_samesite_lax?
@@ -110,6 +112,10 @@ def flag_samesite_strict?
110112
flag_samesite_enforcement?(:strict)
111113
end
112114

115+
def flag_samesite_none?
116+
flag_samesite_enforcement?(:none)
117+
end
118+
113119
def flag_samesite_enforcement?(mode)
114120
return unless config[:samesite]
115121

lib/secure_headers/utils/cookies_config.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ def validate_samesite_config!
4343

4444
# when configuring with booleans, only one enforcement is permitted
4545
def validate_samesite_boolean_config!
46-
if config[:samesite].key?(:lax) && config[:samesite][:lax].is_a?(TrueClass) && config[:samesite].key?(:strict)
47-
raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure lax and strict enforcement is not permitted.")
48-
elsif config[:samesite].key?(:strict) && config[:samesite][:strict].is_a?(TrueClass) && config[:samesite].key?(:lax)
49-
raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure lax and strict enforcement is not permitted.")
46+
if config[:samesite].key?(:lax) && config[:samesite][:lax].is_a?(TrueClass) && (config[:samesite].key?(:strict) || config[:samesite].key?(:none))
47+
raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure lax with strict or no enforcement is not permitted.")
48+
elsif config[:samesite].key?(:strict) && config[:samesite][:strict].is_a?(TrueClass) && (config[:samesite].key?(:lax) || config[:samesite].key?(:none))
49+
raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure strict with lax or no enforcement is not permitted.")
50+
elsif config[:samesite].key?(:none) && config[:samesite][:none].is_a?(TrueClass) && (config[:samesite].key?(:lax) || config[:samesite].key?(:strict))
51+
raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure no enforcement with lax or strict is not permitted.")
5052
end
5153
end
5254

spec/lib/secure_headers/headers/cookie_spec.rb

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,21 @@ module SecureHeaders
6868
end
6969

7070
context "SameSite cookies" do
71-
it "flags SameSite=Lax" do
72-
cookie = Cookie.new(raw_cookie, samesite: { lax: { only: ["_session"] } }, secure: OPT_OUT, httponly: OPT_OUT)
73-
expect(cookie.to_s).to eq("_session=thisisatest; SameSite=Lax")
74-
end
75-
76-
it "flags SameSite=Lax when configured with a boolean" do
77-
cookie = Cookie.new(raw_cookie, samesite: { lax: true}, secure: OPT_OUT, httponly: OPT_OUT)
78-
expect(cookie.to_s).to eq("_session=thisisatest; SameSite=Lax")
79-
end
80-
81-
it "does not flag cookies as SameSite=Lax when excluded" do
82-
cookie = Cookie.new(raw_cookie, samesite: { lax: { except: ["_session"] } }, secure: OPT_OUT, httponly: OPT_OUT)
83-
expect(cookie.to_s).to eq("_session=thisisatest")
84-
end
71+
%w(None Lax Strict).each do |flag|
72+
it "flags SameSite=#{flag}" do
73+
cookie = Cookie.new(raw_cookie, samesite: { flag.downcase.to_sym => { only: ["_session"] } }, secure: OPT_OUT, httponly: OPT_OUT)
74+
expect(cookie.to_s).to eq("_session=thisisatest; SameSite=#{flag}")
75+
end
8576

86-
it "flags SameSite=Strict" do
87-
cookie = Cookie.new(raw_cookie, samesite: { strict: { only: ["_session"] } }, secure: OPT_OUT, httponly: OPT_OUT)
88-
expect(cookie.to_s).to eq("_session=thisisatest; SameSite=Strict")
89-
end
77+
it "flags SameSite=#{flag} when configured with a boolean" do
78+
cookie = Cookie.new(raw_cookie, samesite: { flag.downcase.to_sym => true}, secure: OPT_OUT, httponly: OPT_OUT)
79+
expect(cookie.to_s).to eq("_session=thisisatest; SameSite=#{flag}")
80+
end
9081

91-
it "does not flag cookies as SameSite=Strict when excluded" do
92-
cookie = Cookie.new(raw_cookie, samesite: { strict: { except: ["_session"] }}, secure: OPT_OUT, httponly: OPT_OUT)
93-
expect(cookie.to_s).to eq("_session=thisisatest")
82+
it "does not flag cookies as SameSite=#{flag} when excluded" do
83+
cookie = Cookie.new(raw_cookie, samesite: { flag.downcase.to_sym => { except: ["_session"] } }, secure: OPT_OUT, httponly: OPT_OUT)
84+
expect(cookie.to_s).to eq("_session=thisisatest")
85+
end
9486
end
9587

9688
it "flags SameSite=Strict when configured with a boolean" do
@@ -149,10 +141,15 @@ module SecureHeaders
149141
end.to raise_error(CookiesConfigError)
150142
end
151143

152-
it "raises an exception when SameSite lax and strict enforcement modes are configured with booleans" do
153-
expect do
154-
Cookie.validate_config!(samesite: { lax: true, strict: true})
155-
end.to raise_error(CookiesConfigError)
144+
cookie_options = %i(none lax strict)
145+
cookie_options.each do |flag|
146+
(cookie_options - [flag]).each do |other_flag|
147+
it "raises an exception when SameSite #{flag} and #{other_flag} enforcement modes are configured with booleans" do
148+
expect do
149+
Cookie.validate_config!(samesite: { flag => true, other_flag => true})
150+
end.to raise_error(CookiesConfigError)
151+
end
152+
end
156153
end
157154

158155
it "raises an exception when SameSite lax and strict enforcement modes are configured with booleans" do

0 commit comments

Comments
 (0)