PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Twig/Sandbox/SecurityPolicy.php

https://gitlab.com/dcnf/dcbase.org
PHP | 126 lines | 93 code | 20 blank | 13 comment | 9 complexity | 990f35e5f36a4bbc2d350ad4b81f5f69 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 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. namespace Twig\Sandbox;
  11. use Twig\Markup;
  12. use Twig\Template;
  13. /**
  14. * Represents a security policy which need to be enforced when sandbox mode is enabled.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. final class SecurityPolicy implements SecurityPolicyInterface
  19. {
  20. private $allowedTags;
  21. private $allowedFilters;
  22. private $allowedMethods;
  23. private $allowedProperties;
  24. private $allowedFunctions;
  25. public function __construct(array $allowedTags = [], array $allowedFilters = [], array $allowedMethods = [], array $allowedProperties = [], array $allowedFunctions = [])
  26. {
  27. $this->allowedTags = $allowedTags;
  28. $this->allowedFilters = $allowedFilters;
  29. $this->setAllowedMethods($allowedMethods);
  30. $this->allowedProperties = $allowedProperties;
  31. $this->allowedFunctions = $allowedFunctions;
  32. }
  33. public function setAllowedTags(array $tags): void
  34. {
  35. $this->allowedTags = $tags;
  36. }
  37. public function setAllowedFilters(array $filters): void
  38. {
  39. $this->allowedFilters = $filters;
  40. }
  41. public function setAllowedMethods(array $methods): void
  42. {
  43. $this->allowedMethods = [];
  44. foreach ($methods as $class => $m) {
  45. $this->allowedMethods[$class] = array_map(function ($value) { return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); }, \is_array($m) ? $m : [$m]);
  46. }
  47. }
  48. public function setAllowedProperties(array $properties): void
  49. {
  50. $this->allowedProperties = $properties;
  51. }
  52. public function setAllowedFunctions(array $functions): void
  53. {
  54. $this->allowedFunctions = $functions;
  55. }
  56. public function checkSecurity($tags, $filters, $functions): void
  57. {
  58. foreach ($tags as $tag) {
  59. if (!\in_array($tag, $this->allowedTags)) {
  60. throw new SecurityNotAllowedTagError(sprintf('Tag "%s" is not allowed.', $tag), $tag);
  61. }
  62. }
  63. foreach ($filters as $filter) {
  64. if (!\in_array($filter, $this->allowedFilters)) {
  65. throw new SecurityNotAllowedFilterError(sprintf('Filter "%s" is not allowed.', $filter), $filter);
  66. }
  67. }
  68. foreach ($functions as $function) {
  69. if (!\in_array($function, $this->allowedFunctions)) {
  70. throw new SecurityNotAllowedFunctionError(sprintf('Function "%s" is not allowed.', $function), $function);
  71. }
  72. }
  73. }
  74. public function checkMethodAllowed($obj, $method): void
  75. {
  76. if ($obj instanceof Template || $obj instanceof Markup) {
  77. return;
  78. }
  79. $allowed = false;
  80. $method = strtr($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
  81. foreach ($this->allowedMethods as $class => $methods) {
  82. if ($obj instanceof $class) {
  83. $allowed = \in_array($method, $methods);
  84. break;
  85. }
  86. }
  87. if (!$allowed) {
  88. $class = \get_class($obj);
  89. throw new SecurityNotAllowedMethodError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, $class), $class, $method);
  90. }
  91. }
  92. public function checkPropertyAllowed($obj, $property): void
  93. {
  94. $allowed = false;
  95. foreach ($this->allowedProperties as $class => $properties) {
  96. if ($obj instanceof $class) {
  97. $allowed = \in_array($property, \is_array($properties) ? $properties : [$properties]);
  98. break;
  99. }
  100. }
  101. if (!$allowed) {
  102. $class = \get_class($obj);
  103. throw new SecurityNotAllowedPropertyError(sprintf('Calling "%s" property on a "%s" object is not allowed.', $property, $class), $class, $property);
  104. }
  105. }
  106. }