-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathsecurity.lib.php
More file actions
executable file
·587 lines (529 loc) · 19.9 KB
/
security.lib.php
File metadata and controls
executable file
·587 lines (529 loc) · 19.9 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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Component\HTMLPurifier\Filter\AllowIframes;
use ChamiloSession as Session;
/**
* This is the security library for Chamilo.
*
* This library is based on recommendations found in the PHP5 Certification
* Guide published at PHP|Architect, and other recommendations found on
* http://www.phpsec.org/
* The principles here are that all data is tainted (most scripts of Chamilo are
* open to the public or at least to a certain public that could be malicious
* under specific circumstances). We use the white list approach, where as we
* consider that data can only be used in the database or in a file if it has
* been filtered.
*
* For session fixation, use ...
* For session hijacking, use get_ua() and check_ua()
* For Cross-Site Request Forgeries, use get_token() and check_tocken()
* For basic filtering, use filter()
* For files inclusions (using dynamic paths) use check_rel_path() and check_abs_path()
*
* @author Yannick Warnier <ywarnier@beeznest.org>
*/
/**
* Security class.
*
* Include/require it in your code and call Security::function()
* to use its functionalities.
*
* This class can also be used as a container for filtered data, by creating
* a new Security object and using $secure->filter($new_var,[more options])
* and then using $secure->clean['var'] as a filtered equivalent, although
* this is *not* mandatory at all.
*/
class Security
{
public static $clean = [];
/**
* Checks if the absolute path (directory) given is really under the
* checker path (directory).
*
* @param string Absolute path to be checked (with trailing slash)
* @param string Checker path under which the path
* should be (absolute path, with trailing slash, get it from api_get_path(SYS_COURSE_PATH))
*
* @return bool True if the path is under the checker, false otherwise
*/
public static function check_abs_path($abs_path, $checker_path)
{
// The checker path must be set.
if (empty($checker_path)) {
return false;
}
// Clean $abs_path.
$abs_path = str_replace(['//', '../'], ['/', ''], $abs_path);
$true_path = str_replace("\\", '/', realpath($abs_path));
$checker_path = str_replace("\\", '/', realpath($checker_path));
if (empty($checker_path)) {
return false;
}
$found = strpos($true_path.'/', $checker_path);
if ($found === 0) {
return true;
} else {
// Code specific to Windows and case-insensitive behaviour
if (api_is_windows_os()) {
$found = stripos($true_path.'/', $checker_path);
if ($found === 0) {
return true;
}
}
}
return false;
}
/**
* Checks if the relative path (directory) given is really under the
* checker path (directory).
*
* @param string Relative path to be checked (relative to the current directory) (with trailing slash)
* @param string Checker path under which the path
* should be (absolute path, with trailing slash, get it from api_get_path(SYS_COURSE_PATH))
*
* @return bool True if the path is under the checker, false otherwise
*/
public static function check_rel_path($rel_path, $checker_path)
{
// The checker path must be set.
if (empty($checker_path)) {
return false;
}
$current_path = getcwd(); // No trailing slash.
if (substr($rel_path, -1, 1) != '/') {
$rel_path = '/'.$rel_path;
}
$abs_path = $current_path.$rel_path;
$true_path = str_replace("\\", '/', realpath($abs_path));
$found = strpos($true_path.'/', $checker_path);
if ($found === 0) {
return true;
}
return false;
}
/**
* Filters dangerous filenames (*.php[.]?* and .htaccess) and returns it in
* a non-executable form (for PHP and htaccess, this is still vulnerable to
* other languages' files extensions).
*
* @param string $filename Unfiltered filename
*
* @return string
*/
public static function filter_filename($filename)
{
return disable_dangerous_file($filename);
}
/**
* @return string
*/
public static function getTokenFromSession()
{
return Session::read('sec_token');
}
/**
* This function checks that the token generated in get_token() has been kept (prevents
* Cross-Site Request Forgeries attacks).
*
* @param string The array in which to get the token ('get' or 'post')
*
* @return bool True if it's the right token, false otherwise
*/
public static function check_token($request_type = 'post', FormValidator $form = null)
{
$sessionToken = Session::read('sec_token');
switch ($request_type) {
case 'request':
if (!empty($sessionToken) && isset($_REQUEST['sec_token']) && $sessionToken === $_REQUEST['sec_token']) {
return true;
}
return false;
case 'get':
if (!empty($sessionToken) && isset($_GET['sec_token']) && $sessionToken === $_GET['sec_token']) {
return true;
}
return false;
case 'post':
if (!empty($sessionToken) && isset($_POST['sec_token']) && $sessionToken === $_POST['sec_token']) {
return true;
}
return false;
case 'form':
$token = $form->getSubmitValue('protect_token');
if (!empty($sessionToken) && !empty($token) && $sessionToken === $token) {
return true;
}
return false;
default:
if (!empty($sessionToken) && isset($request_type) && $sessionToken === $request_type) {
return true;
}
return false;
}
return false; // Just in case, don't let anything slip.
}
/**
* Checks the user agent of the client as recorder by get_ua() to prevent
* most session hijacking attacks.
*
* @return bool True if the user agent is the same, false otherwise
*/
public static function check_ua()
{
$security = Session::read('sec_ua');
$securitySeed = Session::read('sec_ua_seed');
if ($security === $_SERVER['HTTP_USER_AGENT'].$securitySeed) {
return true;
}
return false;
}
/**
* Clear the security token from the session.
*/
public static function clear_token()
{
Session::erase('sec_token');
}
/**
* This function sets a random token to be included in a form as a hidden field
* and saves it into the user's session. Returns an HTML form element
* This later prevents Cross-Site Request Forgeries by checking that the user is really
* the one that sent this form in knowingly (this form hasn't been generated from
* another website visited by the user at the same time).
* Check the token with check_token().
*
* @return string Hidden-type input ready to insert into a form
*/
public static function get_HTML_token()
{
$token = md5(uniqid(rand(), true));
$string = '<input type="hidden" name="sec_token" value="'.$token.'" />';
Session::write('sec_token', $token);
return $string;
}
/**
* This function sets a random token to be included in a form as a hidden field
* and saves it into the user's session.
* This later prevents Cross-Site Request Forgeries by checking that the user is really
* the one that sent this form in knowingly (this form hasn't been generated from
* another website visited by the user at the same time).
* Check the token with check_token().
*
* @return string Token
*/
public static function get_token()
{
$token = md5(uniqid(rand(), true));
Session::write('sec_token', $token);
return $token;
}
/**
* @return string
*/
public static function get_existing_token()
{
$token = Session::read('sec_token');
if (!empty($token)) {
return $token;
} else {
return self::get_token();
}
}
/**
* Gets the user agent in the session to later check it with check_ua() to prevent
* most cases of session hijacking.
*/
public static function get_ua()
{
$seed = uniqid(rand(), true);
Session::write('sec_ua_seed', $seed);
Session::write('sec_ua', $_SERVER['HTTP_USER_AGENT'].$seed);
}
/**
* This function returns a variable from the clean array. If the variable doesn't exist,
* it returns null.
*
* @param string Variable name
*
* @return mixed Variable or NULL on error
*/
public static function get($varname)
{
if (isset(self::$clean[$varname])) {
return self::$clean[$varname];
}
return null;
}
/**
* This function tackles the XSS injections.
* Filtering for XSS is very easily done by using the htmlentities() function.
* This kind of filtering prevents JavaScript snippets to be understood as such.
*
* @param string The variable to filter for XSS, this params can be a string or an array (example : array(x,y))
* @param int The user status,constant allowed (STUDENT, COURSEMANAGER, ANONYMOUS, COURSEMANAGERLOWSECURITY)
* @param bool $filter_terms
*
* @return mixed Filtered string or array
*/
public static function remove_XSS($var, $user_status = null, $filter_terms = false)
{
if ($filter_terms) {
$var = self::filter_terms($var);
}
if (empty($user_status)) {
if (api_is_anonymous()) {
$user_status = ANONYMOUS;
} else {
if (api_is_allowed_to_edit()) {
$user_status = COURSEMANAGER;
} else {
$user_status = STUDENT;
}
}
}
if ($user_status == COURSEMANAGERLOWSECURITY) {
return $var; // No filtering.
}
static $purifier = [];
if (!isset($purifier[$user_status])) {
$cache_dir = api_get_path(SYS_ARCHIVE_PATH).'Serializer';
if (!file_exists($cache_dir)) {
$mode = api_get_permissions_for_new_directories();
mkdir($cache_dir, $mode);
}
$config = HTMLPurifier_Config::createDefault();
$config->set('Cache.SerializerPath', $cache_dir);
$config->set('Core.Encoding', api_get_system_encoding());
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
$config->set('HTML.MaxImgLength', '2560');
$config->set('HTML.TidyLevel', 'light');
$config->set('Core.ConvertDocumentToFragment', false);
$config->set('Core.RemoveProcessingInstructions', true);
if (api_get_setting('enable_iframe_inclusion') == 'true') {
$config->set('Filter.Custom', [new AllowIframes()]);
}
// Shows _target attribute in anchors
$config->set('Attr.AllowedFrameTargets', ['_blank', '_top', '_self', '_parent']);
if ($user_status == STUDENT) {
global $allowed_html_student;
$config->set('HTML.SafeEmbed', true);
$config->set('HTML.SafeObject', true);
$config->set('Filter.YouTube', true);
$config->set('HTML.FlashAllowFullScreen', true);
$config->set('HTML.Allowed', $allowed_html_student);
} elseif ($user_status == COURSEMANAGER) {
global $allowed_html_teacher;
$config->set('HTML.SafeEmbed', true);
$config->set('HTML.SafeObject', true);
$config->set('Filter.YouTube', true);
$config->set('HTML.FlashAllowFullScreen', true);
$config->set('HTML.Allowed', $allowed_html_teacher);
} else {
global $allowed_html_anonymous;
$config->set('HTML.Allowed', $allowed_html_anonymous);
}
// We need it for example for the flv player (ids of surrounding div-tags have to be preserved).
$config->set('Attr.EnableID', true);
$config->set('CSS.AllowImportant', true);
// We need for the flv player the css definition display: none;
$config->set('CSS.AllowTricky', true);
$config->set('CSS.Proprietary', true);
// Allow uri scheme.
$config->set('URI.AllowedSchemes', [
'http' => true,
'https' => true,
'mailto' => true,
'ftp' => true,
'nntp' => true,
'news' => true,
'data' => true,
]);
// Allow <video> tag
//$config->set('HTML.Doctype', 'HTML 4.01 Transitional');
$config->set('HTML.SafeIframe', true);
// Set some HTML5 properties
$config->set('HTML.DefinitionID', 'html5-definitions'); // unqiue id
$config->set('HTML.DefinitionRev', 1);
if ($def = $config->maybeGetRawHTMLDefinition()) {
// https://html.spec.whatwg.org/dev/media.html#the-video-element
$def->addElement(
'video',
'Block',
'Optional: (source, Flow) | (Flow, source) | Flow',
'Common',
[
'src' => 'URI',
'type' => 'Text',
'width' => 'Length',
'height' => 'Length',
'poster' => 'URI',
'preload' => 'Enum#auto,metadata,none',
'controls' => 'Bool',
]
);
// https://html.spec.whatwg.org/dev/media.html#the-audio-element
$def->addElement(
'audio',
'Block',
'Optional: (source, Flow) | (Flow, source) | Flow',
'Common',
[
'autoplay' => 'Bool',
'src' => 'URI',
'loop' => 'Bool',
'preload' => 'Enum#auto,metadata,none',
'controls' => 'Bool',
'muted' => 'Bool',
]
);
$def->addElement(
'source',
'Block',
'Flow',
'Common',
['src' => 'URI', 'type' => 'Text']
);
}
$purifier[$user_status] = new HTMLPurifier($config);
}
if (is_array($var)) {
return $purifier[$user_status]->purifyArray($var);
} else {
return $purifier[$user_status]->purify($var);
}
}
/**
* Filter content.
*
* @param string $text to be filter
*
* @return string
*/
public static function filter_terms($text)
{
static $bad_terms = [];
if (empty($bad_terms)) {
$list = api_get_setting('filter_terms');
if (!empty($list)) {
$list = explode("\n", $list);
$list = array_filter($list);
if (!empty($list)) {
foreach ($list as $term) {
$term = str_replace(["\r\n", "\r", "\n", "\t"], '', $term);
$html_entities_value = api_htmlentities($term, ENT_QUOTES, api_get_system_encoding());
$bad_terms[] = $term;
if ($term != $html_entities_value) {
$bad_terms[] = $html_entities_value;
}
}
}
$bad_terms = array_filter($bad_terms);
}
}
$replace = '***';
if (!empty($bad_terms)) {
// Fast way
$new_text = str_ireplace($bad_terms, $replace, $text, $count);
$text = $new_text;
}
return $text;
}
/**
* This method provides specific protection (against XSS and other kinds of attacks)
* for static images (icons) used by the system.
* Image paths are supposed to be given by programmers - people who know what they do, anyway,
* this method encourages a safe practice for generating icon paths, without using heavy solutions
* based on HTMLPurifier for example.
*
* @param string $image_path the input path of the image, it could be relative or absolute URL
*
* @return string returns sanitized image path or an empty string when the image path is not secure
*
* @author Ivan Tcholakov, March 2011
*/
public static function filter_img_path($image_path)
{
static $allowed_extensions = ['png', 'gif', 'jpg', 'jpeg', 'svg', 'webp'];
$image_path = htmlspecialchars(trim($image_path)); // No html code is allowed.
// We allow static images only, query strings are forbidden.
if (strpos($image_path, '?') !== false) {
return '';
}
if (($pos = strpos($image_path, ':')) !== false) {
// Protocol has been specified, let's check it.
if (stripos($image_path, 'javascript:') !== false) {
// Javascript everywhere in the path is not allowed.
return '';
}
// We allow only http: and https: protocols for now.
//if (!preg_match('/^https?:\/\//i', $image_path)) {
// return '';
//}
if (stripos($image_path, 'http://') !== 0 && stripos($image_path, 'https://') !== 0) {
return '';
}
}
// We allow file extensions for images only.
//if (!preg_match('/.+\.(png|gif|jpg|jpeg)$/i', $image_path)) {
// return '';
//}
if (($pos = strrpos($image_path, '.')) !== false) {
if (!in_array(strtolower(substr($image_path, $pos + 1)), $allowed_extensions)) {
return '';
}
} else {
return '';
}
return $image_path;
}
/**
* Get password requirements
* It checks config value 'password_requirements' or uses the "classic"
* Chamilo password requirements.
*
* @return array
*/
public static function getPasswordRequirements()
{
// Default
$requirements = [
'min' => [
'lowercase' => 0,
'uppercase' => 0,
'numeric' => 2,
'length' => 5,
],
];
$passwordRequirements = api_get_configuration_value('password_requirements');
if (!empty($passwordRequirements)) {
$requirements = $passwordRequirements;
}
return $requirements;
}
/**
* Gets password requirements in the platform language using get_lang
* based in platform settings. See function 'self::getPasswordRequirements'.
*
* @return string
*/
public static function getPasswordRequirementsToString($passedConditions = [])
{
$output = '';
$setting = self::getPasswordRequirements();
foreach ($setting as $type => $rules) {
foreach ($rules as $rule => $parameter) {
if (empty($parameter)) {
continue;
}
$output .= sprintf(
get_lang(
'NewPasswordRequirement'.ucfirst($type).'X'.ucfirst($rule)
),
$parameter
);
$output .= '<br />';
}
}
return $output;
}
}