smarty_security.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage Security
  7. * @author Uwe Tews
  8. */
  9. /*
  10. * FIXME: Smarty_Security API
  11. * - getter and setter instead of public properties would allow cultivating an internal cache properly
  12. * - current implementation of isTrustedResourceDir() assumes that Smarty::$template_dir and Smarty::$config_dir are immutable
  13. * the cache is killed every time either of the variables change. That means that two distinct Smarty objects with differing
  14. * $template_dir or $config_dir should NOT share the same Smarty_Security instance,
  15. * as this would lead to (severe) performance penalty! how should this be handled?
  16. */
  17. /**
  18. * This class does contain the security settings
  19. */
  20. class Smarty_Security
  21. {
  22. /**
  23. * This determines how Smarty handles "<?php ... ?>" tags in templates.
  24. * possible values:
  25. * <ul>
  26. * <li>Smarty::PHP_PASSTHRU -> echo PHP tags as they are</li>
  27. * <li>Smarty::PHP_QUOTE -> escape tags as entities</li>
  28. * <li>Smarty::PHP_REMOVE -> remove php tags</li>
  29. * <li>Smarty::PHP_ALLOW -> execute php tags</li>
  30. * </ul>
  31. *
  32. * @var integer
  33. */
  34. public $php_handling = Smarty::PHP_PASSTHRU;
  35. /**
  36. * This is the list of template directories that are considered secure.
  37. * $template_dir is in this list implicitly.
  38. *
  39. * @var array
  40. */
  41. public $secure_dir = array();
  42. /**
  43. * This is an array of directories where trusted php scripts reside.
  44. * {@link $security} is disabled during their inclusion/execution.
  45. *
  46. * @var array
  47. */
  48. public $trusted_dir = array();
  49. /**
  50. * List of regular expressions (PCRE) that include trusted URIs
  51. *
  52. * @var array
  53. */
  54. public $trusted_uri = array();
  55. /**
  56. * This is an array of trusted static classes.
  57. * If empty access to all static classes is allowed.
  58. * If set to 'none' none is allowed.
  59. *
  60. * @var array
  61. */
  62. public $static_classes = array();
  63. /**
  64. * This is an array of trusted PHP functions.
  65. * If empty all functions are allowed.
  66. * To disable all PHP functions set $php_functions = null.
  67. *
  68. * @var array
  69. */
  70. public $php_functions = array(
  71. 'isset', 'empty',
  72. 'count', 'sizeof',
  73. 'in_array', 'is_array',
  74. 'time',
  75. 'nl2br',
  76. );
  77. /**
  78. * This is an array of trusted PHP modifiers.
  79. * If empty all modifiers are allowed.
  80. * To disable all modifier set $php_modifiers = null.
  81. *
  82. * @var array
  83. */
  84. public $php_modifiers = array(
  85. 'escape',
  86. 'count'
  87. );
  88. /**
  89. * This is an array of allowed tags.
  90. * If empty no restriction by allowed_tags.
  91. *
  92. * @var array
  93. */
  94. public $allowed_tags = array();
  95. /**
  96. * This is an array of disabled tags.
  97. * If empty no restriction by disabled_tags.
  98. *
  99. * @var array
  100. */
  101. public $disabled_tags = array();
  102. /**
  103. * This is an array of allowed modifier plugins.
  104. * If empty no restriction by allowed_modifiers.
  105. *
  106. * @var array
  107. */
  108. public $allowed_modifiers = array();
  109. /**
  110. * This is an array of disabled modifier plugins.
  111. * If empty no restriction by disabled_modifiers.
  112. *
  113. * @var array
  114. */
  115. public $disabled_modifiers = array();
  116. /**
  117. * This is an array of trusted streams.
  118. * If empty all streams are allowed.
  119. * To disable all streams set $streams = null.
  120. *
  121. * @var array
  122. */
  123. public $streams = array('file');
  124. /**
  125. * + flag if constants can be accessed from template
  126. *
  127. * @var boolean
  128. */
  129. public $allow_constants = true;
  130. /**
  131. * + flag if super globals can be accessed from template
  132. *
  133. * @var boolean
  134. */
  135. public $allow_super_globals = true;
  136. /**
  137. * Cache for $resource_dir lookup
  138. *
  139. * @var array
  140. */
  141. protected $_resource_dir = null;
  142. /**
  143. * Cache for $template_dir lookup
  144. *
  145. * @var array
  146. */
  147. protected $_template_dir = null;
  148. /**
  149. * Cache for $config_dir lookup
  150. *
  151. * @var array
  152. */
  153. protected $_config_dir = null;
  154. /**
  155. * Cache for $secure_dir lookup
  156. *
  157. * @var array
  158. */
  159. protected $_secure_dir = null;
  160. /**
  161. * Cache for $php_resource_dir lookup
  162. *
  163. * @var array
  164. */
  165. protected $_php_resource_dir = null;
  166. /**
  167. * Cache for $trusted_dir lookup
  168. *
  169. * @var array
  170. */
  171. protected $_trusted_dir = null;
  172. /**
  173. * @param Smarty $smarty
  174. */
  175. public function __construct($smarty)
  176. {
  177. $this->smarty = $smarty;
  178. }
  179. /**
  180. * Check if PHP function is trusted.
  181. *
  182. * @param string $function_name
  183. * @param object $compiler compiler object
  184. *
  185. * @return boolean true if function is trusted
  186. * @throws SmartyCompilerException if php function is not trusted
  187. */
  188. public function isTrustedPhpFunction($function_name, $compiler)
  189. {
  190. if (isset($this->php_functions) && (empty($this->php_functions) || in_array($function_name, $this->php_functions))) {
  191. return true;
  192. }
  193. $compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting");
  194. return false; // should not, but who knows what happens to the compiler in the future?
  195. }
  196. /**
  197. * Check if static class is trusted.
  198. *
  199. * @param string $class_name
  200. * @param object $compiler compiler object
  201. *
  202. * @return boolean true if class is trusted
  203. * @throws SmartyCompilerException if static class is not trusted
  204. */
  205. public function isTrustedStaticClass($class_name, $compiler)
  206. {
  207. if (isset($this->static_classes) && (empty($this->static_classes) || in_array($class_name, $this->static_classes))) {
  208. return true;
  209. }
  210. $compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting");
  211. return false; // should not, but who knows what happens to the compiler in the future?
  212. }
  213. /**
  214. * Check if PHP modifier is trusted.
  215. *
  216. * @param string $modifier_name
  217. * @param object $compiler compiler object
  218. *
  219. * @return boolean true if modifier is trusted
  220. * @throws SmartyCompilerException if modifier is not trusted
  221. */
  222. public function isTrustedPhpModifier($modifier_name, $compiler)
  223. {
  224. if (isset($this->php_modifiers) && (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))) {
  225. return true;
  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. /**
  231. * Check if tag is trusted.
  232. *
  233. * @param string $tag_name
  234. * @param object $compiler compiler object
  235. *
  236. * @return boolean true if tag is trusted
  237. * @throws SmartyCompilerException if modifier is not trusted
  238. */
  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. ) {
  245. return true;
  246. }
  247. // check security settings
  248. if (empty($this->allowed_tags)) {
  249. if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) {
  250. return true;
  251. } else {
  252. $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", $compiler->lex->taglineno);
  253. }
  254. } elseif (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) {
  255. return true;
  256. } else {
  257. $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", $compiler->lex->taglineno);
  258. }
  259. return false; // should not, but who knows what happens to the compiler in the future?
  260. }
  261. /**
  262. * Check if modifier plugin is trusted.
  263. *
  264. * @param string $modifier_name
  265. * @param object $compiler compiler object
  266. *
  267. * @return boolean true if tag is trusted
  268. * @throws SmartyCompilerException if modifier is not trusted
  269. */
  270. public function isTrustedModifier($modifier_name, $compiler)
  271. {
  272. // check for internal always allowed modifier
  273. if (in_array($modifier_name, array('default'))) {
  274. return true;
  275. }
  276. // check security settings
  277. if (empty($this->allowed_modifiers)) {
  278. if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) {
  279. return true;
  280. } else {
  281. $compiler->trigger_template_error("modifier '{$modifier_name}' disabled by security setting", $compiler->lex->taglineno);
  282. }
  283. } elseif (in_array($modifier_name, $this->allowed_modifiers) && !in_array($modifier_name, $this->disabled_modifiers)) {
  284. return true;
  285. } else {
  286. $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting", $compiler->lex->taglineno);
  287. }
  288. return false; // should not, but who knows what happens to the compiler in the future?
  289. }
  290. /**
  291. * Check if stream is trusted.
  292. *
  293. * @param string $stream_name
  294. *
  295. * @return boolean true if stream is trusted
  296. * @throws SmartyException if stream is not trusted
  297. */
  298. public function isTrustedStream($stream_name)
  299. {
  300. if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) {
  301. return true;
  302. }
  303. throw new SmartyException("stream '{$stream_name}' not allowed by security setting");
  304. }
  305. /**
  306. * Check if directory of file resource is trusted.
  307. *
  308. * @param string $filepath
  309. *
  310. * @return boolean true if directory is trusted
  311. * @throws SmartyException if directory is not trusted
  312. */
  313. public function isTrustedResourceDir($filepath)
  314. {
  315. $_template = false;
  316. $_config = false;
  317. $_secure = false;
  318. $_template_dir = $this->smarty->getTemplateDir();
  319. $_config_dir = $this->smarty->getConfigDir();
  320. // check if index is outdated
  321. if ((!$this->_template_dir || $this->_template_dir !== $_template_dir)
  322. || (!$this->_config_dir || $this->_config_dir !== $_config_dir)
  323. || (!empty($this->secure_dir) && (!$this->_secure_dir || $this->_secure_dir !== $this->secure_dir))
  324. ) {
  325. $this->_resource_dir = array();
  326. $_template = true;
  327. $_config = true;
  328. $_secure = !empty($this->secure_dir);
  329. }
  330. // rebuild template dir index
  331. if ($_template) {
  332. $this->_template_dir = $_template_dir;
  333. foreach ($_template_dir as $directory) {
  334. $directory = realpath($directory);
  335. $this->_resource_dir[$directory] = true;
  336. }
  337. }
  338. // rebuild config dir index
  339. if ($_config) {
  340. $this->_config_dir = $_config_dir;
  341. foreach ($_config_dir as $directory) {
  342. $directory = realpath($directory);
  343. $this->_resource_dir[$directory] = true;
  344. }
  345. }
  346. // rebuild secure dir index
  347. if ($_secure) {
  348. $this->_secure_dir = $this->secure_dir;
  349. foreach ((array) $this->secure_dir as $directory) {
  350. $directory = realpath($directory);
  351. $this->_resource_dir[$directory] = true;
  352. }
  353. }
  354. $_filepath = realpath($filepath);
  355. $directory = dirname($_filepath);
  356. $_directory = array();
  357. while (true) {
  358. // remember the directory to add it to _resource_dir in case we're successful
  359. $_directory[$directory] = true;
  360. // test if the directory is trusted
  361. if (isset($this->_resource_dir[$directory])) {
  362. // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
  363. $this->_resource_dir = array_merge($this->_resource_dir, $_directory);
  364. return true;
  365. }
  366. // abort if we've reached root
  367. if (($pos = strrpos($directory, DS)) === false || !isset($directory[1])) {
  368. break;
  369. }
  370. // bubble up one level
  371. $directory = substr($directory, 0, $pos);
  372. }
  373. // give up
  374. throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
  375. }
  376. /**
  377. * Check if URI (e.g. {fetch} or {html_image}) is trusted
  378. * To simplify things, isTrustedUri() resolves all input to "{$PROTOCOL}://{$HOSTNAME}".
  379. * So "http://username:password@hello.world.example.org:8080/some-path?some=query-string"
  380. * is reduced to "http://hello.world.example.org" prior to applying the patters from {@link $trusted_uri}.
  381. *
  382. * @param string $uri
  383. *
  384. * @return boolean true if URI is trusted
  385. * @throws SmartyException if URI is not trusted
  386. * @uses $trusted_uri for list of patterns to match against $uri
  387. */
  388. public function isTrustedUri($uri)
  389. {
  390. $_uri = parse_url($uri);
  391. if (!empty($_uri['scheme']) && !empty($_uri['host'])) {
  392. $_uri = $_uri['scheme'] . '://' . $_uri['host'];
  393. foreach ($this->trusted_uri as $pattern) {
  394. if (preg_match($pattern, $_uri)) {
  395. return true;
  396. }
  397. }
  398. }
  399. throw new SmartyException("URI '{$uri}' not allowed by security setting");
  400. }
  401. /**
  402. * Check if directory of file resource is trusted.
  403. *
  404. * @param string $filepath
  405. *
  406. * @return boolean true if directory is trusted
  407. * @throws SmartyException if PHP directory is not trusted
  408. */
  409. public function isTrustedPHPDir($filepath)
  410. {
  411. if (empty($this->trusted_dir)) {
  412. throw new SmartyException("directory '{$filepath}' not allowed by security setting (no trusted_dir specified)");
  413. }
  414. // check if index is outdated
  415. if (!$this->_trusted_dir || $this->_trusted_dir !== $this->trusted_dir) {
  416. $this->_php_resource_dir = array();
  417. $this->_trusted_dir = $this->trusted_dir;
  418. foreach ((array) $this->trusted_dir as $directory) {
  419. $directory = realpath($directory);
  420. $this->_php_resource_dir[$directory] = true;
  421. }
  422. }
  423. $_filepath = realpath($filepath);
  424. $directory = dirname($_filepath);
  425. $_directory = array();
  426. while (true) {
  427. // remember the directory to add it to _resource_dir in case we're successful
  428. $_directory[] = $directory;
  429. // test if the directory is trusted
  430. if (isset($this->_php_resource_dir[$directory])) {
  431. // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
  432. $this->_php_resource_dir = array_merge($this->_php_resource_dir, $_directory);
  433. return true;
  434. }
  435. // abort if we've reached root
  436. if (($pos = strrpos($directory, DS)) === false || !isset($directory[2])) {
  437. break;
  438. }
  439. // bubble up one level
  440. $directory = substr($directory, 0, $pos);
  441. }
  442. throw new SmartyException("directory '{$_filepath}' not allowed by security setting");
  443. }
  444. }