This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
CSRF.php
executable file
·339 lines (304 loc) · 9.45 KB
/
CSRF.php
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
declare(strict_types=1);
namespace Airship\Engine\Security;
use Airship\Alerts\Security\CSRF\InvalidConfig;
use ParagonIE\ConstantTime\Base64UrlSafe;
use ParagonIE\Halite\Alerts\CannotPerformOperation;
use ParagonIE\Halite\Util as CryptoUtil;
/**
* Class CSRF
* @package Airship\Engine\Security
*/
class CSRF
{
const FORM_TOKEN = '_CSRF_TOKEN';
/**
* @var int
*/
protected $recycleAfter = 1024;
/**
* @var bool Default to FALSE to be friendly to Tor/Mobile users
*/
protected $hmacIP = false;
/**
* @var bool
*/
protected $expireOld = false;
/**
* @var string
*/
protected $ajaxTokenIndex = 'AjaxCSRFToken';
/**
* @var string
*/
protected $sessionIndex = 'CSRF';
/**
* CSRF constructor.
*
* @param array $options
* @throws InvalidConfig
* @throws \TypeError
*/
public function __construct(array $options = [])
{
$this->reconfigure($options);
}
/**
* Insert a CSRF token to a form
*
* @param string $lockTo This CSRF token is only valid for this HTTP request endpoint
* @param bool $echo if true, echo instead of returning
* @return string
*
* @throws \TypeError
*/
public function insertToken(
string $lockTo = '',
bool $echo = true
): string {
if (empty($lockTo)) {
$lockTo = $_SERVER['REQUEST_URI'] ?? '/';
}
$ret = '<input type="hidden"' .
' name="' . Util::noHTML(self::FORM_TOKEN) . '"' .
' value="' . $this->getTokenString($lockTo) . '"' .
' />';
if ($echo) {
echo $ret;
return '';
}
return $ret;
}
/**
* @param string $lockTo
*
* @return string
* @throws \TypeError
*/
public function ajaxToken(string $lockTo = ''): string
{
return $this->getTokenString($lockTo, true);
}
/**
* Retrieve a token array for unit testing endpoints
*
* @param string $lockTo - Only get tokens locked to a particular form
*
* @return string
* @throws \TypeError
*/
public function getTokenString(string $lockTo = '', bool $ajax = false): string
{
$sessionIndex = $ajax
? $this->ajaxTokenIndex
: $this->sessionIndex;
if (!isset($_SESSION[$sessionIndex])) {
$_SESSION[$sessionIndex] = [];
}
if (\preg_match('#/$#', $lockTo)) {
$lockTo = Util::subString($lockTo, 0, Util::stringLength($lockTo) - 1);
}
list($index, $token) = $ajax
? $this->generateAjaxToken($lockTo)
: $this->generateToken($lockTo);
if ($this->hmacIP) {
// Use a keyed BLAKE2b hash to only allow this particular IP to send this request
$token = Base64UrlSafe::encode(
\sodium_crypto_generichash(
$_SERVER['REMOTE_ADDR'] ?? '127.0.0.1',
Base64UrlSafe::decode($token),
\SODIUM_CRYPTO_GENERICHASH_BYTES
)
);
}
return $index . ':' . $token;
}
/**
* Challenge-reponse authentication for AJAX calls.
* @param string $token
*
* @return bool
* @throws CannotPerformOperation
* @throws \TypeError
*/
public function checkAjax(string $token): bool
{
return $this->checkInternal($this->ajaxTokenIndex, $token);
}
/**
* Validate a request based on $_SESSION and $_POST data
*
* @return bool
* @throws CannotPerformOperation
* @throws \TypeError
*/
public function check(): bool
{
if (!isset($_POST[self::FORM_TOKEN]) || !\is_string($_POST[self::FORM_TOKEN])) {
return false;
}
return $this->checkInternal($this->sessionIndex, $_POST[self::FORM_TOKEN]);
}
/**
* Internal check
*
* @param string $sessionIndex
* @param string $providedValue
*
* @return bool
* @throws CannotPerformOperation
* @throws \TypeError
*/
protected function checkInternal(string $sessionIndex, string $providedValue): bool
{
if (!isset($_SESSION[$sessionIndex])) {
// We don't even have a session array initialized
$_SESSION[$sessionIndex] = [];
return false;
}
if (\strpos($providedValue, ':') === false) {
return false;
}
// Let's pull the POST data
list ($index, $token) = \explode(':', $providedValue);
if (empty($index) || empty($token)) {
return false;
}
if (!isset($_SESSION[$sessionIndex][$index])) {
// CSRF Token not found
return false;
}
// Grab the value stored at $index
$stored = $_SESSION[$sessionIndex][$index];
if (!empty($stored['one-time'])) {
// We don't need this anymore
unset($_SESSION[$sessionIndex][$index]);
}
// Which form action="" is this token locked to?
$lockTo = $_SERVER['REQUEST_URI'];
if (\preg_match('#/$#', $lockTo)) {
// Trailing slashes are to be ignored
$lockTo = Util::subString($lockTo, 0, Util::stringLength($lockTo) - 1);
}
if (!empty($stored['lockto'])) {
if (!\hash_equals($lockTo, $stored['lockto'])) {
// Form target did not match the request this token is locked to!
return false;
}
}
// This is the expected token value
// We just stored it wholesale
$expected = $stored['token'];
if ($this->hmacIP) {
// We mixed in the client IP address to generate the output
$expected = Base64UrlSafe::encode(
CryptoUtil::raw_keyed_hash(
$_SERVER['REMOTE_ADDR'] ?? '127.0.0.1',
(string) Base64UrlSafe::decode($stored['token'])
)
);
}
return \hash_equals($token, $expected);
}
/**
* Use this to change the configuration settings.
* Only use this if you know what you are doing.
*
* @param array $options
* @throws InvalidConfig
* @return void
* @throws \TypeError
*/
public function reconfigure(array $options = []): void
{
foreach ($options as $opt => $val) {
switch ($opt) {
case 'ajaxTokenIndex':
case 'recycleAfter':
case 'hmacIP':
case 'expireOld':
case 'sessionIndex':
$this->{$opt} = $val;
break;
default:
throw new InvalidConfig(
\trk('errors.object.invalid_property', $opt, __CLASS__)
);
}
}
}
/**
* Special case handler for AJAX tokens:
*
* @param string $lockTo What URI endpoint this is valid for
* @return array [string, string]
*/
protected function generateAjaxToken(string $lockTo = '', bool $oneTime = false): array
{
return $this->generateToken($lockTo, $oneTime, $this->ajaxTokenIndex);
}
/**
* Generate, store, and return the index and token
*
* @param string $lockTo What URI endpoint this is valid for
* @param bool $oneTime Delete this value after being used once?
* @param string $sessionIndex Is this an AJAX or a Form token?
* @return array [string, string]
*/
protected function generateToken(string $lockTo = '', bool $oneTime = true, string $sessionIndex = ''): array
{
if (empty($sessionIndex)) {
$sessionIndex = $this->sessionIndex;
}
/** @var string $index */
// Create a distinct index:
do {
$index = Base64UrlSafe::encode(
\random_bytes(18)
);
} while (isset($_SESSION[$sessionIndex][$index]));
$token = Base64UrlSafe::encode(\random_bytes(33));
$_SESSION[$sessionIndex][$index] = [
'created' => \intval(\date('YmdHis')),
'one-time' => $oneTime,
'uri' => isset($_SERVER['REQUEST_URI'])
? $_SERVER['REQUEST_URI']
: $_SERVER['SCRIPT_NAME'],
'token' => $token
];
if (!empty($lockTo)) {
// Get rid of trailing slashes.
if (\preg_match('#/$#', $lockTo)) {
$lockTo = Util::subString($lockTo, 0, Util::stringLength($lockTo) - 1);
}
$_SESSION[$sessionIndex][$index]['lockto'] = $lockTo;
}
$this->recycleTokens();
return [$index, $token];
}
/**
* Enforce an upper limit on the number of tokens stored in session state
* by removing the oldest tokens first.
*
* @return void
*/
protected function recycleTokens(): void
{
if (!$this->expireOld) {
// This is turned off.
return;
}
foreach ([$this->sessionIndex, $this->ajaxTokenIndex] as $idx) {
// Sort by creation time
\uasort(
$_SESSION[$idx],
function (array $a, array $b): int {
return (int) ($a['created'] <=> $b['created']);
}
);
while (\count($_SESSION[$idx]) > $this->recycleAfter) {
\array_shift($_SESSION[$idx]);
}
}
}
}