@@ -164,4 +164,79 @@ public function testInvalidTokenRequestData($method) {
164
164
$ this ->component ->startUp ($ event );
165
165
}
166
166
167
+ /**
168
+ * Test that CSRF checks are not applied to request action requests.
169
+ *
170
+ * @return void
171
+ */
172
+ public function testCsrfValidationSkipsRequestAction () {
173
+ $ _SERVER ['REQUEST_METHOD ' ] = 'POST ' ;
174
+
175
+ $ controller = $ this ->getMock ('Cake\Controller\Controller ' );
176
+ $ controller ->request = new Request ([
177
+ 'params ' => ['requested ' => 1 ],
178
+ 'post ' => ['_csrfToken ' => 'nope ' ],
179
+ 'cookies ' => ['csrfToken ' => 'testing123 ' ]
180
+ ]);
181
+ $ controller ->response = new Response ();
182
+
183
+ $ event = new Event ('Controller.startup ' , $ controller );
184
+ $ result = $ this ->component ->startUp ($ event );
185
+ $ this ->assertNull ($ result , 'No error. ' );
186
+ }
187
+
188
+ /**
189
+ * Test that the configuration options work.
190
+ *
191
+ * @return void
192
+ */
193
+ public function testConfigurationCookieCreate () {
194
+ $ _SERVER ['REQUEST_METHOD ' ] = 'GET ' ;
195
+
196
+ $ controller = $ this ->getMock ('Cake\Controller\Controller ' );
197
+ $ controller ->request = new Request (['base ' => '/dir ' ]);
198
+ $ controller ->response = new Response ();
199
+
200
+ $ component = new CsrfComponent ($ this ->registry , [
201
+ 'cookieName ' => 'token ' ,
202
+ 'expiry ' => 90 ,
203
+ ]);
204
+
205
+ $ event = new Event ('Controller.startup ' , $ controller );
206
+ $ component ->startUp ($ event );
207
+
208
+ $ this ->assertEmpty ($ controller ->response ->cookie ('csrfToken ' ));
209
+ $ cookie = $ controller ->response ->cookie ('token ' );
210
+ $ this ->assertNotEmpty ($ cookie , 'Should set a token. ' );
211
+ $ this ->assertRegExp ('/^[a-f0-9]+$/ ' , $ cookie ['value ' ], 'Should look like a hash. ' );
212
+ $ this ->assertEquals (90 , $ cookie ['expiry ' ], 'session duration. ' );
213
+ $ this ->assertEquals ('/dir ' , $ cookie ['path ' ], 'session path. ' );
214
+ }
215
+
216
+ /**
217
+ * Test that the configuration options work.
218
+ *
219
+ * @return void
220
+ */
221
+ public function testConfigurationValidate () {
222
+ $ _SERVER ['REQUEST_METHOD ' ] = 'POST ' ;
223
+
224
+ $ controller = $ this ->getMock ('Cake\Controller\Controller ' );
225
+ $ controller ->request = new Request ([
226
+ 'cookies ' => ['csrfToken ' => 'nope ' , 'token ' => 'yes ' ],
227
+ 'post ' => ['_csrfToken ' => 'no match ' , 'token ' => 'yes ' ],
228
+ ]);
229
+ $ controller ->response = new Response ();
230
+
231
+ $ component = new CsrfComponent ($ this ->registry , [
232
+ 'cookieName ' => 'token ' ,
233
+ 'field ' => 'token ' ,
234
+ 'expiry ' => 90 ,
235
+ ]);
236
+
237
+ $ event = new Event ('Controller.startup ' , $ controller );
238
+ $ result = $ component ->startUp ($ event );
239
+ $ this ->assertNull ($ result , 'Config settings should work. ' );
240
+ }
241
+
167
242
}
0 commit comments