11 * FIXME: Smarty_Security API
12 * - getter and setter instead of public properties would allow cultivating an internal cache properly
13 * - current implementation of isTrustedResourceDir() assumes that Smarty::$template_dir and Smarty::$config_dir are immutable
14 * the cache is killed every time either of the variables change. That means that two distinct 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?
20 * This class does contain the security settings
25 * This determines how Smarty handles "<?php ... ?>" tags in templates.
28 * <li>Smarty::PHP_PASSTHRU -> echo PHP tags as they are</li>
29 * <li>Smarty::PHP_QUOTE -> escape tags as entities</li>
30 * <li>Smarty::PHP_REMOVE -> remove php tags</li>
31 * <li>Smarty::PHP_ALLOW -> execute php tags</li>
36 public $php_handling = Smarty::PHP_PASSTHRU;
39 * This is the list of template directories that are considered secure.
40 * $template_dir is in this list implicitly.
44 public $secure_dir = array();
47 * This is an array of directories where trusted php scripts reside.
48 * {@link $security} is disabled during their inclusion/execution.
52 public $trusted_dir = array();
55 * List of regular expressions (PCRE) that include trusted URIs
59 public $trusted_uri = array();
62 * List of trusted constants names
66 public $trusted_constants = array();
69 * This is an array of trusted static classes.
70 * If empty access to all static classes is allowed.
71 * If set to 'none' none is allowed.
75 public $static_classes = array();
78 * This is an nested array of trusted classes and static methods.
79 * If empty access to all static classes and methods is allowed.
82 * 'class_1' => array('method_1', 'method_2'), // allowed methods listed
83 * 'class_2' => array(), // all methods of class allowed
85 * If set to null none is allowed.
89 public $trusted_static_methods = array();
92 * This is an array of trusted static properties.
93 * If empty access to all static classes and properties is allowed.
96 * 'class_1' => array('prop_1', 'prop_2'), // allowed properties listed
97 * 'class_2' => array(), // all properties of class allowed
99 * If set to null none is allowed.
103 public $trusted_static_properties = array();
106 * This is an array of trusted PHP functions.
107 * If empty all functions are allowed.
108 * To disable all PHP functions set $php_functions = null.
112 public $php_functions = array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array', 'time',);
115 * This is an array of trusted PHP modifiers.
116 * If empty all modifiers are allowed.
117 * To disable all modifier set $php_modifiers = null.
121 public $php_modifiers = array('escape', 'count', 'nl2br',);
124 * This is an array of allowed tags.
125 * If empty no restriction by allowed_tags.
129 public $allowed_tags = array();
132 * This is an array of disabled tags.
133 * If empty no restriction by disabled_tags.
137 public $disabled_tags = array();
140 * This is an array of allowed modifier plugins.
141 * If empty no restriction by allowed_modifiers.
145 public $allowed_modifiers = array();
148 * This is an array of disabled modifier plugins.
149 * If empty no restriction by disabled_modifiers.
153 public $disabled_modifiers = array();
156 * This is an array of disabled special $smarty variables.
160 public $disabled_special_smarty_vars = array();
163 * This is an array of trusted streams.
164 * If empty all streams are allowed.
165 * To disable all streams set $streams = null.
169 public $streams = array('file');
172 * + flag if constants can be accessed from template
176 public $allow_constants = true;
179 * + flag if super globals can be accessed from template
183 public $allow_super_globals = true;
186 * max template nesting level
190 public $max_template_nesting = 0;
193 * current template nesting level
197 private $_current_template_nesting = 0;
200 * Cache for $resource_dir lookup
204 protected $_resource_dir = array();
207 * Cache for $template_dir lookup
211 protected $_template_dir = array();
214 * Cache for $config_dir lookup
218 protected $_config_dir = array();
221 * Cache for $secure_dir lookup
225 protected $_secure_dir = array();
228 * Cache for $php_resource_dir lookup
232 protected $_php_resource_dir = null;
235 * Cache for $trusted_dir lookup
239 protected $_trusted_dir = null;
242 * Cache for include path status
246 protected $_include_path_status = false;
249 * Cache for $_include_array lookup
253 protected $_include_dir = array();
256 * @param Smarty $smarty
258 public function __construct($smarty)
260 $this->smarty = $smarty;
261 $this->smarty->_cache[ 'template_dir_new' ] = true;
262 $this->smarty->_cache[ 'config_dir_new' ] = true;
266 * Check if PHP function is trusted.
268 * @param string $function_name
269 * @param object $compiler compiler object
271 * @return boolean true if function is trusted
272 * @throws SmartyCompilerException if php function is not trusted
274 public function isTrustedPhpFunction($function_name, $compiler)
276 if (isset($this->php_functions) &&
277 (empty($this->php_functions) || in_array($function_name, $this->php_functions))
282 $compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting");
284 return false; // should not, but who knows what happens to the compiler in the future?
288 * Check if static class is trusted.
290 * @param string $class_name
291 * @param object $compiler compiler object
293 * @return boolean true if class is trusted
294 * @throws SmartyCompilerException if static class is not trusted
296 public function isTrustedStaticClass($class_name, $compiler)
298 if (isset($this->static_classes) &&
299 (empty($this->static_classes) || in_array($class_name, $this->static_classes))
304 $compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting");
306 return false; // should not, but who knows what happens to the compiler in the future?
310 * Check if static class method/property is trusted.
312 * @param string $class_name
313 * @param string $params
314 * @param object $compiler compiler object
316 * @return boolean true if class method is trusted
317 * @throws SmartyCompilerException if static class method is not trusted
319 public function isTrustedStaticClassAccess($class_name, $params, $compiler)
321 if (!isset($params[ 2 ])) {
323 return $this->isTrustedStaticClass($class_name, $compiler);
325 if ($params[ 2 ] == 'method') {
326 $allowed = $this->trusted_static_methods;
327 $name = substr($params[ 0 ], 0, strpos($params[ 0 ], '('));
329 $allowed = $this->trusted_static_properties;
331 $name = substr($params[ 0 ], 1);
333 if (isset($allowed)) {
334 if (empty($allowed)) {
336 return $this->isTrustedStaticClass($class_name, $compiler);
338 if (isset($allowed[ $class_name ]) &&
339 (empty($allowed[ $class_name ]) || in_array($name, $allowed[ $class_name ]))
344 $compiler->trigger_template_error("access to static class '{$class_name}' {$params[2]} '{$name}' not allowed by security setting");
345 return false; // should not, but who knows what happens to the compiler in the future?
349 * Check if PHP modifier is trusted.
351 * @param string $modifier_name
352 * @param object $compiler compiler object
354 * @return boolean true if modifier is trusted
355 * @throws SmartyCompilerException if modifier is not trusted
357 public function isTrustedPhpModifier($modifier_name, $compiler)
359 if (isset($this->php_modifiers) &&
360 (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))
365 $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting");
367 return false; // should not, but who knows what happens to the compiler in the future?
371 * Check if tag is trusted.
373 * @param string $tag_name
374 * @param object $compiler compiler object
376 * @return boolean true if tag is trusted
377 * @throws SmartyCompilerException if modifier is not trusted
379 public function isTrustedTag($tag_name, $compiler)
381 // check for internal always required tags
382 if (in_array($tag_name,
383 array('assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin',
384 'private_object_block_function', 'private_object_function', 'private_registered_function',
385 'private_registered_block', 'private_special_variable', 'private_print_expression',
386 'private_modifier'))) {
389 // check security settings
390 if (empty($this->allowed_tags)) {
391 if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) {
394 $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", null, true);
396 } elseif (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) {
399 $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", null, true);
402 return false; // should not, but who knows what happens to the compiler in the future?
406 * Check if special $smarty variable is trusted.
408 * @param string $var_name
409 * @param object $compiler compiler object
411 * @return boolean true if tag is trusted
412 * @throws SmartyCompilerException if modifier is not trusted
414 public function isTrustedSpecialSmartyVar($var_name, $compiler)
416 if (!in_array($var_name, $this->disabled_special_smarty_vars)) {
419 $compiler->trigger_template_error("special variable '\$smarty.{$var_name}' not allowed by security setting",
423 return false; // should not, but who knows what happens to the compiler in the future?
427 * Check if modifier plugin is trusted.
429 * @param string $modifier_name
430 * @param object $compiler compiler object
432 * @return boolean true if tag is trusted
433 * @throws SmartyCompilerException if modifier is not trusted
435 public function isTrustedModifier($modifier_name, $compiler)
437 // check for internal always allowed modifier
438 if (in_array($modifier_name, array('default'))) {
441 // check security settings
442 if (empty($this->allowed_modifiers)) {
443 if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) {
446 $compiler->trigger_template_error("modifier '{$modifier_name}' disabled by security setting", null,
449 } elseif (in_array($modifier_name, $this->allowed_modifiers) &&
450 !in_array($modifier_name, $this->disabled_modifiers)
454 $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting", null,
458 return false; // should not, but who knows what happens to the compiler in the future?
462 * Check if constants are enabled or trusted
464 * @param string $const constant name
465 * @param object $compiler compiler object
469 public function isTrustedConstant($const, $compiler)
471 if (in_array($const, array('true', 'false', 'null'))) {
474 if (!empty($this->trusted_constants)) {
475 if (!in_array(strtolower($const), $this->trusted_constants)) {
476 $compiler->trigger_template_error("Security: access to constant '{$const}' not permitted");
481 if ($this->allow_constants) {
484 $compiler->trigger_template_error("Security: access to constants not permitted");
489 * Check if stream is trusted.
491 * @param string $stream_name
493 * @return boolean true if stream is trusted
494 * @throws SmartyException if stream is not trusted
496 public function isTrustedStream($stream_name)
498 if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) {
502 throw new SmartyException("stream '{$stream_name}' not allowed by security setting");
506 * Check if directory of file resource is trusted.
508 * @param string $filepath
509 * @param null|bool $isConfig
511 * @return bool true if directory is trusted
512 * @throws \SmartyException if directory is not trusted
514 public function isTrustedResourceDir($filepath, $isConfig = null)
516 if ($this->_include_path_status !== $this->smarty->use_include_path) {
517 foreach ($this->_include_dir as $directory) {
518 unset($this->_resource_dir[ $directory ]);
520 if ($this->smarty->use_include_path) {
521 $this->_include_dir = array();
522 $_dirs = $this->smarty->ext->_getIncludePath->getIncludePathDirs($this->smarty);
523 foreach ($_dirs as $directory) {
524 $this->_include_dir[] = $directory;
525 $this->_resource_dir[ $directory ] = true;
528 $this->_include_path_status = $this->smarty->use_include_path;
530 if ($isConfig !== true &&
531 (!isset($this->smarty->_cache[ 'template_dir_new' ]) || $this->smarty->_cache[ 'template_dir_new' ])
533 $_dir = $this->smarty->getTemplateDir();
534 if ($this->_template_dir !== $_dir) {
535 foreach ($this->_template_dir as $directory) {
536 unset($this->_resource_dir[ $directory ]);
538 foreach ($_dir as $directory) {
539 $this->_resource_dir[ $directory ] = true;
541 $this->_template_dir = $_dir;
543 $this->smarty->_cache[ 'template_dir_new' ] = false;
545 if ($isConfig !== false &&
546 (!isset($this->smarty->_cache[ 'config_dir_new' ]) || $this->smarty->_cache[ 'config_dir_new' ])
548 $_dir = $this->smarty->getConfigDir();
549 if ($this->_config_dir !== $_dir) {
550 foreach ($this->_config_dir as $directory) {
551 unset($this->_resource_dir[ $directory ]);
553 foreach ($_dir as $directory) {
554 $this->_resource_dir[ $directory ] = true;
556 $this->_config_dir = $_dir;
558 $this->smarty->_cache[ 'config_dir_new' ] = false;
560 if ($this->_secure_dir !== (array) $this->secure_dir) {
561 foreach ($this->_secure_dir as $directory) {
562 unset($this->_resource_dir[ $directory ]);
564 foreach ((array) $this->secure_dir as $directory) {
565 $directory = $this->smarty->_realpath($directory . DIRECTORY_SEPARATOR, true);
566 $this->_resource_dir[ $directory ] = true;
568 $this->_secure_dir = (array) $this->secure_dir;
570 $this->_resource_dir = $this->_checkDir($filepath, $this->_resource_dir);
575 * Check if URI (e.g. {fetch} or {html_image}) is trusted
576 * To simplify things, isTrustedUri() resolves all input to "{$PROTOCOL}://{$HOSTNAME}".
577 * So "http://username:password@hello.world.example.org:8080/some-path?some=query-string"
578 * is reduced to "http://hello.world.example.org" prior to applying the patters from {@link $trusted_uri}.
582 * @return boolean true if URI is trusted
583 * @throws SmartyException if URI is not trusted
584 * @uses $trusted_uri for list of patterns to match against $uri
586 public function isTrustedUri($uri)
588 $_uri = parse_url($uri);
589 if (!empty($_uri[ 'scheme' ]) && !empty($_uri[ 'host' ])) {
590 $_uri = $_uri[ 'scheme' ] . '://' . $_uri[ 'host' ];
591 foreach ($this->trusted_uri as $pattern) {
592 if (preg_match($pattern, $_uri)) {
598 throw new SmartyException("URI '{$uri}' not allowed by security setting");
602 * Check if directory of file resource is trusted.
604 * @param string $filepath
606 * @return boolean true if directory is trusted
607 * @throws SmartyException if PHP directory is not trusted
609 public function isTrustedPHPDir($filepath)
611 if (empty($this->trusted_dir)) {
612 throw new SmartyException("directory '{$filepath}' not allowed by security setting (no trusted_dir specified)");
615 // check if index is outdated
616 if (!$this->_trusted_dir || $this->_trusted_dir !== $this->trusted_dir) {
617 $this->_php_resource_dir = array();
619 $this->_trusted_dir = $this->trusted_dir;
620 foreach ((array) $this->trusted_dir as $directory) {
621 $directory = $this->smarty->_realpath($directory . DIRECTORY_SEPARATOR, true);
622 $this->_php_resource_dir[ $directory ] = true;
626 $this->_php_resource_dir =
627 $this->_checkDir($this->smarty->_realpath($filepath, true), $this->_php_resource_dir);
632 * Check if file is inside a valid directory
634 * @param string $filepath
635 * @param array $dirs valid directories
638 * @throws \SmartyException
640 private function _checkDir($filepath, $dirs)
642 $directory = dirname($filepath) . DIRECTORY_SEPARATOR;
643 $_directory = array();
645 // remember the directory to add it to _resource_dir in case we're successful
646 $_directory[ $directory ] = true;
647 // test if the directory is trusted
648 if (isset($dirs[ $directory ])) {
649 // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
650 $dirs = array_merge($dirs, $_directory);
654 // abort if we've reached root
655 if (!preg_match('#[\\\/][^\\\/]+[\\\/]$#', $directory)) {
658 // bubble up one level
659 $directory = preg_replace('#[\\\/][^\\\/]+[\\\/]$#', DIRECTORY_SEPARATOR, $directory);
663 throw new SmartyException("directory '{$filepath}' not allowed by security setting");
667 * Loads security class and enables security
669 * @param \Smarty $smarty
670 * @param string|Smarty_Security $security_class if a string is used, it must be class-name
672 * @return \Smarty current Smarty instance for chaining
673 * @throws \SmartyException when an invalid class name is provided
675 public static function enableSecurity(Smarty $smarty, $security_class)
677 if ($security_class instanceof Smarty_Security) {
678 $smarty->security_policy = $security_class;
680 } elseif (is_object($security_class)) {
681 throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security.");
683 if ($security_class == null) {
684 $security_class = $smarty->security_class;
686 if (!class_exists($security_class)) {
687 throw new SmartyException("Security class '$security_class' is not defined");
688 } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
689 throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
691 $smarty->security_policy = new $security_class($smarty);
696 * Start template processing
700 * @throws SmartyException
702 public function startTemplate($template)
704 if ($this->max_template_nesting > 0 && $this->_current_template_nesting ++ >= $this->max_template_nesting) {
705 throw new SmartyException("maximum template nesting level of '{$this->max_template_nesting}' exceeded when calling '{$template->template_resource}'");
710 * Exit template processing
713 public function endTemplate()
715 if ($this->max_template_nesting > 0) {
716 $this->_current_template_nesting --;
721 * Register callback functions call at start/end of template rendering
723 * @param \Smarty_Internal_Template $template
725 public function registerCallBacks(Smarty_Internal_Template $template)
727 $template->startRenderCallbacks[] = array($this, 'startTemplate');
728 $template->endRenderCallbacks[] = array($this, 'endTemplate');