SecurityPolicy.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Represents a security policy which need to be enforced when sandbox mode is enabled.
  12. *
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_Sandbox_SecurityPolicy implements Twig_Sandbox_SecurityPolicyInterface
  16. {
  17. protected $allowedTags;
  18. protected $allowedFilters;
  19. protected $allowedMethods;
  20. protected $allowedProperties;
  21. protected $allowedFunctions;
  22. public function __construct(array $allowedTags = array(), array $allowedFilters = array(), array $allowedMethods = array(), array $allowedProperties = array(), array $allowedFunctions = array())
  23. {
  24. $this->allowedTags = $allowedTags;
  25. $this->allowedFilters = $allowedFilters;
  26. $this->setAllowedMethods($allowedMethods);
  27. $this->allowedProperties = $allowedProperties;
  28. $this->allowedFunctions = $allowedFunctions;
  29. }
  30. public function setAllowedTags(array $tags)
  31. {
  32. $this->allowedTags = $tags;
  33. }
  34. public function setAllowedFilters(array $filters)
  35. {
  36. $this->allowedFilters = $filters;
  37. }
  38. public function setAllowedMethods(array $methods)
  39. {
  40. $this->allowedMethods = array();
  41. foreach ($methods as $class => $m) {
  42. $this->allowedMethods[$class] = array_map('strtolower', is_array($m) ? $m : array($m));
  43. }
  44. }
  45. public function setAllowedProperties(array $properties)
  46. {
  47. $this->allowedProperties = $properties;
  48. }
  49. public function setAllowedFunctions(array $functions)
  50. {
  51. $this->allowedFunctions = $functions;
  52. }
  53. public function checkSecurity($tags, $filters, $functions)
  54. {
  55. foreach ($tags as $tag) {
  56. if (!in_array($tag, $this->allowedTags)) {
  57. throw new Twig_Sandbox_SecurityNotAllowedTagError(sprintf('Tag "%s" is not allowed.', $tag), $tag);
  58. }
  59. }
  60. foreach ($filters as $filter) {
  61. if (!in_array($filter, $this->allowedFilters)) {
  62. throw new Twig_Sandbox_SecurityNotAllowedFilterError(sprintf('Filter "%s" is not allowed.', $filter), $filter);
  63. }
  64. }
  65. foreach ($functions as $function) {
  66. if (!in_array($function, $this->allowedFunctions)) {
  67. throw new Twig_Sandbox_SecurityNotAllowedFunctionError(sprintf('Function "%s" is not allowed.', $function), $function);
  68. }
  69. }
  70. }
  71. public function checkMethodAllowed($obj, $method)
  72. {
  73. if ($obj instanceof Twig_TemplateInterface || $obj instanceof Twig_Markup) {
  74. return true;
  75. }
  76. $allowed = false;
  77. $method = strtolower($method);
  78. foreach ($this->allowedMethods as $class => $methods) {
  79. if ($obj instanceof $class) {
  80. $allowed = in_array($method, $methods);
  81. break;
  82. }
  83. }
  84. if (!$allowed) {
  85. throw new Twig_Sandbox_SecurityError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, get_class($obj)));
  86. }
  87. }
  88. public function checkPropertyAllowed($obj, $property)
  89. {
  90. $allowed = false;
  91. foreach ($this->allowedProperties as $class => $properties) {
  92. if ($obj instanceof $class) {
  93. $allowed = in_array($property, is_array($properties) ? $properties : array($properties));
  94. break;
  95. }
  96. }
  97. if (!$allowed) {
  98. throw new Twig_Sandbox_SecurityError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, get_class($obj)));
  99. }
  100. }
  101. }