Passed
Push — v5 ( 6f63c4...b606a4 )
by Andrew
20:29 queued 15:13
created

BlacklistSecurityPolicy::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace nystudio107\crafttwigsandbox\twig;
4
5
use nystudio107\crafttwigsandbox\helpers\SecurityPolicy;
6
use Twig\Markup;
7
use Twig\Sandbox\SecurityNotAllowedFilterError;
8
use Twig\Sandbox\SecurityNotAllowedFunctionError;
9
use Twig\Sandbox\SecurityNotAllowedMethodError;
10
use Twig\Sandbox\SecurityNotAllowedPropertyError;
11
use Twig\Sandbox\SecurityNotAllowedTagError;
12
use Twig\Template;
13
14
class BlacklistSecurityPolicy extends BaseSecurityPolicy
15
{
16
    // Public Methods
17
    // =========================================================================
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function __construct($config = [])
23
    {
24
        if (empty($config)) {
25
            $config = SecurityPolicy::getConfigFromFile('blacklist-sandbox', '@vendor/nystudio107/craft-twig-sandbox/src/config');
26
            unset($config['class']);
27
        }
28
        parent::__construct($config);
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function checkSecurity($tags, $filters, $functions): void
35
    {
36
        foreach ($tags as $tag) {
37
            if (in_array($tag, $this->getTwigTags(), true)) {
38
                throw new SecurityNotAllowedTagError(sprintf('Tag "%s" is not allowed.', $tag), $tag);
39
            }
40
        }
41
42
        foreach ($filters as $filter) {
43
            if (in_array($filter, $this->getTwigFilters(), true)) {
44
                throw new SecurityNotAllowedFilterError(sprintf('Filter "%s" is not allowed.', $filter), $filter);
45
            }
46
        }
47
48
        foreach ($functions as $function) {
49
            if (in_array($function, $this->getTwigFunctions(), true)) {
50
                throw new SecurityNotAllowedFunctionError(sprintf('Function "%s" is not allowed.', $function), $function);
51
            }
52
        }
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    public function checkMethodAllowed($obj, $method): void
59
    {
60
        if ($obj instanceof Template || $obj instanceof Markup) {
61
            return;
62
        }
63
64
        $method = strtr($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
65
        $allowed = true;
66
        foreach ($this->getTwigMethods() as $class => $methods) {
67
            if ($obj instanceof $class) {
68
                if ($methods[0] === '*' || in_array($method, $methods, true)) {
69
                    $allowed = false;
70
                    break;
71
                }
72
            }
73
        }
74
75
        if (!$allowed) {
76
            $class = \get_class($obj);
77
            throw new SecurityNotAllowedMethodError(sprintf('Calling "%s" method on a "%s" object is not allowed.', $method, $class), $class, $method);
78
        }
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function checkPropertyAllowed($obj, $property): void
85
    {
86
        $allowed = true;
87
        $property = strtr($property, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz');
88
        foreach ($this->getTwigProperties() as $class => $properties) {
89
            if ($obj instanceof $class) {
90
                if ($properties[0] === '*' || in_array($property, $properties, true)) {
91
                    $allowed = false;
92
                    break;
93
                }
94
            }
95
        }
96
97
        if (!$allowed) {
98
            $class = \get_class($obj);
99
            throw new SecurityNotAllowedPropertyError(sprintf('Accessing "%s" property on a "%s" object is not allowed.', $property, $class), $class, $property);
100
        }
101
    }
102
}
103