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