PEEL Shopping
Open source ecommerce : PEEL Shopping
smarty_security.php
Go to the documentation of this file.
1 <?php
10 /*
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?
17  */
18 
23 
43  public $secure_dir = array();
50  public $trusted_dir = array();
56  public $trusted_uri = array();
64  public $static_classes = array();
72  public $php_functions = array(
73  'isset', 'empty',
74  'count', 'sizeof',
75  'in_array', 'is_array',
76  'time',
77  'nl2br',
78  );
86  public $php_modifiers = array(
87  'escape',
88  'count'
89  );
96  public $allowed_tags = array();
103  public $disabled_tags = array();
110  public $allowed_modifiers = array();
117  public $disabled_modifiers = array();
125  public $streams = array('file');
130  public $allow_constants = true;
135  public $allow_super_globals = true;
136 
141  protected $_resource_dir = null;
146  protected $_template_dir = null;
151  protected $_config_dir = null;
156  protected $_secure_dir = null;
161  protected $_php_resource_dir = null;
166  protected $_trusted_dir = null;
167 
168 
172  public function __construct($smarty)
173  {
174  $this->smarty = $smarty;
175  }
176 
185  public function isTrustedPhpFunction($function_name, $compiler)
186  {
187  if (isset($this->php_functions) && (empty($this->php_functions) || in_array($function_name, $this->php_functions))) {
188  return true;
189  }
190 
191  $compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting");
192  return false; // should not, but who knows what happens to the compiler in the future?
193  }
194 
203  public function isTrustedStaticClass($class_name, $compiler)
204  {
205  if (isset($this->static_classes) && (empty($this->static_classes) || in_array($class_name, $this->static_classes))) {
206  return true;
207  }
208 
209  $compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting");
210  return false; // should not, but who knows what happens to the compiler in the future?
211  }
212 
221  public function isTrustedPhpModifier($modifier_name, $compiler)
222  {
223  if (isset($this->php_modifiers) && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))) {
224  return true;
225  }
226 
227  $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting");
228  return false; // should not, but who knows what happens to the compiler in the future?
229  }
230 
239  public function isTrustedTag($tag_name, $compiler)
240  {
241  // check for internal always required tags
242  if (in_array($tag_name, array('assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin', 'private_object_block_function',
243  'private_object_function', 'private_registered_function', 'private_registered_block', 'private_special_variable', 'private_print_expression', 'private_modifier'))) {
244  return true;
245  }
246  // check security settings
247  if (empty($this->allowed_tags)) {
248  if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) {
249  return true;
250  } else {
251  $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", $compiler->lex->taglineno);
252  }
253  } else if (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) {
254  return true;
255  } else {
256  $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", $compiler->lex->taglineno);
257  }
258  return false; // should not, but who knows what happens to the compiler in the future?
259  }
260 
269  public function isTrustedModifier($modifier_name, $compiler)
270  {
271  // check for internal always allowed modifier
272  if (in_array($modifier_name, array('default'))) {
273  return true;
274  }
275  // check security settings
276  if (empty($this->allowed_modifiers)) {
277  if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) {
278  return true;
279  } else {
280  $compiler->trigger_template_error("modifier '{$modifier_name}' disabled by security setting", $compiler->lex->taglineno);
281  }
282  } else if (in_array($modifier_name, $this->allowed_modifiers) && !in_array($modifier_name, $this->disabled_modifiers)) {
283  return true;
284  } else {
285  $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting", $compiler->lex->taglineno);
286  }
287  return false; // should not, but who knows what happens to the compiler in the future?
288  }
289 
297  public function isTrustedStream($stream_name)
298  {
299  if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) {
300  return true;
301  }
302 
303  throw new SmartyException("stream '{$stream_name}' not allowed by security setting");
304  }
305 
313  public function isTrustedResourceDir($filepath)
314  {
315  $_template = false;
316  $_config = false;
317  $_secure = false;
318 
319  $_template_dir = $this->smarty->getTemplateDir();
320  $_config_dir = $this->smarty->getConfigDir();
321 
322  // check if index is outdated
323  if ((!$this->_template_dir || $this->_template_dir !== $_template_dir)
324  || (!$this->_config_dir || $this->_config_dir !== $_config_dir)
325  || (!empty($this->secure_dir) && (!$this->_secure_dir || $this->_secure_dir !== $this->secure_dir))
326  ) {
327  $this->_resource_dir = array();
328  $_template = true;
329  $_config = true;
330  $_secure = !empty($this->secure_dir);
331  }
332 
333  // rebuild template dir index
334  if ($_template) {
335  $this->_template_dir = $_template_dir;
336  foreach ($_template_dir as $directory) {
337  $directory = realpath($directory);
338  $this->_resource_dir[$directory] = true;
339  }
340  }
341 
342  // rebuild config dir index
343  if ($_config) {
344  $this->_config_dir = $_config_dir;
345  foreach ($_config_dir as $directory) {
346  $directory = realpath($directory);
347  $this->_resource_dir[$directory] = true;
348  }
349  }
350 
351  // rebuild secure dir index
352  if ($_secure) {
353  $this->_secure_dir = $this->secure_dir;
354  foreach ((array) $this->secure_dir as $directory) {
355  $directory = realpath($directory);
356  $this->_resource_dir[$directory] = true;
357  }
358  }
359 
360  $_filepath = realpath($filepath);
361  $directory = dirname($_filepath);
362  $_directory = array();
363  while (true) {
364  // remember the directory to add it to _resource_dir in case we're successful
365  $_directory[$directory] = true;
366  // test if the directory is trusted
367  if (isset($this->_resource_dir[$directory])) {
368  // merge sub directories of current $directory into _resource_dir to speed up subsequent lookups
369  $this->_resource_dir = array_merge($this->_resource_dir, $_directory);
370  return true;
371  }
372  // abort if we've reached root
373  if (($pos = strrpos($directory, DS)) === false || !isset($directory[1])) {
374  break;
375  }
376  // bubble up one level
377  $directory = substr($directory, 0, $pos);
378  }
379 
380  // give up
381  throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
382  }
383 
395  public function isTrustedUri($uri)
396  {
397  $_uri = parse_url($uri);
398  if (!empty($_uri['scheme']) && !empty($_uri['host'])) {
399  $_uri = $_uri['scheme'] . '://' . $_uri['host'];
400  foreach ($this->trusted_uri as $pattern) {
401  if (preg_match($pattern, $_uri)) {
402  return true;
403  }
404  }
405  }
406 
407  throw new SmartyException("URI '{$uri}' not allowed by security setting");
408  }
409 
417  public function isTrustedPHPDir($filepath)
418  {
419  if (empty($this->trusted_dir)) {
420  throw new SmartyException("directory '{$filepath}' not allowed by security setting (no trusted_dir specified)");
421  }
422 
423  // check if index is outdated
424  if (!$this->_trusted_dir || $this->_trusted_dir !== $this->trusted_dir) {
425  $this->_php_resource_dir = array();
426 
427  $this->_trusted_dir = $this->trusted_dir;
428  foreach ((array) $this->trusted_dir as $directory) {
429  $directory = realpath($directory);
430  $this->_php_resource_dir[$directory] = true;
431  }
432  }
433 
434  $_filepath = realpath($filepath);
435  $directory = dirname($_filepath);
436  $_directory = array();
437  while (true) {
438  // remember the directory to add it to _resource_dir in case we're successful
439  $_directory[] = $directory;
440  // test if the directory is trusted
441  if (isset($this->_php_resource_dir[$directory])) {
442  // merge sub directories of current $directory into _resource_dir to speed up subsequent lookups
443  $this->_php_resource_dir = array_merge($this->_php_resource_dir, $_directory);
444  return true;
445  }
446  // abort if we've reached root
447  if (($pos = strrpos($directory, DS)) === false || !isset($directory[2])) {
448  break;
449  }
450  // bubble up one level
451  $directory = substr($directory, 0, $pos);
452  }
453 
454  throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
455  }
456 
457 }
458 
459 ?>
isTrustedPhpModifier($modifier_name, $compiler)
Check if PHP modifier is trusted.
isTrustedPhpFunction($function_name, $compiler)
Check if PHP function is trusted.
isTrustedStaticClass($class_name, $compiler)
Check if static class is trusted.
isTrustedUri($uri)
Check if URI (e.g.
const PHP_PASSTHRU
modes for handling of "" tags in templates.
isTrustedStream($stream_name)
Check if stream is trusted.
This class does contain the security settings.
isTrustedTag($tag_name, $compiler)
Check if tag is trusted.
isTrustedResourceDir($filepath)
Check if directory of file resource is trusted.
isTrustedPHPDir($filepath)
Check if directory of file resource is trusted.
isTrustedModifier($modifier_name, $compiler)
Check if modifier plugin is trusted.

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:17 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.