This repository was archived by the owner on May 16, 2020. It is now read-only.
forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityTest.php
More file actions
110 lines (78 loc) · 3.08 KB
/
Copy pathSecurityTest.php
File metadata and controls
110 lines (78 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php namespace CodeIgniter\Security;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\HTTP\IncomingRequest;
use Tests\Support\Config\MockAppConfig;
use CodeIgniter\Security\Exceptions\SecurityException;
use Tests\Support\Security\MockSecurity;
//--------------------------------------------------------------------
/**
* @backupGlobals enabled
*/
class SecurityTest extends \CIUnitTestCase {
protected function setUp()
{
parent::setUp();
$_COOKIE = [];
}
//--------------------------------------------------------------------
public function testBasicConfigIsSaved()
{
$security = new Security(new MockAppConfig());
$hash = $security->getCSRFHash();
$this->assertEquals(32, strlen($hash));
$this->assertEquals('csrf_test_name', $security->getCSRFTokenName());
}
//--------------------------------------------------------------------
public function testHashIsReadFromCookie()
{
$_COOKIE = [
'csrf_cookie_name' => '8b9218a55906f9dcc1dc263dce7f005a',
];
$security = new Security(new MockAppConfig());
$this->assertEquals('8b9218a55906f9dcc1dc263dce7f005a', $security->getCSRFHash());
}
//--------------------------------------------------------------------
public function testCSRFVerifySetsCookieWhenNotPOST()
{
$security = new MockSecurity(new MockAppConfig());
$_SERVER['REQUEST_METHOD'] = 'GET';
$security->CSRFVerify(new Request(new MockAppConfig()));
$this->assertEquals($_COOKIE['csrf_cookie_name'], $security->getCSRFHash());
}
//--------------------------------------------------------------------
public function testCSRFVerifyThrowsExceptionOnNoMatch()
{
$security = new MockSecurity(new MockAppConfig());
$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
$_COOKIE = [
'csrf_cookie_name' => '8b9218a55906f9dcc1dc263dce7f005b',
];
$this->expectException(SecurityException::class);
$security->CSRFVerify($request);
}
//--------------------------------------------------------------------
public function testCSRFVerifyReturnsSelfOnMatch()
{
$security = new MockSecurity(new MockAppConfig());
$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['csrf_test_name'] = '8b9218a55906f9dcc1dc263dce7f005a';
$_COOKIE = [
'csrf_cookie_name' => '8b9218a55906f9dcc1dc263dce7f005a',
];
$this->assertInstanceOf('CodeIgniter\Security\Security', $security->CSRFVerify($request));
$this->assertLogged('info', 'CSRF token verified');
}
//--------------------------------------------------------------------
public function testSanitizeFilename()
{
$security = new MockSecurity(new MockAppConfig());
$filename = './<!--foo-->';
$this->assertEquals('foo', $security->sanitizeFilename($filename));
}
//--------------------------------------------------------------------
}