Skip to content

Commit 198c264

Browse files
committed
CSRF more tests
1 parent 6db0ddf commit 198c264

1 file changed

Lines changed: 82 additions & 2 deletions

File tree

tests/system/Security/SecurityTest.php

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testCSRFVerifySetsCookieWhenNotPOST()
6262

6363
//--------------------------------------------------------------------
6464

65-
public function testCSRFVerifyThrowsExceptionOnNoMatch()
65+
public function testCSRFVerifyPostThrowsExceptionOnNoMatch()
6666
{
6767
$security = new MockSecurity(new MockAppConfig());
6868
$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
@@ -79,19 +79,99 @@ public function testCSRFVerifyThrowsExceptionOnNoMatch()
7979

8080
//--------------------------------------------------------------------
8181

82-
public function testCSRFVerifyReturnsSelfOnMatch()
82+
public function testCSRFVerifyPostReturnsSelfOnMatch()
8383
{
8484
$security = new MockSecurity(new MockAppConfig());
8585
$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
8686

8787
$_SERVER['REQUEST_METHOD'] = 'POST';
88+
$_POST['foo'] = 'bar';
8889
$_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
8990
$_COOKIE = [
9091
'csrf_cookie_name' => '8b9218a55906f9dcc1dc263dce7f005a',
9192
];
9293

9394
$this->assertInstanceOf('CodeIgniter\Security\Security', $security->CSRFVerify($request));
9495
$this->assertLogged('info', 'CSRF token verified');
96+
97+
$this->assertTrue(count($_POST) === 1);
98+
}
99+
100+
//--------------------------------------------------------------------
101+
102+
public function testCSRFVerifyHeaderThrowsExceptionOnNoMatch()
103+
{
104+
$security = new MockSecurity(new MockAppConfig());
105+
$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
106+
107+
$request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005a');
108+
109+
$_SERVER['REQUEST_METHOD'] = 'POST';
110+
$_COOKIE = [
111+
'csrf_cookie_name' => '8b9218a55906f9dcc1dc263dce7f005b',
112+
];
113+
114+
$this->expectException(SecurityException::class);
115+
$security->CSRFVerify($request);
116+
}
117+
118+
//--------------------------------------------------------------------
119+
120+
public function testCSRFVerifyHeaderReturnsSelfOnMatch()
121+
{
122+
$security = new MockSecurity(new MockAppConfig());
123+
$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
124+
125+
$request->setHeader('X-CSRF-TOKEN', '8b9218a55906f9dcc1dc263dce7f005a');
126+
127+
$_SERVER['REQUEST_METHOD'] = 'POST';
128+
$_POST['foo'] = 'bar';
129+
$_COOKIE = [
130+
'csrf_cookie_name' => '8b9218a55906f9dcc1dc263dce7f005a',
131+
];
132+
133+
$this->assertInstanceOf('CodeIgniter\Security\Security', $security->CSRFVerify($request));
134+
$this->assertLogged('info', 'CSRF token verified');
135+
136+
$this->assertTrue(count($_POST) === 1);
137+
}
138+
139+
//--------------------------------------------------------------------
140+
141+
public function testCSRFVerifyJsonThrowsExceptionOnNoMatch()
142+
{
143+
$security = new MockSecurity(new MockAppConfig());
144+
$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
145+
146+
$request->setBody('{"csrf_test_name":"8b9218a55906f9dcc1dc263dce7f005a"}');
147+
148+
$_SERVER['REQUEST_METHOD'] = 'POST';
149+
$_COOKIE = [
150+
'csrf_cookie_name' => '8b9218a55906f9dcc1dc263dce7f005b',
151+
];
152+
153+
$this->expectException(SecurityException::class);
154+
$security->CSRFVerify($request);
155+
}
156+
157+
//--------------------------------------------------------------------
158+
159+
public function testCSRFVerifyJsonReturnsSelfOnMatch()
160+
{
161+
$security = new MockSecurity(new MockAppConfig());
162+
$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
163+
164+
$request->setBody('{"csrf_test_name":"8b9218a55906f9dcc1dc263dce7f005a","foo":"bar"}');
165+
166+
$_SERVER['REQUEST_METHOD'] = 'POST';
167+
$_COOKIE = [
168+
'csrf_cookie_name' => '8b9218a55906f9dcc1dc263dce7f005a',
169+
];
170+
171+
$this->assertInstanceOf('CodeIgniter\Security\Security', $security->CSRFVerify($request));
172+
$this->assertLogged('info', 'CSRF token verified');
173+
174+
$this->assertTrue($request->getBody() === '{"foo":"bar"}');
95175
}
96176

97177
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)