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;
38 * This is the list of template directories that are considered secure.
39 * $template_dir is in this list implicitly.
43 public $secure_dir = array();
45 * This is an array of directories where trusted php scripts reside.
46 * {@link $security} is disabled during their inclusion/execution.
50 public $trusted_dir = array();
52 * List of regular expressions (PCRE) that include trusted URIs
56 public $trusted_uri = array();
58 * This is an array of trusted static classes.
59 * If empty access to all static classes is allowed.
60 * If set to 'none' none is allowed.
64 public $static_classes = array();
66 * This is an array of trusted PHP functions.
67 * If empty all functions are allowed.
68 * To disable all PHP functions set $php_functions = null.
72 public $php_functions = array(
75 'in_array', 'is_array',
80 * This is an array of trusted PHP modifiers.
81 * If empty all modifiers are allowed.
82 * To disable all modifier set $modifiers = null.
86 public $php_modifiers = array(
91 * This is an array of allowed tags.
92 * If empty no restriction by allowed_tags.
96 public $allowed_tags = array();
98 * This is an array of disabled tags.
99 * If empty no restriction by disabled_tags.
103 public $disabled_tags = array();
105 * This is an array of allowed modifier plugins.
106 * If empty no restriction by allowed_modifiers.
110 public $allowed_modifiers = array();
112 * This is an array of disabled modifier plugins.
113 * If empty no restriction by disabled_modifiers.
117 public $disabled_modifiers = array();
119 * This is an array of trusted streams.
120 * If empty all streams are allowed.
121 * To disable all streams set $streams = null.
125 public $streams = array('file');
127 * + flag if constants can be accessed from template
131 public $allow_constants = true;
133 * + flag if super globals can be accessed from template
137 public $allow_super_globals = true;
140 * Cache for $resource_dir lookup
144 protected $_resource_dir = null;
146 * Cache for $template_dir lookup
150 protected $_template_dir = null;
152 * Cache for $config_dir lookup
156 protected $_config_dir = null;
158 * Cache for $secure_dir lookup
162 protected $_secure_dir = null;
164 * Cache for $php_resource_dir lookup
168 protected $_php_resource_dir = null;
170 * Cache for $trusted_dir lookup
174 protected $_trusted_dir = null;
177 * @param Smarty $smarty
179 public function __construct($smarty)
181 $this->smarty = $smarty;
185 * Check if PHP function is trusted.
187 * @param string $function_name
188 * @param object $compiler compiler object
190 * @return boolean true if function is trusted
191 * @throws SmartyCompilerException if php function is not trusted
193 public function isTrustedPhpFunction($function_name, $compiler)
195 if (isset($this->php_functions) && (empty($this->php_functions) || in_array($function_name, $this->php_functions))) {
199 $compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting");
201 return false; // should not, but who knows what happens to the compiler in the future?
205 * Check if static class is trusted.
207 * @param string $class_name
208 * @param object $compiler compiler object
210 * @return boolean true if class is trusted
211 * @throws SmartyCompilerException if static class is not trusted
213 public function isTrustedStaticClass($class_name, $compiler)
215 if (isset($this->static_classes) && (empty($this->static_classes) || in_array($class_name, $this->static_classes))) {
219 $compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting");
221 return false; // should not, but who knows what happens to the compiler in the future?
225 * Check if PHP modifier is trusted.
227 * @param string $modifier_name
228 * @param object $compiler compiler object
230 * @return boolean true if modifier is trusted
231 * @throws SmartyCompilerException if modifier is not trusted
233 public function isTrustedPhpModifier($modifier_name, $compiler)
235 if (isset($this->php_modifiers) && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))) {
239 $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting");
241 return false; // should not, but who knows what happens to the compiler in the future?
245 * Check if tag is trusted.
247 * @param string $tag_name
248 * @param object $compiler compiler object
250 * @return boolean true if tag is trusted
251 * @throws SmartyCompilerException if modifier is not trusted
253 public function isTrustedTag($tag_name, $compiler)
255 // check for internal always required tags
256 if (in_array($tag_name, array('assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin', 'private_object_block_function',
257 'private_object_function', 'private_registered_function', 'private_registered_block', 'private_special_variable', 'private_print_expression', 'private_modifier'))
261 // check security settings
262 if (empty($this->allowed_tags)) {
263 if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) {
266 $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", $compiler->lex->taglineno);
268 } elseif (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) {
271 $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", $compiler->lex->taglineno);
274 return false; // should not, but who knows what happens to the compiler in the future?
278 * Check if modifier plugin is trusted.
280 * @param string $modifier_name
281 * @param object $compiler compiler object
283 * @return boolean true if tag is trusted
284 * @throws SmartyCompilerException if modifier is not trusted
286 public function isTrustedModifier($modifier_name, $compiler)
288 // check for internal always allowed modifier
289 if (in_array($modifier_name, array('default'))) {
292 // check security settings
293 if (empty($this->allowed_modifiers)) {
294 if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) {
297 $compiler->trigger_template_error("modifier '{$modifier_name}' disabled by security setting", $compiler->lex->taglineno);
299 } elseif (in_array($modifier_name, $this->allowed_modifiers) && !in_array($modifier_name, $this->disabled_modifiers)) {
302 $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting", $compiler->lex->taglineno);
305 return false; // should not, but who knows what happens to the compiler in the future?
309 * Check if stream is trusted.
311 * @param string $stream_name
313 * @return boolean true if stream is trusted
314 * @throws SmartyException if stream is not trusted
316 public function isTrustedStream($stream_name)
318 if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) {
322 throw new SmartyException("stream '{$stream_name}' not allowed by security setting");
326 * Check if directory of file resource is trusted.
328 * @param string $filepath
330 * @return boolean true if directory is trusted
331 * @throws SmartyException if directory is not trusted
333 public function isTrustedResourceDir($filepath)
339 $_template_dir = $this->smarty->getTemplateDir();
340 $_config_dir = $this->smarty->getConfigDir();
342 // check if index is outdated
343 if ((!$this->_template_dir || $this->_template_dir !== $_template_dir)
344 || (!$this->_config_dir || $this->_config_dir !== $_config_dir)
345 || (!empty($this->secure_dir) && (!$this->_secure_dir || $this->_secure_dir !== $this->secure_dir))
347 $this->_resource_dir = array();
350 $_secure = !empty($this->secure_dir);
353 // rebuild template dir index
355 $this->_template_dir = $_template_dir;
356 foreach ($_template_dir as $directory) {
357 $directory = realpath($directory);
358 $this->_resource_dir[$directory] = true;
362 // rebuild config dir index
364 $this->_config_dir = $_config_dir;
365 foreach ($_config_dir as $directory) {
366 $directory = realpath($directory);
367 $this->_resource_dir[$directory] = true;
371 // rebuild secure dir index
373 $this->_secure_dir = $this->secure_dir;
374 foreach ((array) $this->secure_dir as $directory) {
375 $directory = realpath($directory);
376 $this->_resource_dir[$directory] = true;
380 $_filepath = realpath($filepath);
381 $directory = dirname($_filepath);
382 $_directory = array();
384 // remember the directory to add it to _resource_dir in case we're successful
385 $_directory[$directory] = true;
386 // test if the directory is trusted
387 if (isset($this->_resource_dir[$directory])) {
388 // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
389 $this->_resource_dir = array_merge($this->_resource_dir, $_directory);
393 // abort if we've reached root
394 if (($pos = strrpos($directory, DS)) === false || !isset($directory[1])) {
397 // bubble up one level
398 $directory = substr($directory, 0, $pos);
402 throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
406 * Check if URI (e.g. {fetch} or {html_image}) is trusted
407 * To simplify things, isTrustedUri() resolves all input to "{$PROTOCOL}://{$HOSTNAME}".
408 * So "http://username:password@hello.world.example.org:8080/some-path?some=query-string"
409 * is reduced to "http://hello.world.example.org" prior to applying the patters from {@link $trusted_uri}.
413 * @return boolean true if URI is trusted
414 * @throws SmartyException if URI is not trusted
415 * @uses $trusted_uri for list of patterns to match against $uri
417 public function isTrustedUri($uri)
419 $_uri = parse_url($uri);
420 if (!empty($_uri['scheme']) && !empty($_uri['host'])) {
421 $_uri = $_uri['scheme'] . '://' . $_uri['host'];
422 foreach ($this->trusted_uri as $pattern) {
423 if (preg_match($pattern, $_uri)) {
429 throw new SmartyException("URI '{$uri}' not allowed by security setting");
433 * Check if directory of file resource is trusted.
435 * @param string $filepath
437 * @return boolean true if directory is trusted
438 * @throws SmartyException if PHP directory is not trusted
440 public function isTrustedPHPDir($filepath)
442 if (empty($this->trusted_dir)) {
443 throw new SmartyException("directory '{$filepath}' not allowed by security setting (no trusted_dir specified)");
446 // check if index is outdated
447 if (!$this->_trusted_dir || $this->_trusted_dir !== $this->trusted_dir) {
448 $this->_php_resource_dir = array();
450 $this->_trusted_dir = $this->trusted_dir;
451 foreach ((array) $this->trusted_dir as $directory) {
452 $directory = realpath($directory);
453 $this->_php_resource_dir[$directory] = true;
457 $_filepath = realpath($filepath);
458 $directory = dirname($_filepath);
459 $_directory = array();
461 // remember the directory to add it to _resource_dir in case we're successful
462 $_directory[] = $directory;
463 // test if the directory is trusted
464 if (isset($this->_php_resource_dir[$directory])) {
465 // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
466 $this->_php_resource_dir = array_merge($this->_php_resource_dir, $_directory);
470 // abort if we've reached root
471 if (($pos = strrpos($directory, DS)) === false || !isset($directory[2])) {
474 // bubble up one level
475 $directory = substr($directory, 0, $pos);
478 throw new SmartyException("directory '{$_filepath}' not allowed by security setting");