@@ -62,38 +62,40 @@ module SecureHeaders
6262 end
6363
6464 context "SameSite cookies" do
65- context "when configured with a boolean" do
66- it "flags cookies as SameSite" do
67- cookie = Cookie . new ( raw_cookie , samesite : true )
68- expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_REGEXP )
69- end
65+ it "flags SameSite=Lax" do
66+ cookie = Cookie . new ( raw_cookie , samesite : { lax : { only : [ "_session" ] } } )
67+ expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_LAX_REGEXP )
7068 end
7169
72- context "when configured with a Hash" do
73- it "flags SameSite=Lax" do
74- cookie = Cookie . new ( raw_cookie , samesite : { lax : { only : [ "_session" ] } } )
75- expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_LAX_REGEXP )
76- end
70+ it "flags SameSite=Lax when configured with a boolean" do
71+ cookie = Cookie . new ( raw_cookie , samesite : { lax : true } )
72+ expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_LAX_REGEXP )
73+ end
7774
78- it "does not flag cookies as SameSite=Lax when excluded" do
79- cookie = Cookie . new ( raw_cookie , samesite : { lax : { except : [ "_session" ] } } )
80- expect ( cookie . to_s ) . not_to match ( Cookie ::SAMESITE_LAX_REGEXP )
81- end
75+ it "does not flag cookies as SameSite=Lax when excluded" do
76+ cookie = Cookie . new ( raw_cookie , samesite : { lax : { except : [ "_session" ] } } )
77+ expect ( cookie . to_s ) . not_to match ( Cookie ::SAMESITE_LAX_REGEXP )
78+ end
8279
83- it "flags SameSite=Strict" do
84- cookie = Cookie . new ( raw_cookie , samesite : { strict : { only : [ "_session" ] } } )
85- expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_STRICT_REGEXP )
86- end
80+ it "flags SameSite=Strict" do
81+ cookie = Cookie . new ( raw_cookie , samesite : { strict : { only : [ "_session" ] } } )
82+ expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_STRICT_REGEXP )
83+ end
8784
88- it "does not flag cookies as SameSite=Strict when excluded" do
89- cookie = Cookie . new ( raw_cookie , samesite : { strict : { except : [ "_session" ] } } )
90- expect ( cookie . to_s ) . not_to match ( Cookie ::SAMESITE_STRICT_REGEXP )
91- end
85+ it "does not flag cookies as SameSite=Strict when excluded" do
86+ cookie = Cookie . new ( raw_cookie , samesite : { strict : { except : [ "_session" ] } } )
87+ expect ( cookie . to_s ) . not_to match ( Cookie ::SAMESITE_STRICT_REGEXP )
88+ end
9289
93- it "flags properly when both lax and strict are configured" do
94- cookie = Cookie . new ( raw_cookie , samesite : { strict : { only : [ "_session" ] } , lax : { only : [ "_additional_session" ] } } )
95- expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_STRICT_REGEXP )
96- end
90+ it "flags SameSite=Strict when configured with a boolean" do
91+ cookie = Cookie . new ( raw_cookie , samesite : { strict : true } )
92+ expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_STRICT_REGEXP )
93+ end
94+
95+ it "flags properly when both lax and strict are configured" do
96+ raw_cookie = "_session=thisisatest"
97+ cookie = Cookie . new ( raw_cookie , samesite : { strict : { only : [ "_session" ] } , lax : { only : [ "_additional_session" ] } } )
98+ expect ( cookie . to_s ) . to match ( Cookie ::SAMESITE_STRICT_REGEXP )
9799 end
98100 end
99101 end
@@ -117,9 +119,39 @@ module SecureHeaders
117119 end . to raise_error ( CookiesConfigError )
118120 end
119121
122+ it "raises an exception when SameSite is not configured with a Hash" do
123+ expect do
124+ Cookie . validate_config! ( samesite : true )
125+ end . to raise_error ( CookiesConfigError )
126+ end
127+
128+ it "raises an exception when SameSite lax and strict enforcement modes are configured with booleans" do
129+ expect do
130+ Cookie . validate_config! ( samesite : { lax : true , strict : true } )
131+ end . to raise_error ( CookiesConfigError )
132+ end
133+
134+ it "raises an exception when SameSite lax and strict enforcement modes are configured with booleans" do
135+ expect do
136+ Cookie . validate_config! ( samesite : { lax : true , strict : { only : [ "_anything" ] } } )
137+ end . to raise_error ( CookiesConfigError )
138+ end
139+
120140 it "raises an exception when both only and except filters are provided to SameSite configurations" do
121141 expect do
122- Cookie . validate_config! ( samesite : { lax : { only : [ ] , except : [ ] } } )
142+ Cookie . validate_config! ( samesite : { lax : { only : [ "_anything" ] , except : [ "_anythingelse" ] } } )
143+ end . to raise_error ( CookiesConfigError )
144+ end
145+
146+ it "raises an exception when both lax and strict only filters are provided to SameSite configurations" do
147+ expect do
148+ Cookie . validate_config! ( samesite : { lax : { only : [ "_anything" ] } , strict : { only : [ "_anything" ] } } )
149+ end . to raise_error ( CookiesConfigError )
150+ end
151+
152+ it "raises an exception when both lax and strict only filters are provided to SameSite configurations" do
153+ expect do
154+ Cookie . validate_config! ( samesite : { lax : { except : [ "_anything" ] } , strict : { except : [ "_anything" ] } } )
123155 end . to raise_error ( CookiesConfigError )
124156 end
125157 end
0 commit comments