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