Skip to content

Commit b9113aa

Browse files
committed
Add tests for configuring cookie creation.
1 parent 9d62f2d commit b9113aa

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

Cake/Controller/Component/CsrfComponent.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class CsrfComponent extends Component {
6262
* the request is a GET request, and the cookie value
6363
* is absent a cookie will be set.
6464
*
65+
* RequestAction requests do not get checked, nor will
66+
* they set a cookie should it be missing.
67+
*
6568
* @param Cake\Event\Event $event
6669
* @return void
6770
*/
@@ -71,6 +74,10 @@ public function startup(Event $event) {
7174
$response = $controller->response;
7275
$cookieName = $this->settings['cookieName'];
7376

77+
if ($request->is('requested')) {
78+
return;
79+
}
80+
7481
if ($request->is('get') && $request->cookie($cookieName) === null) {
7582
$this->_setCookie($request, $response);
7683
}

Cake/Test/TestCase/Controller/Component/CsrfComponentTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,79 @@ public function testInvalidTokenRequestData($method) {
164164
$this->component->startUp($event);
165165
}
166166

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+
167242
}

0 commit comments

Comments
 (0)