-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTokenStoreTest.php
More file actions
183 lines (151 loc) · 4.39 KB
/
TokenStoreTest.php
File metadata and controls
183 lines (151 loc) · 4.39 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace GT\Csrf\Test;
use Exception;
use GT\Csrf\ArrayTokenStore;
use GT\Csrf\Exception\CsrfException;
use GT\Csrf\Exception\CsrfTokenInvalidException;
use GT\Csrf\Exception\CsrfTokenMissingException;
use GT\Csrf\Exception\CsrfTokenSpentException;
use GT\Csrf\HTMLDocumentProtector;
use PHPUnit\Framework\TestCase;
use stdClass;
class TokenStoreTest extends TestCase {
const ONE_FORM = <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Test HTML</title>
</head>
<body>
<h1>This HTML is for the unit test.</h1>
<p>Hello, CSRF!</p>
<form method="post">
<input required />
<button>Submit me</button>
</form>
</body>
</html>
HTML;
/** no post request received */
public function testVerify_noPost():void {
$exception = null;
try {
$sut = new ArrayTokenStore();
$sut->verify([]);
}
catch(CsrfException $exception) {}
self::assertNull($exception);
}
/** POST request received but without a token */
public function testVerify_noToken():void {
$post = [];
$post["doink"] = "binky";
$sut = new ArrayTokenStore();
$this->expectException(CSRFTokenMissingException::class);
$sut->verify($post);
}
/** POST request received with token but invalid */
public function testVerify_invalidToken():void {
$post = [];
$post["doink"] = "binky";
$post[HTMLDocumentProtector::TOKEN_NAME] = "12321";
$sut = new ArrayTokenStore();
$this->expectException(CSRFTokenInvalidException::class);
$sut->verify($post);
}
/** POST request received with token but invalid */
public function testVerify_spentToken():void {
$tokenStore = new ArrayTokenStore();
$token = $tokenStore->generateNewToken();
$tokenStore->saveToken($token);
$tokenStore->consumeToken($token);
$post = [];
$post["doink"] = "binky";
// add the token as if it were from a previous page
$post[HTMLDocumentProtector::TOKEN_NAME] = $token;
$this->expectException(CSRFTokenSpentException::class);
$tokenStore->verify($post);
}
/** POST request received with token and valid */
public function testVerify_validToken():void {
$tokenStore = new ArrayTokenStore();
$token = $tokenStore->generateNewToken();
$tokenStore->saveToken($token);
$post = [];
$post["doink"] = "binky";
// add the token as if it were from a previous page
$post[HTMLDocumentProtector::TOKEN_NAME] = $token;
$exception = null;
try {
$tokenStore->verify($post);
}
catch(CsrfException $exception) {}
self::assertNull($exception);
}
/**
* php.gt/webengine provides user input as a custom object
* with an asArray function.
*/
public function testVerify_validTokenObj():void {
$tokenStore = new ArrayTokenStore();
$token = $tokenStore->generateNewToken();
$tokenStore->saveToken($token);
$arrayData = [
HTMLDocumentProtector::TOKEN_NAME => $token,
"example" => uniqid(),
"test" => "testValidTokenObj",
];
$mockBuilder = self::getMockBuilder(StdClass::class);
$mockBuilder->addMethods(["asArray"]);
$post = $mockBuilder->getMock();
$post->method("asArray")
->willReturn($arrayData);
$exception = null;
try {
$tokenStore->verify($post);
}
catch(CsrfException $exception) {}
self::assertNull($exception);
}
/** check that repeated calls to the token generator result in unique tokens */
public function testGenerateNewToken_codesAreUnique():void {
$sut = new ArrayTokenStore();
$previousTokens = [];
$iterations = 5;
for($i = 0; $i < $iterations; $i++) {
$token = $sut->generateNewToken();
self::assertArrayNotHasKey($token, $previousTokens);
$previousTokens[$token] = null;
}
self::assertCount($iterations, $previousTokens);
}
public function testSaveToken_tokenLengthChange():void {
$sut = new ArrayTokenStore();
$tokenLength = 16;
$sut->setTokenLength($tokenLength);
$token = $sut->generateNewToken();
self::assertStringStartsWith("CSRF_", $token);
self::assertEquals(
strlen("CSRF_") + $tokenLength,
strlen($token)
);
// now make sure the custom-length token is successfully stored
$sut->saveToken($token);
$exception = null;
try {
$sut->verifyToken($token);
}
catch(Exception $exception) {}
self::assertNull($exception);
}
public function testGenerateNewToken_usesCsrfUlidPrefix():void {
$sut = new ArrayTokenStore();
$token = $sut->generateNewToken();
self::assertStringStartsWith("CSRF_", $token);
self::assertSame(
strlen("CSRF_") + 32,
strlen($token)
);
}
}