-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSecurity.php
416 lines (367 loc) · 10.3 KB
/
Security.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Security;
use CodeIgniter\Cookie\Cookie;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\Security\Exceptions\SecurityException;
use Config\App;
use Config\Cookie as CookieConfig;
use Config\Security as SecurityConfig;
/**
* Class Security
*
* Provides methods that help protect your site against
* Cross-Site Request Forgery attacks.
*/
class Security implements SecurityInterface
{
/**
* CSRF Hash
*
* Random hash for Cross Site Request Forgery protection cookie
*
* @var string|null
*/
protected $hash;
/**
* CSRF Token Name
*
* Token name for Cross Site Request Forgery protection cookie.
*
* @var string
*/
protected $tokenName = 'csrf_token_name';
/**
* CSRF Header Name
*
* Token name for Cross Site Request Forgery protection cookie.
*
* @var string
*/
protected $headerName = 'X-CSRF-TOKEN';
/**
* The CSRF Cookie instance.
*
* @var Cookie
*/
protected $cookie;
/**
* CSRF Cookie Name
*
* Cookie name for Cross Site Request Forgery protection cookie.
*
* @var string
*/
protected $cookieName = 'csrf_cookie_name';
/**
* CSRF Expires
*
* Expiration time for Cross Site Request Forgery protection cookie.
*
* Defaults to two hours (in seconds).
*
* @var int
*
* @deprecated
*/
protected $expires = 7200;
/**
* CSRF Regenerate
*
* Regenerate CSRF Token on every request.
*
* @var bool
*/
protected $regenerate = true;
/**
* CSRF Redirect
*
* Redirect to previous page with error on failure.
*
* @var bool
*/
protected $redirect = true;
/**
* CSRF SameSite
*
* Setting for CSRF SameSite cookie token.
*
* Allowed values are: None - Lax - Strict - ''.
*
* Defaults to `Lax` as recommended in this link:
*
* @see https://portswigger.net/web-security/csrf/samesite-cookies
*
* @var string
*
* @deprecated
*/
protected $samesite = Cookie::SAMESITE_LAX;
/**
* Constructor.
*
* Stores our configuration and fires off the init() method to setup
* initial state.
*/
public function __construct(App $config)
{
/** @var SecurityConfig $security */
$security = config('Security');
// Store CSRF-related configurations
$this->tokenName = $security->tokenName ?? $config->CSRFTokenName ?? $this->tokenName;
$this->headerName = $security->headerName ?? $config->CSRFHeaderName ?? $this->headerName;
$this->regenerate = $security->regenerate ?? $config->CSRFRegenerate ?? $this->regenerate;
$rawCookieName = $security->cookieName ?? $config->CSRFCookieName ?? $this->cookieName;
/** @var CookieConfig $cookie */
$cookie = config('Cookie');
$cookiePrefix = $cookie->prefix ?? $config->cookiePrefix;
$this->cookieName = $cookiePrefix . $rawCookieName;
$expires = $security->expires ?? $config->CSRFExpire ?? 7200;
Cookie::setDefaults($cookie);
$this->cookie = new Cookie($rawCookieName, $this->generateHash(), [
'expires' => $expires === 0 ? 0 : time() + $expires,
]);
}
/**
* CSRF Verify
*
* @throws SecurityException
*
* @return $this|false
*
* @deprecated Use `CodeIgniter\Security\Security::verify()` instead of using this method.
*
* @codeCoverageIgnore
*/
public function CSRFVerify(RequestInterface $request)
{
return $this->verify($request);
}
/**
* Returns the CSRF Hash.
*
* @deprecated Use `CodeIgniter\Security\Security::getHash()` instead of using this method.
*
* @codeCoverageIgnore
*/
public function getCSRFHash(): ?string
{
return $this->getHash();
}
/**
* Returns the CSRF Token Name.
*
* @deprecated Use `CodeIgniter\Security\Security::getTokenName()` instead of using this method.
*
* @codeCoverageIgnore
*/
public function getCSRFTokenName(): string
{
return $this->getTokenName();
}
/**
* CSRF Verify
*
* @throws SecurityException
*
* @return $this|false
*/
public function verify(RequestInterface $request)
{
// If it's not a POST request we will set the CSRF cookie.
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') {
return $this->sendCookie($request);
}
// Does the token exist in POST, HEADER or optionally php:://input - json data.
if ($request->hasHeader($this->headerName) && ! empty($request->getHeader($this->headerName)->getValue())) {
$tokenName = $request->getHeader($this->headerName)->getValue();
} else {
$json = json_decode($request->getBody());
if (! empty($request->getBody()) && ! empty($json) && json_last_error() === JSON_ERROR_NONE) {
$tokenName = $json->{$this->tokenName} ?? null;
} else {
$tokenName = null;
}
}
$token = $_POST[$this->tokenName] ?? $tokenName;
// Does the tokens exist in both the POST/POSTed JSON and COOKIE arrays and match?
if (! isset($token, $_COOKIE[$this->cookieName]) || ! hash_equals($token, $_COOKIE[$this->cookieName])) {
throw SecurityException::forDisallowedAction();
}
if (isset($_POST[$this->tokenName])) {
// We kill this since we're done and we don't want to pollute the POST array.
unset($_POST[$this->tokenName]);
$request->setGlobal('post', $_POST);
} elseif (isset($json->{$this->tokenName})) {
// We kill this since we're done and we don't want to pollute the JSON data.
unset($json->{$this->tokenName});
$request->setBody(json_encode($json));
}
if ($this->regenerate) {
$this->hash = null;
unset($_COOKIE[$this->cookieName]);
}
$this->cookie = $this->cookie->withValue($this->generateHash());
$this->sendCookie($request);
log_message('info', 'CSRF token verified.');
return $this;
}
/**
* Returns the CSRF Hash.
*/
public function getHash(): ?string
{
return $this->hash;
}
/**
* Returns the CSRF Token Name.
*/
public function getTokenName(): string
{
return $this->tokenName;
}
/**
* Returns the CSRF Header Name.
*/
public function getHeaderName(): string
{
return $this->headerName;
}
/**
* Returns the CSRF Cookie Name.
*/
public function getCookieName(): string
{
return $this->cookieName;
}
/**
* Check if CSRF cookie is expired.
*
* @deprecated
*
* @codeCoverageIgnore
*/
public function isExpired(): bool
{
return $this->cookie->isExpired();
}
/**
* Check if request should be redirect on failure.
*/
public function shouldRedirect(): bool
{
return $this->redirect;
}
/**
* Sanitize Filename
*
* Tries to sanitize filenames in order to prevent directory traversal attempts
* and other security threats, which is particularly useful for files that
* were supplied via user input.
*
* If it is acceptable for the user input to include relative paths,
* e.g. file/in/some/approved/folder.txt, you can set the second optional
* parameter, $relative_path to TRUE.
*
* @param string $str Input file name
* @param bool $relativePath Whether to preserve paths
*/
public function sanitizeFilename(string $str, bool $relativePath = false): string
{
// List of sanitize filename strings
$bad = [
'../',
'<!--',
'-->',
'<',
'>',
"'",
'"',
'&',
'$',
'#',
'{',
'}',
'[',
']',
'=',
';',
'?',
'%20',
'%22',
'%3c',
'%253c',
'%3e',
'%0e',
'%28',
'%29',
'%2528',
'%26',
'%24',
'%3f',
'%3b',
'%3d',
];
if (! $relativePath) {
$bad[] = './';
$bad[] = '/';
}
$str = remove_invisible_characters($str, false);
do {
$old = $str;
$str = str_replace($bad, '', $str);
} while ($old !== $str);
return stripslashes($str);
}
/**
* Generates the CSRF Hash.
*/
protected function generateHash(): string
{
if ($this->hash === null) {
// If the cookie exists we will use its value.
// We don't necessarily want to regenerate it with
// each page load since a page could contain embedded
// sub-pages causing this feature to fail
if (isset($_COOKIE[$this->cookieName])
&& is_string($_COOKIE[$this->cookieName])
&& preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->cookieName]) === 1
) {
return $this->hash = $_COOKIE[$this->cookieName];
}
$this->hash = bin2hex(random_bytes(16));
}
return $this->hash;
}
/**
* CSRF Send Cookie
*
* @return false|Security
*/
protected function sendCookie(RequestInterface $request)
{
if ($this->cookie->isSecure() && ! $request->isSecure()) {
return false;
}
$this->doSendCookie();
log_message('info', 'CSRF cookie sent.');
return $this;
}
/**
* Actual dispatching of cookies.
* Extracted for this to be unit tested.
*
* @codeCoverageIgnore
*/
protected function doSendCookie(): void
{
cookies([$this->cookie], false)->dispatch();
}
}