PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/_html-warrior/externals/smarty/libs/sysplugins/smarty_security.php

https://github.com/halka139/html-warrior
PHP | 411 lines | 186 code | 29 blank | 196 comment | 51 complexity | 335d4255bb05b2767b655b5c046e2de3 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage Security
  7. * @author Uwe Tews
  8. */
  9. /**
  10. * This class does contain the security settings
  11. */
  12. class Smarty_Security {
  13. /**
  14. * This determines how Smarty handles "<?php ... ?>" tags in templates.
  15. * possible values:
  16. * <ul>
  17. * <li>Smarty::PHP_PASSTHRU -> echo PHP tags as they are</li>
  18. * <li>Smarty::PHP_QUOTE -> escape tags as entities</li>
  19. * <li>Smarty::PHP_REMOVE -> remove php tags</li>
  20. * <li>Smarty::PHP_ALLOW -> execute php tags</li>
  21. * </ul>
  22. *
  23. * @var integer
  24. */
  25. public $php_handling = Smarty::PHP_PASSTHRU;
  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. * This is an array of directories where trusted php scripts reside.
  35. * {@link $security} is disabled during their inclusion/execution.
  36. *
  37. * @var array
  38. */
  39. public $trusted_dir = array();
  40. /**
  41. * This is an array of trusted static classes.
  42. *
  43. * If empty access to all static classes is allowed.
  44. * If set to 'none' none is allowed.
  45. * @var array
  46. */
  47. public $static_classes = array();
  48. /**
  49. * This is an array of trusted PHP functions.
  50. *
  51. * If empty all functions are allowed.
  52. * To disable all PHP functions set $php_functions = null.
  53. * @var array
  54. */
  55. public $php_functions = array(
  56. 'isset', 'empty',
  57. 'count', 'sizeof',
  58. 'in_array', 'is_array',
  59. 'time',
  60. 'nl2br',
  61. );
  62. /**
  63. * This is an array of trusted PHP modifers.
  64. *
  65. * If empty all modifiers are allowed.
  66. * To disable all modifier set $modifiers = null.
  67. * @var array
  68. */
  69. public $php_modifiers = array(
  70. 'escape',
  71. 'count'
  72. );
  73. /**
  74. * This is an array of allowed tags.
  75. *
  76. * If empty no restriction by allowed_tags.
  77. * @var array
  78. */
  79. public $allowed_tags = array();
  80. /**
  81. * This is an array of disabled tags.
  82. *
  83. * If empty no restriction by disabled_tags.
  84. * @var array
  85. */
  86. public $disabled_tags = array();
  87. /**
  88. * This is an array of allowed modifier plugins.
  89. *
  90. * If empty no restriction by allowed_modifier.
  91. * @var array
  92. */
  93. public $allowed_modifier = array();
  94. /**
  95. * This is an array of disabled modifier plugins.
  96. *
  97. * If empty no restriction by disabled_modifier.
  98. * @var array
  99. */
  100. public $disabled_modifier = array();
  101. /**
  102. * This is an array of trusted streams.
  103. *
  104. * If empty all streams are allowed.
  105. * To disable all streams set $streams = null.
  106. * @var array
  107. */
  108. public $streams = array('file');
  109. /**
  110. * + flag if constants can be accessed from template
  111. * @var boolean
  112. */
  113. public $allow_constants = true;
  114. /**
  115. * + flag if super globals can be accessed from template
  116. * @var boolean
  117. */
  118. public $allow_super_globals = true;
  119. /**
  120. * @param Smarty $smarty
  121. */
  122. public function __construct($smarty)
  123. {
  124. $this->smarty = $smarty;
  125. }
  126. /**
  127. * @var string
  128. */
  129. protected $_resource_dir = null;
  130. /**
  131. * @var string
  132. */
  133. protected $_template_dir = null;
  134. /**
  135. * @var string
  136. */
  137. protected $_config_dir = null;
  138. /**
  139. * @var string
  140. */
  141. protected $_secure_dir = null;
  142. /**
  143. * @var string
  144. */
  145. protected $_php_resource_dir = null;
  146. /**
  147. * @var string
  148. */
  149. protected $_trusted_dir = null;
  150. /**
  151. * Check if PHP function is trusted.
  152. *
  153. * @param string $function_name
  154. * @param object $compiler compiler object
  155. * @return boolean true if function is trusted
  156. * @throws SmartyCompilerException if php function is not trusted
  157. */
  158. public function isTrustedPhpFunction($function_name, $compiler)
  159. {
  160. if (isset($this->php_functions) && (empty($this->php_functions) || in_array($function_name, $this->php_functions))) {
  161. return true;
  162. }
  163. $compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting");
  164. return false; // should not, but who knows what happens to the compiler in the future?
  165. }
  166. /**
  167. * Check if static class is trusted.
  168. *
  169. * @param string $class_name
  170. * @param object $compiler compiler object
  171. * @return boolean true if class is trusted
  172. * @throws SmartyCompilerException if static class is not trusted
  173. */
  174. public function isTrustedStaticClass($class_name, $compiler)
  175. {
  176. if (isset($this->static_classes) && (empty($this->static_classes) || in_array($class_name, $this->static_classes))) {
  177. return true;
  178. }
  179. $compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting");
  180. return false; // should not, but who knows what happens to the compiler in the future?
  181. }
  182. /**
  183. * Check if PHP modifier is trusted.
  184. *
  185. * @param string $modifier_name
  186. * @param object $compiler compiler object
  187. * @return boolean true if modifier is trusted
  188. * @throws SmartyCompilerException if modifier is not trusted
  189. */
  190. public function isTrustedPhpModifier($modifier_name, $compiler)
  191. {
  192. if (isset($this->php_modifiers) && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))) {
  193. return true;
  194. }
  195. $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting");
  196. return false; // should not, but who knows what happens to the compiler in the future?
  197. }
  198. /**
  199. * Check if tag is trusted.
  200. *
  201. * @param string $tag_name
  202. * @param object $compiler compiler object
  203. * @return boolean true if tag is trusted
  204. * @throws SmartyCompilerException if modifier is not trusted
  205. */
  206. public function isTrustedTag($tag_name, $compiler)
  207. {
  208. // check for internal always required tags
  209. if (in_array($tag_name, array('assign', 'call', 'private_filter', 'private_block_plugin', 'private_function_plugin', 'private_object_block_function',
  210. 'private_object_function', 'private_registered_function', 'private_registered_block', 'private_special_variable', 'private_print_expression', 'private_modifier'))) {
  211. return true;
  212. }
  213. // check security settings
  214. if (empty($this->allowed_tags)) {
  215. if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) {
  216. return true;
  217. } else {
  218. $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", $compiler->lex->taglineno);
  219. }
  220. } else if (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) {
  221. return true;
  222. } else {
  223. $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", $compiler->lex->taglineno);
  224. }
  225. return false; // should not, but who knows what happens to the compiler in the future?
  226. }
  227. /**
  228. * Check if modifier plugin is trusted.
  229. *
  230. * @param string $modifier_name
  231. * @param object $compiler compiler object
  232. * @return boolean true if tag is trusted
  233. * @throws SmartyCompilerException if modifier is not trusted
  234. */
  235. public function isTrustedModifier($modifier_name, $compiler)
  236. {
  237. // check for internal always allowed modifier
  238. if (in_array($modifier_name, array('default'))) {
  239. return true;
  240. }
  241. // check security settings
  242. if (empty($this->allowed_modifier)) {
  243. if (empty($this->disabled_modifier) || !in_array($modifier_name, $this->disabled_modifier)) {
  244. return true;
  245. } else {
  246. $compiler->trigger_template_error("modifier '{$modifier_name}' disabled by security setting", $compiler->lex->taglineno);
  247. }
  248. } else if (in_array($modifier_name, $this->allowed_modifier) && !in_array($modifier_name, $this->disabled_modifier)) {
  249. return true;
  250. } else {
  251. $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting", $compiler->lex->taglineno);
  252. }
  253. return false; // should not, but who knows what happens to the compiler in the future?
  254. }
  255. /**
  256. * Check if stream is trusted.
  257. *
  258. * @param string $stream_name
  259. * @return boolean true if stream is trusted
  260. * @throws SmartyException if stream is not trusted
  261. */
  262. public function isTrustedStream($stream_name)
  263. {
  264. if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) {
  265. return true;
  266. }
  267. throw new SmartyException("stream '{$stream_name}' not allowed by security setting");
  268. }
  269. /**
  270. * Check if directory of file resource is trusted.
  271. *
  272. * @param string $filepath
  273. * @return boolean true if directory is trusted
  274. * @throws SmartyException if directory is not trusted
  275. */
  276. public function isTrustedResourceDir($filepath)
  277. {
  278. $_template = false;
  279. $_config = false;
  280. $_secure = false;
  281. $_template_dir = $this->smarty->getTemplateDir();
  282. $_config_dir = $this->smarty->getConfigDir();
  283. // check if index is outdated
  284. if ((!$this->_template_dir || $this->_template_dir !== $_template_dir)
  285. || (!$this->_config_dir || $this->_config_dir !== $_config_dir)
  286. || (!empty($this->secure_dir) && (!$this->_secure_dir || $this->_secure_dir !== $this->secure_dir))
  287. ) {
  288. $this->_resource_dir = array();
  289. $_template = true;
  290. $_config = true;
  291. $_secure = !empty($this->secure_dir);
  292. }
  293. // rebuild template dir index
  294. if ($_template) {
  295. $this->_template_dir = $_template_dir;
  296. foreach ($_template_dir as $directory) {
  297. $directory = realpath($directory);
  298. $this->_resource_dir[$directory] = true;
  299. }
  300. }
  301. // rebuild config dir index
  302. if ($_config) {
  303. $this->_config_dir = $_config_dir;
  304. foreach ($_config_dir as $directory) {
  305. $directory = realpath($directory);
  306. $this->_resource_dir[$directory] = true;
  307. }
  308. }
  309. // rebuild secure dir index
  310. if ($_secure) {
  311. $this->_secure_dir = $this->secure_dir;
  312. foreach ((array) $this->secure_dir as $directory) {
  313. $directory = realpath($directory);
  314. $this->_resource_dir[$directory] = true;
  315. }
  316. }
  317. $_filepath = realpath($filepath);
  318. $directory = dirname($_filepath);
  319. $_directory = array();
  320. while (true) {
  321. // remember the directory to add it to _resource_dir in case we're successful
  322. $_directory[] = $directory;
  323. // test if the directory is trusted
  324. if (isset($this->_resource_dir[$directory])) {
  325. // merge sub directories of current $directory into _resource_dir to speed up subsequent lookups
  326. $this->_resource_dir = array_merge($this->_resource_dir, $_directory);
  327. return true;
  328. }
  329. // abort if we've reached root
  330. if (($pos = strrpos($directory, DS)) === false || strlen($directory) < 2) {
  331. break;
  332. }
  333. // bubble up one level
  334. $directory = substr($directory, 0, $pos);
  335. }
  336. // give up
  337. throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
  338. }
  339. /**
  340. * Check if directory of file resource is trusted.
  341. *
  342. * @param string $filepath
  343. * @return boolean true if directory is trusted
  344. * @throws SmartyException if PHP directory is not trusted
  345. */
  346. public function isTrustedPHPDir($filepath)
  347. {
  348. if (empty($this->trusted_dir)) {
  349. throw new SmartyException("directory '{$filepath}' not allowed by security setting (no trusted_dir specified)");
  350. }
  351. // check if index is outdated
  352. if (!$this->_trusted_dir || $this->_trusted_dir !== $this->trusted_dir) {
  353. $this->_php_resource_dir = array();
  354. $this->_trusted_dir = $this->trusted_dir;
  355. foreach ((array) $this->trusted_dir as $directory) {
  356. $directory = realpath($directory);
  357. $this->_php_resource_dir[$directory] = true;
  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;
  366. // test if the directory is trusted
  367. if (isset($this->_php_resource_dir[$directory])) {
  368. // merge sub directories of current $directory into _resource_dir to speed up subsequent lookups
  369. $this->_php_resource_dir = array_merge($this->_php_resource_dir, $_directory);
  370. return true;
  371. }
  372. // abort if we've reached root
  373. if (($pos = strrpos($directory, DS)) === false || strlen($directory) < 2) {
  374. break;
  375. }
  376. // bubble up one level
  377. $directory = substr($directory, 0, $pos);
  378. }
  379. throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
  380. }
  381. }
  382. ?>