aboutsummaryrefslogtreecommitdiffstats
path: root/library/Smarty/libs/sysplugins/smarty_security.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Smarty/libs/sysplugins/smarty_security.php')
-rw-r--r--library/Smarty/libs/sysplugins/smarty_security.php309
1 files changed, 190 insertions, 119 deletions
diff --git a/library/Smarty/libs/sysplugins/smarty_security.php b/library/Smarty/libs/sysplugins/smarty_security.php
index af1ca5531..9d48bcb21 100644
--- a/library/Smarty/libs/sysplugins/smarty_security.php
+++ b/library/Smarty/libs/sysplugins/smarty_security.php
@@ -34,6 +34,7 @@ class Smarty_Security
* @var integer
*/
public $php_handling = Smarty::PHP_PASSTHRU;
+
/**
* This is the list of template directories that are considered secure.
* $template_dir is in this list implicitly.
@@ -41,6 +42,7 @@ class Smarty_Security
* @var array
*/
public $secure_dir = array();
+
/**
* This is an array of directories where trusted php scripts reside.
* {@link $security} is disabled during their inclusion/execution.
@@ -48,18 +50,21 @@ class Smarty_Security
* @var array
*/
public $trusted_dir = array();
+
/**
* List of regular expressions (PCRE) that include trusted URIs
*
* @var array
*/
public $trusted_uri = array();
+
/**
* List of trusted constants names
*
* @var array
*/
public $trusted_constants = array();
+
/**
* This is an array of trusted static classes.
* If empty access to all static classes is allowed.
@@ -96,6 +101,7 @@ class Smarty_Security
* @var array
*/
public $trusted_static_properties = array();
+
/**
* This is an array of trusted PHP functions.
* If empty all functions are allowed.
@@ -103,12 +109,8 @@ class Smarty_Security
*
* @var array
*/
- public $php_functions = array(
- 'isset', 'empty',
- 'count', 'sizeof',
- 'in_array', 'is_array',
- 'time',
- );
+ public $php_functions = array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array', 'time',);
+
/**
* This is an array of trusted PHP modifiers.
* If empty all modifiers are allowed.
@@ -116,11 +118,8 @@ class Smarty_Security
*
* @var array
*/
- public $php_modifiers = array(
- 'escape',
- 'count',
- 'nl2br',
- );
+ public $php_modifiers = array('escape', 'count', 'nl2br',);
+
/**
* This is an array of allowed tags.
* If empty no restriction by allowed_tags.
@@ -128,6 +127,7 @@ class Smarty_Security
* @var array
*/
public $allowed_tags = array();
+
/**
* This is an array of disabled tags.
* If empty no restriction by disabled_tags.
@@ -135,6 +135,7 @@ class Smarty_Security
* @var array
*/
public $disabled_tags = array();
+
/**
* This is an array of allowed modifier plugins.
* If empty no restriction by allowed_modifiers.
@@ -142,6 +143,7 @@ class Smarty_Security
* @var array
*/
public $allowed_modifiers = array();
+
/**
* This is an array of disabled modifier plugins.
* If empty no restriction by disabled_modifiers.
@@ -149,12 +151,14 @@ class Smarty_Security
* @var array
*/
public $disabled_modifiers = array();
+
/**
* This is an array of disabled special $smarty variables.
*
* @var array
*/
public $disabled_special_smarty_vars = array();
+
/**
* This is an array of trusted streams.
* If empty all streams are allowed.
@@ -163,60 +167,70 @@ class Smarty_Security
* @var array
*/
public $streams = array('file');
+
/**
* + flag if constants can be accessed from template
*
* @var boolean
*/
public $allow_constants = true;
+
/**
* + flag if super globals can be accessed from template
*
* @var boolean
*/
public $allow_super_globals = true;
+
/**
* max template nesting level
*
* @var int
*/
public $max_template_nesting = 0;
+
/**
* current template nesting level
*
* @var int
*/
private $_current_template_nesting = 0;
+
/**
* Cache for $resource_dir lookup
*
* @var array
*/
- protected $_resource_dir = null;
+ protected $_resource_dir = array();
+
/**
* Cache for $template_dir lookup
*
* @var array
*/
- protected $_template_dir = null;
+ protected $_template_dir = array();
+
/**
* Cache for $config_dir lookup
*
* @var array
*/
- protected $_config_dir = null;
+ protected $_config_dir = array();
+
/**
* Cache for $secure_dir lookup
*
* @var array
*/
- protected $_secure_dir = null;
+ protected $_secure_dir = array();
+
/**
* Cache for $php_resource_dir lookup
*
* @var array
*/
protected $_php_resource_dir = null;
+
/**
* Cache for $trusted_dir lookup
*
@@ -225,6 +239,20 @@ class Smarty_Security
protected $_trusted_dir = null;
/**
+ * Cache for include path status
+ *
+ * @var bool
+ */
+ protected $_include_path_status = false;
+
+ /**
+ * Cache for $_include_array lookup
+ *
+ * @var array
+ */
+ protected $_include_dir = array();
+
+ /**
* @param Smarty $smarty
*/
public function __construct($smarty)
@@ -243,7 +271,9 @@ class Smarty_Security
*/
public function isTrustedPhpFunction($function_name, $compiler)
{
- if (isset($this->php_functions) && (empty($this->php_functions) || in_array($function_name, $this->php_functions))) {
+ if (isset($this->php_functions) &&
+ (empty($this->php_functions) || in_array($function_name, $this->php_functions))
+ ) {
return true;
}
@@ -263,7 +293,9 @@ class Smarty_Security
*/
public function isTrustedStaticClass($class_name, $compiler)
{
- if (isset($this->static_classes) && (empty($this->static_classes) || in_array($class_name, $this->static_classes))) {
+ if (isset($this->static_classes) &&
+ (empty($this->static_classes) || in_array($class_name, $this->static_classes))
+ ) {
return true;
}
@@ -301,9 +333,8 @@ class Smarty_Security
// fall back
return $this->isTrustedStaticClass($class_name, $compiler);
}
- if (isset($allowed[$class_name])
- && (empty($allowed[$class_name])
- || in_array($name, $allowed[$class_name]))
+ if (isset($allowed[$class_name]) &&
+ (empty($allowed[$class_name]) || in_array($name, $allowed[$class_name]))
) {
return true;
}
@@ -323,7 +354,9 @@ class Smarty_Security
*/
public function isTrustedPhpModifier($modifier_name, $compiler)
{
- if (isset($this->php_modifiers) && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))) {
+ if (isset($this->php_modifiers) &&
+ (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))
+ ) {
return true;
}
@@ -344,9 +377,11 @@ class Smarty_Security
public function isTrustedTag($tag_name, $compiler)
{
// check for internal always required tags
- if (in_array($tag_name, array('assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin', 'private_object_block_function',
- 'private_object_function', 'private_registered_function', 'private_registered_block', 'private_special_variable', 'private_print_expression', 'private_modifier'))
- ) {
+ if (in_array($tag_name, array('assign', 'call', 'private_filter', 'private_block_plugin',
+ 'private_function_plugin', 'private_object_block_function',
+ 'private_object_function', 'private_registered_function',
+ 'private_registered_block', 'private_special_variable',
+ 'private_print_expression', 'private_modifier'))) {
return true;
}
// check security settings
@@ -354,12 +389,12 @@ class Smarty_Security
if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) {
return true;
} else {
- $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", $compiler->lex->taglineno);
+ $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", null, true);
}
} elseif (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) {
return true;
} else {
- $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", $compiler->lex->taglineno);
+ $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", null, true);
}
return false; // should not, but who knows what happens to the compiler in the future?
@@ -379,7 +414,7 @@ class Smarty_Security
if (!in_array($var_name, $this->disabled_special_smarty_vars)) {
return true;
} else {
- $compiler->trigger_template_error("special variable '\$smarty.{$var_name}' not allowed by security setting", $compiler->lex->taglineno);
+ $compiler->trigger_template_error("special variable '\$smarty.{$var_name}' not allowed by security setting", null, true);
}
return false; // should not, but who knows what happens to the compiler in the future?
@@ -405,12 +440,14 @@ class Smarty_Security
if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) {
return true;
} else {
- $compiler->trigger_template_error("modifier '{$modifier_name}' disabled by security setting", $compiler->lex->taglineno);
+ $compiler->trigger_template_error("modifier '{$modifier_name}' disabled by security setting", null, true);
}
- } elseif (in_array($modifier_name, $this->allowed_modifiers) && !in_array($modifier_name, $this->disabled_modifiers)) {
+ } elseif (in_array($modifier_name, $this->allowed_modifiers) &&
+ !in_array($modifier_name, $this->disabled_modifiers)
+ ) {
return true;
} else {
- $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting", $compiler->lex->taglineno);
+ $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting", null, true);
}
return false; // should not, but who knows what happens to the compiler in the future?
@@ -419,7 +456,7 @@ class Smarty_Security
/**
* Check if constants are enabled or trusted
*
- * @param string $const contant name
+ * @param string $const constant name
* @param object $compiler compiler object
*
* @return bool
@@ -463,81 +500,70 @@ class Smarty_Security
/**
* Check if directory of file resource is trusted.
*
- * @param string $filepath
+ * @param string $filepath
+ * @param null|bool $isConfig
*
- * @return boolean true if directory is trusted
- * @throws SmartyException if directory is not trusted
+ * @return bool true if directory is trusted
+ * @throws \SmartyException if directory is not trusted
*/
- public function isTrustedResourceDir($filepath)
+ public function isTrustedResourceDir($filepath, $isConfig = null)
{
- $_template = false;
- $_config = false;
- $_secure = false;
-
- $_template_dir = $this->smarty->getTemplateDir();
- $_config_dir = $this->smarty->getConfigDir();
-
- // check if index is outdated
- if ((!$this->_template_dir || $this->_template_dir !== $_template_dir)
- || (!$this->_config_dir || $this->_config_dir !== $_config_dir)
- || (!empty($this->secure_dir) && (!$this->_secure_dir || $this->_secure_dir !== $this->secure_dir))
- ) {
- $this->_resource_dir = array();
- $_template = true;
- $_config = true;
- $_secure = !empty($this->secure_dir);
- }
-
- // rebuild template dir index
- if ($_template) {
- $this->_template_dir = $_template_dir;
- foreach ($_template_dir as $directory) {
- $directory = realpath($directory);
- $this->_resource_dir[$directory] = true;
+ if ($this->_include_path_status !== $this->smarty->use_include_path) {
+ foreach ($this->_include_dir as $directory) {
+ unset($this->_resource_dir[$directory]);
}
+ if ($this->smarty->use_include_path) {
+ $this->_include_dir = array();
+ $_dirs = $this->smarty->ext->_getIncludePath->getIncludePathDirs($this->smarty);
+ foreach ($_dirs as $directory) {
+ $this->_include_dir[] = $directory;
+ $this->_resource_dir[$directory] = true;
+ }
+ }
+ $this->_include_path_status = $this->smarty->use_include_path;
}
-
- // rebuild config dir index
- if ($_config) {
- $this->_config_dir = $_config_dir;
- foreach ($_config_dir as $directory) {
- $directory = realpath($directory);
- $this->_resource_dir[$directory] = true;
+ if ($isConfig !== true &&
+ (!isset($this->smarty->_cache['template_dir_new']) || $this->smarty->_cache['template_dir_new'])
+ ) {
+ $_dir = $this->smarty->getTemplateDir();
+ if ($this->_template_dir !== $_dir) {
+ foreach ($this->_template_dir as $directory) {
+ unset($this->_resource_dir[$directory]);
+ }
+ foreach ($_dir as $directory) {
+ $this->_resource_dir[$directory] = true;
+ }
+ $this->_template_dir = $_dir;
}
+ $this->smarty->_cache['template_dir_new'] = false;
}
-
- // rebuild secure dir index
- if ($_secure) {
- $this->_secure_dir = $this->secure_dir;
- foreach ((array) $this->secure_dir as $directory) {
- $directory = realpath($directory);
- $this->_resource_dir[$directory] = true;
+ if ($isConfig !== false &&
+ (!isset($this->smarty->_cache['config_dir_new']) || $this->smarty->_cache['config_dir_new'])
+ ) {
+ $_dir = $this->smarty->getConfigDir();
+ if ($this->_config_dir !== $_dir) {
+ foreach ($this->_config_dir as $directory) {
+ unset($this->_resource_dir[$directory]);
+ }
+ foreach ($_dir as $directory) {
+ $this->_resource_dir[$directory] = true;
+ }
+ $this->_config_dir = $_dir;
}
+ $this->smarty->_cache['config_dir_new'] = false;
}
-
- $_filepath = realpath($filepath);
- $directory = dirname($_filepath);
- $_directory = array();
- while (true) {
- // remember the directory to add it to _resource_dir in case we're successful
- $_directory[$directory] = true;
- // test if the directory is trusted
- if (isset($this->_resource_dir[$directory])) {
- // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
- $this->_resource_dir = array_merge($this->_resource_dir, $_directory);
-
- return true;
+ if ($this->_secure_dir !== (array) $this->secure_dir) {
+ foreach ($this->_secure_dir as $directory) {
+ unset($this->_resource_dir[$directory]);
}
- // abort if we've reached root
- if (($pos = strrpos($directory, DS)) === false || !isset($directory[1])) {
- break;
+ foreach ((array) $this->secure_dir as $directory) {
+ $directory = $this->smarty->_realpath($directory . DS, true);
+ $this->_resource_dir[$directory] = true;
}
- // bubble up one level
- $directory = substr($directory, 0, $pos);
+ $this->_secure_dir = (array) $this->secure_dir;
}
-
- // give up
- throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
+ $this->_resource_dir = $this->_checkDir($filepath, $this->_resource_dir);
+ return true;
}
/**
@@ -587,33 +613,13 @@ class Smarty_Security
$this->_trusted_dir = $this->trusted_dir;
foreach ((array) $this->trusted_dir as $directory) {
- $directory = realpath($directory);
+ $directory = $this->smarty->_realpath($directory . DS, true);
$this->_php_resource_dir[$directory] = true;
}
}
- $_filepath = realpath($filepath);
- $directory = dirname($_filepath);
- $_directory = array();
- while (true) {
- // remember the directory to add it to _resource_dir in case we're successful
- $_directory[] = $directory;
- // test if the directory is trusted
- if (isset($this->_php_resource_dir[$directory])) {
- // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
- $this->_php_resource_dir = array_merge($this->_php_resource_dir, $_directory);
-
- return true;
- }
- // abort if we've reached root
- if (($pos = strrpos($directory, DS)) === false || !isset($directory[2])) {
- break;
- }
- // bubble up one level
- $directory = substr($directory, 0, $pos);
- }
-
- throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
+ $this->_php_resource_dir = $this->_checkDir($this->smarty->_realpath($filepath, true), $this->_php_resource_dir);
+ return true;
}
/**
@@ -633,12 +639,77 @@ class Smarty_Security
/**
* Exit template processing
*
- * @param $template
+ * @internal param $template
*/
- public function exitTemplate($template)
+ public function exitTemplate()
{
if ($this->max_template_nesting > 0) {
$this->_current_template_nesting --;
}
}
+
+ /**
+ * Check if file is inside a valid directory
+ *
+ * @param string $filepath
+ * @param array $dirs valid directories
+ *
+ * @return array
+ * @throws \SmartyException
+ */
+ private function _checkDir($filepath, $dirs)
+ {
+ $directory = dirname($filepath) . DS;
+ $_directory = array();
+ while (true) {
+ // remember the directory to add it to _resource_dir in case we're successful
+ $_directory[$directory] = true;
+ // test if the directory is trusted
+ if (isset($dirs[$directory])) {
+ // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
+ $dirs = array_merge($dirs, $_directory);
+
+ return $dirs;
+ }
+ // abort if we've reached root
+ if (!preg_match('#[\\\/][^\\\/]+[\\\/]$#', $directory)) {
+ break;
+ }
+ // bubble up one level
+ $directory = preg_replace('#[\\\/][^\\\/]+[\\\/]$#', DS, $directory);
+ }
+
+ // give up
+ throw new SmartyException("directory '{$filepath}' not allowed by security setting");
+ }
+
+ /**
+ * Loads security class and enables security
+ *
+ * @param \Smarty $smarty
+ * @param string|Smarty_Security $security_class if a string is used, it must be class-name
+ *
+ * @return \Smarty current Smarty instance for chaining
+ * @throws \SmartyException when an invalid class name is provided
+ */
+ public static function enableSecurity(Smarty $smarty, $security_class)
+ {
+ if ($security_class instanceof Smarty_Security) {
+ $smarty->security_policy = $security_class;
+ return;
+ } elseif (is_object($security_class)) {
+ throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security.");
+ }
+ if ($security_class == null) {
+ $security_class = $smarty->security_class;
+ }
+ if (!class_exists($security_class)) {
+ throw new SmartyException("Security class '$security_class' is not defined");
+ } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
+ throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
+ } else {
+ $smarty->security_policy = new $security_class($smarty);
+ }
+ return;
+ }
}