You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_security.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. * List of trusted constants names
  57. *
  58. * @var array
  59. */
  60. public $trusted_constants = array();
  61. /**
  62. * This is an array of trusted static classes.
  63. * If empty access to all static classes is allowed.
  64. * If set to 'none' none is allowed.
  65. *
  66. * @var array
  67. */
  68. public $static_classes = array();
  69. /**
  70. * This is an nested array of trusted classes and static methods.
  71. * If empty access to all static classes and methods is allowed.
  72. * Format:
  73. * array (
  74. * 'class_1' => array('method_1', 'method_2'), // allowed methods listed
  75. * 'class_2' => array(), // all methods of class allowed
  76. * )
  77. * If set to null none is allowed.
  78. *
  79. * @var array
  80. */
  81. public $trusted_static_methods = array();
  82. /**
  83. * This is an array of trusted static properties.
  84. * If empty access to all static classes and properties is allowed.
  85. * Format:
  86. * array (
  87. * 'class_1' => array('prop_1', 'prop_2'), // allowed properties listed
  88. * 'class_2' => array(), // all properties of class allowed
  89. * )
  90. * If set to null none is allowed.
  91. *
  92. * @var array
  93. */
  94. public $trusted_static_properties = array();
  95. /**
  96. * This is an array of trusted PHP functions.
  97. * If empty all functions are allowed.
  98. * To disable all PHP functions set $php_functions = null.
  99. *
  100. * @var array
  101. */
  102. public $php_functions = array('isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array', 'time',);
  103. /**
  104. * This is an array of trusted PHP modifiers.
  105. * If empty all modifiers are allowed.
  106. * To disable all modifier set $php_modifiers = null.
  107. *
  108. * @var array
  109. */
  110. public $php_modifiers = array('escape', 'count', 'nl2br',);
  111. /**
  112. * This is an array of allowed tags.
  113. * If empty no restriction by allowed_tags.
  114. *
  115. * @var array
  116. */
  117. public $allowed_tags = array();
  118. /**
  119. * This is an array of disabled tags.
  120. * If empty no restriction by disabled_tags.
  121. *
  122. * @var array
  123. */
  124. public $disabled_tags = array();
  125. /**
  126. * This is an array of allowed modifier plugins.
  127. * If empty no restriction by allowed_modifiers.
  128. *
  129. * @var array
  130. */
  131. public $allowed_modifiers = array();
  132. /**
  133. * This is an array of disabled modifier plugins.
  134. * If empty no restriction by disabled_modifiers.
  135. *
  136. * @var array
  137. */
  138. public $disabled_modifiers = array();
  139. /**
  140. * This is an array of disabled special $smarty variables.
  141. *
  142. * @var array
  143. */
  144. public $disabled_special_smarty_vars = array();
  145. /**
  146. * This is an array of trusted streams.
  147. * If empty all streams are allowed.
  148. * To disable all streams set $streams = null.
  149. *
  150. * @var array
  151. */
  152. public $streams = array('file');
  153. /**
  154. * + flag if constants can be accessed from template
  155. *
  156. * @var boolean
  157. */
  158. public $allow_constants = true;
  159. /**
  160. * + flag if super globals can be accessed from template
  161. *
  162. * @var boolean
  163. */
  164. public $allow_super_globals = true;
  165. /**
  166. * max template nesting level
  167. *
  168. * @var int
  169. */
  170. public $max_template_nesting = 0;
  171. /**
  172. * current template nesting level
  173. *
  174. * @var int
  175. */
  176. private $_current_template_nesting = 0;
  177. /**
  178. * Cache for $resource_dir lookup
  179. *
  180. * @var array
  181. */
  182. protected $_resource_dir = array();
  183. /**
  184. * Cache for $template_dir lookup
  185. *
  186. * @var array
  187. */
  188. protected $_template_dir = array();
  189. /**
  190. * Cache for $config_dir lookup
  191. *
  192. * @var array
  193. */
  194. protected $_config_dir = array();
  195. /**
  196. * Cache for $secure_dir lookup
  197. *
  198. * @var array
  199. */
  200. protected $_secure_dir = array();
  201. /**
  202. * Cache for $php_resource_dir lookup
  203. *
  204. * @var array
  205. */
  206. protected $_php_resource_dir = null;
  207. /**
  208. * Cache for $trusted_dir lookup
  209. *
  210. * @var array
  211. */
  212. protected $_trusted_dir = null;
  213. /**
  214. * Cache for include path status
  215. *
  216. * @var bool
  217. */
  218. protected $_include_path_status = false;
  219. /**
  220. * Cache for $_include_array lookup
  221. *
  222. * @var array
  223. */
  224. protected $_include_dir = array();
  225. /**
  226. * @param Smarty $smarty
  227. */
  228. public function __construct($smarty)
  229. {
  230. $this->smarty = $smarty;
  231. }
  232. /**
  233. * Check if PHP function is trusted.
  234. *
  235. * @param string $function_name
  236. * @param object $compiler compiler object
  237. *
  238. * @return boolean true if function is trusted
  239. * @throws SmartyCompilerException if php function is not trusted
  240. */
  241. public function isTrustedPhpFunction($function_name, $compiler)
  242. {
  243. if (isset($this->php_functions) &&
  244. (empty($this->php_functions) || in_array($function_name, $this->php_functions))
  245. ) {
  246. return true;
  247. }
  248. $compiler->trigger_template_error("PHP function '{$function_name}' not allowed by security setting");
  249. return false; // should not, but who knows what happens to the compiler in the future?
  250. }
  251. /**
  252. * Check if static class is trusted.
  253. *
  254. * @param string $class_name
  255. * @param object $compiler compiler object
  256. *
  257. * @return boolean true if class is trusted
  258. * @throws SmartyCompilerException if static class is not trusted
  259. */
  260. public function isTrustedStaticClass($class_name, $compiler)
  261. {
  262. if (isset($this->static_classes) &&
  263. (empty($this->static_classes) || in_array($class_name, $this->static_classes))
  264. ) {
  265. return true;
  266. }
  267. $compiler->trigger_template_error("access to static class '{$class_name}' not allowed by security setting");
  268. return false; // should not, but who knows what happens to the compiler in the future?
  269. }
  270. /**
  271. * Check if static class method/property is trusted.
  272. *
  273. * @param string $class_name
  274. * @param string $params
  275. * @param object $compiler compiler object
  276. *
  277. * @return boolean true if class method is trusted
  278. * @throws SmartyCompilerException if static class method is not trusted
  279. */
  280. public function isTrustedStaticClassAccess($class_name, $params, $compiler)
  281. {
  282. if (!isset($params[2])) {
  283. // fall back
  284. return $this->isTrustedStaticClass($class_name, $compiler);
  285. }
  286. if ($params[2] == 'method') {
  287. $allowed = $this->trusted_static_methods;
  288. $name = substr($params[0], 0, strpos($params[0], '('));
  289. } else {
  290. $allowed = $this->trusted_static_properties;
  291. // strip '$'
  292. $name = substr($params[0], 1);
  293. }
  294. if (isset($allowed)) {
  295. if (empty($allowed)) {
  296. // fall back
  297. return $this->isTrustedStaticClass($class_name, $compiler);
  298. }
  299. if (isset($allowed[$class_name]) &&
  300. (empty($allowed[$class_name]) || in_array($name, $allowed[$class_name]))
  301. ) {
  302. return true;
  303. }
  304. }
  305. $compiler->trigger_template_error("access to static class '{$class_name}' {$params[2]} '{$name}' not allowed by security setting");
  306. return false; // should not, but who knows what happens to the compiler in the future?
  307. }
  308. /**
  309. * Check if PHP modifier is trusted.
  310. *
  311. * @param string $modifier_name
  312. * @param object $compiler compiler object
  313. *
  314. * @return boolean true if modifier is trusted
  315. * @throws SmartyCompilerException if modifier is not trusted
  316. */
  317. public function isTrustedPhpModifier($modifier_name, $compiler)
  318. {
  319. if (isset($this->php_modifiers) &&
  320. (empty($this->php_modifiers) || in_array($modifier_name, $this->php_modifiers))
  321. ) {
  322. return true;
  323. }
  324. $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting");
  325. return false; // should not, but who knows what happens to the compiler in the future?
  326. }
  327. /**
  328. * Check if tag is trusted.
  329. *
  330. * @param string $tag_name
  331. * @param object $compiler compiler object
  332. *
  333. * @return boolean true if tag is trusted
  334. * @throws SmartyCompilerException if modifier is not trusted
  335. */
  336. public function isTrustedTag($tag_name, $compiler)
  337. {
  338. // check for internal always required tags
  339. if (in_array($tag_name, array('assign', 'call', 'private_filter', 'private_block_plugin',
  340. 'private_function_plugin', 'private_object_block_function',
  341. 'private_object_function', 'private_registered_function',
  342. 'private_registered_block', 'private_special_variable',
  343. 'private_print_expression', 'private_modifier'))) {
  344. return true;
  345. }
  346. // check security settings
  347. if (empty($this->allowed_tags)) {
  348. if (empty($this->disabled_tags) || !in_array($tag_name, $this->disabled_tags)) {
  349. return true;
  350. } else {
  351. $compiler->trigger_template_error("tag '{$tag_name}' disabled by security setting", null, true);
  352. }
  353. } elseif (in_array($tag_name, $this->allowed_tags) && !in_array($tag_name, $this->disabled_tags)) {
  354. return true;
  355. } else {
  356. $compiler->trigger_template_error("tag '{$tag_name}' not allowed by security setting", null, true);
  357. }
  358. return false; // should not, but who knows what happens to the compiler in the future?
  359. }
  360. /**
  361. * Check if special $smarty variable is trusted.
  362. *
  363. * @param string $var_name
  364. * @param object $compiler compiler object
  365. *
  366. * @return boolean true if tag is trusted
  367. * @throws SmartyCompilerException if modifier is not trusted
  368. */
  369. public function isTrustedSpecialSmartyVar($var_name, $compiler)
  370. {
  371. if (!in_array($var_name, $this->disabled_special_smarty_vars)) {
  372. return true;
  373. } else {
  374. $compiler->trigger_template_error("special variable '\$smarty.{$var_name}' not allowed by security setting", null, true);
  375. }
  376. return false; // should not, but who knows what happens to the compiler in the future?
  377. }
  378. /**
  379. * Check if modifier plugin is trusted.
  380. *
  381. * @param string $modifier_name
  382. * @param object $compiler compiler object
  383. *
  384. * @return boolean true if tag is trusted
  385. * @throws SmartyCompilerException if modifier is not trusted
  386. */
  387. public function isTrustedModifier($modifier_name, $compiler)
  388. {
  389. // check for internal always allowed modifier
  390. if (in_array($modifier_name, array('default'))) {
  391. return true;
  392. }
  393. // check security settings
  394. if (empty($this->allowed_modifiers)) {
  395. if (empty($this->disabled_modifiers) || !in_array($modifier_name, $this->disabled_modifiers)) {
  396. return true;
  397. } else {
  398. $compiler->trigger_template_error("modifier '{$modifier_name}' disabled by security setting", null, true);
  399. }
  400. } elseif (in_array($modifier_name, $this->allowed_modifiers) &&
  401. !in_array($modifier_name, $this->disabled_modifiers)
  402. ) {
  403. return true;
  404. } else {
  405. $compiler->trigger_template_error("modifier '{$modifier_name}' not allowed by security setting", null, true);
  406. }
  407. return false; // should not, but who knows what happens to the compiler in the future?
  408. }
  409. /**
  410. * Check if constants are enabled or trusted
  411. *
  412. * @param string $const constant name
  413. * @param object $compiler compiler object
  414. *
  415. * @return bool
  416. */
  417. public function isTrustedConstant($const, $compiler)
  418. {
  419. if (in_array($const, array('true', 'false', 'null'))) {
  420. return true;
  421. }
  422. if (!empty($this->trusted_constants)) {
  423. if (!in_array($const, $this->trusted_constants)) {
  424. $compiler->trigger_template_error("Security: access to constant '{$const}' not permitted");
  425. return false;
  426. }
  427. return true;
  428. }
  429. if ($this->allow_constants) {
  430. return true;
  431. }
  432. $compiler->trigger_template_error("Security: access to constants not permitted");
  433. return false;
  434. }
  435. /**
  436. * Check if stream is trusted.
  437. *
  438. * @param string $stream_name
  439. *
  440. * @return boolean true if stream is trusted
  441. * @throws SmartyException if stream is not trusted
  442. */
  443. public function isTrustedStream($stream_name)
  444. {
  445. if (isset($this->streams) && (empty($this->streams) || in_array($stream_name, $this->streams))) {
  446. return true;
  447. }
  448. throw new SmartyException("stream '{$stream_name}' not allowed by security setting");
  449. }
  450. /**
  451. * Check if directory of file resource is trusted.
  452. *
  453. * @param string $filepath
  454. * @param null|bool $isConfig
  455. *
  456. * @return bool true if directory is trusted
  457. * @throws \SmartyException if directory is not trusted
  458. */
  459. public function isTrustedResourceDir($filepath, $isConfig = null)
  460. {
  461. if ($this->_include_path_status !== $this->smarty->use_include_path) {
  462. foreach ($this->_include_dir as $directory) {
  463. unset($this->_resource_dir[$directory]);
  464. }
  465. if ($this->smarty->use_include_path) {
  466. $this->_include_dir = array();
  467. $_dirs = $this->smarty->ext->_getIncludePath->getIncludePathDirs($this->smarty);
  468. foreach ($_dirs as $directory) {
  469. $this->_include_dir[] = $directory;
  470. $this->_resource_dir[$directory] = true;
  471. }
  472. }
  473. $this->_include_path_status = $this->smarty->use_include_path;
  474. }
  475. if ($isConfig !== true &&
  476. (!isset($this->smarty->_cache['template_dir_new']) || $this->smarty->_cache['template_dir_new'])
  477. ) {
  478. $_dir = $this->smarty->getTemplateDir();
  479. if ($this->_template_dir !== $_dir) {
  480. foreach ($this->_template_dir as $directory) {
  481. unset($this->_resource_dir[$directory]);
  482. }
  483. foreach ($_dir as $directory) {
  484. $this->_resource_dir[$directory] = true;
  485. }
  486. $this->_template_dir = $_dir;
  487. }
  488. $this->smarty->_cache['template_dir_new'] = false;
  489. }
  490. if ($isConfig !== false &&
  491. (!isset($this->smarty->_cache['config_dir_new']) || $this->smarty->_cache['config_dir_new'])
  492. ) {
  493. $_dir = $this->smarty->getConfigDir();
  494. if ($this->_config_dir !== $_dir) {
  495. foreach ($this->_config_dir as $directory) {
  496. unset($this->_resource_dir[$directory]);
  497. }
  498. foreach ($_dir as $directory) {
  499. $this->_resource_dir[$directory] = true;
  500. }
  501. $this->_config_dir = $_dir;
  502. }
  503. $this->smarty->_cache['config_dir_new'] = false;
  504. }
  505. if ($this->_secure_dir !== (array) $this->secure_dir) {
  506. foreach ($this->_secure_dir as $directory) {
  507. unset($this->_resource_dir[$directory]);
  508. }
  509. foreach ((array) $this->secure_dir as $directory) {
  510. $directory = $this->smarty->_realpath($directory . DS, true);
  511. $this->_resource_dir[$directory] = true;
  512. }
  513. $this->_secure_dir = (array) $this->secure_dir;
  514. }
  515. $this->_resource_dir = $this->_checkDir($filepath, $this->_resource_dir);
  516. return true;
  517. }
  518. /**
  519. * Check if URI (e.g. {fetch} or {html_image}) is trusted
  520. * To simplify things, isTrustedUri() resolves all input to "{$PROTOCOL}://{$HOSTNAME}".
  521. * So "http://username:password@hello.world.example.org:8080/some-path?some=query-string"
  522. * is reduced to "http://hello.world.example.org" prior to applying the patters from {@link $trusted_uri}.
  523. *
  524. * @param string $uri
  525. *
  526. * @return boolean true if URI is trusted
  527. * @throws SmartyException if URI is not trusted
  528. * @uses $trusted_uri for list of patterns to match against $uri
  529. */
  530. public function isTrustedUri($uri)
  531. {
  532. $_uri = parse_url($uri);
  533. if (!empty($_uri['scheme']) && !empty($_uri['host'])) {
  534. $_uri = $_uri['scheme'] . '://' . $_uri['host'];
  535. foreach ($this->trusted_uri as $pattern) {
  536. if (preg_match($pattern, $_uri)) {
  537. return true;
  538. }
  539. }
  540. }
  541. throw new SmartyException("URI '{$uri}' not allowed by security setting");
  542. }
  543. /**
  544. * Check if directory of file resource is trusted.
  545. *
  546. * @param string $filepath
  547. *
  548. * @return boolean true if directory is trusted
  549. * @throws SmartyException if PHP directory is not trusted
  550. */
  551. public function isTrustedPHPDir($filepath)
  552. {
  553. if (empty($this->trusted_dir)) {
  554. throw new SmartyException("directory '{$filepath}' not allowed by security setting (no trusted_dir specified)");
  555. }
  556. // check if index is outdated
  557. if (!$this->_trusted_dir || $this->_trusted_dir !== $this->trusted_dir) {
  558. $this->_php_resource_dir = array();
  559. $this->_trusted_dir = $this->trusted_dir;
  560. foreach ((array) $this->trusted_dir as $directory) {
  561. $directory = $this->smarty->_realpath($directory . DS, true);
  562. $this->_php_resource_dir[$directory] = true;
  563. }
  564. }
  565. $this->_php_resource_dir = $this->_checkDir($this->smarty->_realpath($filepath, true), $this->_php_resource_dir);
  566. return true;
  567. }
  568. /**
  569. * Start template processing
  570. *
  571. * @param $template
  572. *
  573. * @throws SmartyException
  574. */
  575. public function startTemplate($template)
  576. {
  577. if ($this->max_template_nesting > 0 && $this->_current_template_nesting ++ >= $this->max_template_nesting) {
  578. throw new SmartyException("maximum template nesting level of '{$this->max_template_nesting}' exceeded when calling '{$template->template_resource}'");
  579. }
  580. }
  581. /**
  582. * Exit template processing
  583. *
  584. * @internal param $template
  585. */
  586. public function exitTemplate()
  587. {
  588. if ($this->max_template_nesting > 0) {
  589. $this->_current_template_nesting --;
  590. }
  591. }
  592. /**
  593. * Check if file is inside a valid directory
  594. *
  595. * @param string $filepath
  596. * @param array $dirs valid directories
  597. *
  598. * @return array
  599. * @throws \SmartyException
  600. */
  601. private function _checkDir($filepath, $dirs)
  602. {
  603. $directory = dirname($filepath) . DS;
  604. $_directory = array();
  605. while (true) {
  606. // remember the directory to add it to _resource_dir in case we're successful
  607. $_directory[$directory] = true;
  608. // test if the directory is trusted
  609. if (isset($dirs[$directory])) {
  610. // merge sub directories of current $directory into _resource_dir to speed up subsequent lookup
  611. $dirs = array_merge($dirs, $_directory);
  612. return $dirs;
  613. }
  614. // abort if we've reached root
  615. if (!preg_match('#[\\\/][^\\\/]+[\\\/]$#', $directory)) {
  616. break;
  617. }
  618. // bubble up one level
  619. $directory = preg_replace('#[\\\/][^\\\/]+[\\\/]$#', DS, $directory);
  620. }
  621. // give up
  622. throw new SmartyException("directory '{$filepath}' not allowed by security setting");
  623. }
  624. /**
  625. * Loads security class and enables security
  626. *
  627. * @param \Smarty $smarty
  628. * @param string|Smarty_Security $security_class if a string is used, it must be class-name
  629. *
  630. * @return \Smarty current Smarty instance for chaining
  631. * @throws \SmartyException when an invalid class name is provided
  632. */
  633. public static function enableSecurity(Smarty $smarty, $security_class)
  634. {
  635. if ($security_class instanceof Smarty_Security) {
  636. $smarty->security_policy = $security_class;
  637. return;
  638. } elseif (is_object($security_class)) {
  639. throw new SmartyException("Class '" . get_class($security_class) . "' must extend Smarty_Security.");
  640. }
  641. if ($security_class == null) {
  642. $security_class = $smarty->security_class;
  643. }
  644. if (!class_exists($security_class)) {
  645. throw new SmartyException("Security class '$security_class' is not defined");
  646. } elseif ($security_class !== 'Smarty_Security' && !is_subclass_of($security_class, 'Smarty_Security')) {
  647. throw new SmartyException("Class '$security_class' must extend Smarty_Security.");
  648. } else {
  649. $smarty->security_policy = new $security_class($smarty);
  650. }
  651. return;
  652. }
  653. }