1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Smarty plugin |
4
|
|
|
* |
5
|
|
|
* @package Smarty |
6
|
|
|
* @subpackage Security |
7
|
|
|
* @author Uwe Tews |
8
|
|
|
*/ |
9
|
|
|
/** |
10
|
|
|
* FIXME: Smarty_Security API |
11
|
|
|
* - getter and setter instead of public properties would allow cultivating an internal cache properly |
12
|
|
|
* - current implementation of isTrustedResourceDir() assumes that Smarty::$template_dir and Smarty::$config_dir |
13
|
|
|
* are immutable the cache is killed every time either of the variables change. That means that two distinct |
14
|
|
|
* Smarty objects with differing |
15
|
|
|
* $template_dir or $config_dir should NOT share the same Smarty_Security instance, |
16
|
|
|
* as this would lead to (severe) performance penalty! how should this be handled? |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This class does contain the security settings |
21
|
|
|
*/ |
22
|
|
|
#[\AllowDynamicProperties] |
23
|
|
|
class Smarty_Security |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This is the list of template directories that are considered secure. |
28
|
|
|
* $template_dir is in this list implicitly. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
public $secure_dir = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* This is an array of directories where trusted php scripts reside. |
36
|
|
|
* {@link $security} is disabled during their inclusion/execution. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public $trusted_dir = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* List of regular expressions (PCRE) that include trusted URIs |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
public $trusted_uri = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* List of trusted constants names |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
public $trusted_constants = array(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This is an array of trusted static classes. |
58
|
|
|
* If empty access to all static classes is allowed. |
59
|
|
|
* If set to 'none' none is allowed. |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
public $static_classes = array(); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* This is an nested array of trusted classes and static methods. |
67
|
|
|
* If empty access to all static classes and methods is allowed. |
68
|
|
|
* Format: |
69
|
|
|
* array ( |
70
|
|
|
* 'class_1' => array('method_1', 'method_2'), // allowed methods listed |
71
|
|
|
* 'class_2' => array(), // all methods of class allowed |
72
|
|
|
* ) |
73
|
|
|
* If set to null none is allowed. |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
public $trusted_static_methods = array(); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* This is an array of trusted static properties. |
81
|
|
|
* If empty access to all static classes and properties is allowed. |
82
|
|
|
* Format: |
83
|
|
|
* array ( |
84
|
|
|
* 'class_1' => array('prop_1', 'prop_2'), // allowed properties listed |
85
|
|
|
* 'class_2' => array(), // all properties of class allowed |
86
|
|
|
* ) |
87
|
|
|
* If set to null none is allowed. |
88
|
|
|
* |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
public $trusted_static_properties = array(); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* This is an array of trusted PHP functions. |
95
|
|
|
* If empty all functions are allowed. |
96
|
|
|
* To disable all PHP functions set $php_functions = null. |
97
|
|
|
* |
98
|
|
|
* @var array |
99
|
|
|
*/ |
100
|
|
|
public $php_functions = array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array', 'time',); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* This is an array of trusted PHP modifiers. |
104
|
|
|
* If empty all modifiers are allowed. |
105
|
|
|
* To disable all modifier set $php_modifiers = null. |
106
|
|
|
* |
107
|
|
|
* @var array |
108
|
|
|
*/ |
109
|
|
|
public $php_modifiers = array('escape', 'count', 'sizeof', 'nl2br',); |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* This is an array of allowed tags. |
113
|
|
|
* If empty no restriction by allowed_tags. |
114
|
|
|
* |
115
|
|
|
* @var array |
116
|
|
|
*/ |
117
|
|
|
public $allowed_tags = array(); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* This is an array of disabled tags. |
121
|
|
|
* If empty no restriction by disabled_tags. |
122
|
|
|
* |
123
|
|
|
* @var array |
124
|
|
|
*/ |
125
|
|
|
public $disabled_tags = array(); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* This is an array of allowed modifier plugins. |
129
|
|
|
* If empty no restriction by allowed_modifiers. |
130
|
|
|
* |
131
|
|
|
* @var array |
132
|
|
|
*/ |
133
|
|
|
public $allowed_modifiers = array(); |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* This is an array of disabled modifier plugins. |
137
|
|
|
* If empty no restriction by disabled_modifiers. |
138
|
|
|
* |
139
|
|
|
* @var array |
140
|
|
|
*/ |
141
|
|
|
public $disabled_modifiers = array(); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* This is an array of disabled special $smarty variables. |
145
|
|
|
* |
146
|
|
|
* @var array |
147
|
|
|
*/ |
148
|
|
|
public $disabled_special_smarty_vars = array(); |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* This is an array of trusted streams. |
152
|
|
|
* If empty all streams are allowed. |
153
|
|
|
* To disable all streams set $streams = null. |
154
|
|
|
* |
155
|
|
|
* @var array |
156
|
|
|
*/ |
157
|
|
|
public $streams = array('file'); |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* + flag if constants can be accessed from template |
161
|
|
|
* |
162
|
|
|
* @var boolean |
163
|
|
|
*/ |
164
|
|
|
public $allow_constants = true; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* + flag if super globals can be accessed from template |
168
|
|
|
* |
169
|
|
|
* @var boolean |
170
|
|
|
*/ |
171
|
|
|
public $allow_super_globals = true; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* max template nesting level |
175
|
|
|
* |
176
|
|
|
* @var int |
177
|
|
|
*/ |
178
|
|
|
public $max_template_nesting = 0; |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* current template nesting level |
182
|
|
|
* |
183
|
|
|
* @var int |
184
|
|
|
*/ |
185
|
|
|
private $_current_template_nesting = 0; |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Cache for $resource_dir lookup |
189
|
|
|
* |
190
|
|
|
* @var array |
191
|
|
|
*/ |
192
|
|
|
protected $_resource_dir = array(); |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Cache for $template_dir lookup |
196
|
|
|
* |
197
|
|
|
* @var array |
198
|
|
|
*/ |
199
|
|
|
protected $_template_dir = array(); |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Cache for $config_dir lookup |
203
|
|
|
* |
204
|
|
|
* @var array |
205
|
|
|
*/ |
206
|
|
|
protected $_config_dir = array(); |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Cache for $secure_dir lookup |
210
|
|
|
* |
211
|
|
|
* @var array |
212
|
|
|
*/ |
213
|
|
|
protected $_secure_dir = array(); |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Cache for $php_resource_dir lookup |
217
|
|
|
* |
218
|
|
|
* @var array |
219
|
|
|
*/ |
220
|
|
|
protected $_php_resource_dir = null; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Cache for $trusted_dir lookup |
224
|
|
|
* |
225
|
|
|
* @var array |
226
|
|
|
*/ |
227
|
|
|
protected $_trusted_dir = null; |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Cache for include path status |
231
|
|
|
* |
232
|
|
|
* @var bool |
233
|
|
|
*/ |
234
|
|
|
protected $_include_path_status = false; |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Cache for $_include_array lookup |
238
|
|
|
* |
239
|
|
|
* @var array |
240
|
|
|
*/ |
241
|
|
|
protected $_include_dir = array(); |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param Smarty $smarty |
245
|
|
|
*/ |
246
|
|
|
public function __construct($smarty) |
247
|
|
|
{ |
248
|
|
|
$this->smarty = $smarty; |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Check if PHP function is trusted. |
253
|
|
|
* |
254
|
|
|
* @param string $function_name |
255
|
|
|
* @param object $compiler compiler object |
256
|
|
|
* @deprecated |
257
|
|
|
* @return boolean true if function is trusted |
258
|
|
|
*/ |
259
|
|
|
public function isTrustedPhpFunction($function_name, $compiler) |
260
|
|
|
{ |
261
|
|
|
if (isset($this->php_functions) |
262
|
|
|
&& (empty($this->php_functions) || in_array($function_name, $this->php_functions)) |
263
|
|
|
) { |
264
|
|
|
return true; |
265
|
|
|
} |
266
|
|
|
$compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting"); |
267
|
|
|
return false; // should not, but who knows what happens to the compiler in the future? |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Check if static class is trusted. |
272
|
|
|
* |
273
|
|
|
* @param string $class_name |
274
|
|
|
* @param object $compiler compiler object |
275
|
|
|
* |
276
|
|
|
* @return boolean true if class is trusted |
277
|
|
|
*/ |
278
|
|
|
public function isTrustedStaticClass($class_name, $compiler) |
279
|
|
|
{ |
280
|
|
|
if (isset($this->static_classes) |
281
|
|
|
&& (empty($this->static_classes) || in_array($class_name, $this->static_classes)) |
282
|
|
|
) { |
283
|
|
|
return true; |
284
|
|
|
} |
285
|
|
|
$compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting"); |
286
|
|
|
return false; // should not, but who knows what happens to the compiler in the future? |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Check if static class method/property is trusted. |
291
|
|
|
* |
292
|
|
|
* @param string $class_name |
293
|
|
|
* @param string $params |
294
|
|
|
* @param object $compiler compiler object |
295
|
|
|
* |
296
|
|
|
* @return boolean true if class method is trusted |
297
|
|
|
*/ |
298
|
|
|
public function isTrustedStaticClassAccess($class_name, $params, $compiler) |
299
|
|
|
{ |
300
|
|
|
if (!isset($params[ 2 ])) { |
301
|
|
|
// fall back |
302
|
|
|
return $this->isTrustedStaticClass($class_name, $compiler); |
303
|
|
|
} |
304
|
|
|
if ($params[ 2 ] === 'method') { |
305
|
|
|
$allowed = $this->trusted_static_methods; |
306
|
|
|
$name = substr($params[ 0 ], 0, strpos($params[ 0 ], '(')); |
307
|
|
|
} else { |
308
|
|
|
$allowed = $this->trusted_static_properties; |
309
|
|
|
// strip '$' |
310
|
|
|
$name = substr($params[ 0 ], 1); |
311
|
|
|
} |
312
|
|
|
if (isset($allowed)) { |
313
|
|
|
if (empty($allowed)) { |
314
|
|
|
// fall back |
315
|
|
|
return $this->isTrustedStaticClass($class_name, $compiler); |
316
|
|
|
} |
317
|
|
|
if (isset($allowed[ $class_name ]) |
318
|
|
|
&& (empty($allowed[ $class_name ]) || in_array($name, $allowed[ $class_name ])) |
319
|
|
|
) { |
320
|
|
|
return true; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
$compiler->trigger_template_error("access to static class '{$class_name}' {$params[2]} '{$name}' not allowed by security setting"); |
324
|
|
|
return false; // should not, but who knows what happens to the compiler in the future? |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Check if PHP modifier is trusted. |
329
|
|
|
* |
330
|
|
|
* @param string $modifier_name |
331
|
|
|
* @param object $compiler compiler object |
332
|
|
|
* @deprecated |
333
|
|
|
* @return boolean true if modifier is trusted |
334
|
|
|
*/ |
335
|
|
|
public function isTrustedPhpModifier($modifier_name, $compiler) |
336
|
|
|
{ |
337
|
|
|
if (isset($this->php_modifiers) |
338
|
|
|
&& (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers)) |
339
|
|
|
) { |
340
|
|
|
return true; |
341
|
|
|
} |
342
|
|
|
$compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting"); |
343
|
|
|
return false; // should not, but who knows what happens to the compiler in the future? |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Check if tag is trusted. |
348
|
|
|
* |
349
|
|
|
* @param string $tag_name |
350
|
|
|
* @param object $compiler compiler object |
351
|
|
|
* |
352
|
|
|
* @return boolean true if tag is trusted |
353
|
|
|
*/ |
354
|
|
|
public function isTrustedTag($tag_name, $compiler) |
355
|
|
|
{ |
356
|
|
|
// check for internal always required tags |
357
|
|
|
if (in_array( |
358
|
|
|
$tag_name, |
359
|
|
|
array( |
360
|
|
|
'assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin', |
361
|
|
|
'private_object_block_function', 'private_object_function', 'private_registered_function', |
362
|
|
|
'private_registered_block', 'private_special_variable', 'private_print_expression', |
363
|
|
|
'private_modifier' |
364
|
|
|
) |
365
|
|
|
) |
366
|
|
|
) { |
367
|
|
|
return true; |
368
|
|
|
} |
369
|
|
|
// check security settings |
370
|
|
|
if (empty($this->allowed_tags)) { |
371
|
|
|
if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) { |
372
|
|
|
return true; |
373
|
|
|
} else { |
374
|
|
|
$compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", null, true); |
375
|
|
|
} |
376
|
|
|
} elseif (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) { |
377
|
|
|
return true; |
378
|
|
|
} else { |
379
|
|
|
$compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", null, true); |
380
|
|
|
} |
381
|
|
|
return false; // should not, but who knows what happens to the compiler in the future? |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Check if special $smarty variable is trusted. |
386
|
|
|
* |
387
|
|
|
* @param string $var_name |
388
|
|
|
* @param object $compiler compiler object |
389
|
|
|
* |
390
|
|
|
* @return boolean true if tag is trusted |
391
|
|
|
*/ |
392
|
|
|
public function isTrustedSpecialSmartyVar($var_name, $compiler) |
393
|
|
|
{ |
394
|
|
|
if (!in_array($var_name, $this->disabled_special_smarty_vars)) { |
395
|
|
|
return true; |
396
|
|
|
} else { |
397
|
|
|
$compiler->trigger_template_error( |
398
|
|
|
"special variable '\$smarty.{$var_name}' not allowed by security setting", |
399
|
|
|
null, |
400
|
|
|
true |
401
|
|
|
); |
402
|
|
|
} |
403
|
|
|
return false; // should not, but who knows what happens to the compiler in the future? |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Check if modifier plugin is trusted. |
408
|
|
|
* |
409
|
|
|
* @param string $modifier_name |
410
|
|
|
* @param object $compiler compiler object |
411
|
|
|
* |
412
|
|
|
* @return boolean true if tag is trusted |
413
|
|
|
*/ |
414
|
|
|
public function isTrustedModifier($modifier_name, $compiler) |
415
|
|
|
{ |
416
|
|
|
// check for internal always allowed modifier |
417
|
|
|
if (in_array($modifier_name, array('default'))) { |
418
|
|
|
return true; |
419
|
|
|
} |
420
|
|
|
// check security settings |
421
|
|
|
if (empty($this->allowed_modifiers)) { |
422
|
|
|
if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) { |
423
|
|
|
return true; |
424
|
|
|
} else { |
425
|
|
|
$compiler->trigger_template_error( |
426
|
|
|
"modifier '{$modifier_name}' disabled by security setting", |
427
|
|
|
null, |
428
|
|
|
true |
429
|
|
|
); |
430
|
|
|
} |
431
|
|
|
} elseif (in_array($modifier_name, $this->allowed_modifiers) |
432
|
|
|
&& !in_array($modifier_name, $this->disabled_modifiers) |
433
|
|
|
) { |
434
|
|
|
return true; |
435
|
|
|
} else { |
436
|
|
|
$compiler->trigger_template_error( |
437
|
|
|
"modifier '{$modifier_name}' not allowed by security setting", |
438
|
|
|
null, |
439
|
|
|
true |
440
|
|
|
); |
441
|
|
|
} |
442
|
|
|
return false; // should not, but who knows what happens to the compiler in the future? |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Check if constants are enabled or trusted |
447
|
|
|
* |
448
|
|
|
* @param string $const constant name |
449
|
|
|
* @param object $compiler compiler object |
450
|
|
|
* |
451
|
|
|
* @return bool |
452
|
|
|
*/ |
453
|
|
|
public function isTrustedConstant($const, $compiler) |
454
|
|
|
{ |
455
|
|
|
if (in_array($const, array('true', 'false', 'null'))) { |
456
|
|
|
return true; |
457
|
|
|
} |
458
|
|
|
if (!empty($this->trusted_constants)) { |
459
|
|
|
if (!in_array(strtolower($const), $this->trusted_constants)) { |
460
|
|
|
$compiler->trigger_template_error("Security: access to constant '{$const}' not permitted"); |
461
|
|
|
return false; |
462
|
|
|
} |
463
|
|
|
return true; |
464
|
|
|
} |
465
|
|
|
if ($this->allow_constants) { |
466
|
|
|
return true; |
467
|
|
|
} |
468
|
|
|
$compiler->trigger_template_error("Security: access to constants not permitted"); |
469
|
|
|
return false; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Check if stream is trusted. |
474
|
|
|
* |
475
|
|
|
* @param string $stream_name |
476
|
|
|
* |
477
|
|
|
* @return boolean true if stream is trusted |
478
|
|
|
* @throws SmartyException if stream is not trusted |
479
|
|
|
*/ |
480
|
|
|
public function isTrustedStream($stream_name) |
481
|
|
|
{ |
482
|
|
|
if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) { |
483
|
|
|
return true; |
484
|
|
|
} |
485
|
|
|
throw new SmartyException("stream '{$stream_name}' not allowed by security setting"); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Check if directory of file resource is trusted. |
490
|
|
|
* |
491
|
|
|
* @param string $filepath |
492
|
|
|
* @param null|bool $isConfig |
493
|
|
|
* |
494
|
|
|
* @return bool true if directory is trusted |
495
|
|
|
* @throws \SmartyException if directory is not trusted |
496
|
|
|
*/ |
497
|
|
|
public function isTrustedResourceDir($filepath, $isConfig = null) |
|
|
|
|
498
|
|
|
{ |
499
|
|
|
if ($this->_include_path_status !== $this->smarty->use_include_path) { |
500
|
|
|
$_dir = |
501
|
|
|
$this->smarty->use_include_path ? $this->smarty->ext->_getIncludePath->getIncludePathDirs($this->smarty) : array(); |
502
|
|
|
if ($this->_include_dir !== $_dir) { |
503
|
|
|
$this->_updateResourceDir($this->_include_dir, $_dir); |
504
|
|
|
$this->_include_dir = $_dir; |
505
|
|
|
} |
506
|
|
|
$this->_include_path_status = $this->smarty->use_include_path; |
507
|
|
|
} |
508
|
|
|
$_dir = $this->smarty->getTemplateDir(); |
509
|
|
|
if ($this->_template_dir !== $_dir) { |
510
|
|
|
$this->_updateResourceDir($this->_template_dir, $_dir); |
511
|
|
|
$this->_template_dir = $_dir; |
512
|
|
|
} |
513
|
|
|
$_dir = $this->smarty->getConfigDir(); |
514
|
|
|
if ($this->_config_dir !== $_dir) { |
515
|
|
|
$this->_updateResourceDir($this->_config_dir, $_dir); |
516
|
|
|
$this->_config_dir = $_dir; |
517
|
|
|
} |
518
|
|
|
if ($this->_secure_dir !== $this->secure_dir) { |
519
|
|
|
$this->secure_dir = (array)$this->secure_dir; |
520
|
|
|
foreach ($this->secure_dir as $k => $d) { |
521
|
|
|
$this->secure_dir[ $k ] = $this->smarty->_realpath($d . DIRECTORY_SEPARATOR, true); |
522
|
|
|
} |
523
|
|
|
$this->_updateResourceDir($this->_secure_dir, $this->secure_dir); |
524
|
|
|
$this->_secure_dir = $this->secure_dir; |
525
|
|
|
} |
526
|
|
|
$addPath = $this->_checkDir($filepath, $this->_resource_dir); |
527
|
|
|
if ($addPath !== false) { |
|
|
|
|
528
|
|
|
$this->_resource_dir = array_merge($this->_resource_dir, $addPath); |
529
|
|
|
} |
530
|
|
|
return true; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Check if URI (e.g. {fetch} or {html_image}) is trusted |
535
|
|
|
* To simplify things, isTrustedUri() resolves all input to "{$PROTOCOL}://{$HOSTNAME}". |
536
|
|
|
* So "http://username:[email protected]:8080/some-path?some=query-string" |
537
|
|
|
* is reduced to "http://hello.world.example.org" prior to applying the patters from {@link $trusted_uri}. |
538
|
|
|
* |
539
|
|
|
* @param string $uri |
540
|
|
|
* |
541
|
|
|
* @return boolean true if URI is trusted |
542
|
|
|
* @throws SmartyException if URI is not trusted |
543
|
|
|
* @uses $trusted_uri for list of patterns to match against $uri |
544
|
|
|
*/ |
545
|
|
|
public function isTrustedUri($uri) |
546
|
|
|
{ |
547
|
|
|
$_uri = parse_url($uri); |
548
|
|
|
if (!empty($_uri[ 'scheme' ]) && !empty($_uri[ 'host' ])) { |
549
|
|
|
$_uri = $_uri[ 'scheme' ] . '://' . $_uri[ 'host' ]; |
550
|
|
|
foreach ($this->trusted_uri as $pattern) { |
551
|
|
|
if (preg_match($pattern, $_uri)) { |
552
|
|
|
return true; |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
throw new SmartyException("URI '{$uri}' not allowed by security setting"); |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Remove old directories and its sub folders, add new directories |
561
|
|
|
* |
562
|
|
|
* @param array $oldDir |
563
|
|
|
* @param array $newDir |
564
|
|
|
*/ |
565
|
|
|
private function _updateResourceDir($oldDir, $newDir) |
566
|
|
|
{ |
567
|
|
|
foreach ($oldDir as $directory) { |
568
|
|
|
// $directory = $this->smarty->_realpath($directory, true); |
569
|
|
|
$length = strlen($directory); |
570
|
|
|
foreach ($this->_resource_dir as $dir) { |
571
|
|
|
if (substr($dir, 0, $length) === $directory) { |
572
|
|
|
unset($this->_resource_dir[ $dir ]); |
573
|
|
|
} |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
foreach ($newDir as $directory) { |
577
|
|
|
// $directory = $this->smarty->_realpath($directory, true); |
578
|
|
|
$this->_resource_dir[ $directory ] = true; |
579
|
|
|
} |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Check if file is inside a valid directory |
584
|
|
|
* |
585
|
|
|
* @param string $filepath |
586
|
|
|
* @param array $dirs valid directories |
587
|
|
|
* |
588
|
|
|
* @return array|bool |
589
|
|
|
* @throws \SmartyException |
590
|
|
|
*/ |
591
|
|
|
private function _checkDir($filepath, $dirs) |
592
|
|
|
{ |
593
|
|
|
$directory = dirname($this->smarty->_realpath($filepath, true)) . DIRECTORY_SEPARATOR; |
594
|
|
|
$_directory = array(); |
595
|
|
|
if (!preg_match('#[\\\\/][.][.][\\\\/]#', $directory)) { |
596
|
|
|
while (true) { |
597
|
|
|
// test if the directory is trusted |
598
|
|
|
if (isset($dirs[ $directory ])) { |
599
|
|
|
return $_directory; |
600
|
|
|
} |
601
|
|
|
// abort if we've reached root |
602
|
|
|
if (!preg_match('#[\\\\/][^\\\\/]+[\\\\/]$#', $directory)) { |
603
|
|
|
// give up |
604
|
|
|
break; |
605
|
|
|
} |
606
|
|
|
// remember the directory to add it to _resource_dir in case we're successful |
607
|
|
|
$_directory[ $directory ] = true; |
608
|
|
|
// bubble up one level |
609
|
|
|
$directory = preg_replace('#[\\\\/][^\\\\/]+[\\\\/]$#', DIRECTORY_SEPARATOR, $directory); |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
// give up |
613
|
|
|
throw new SmartyException(sprintf('Smarty Security: not trusted file path \'%s\' ', $filepath)); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* Loads security class and enables security |
618
|
|
|
* |
619
|
|
|
* @param \Smarty $smarty |
620
|
|
|
* @param string|Smarty_Security $security_class if a string is used, it must be class-name |
621
|
|
|
* |
622
|
|
|
* @return \Smarty current Smarty instance for chaining |
623
|
|
|
* @throws \SmartyException when an invalid class name is provided |
624
|
|
|
*/ |
625
|
|
|
public static function enableSecurity(Smarty $smarty, $security_class) |
626
|
|
|
{ |
627
|
|
|
if ($security_class instanceof Smarty_Security) { |
628
|
|
|
$smarty->security_policy = $security_class; |
629
|
|
|
return $smarty; |
630
|
|
|
} elseif (is_object($security_class)) { |
|
|
|
|
631
|
|
|
throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security."); |
632
|
|
|
} |
633
|
|
|
if ($security_class === null) { |
|
|
|
|
634
|
|
|
$security_class = $smarty->security_class; |
635
|
|
|
} |
636
|
|
|
if (!class_exists($security_class)) { |
637
|
|
|
throw new SmartyException("Security class '$security_class' is not defined"); |
638
|
|
|
} elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) { |
639
|
|
|
throw new SmartyException("Class '$security_class' must extend Smarty_Security."); |
640
|
|
|
} else { |
641
|
|
|
$smarty->security_policy = new $security_class($smarty); |
642
|
|
|
} |
643
|
|
|
return $smarty; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Start template processing |
648
|
|
|
* |
649
|
|
|
* @param $template |
650
|
|
|
* |
651
|
|
|
* @throws SmartyException |
652
|
|
|
*/ |
653
|
|
|
public function startTemplate($template) |
654
|
|
|
{ |
655
|
|
|
if ($this->max_template_nesting > 0 && $this->_current_template_nesting++ >= $this->max_template_nesting) { |
656
|
|
|
throw new SmartyException("maximum template nesting level of '{$this->max_template_nesting}' exceeded when calling '{$template->template_resource}'"); |
657
|
|
|
} |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* Exit template processing |
662
|
|
|
*/ |
663
|
|
|
public function endTemplate() |
664
|
|
|
{ |
665
|
|
|
if ($this->max_template_nesting > 0) { |
666
|
|
|
$this->_current_template_nesting--; |
667
|
|
|
} |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* Register callback functions call at start/end of template rendering |
672
|
|
|
* |
673
|
|
|
* @param \Smarty_Internal_Template $template |
674
|
|
|
*/ |
675
|
|
|
public function registerCallBacks(Smarty_Internal_Template $template) |
676
|
|
|
{ |
677
|
|
|
$template->startRenderCallbacks[] = array($this, 'startTemplate'); |
678
|
|
|
$template->endRenderCallbacks[] = array($this, 'endTemplate'); |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|