XssProtectionTrait::cleanProtectedList()   B
last analyzed

Complexity

Conditions 9
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 8.0555
cc 9
nc 2
nop 2
crap 9
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * php version 7.1.0
11
 *
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Firewall;
24
25
use Shieldon\Security\Xss;
26
use function array_keys;
27
use function array_search;
28
29
/*
30
 * Xss Protection Trait is loaded in Firewall instance only.
31
 */
32
trait XssProtectionTrait
33
{
34
    /**
35
     * Get options from the configuration file.
36
     * This method is same as `$this->getConfig()` but returning value from array directly.
37
     *
38
     * @param string $option  The option of the section in the the configuration.
39
     * @param string $section The section in the configuration.
40
     *
41
     * @return mixed
42
     */
43
    abstract protected function getOption(string $option, string $section = '');
44
45
    /**
46
     * Refresh / refetch the server request if needed.
47
     *
48
     * @return void
49
     */
50
    abstract protected function refreshRequest(): void;
51
52
    /**
53
     * Set up the XSS protection.
54
     *
55
     * @return void
56
     */
57 83
    protected function setupXssProtection(): void
58
    {
59 83
        $enable = $this->getOption('xss_protection');
60 83
        $protectedList = $this->getOption('xss_protected_list');
61 83
        $key = array_search(true, $enable);
62
63 83
        if (empty($key) && empty($protectedList)) {
64 1
            return;
65
        }
66
67 83
        $xss = new Xss();
68
69 83
        $this->cleanPost($enable, $xss);
70 83
        $this->cleanGet($enable, $xss);
71 83
        $this->cleanCookie($enable, $xss);
72 83
        $this->cleanProtectedList($protectedList, $xss);
73
74 83
        $this->refreshRequest();
75
    }
76
77
    /**
78
     * Clean the $_POST superglobal.
79
     *
80
     * @param array $enable The option to enable filtering $_POST.
81
     * @param Xss   $xss    The Xss instance.
82
     *
83
     * @return void
84
     */
85 83
    private function cleanPost(array $enable, Xss $xss): void
86
    {
87 83
        if ($enable['post']) {
88
            $this->kernel->setClosure(
89 54
                'xss_post',
90 54
                function () use ($xss) {
91 54
                    if (!empty($_POST)) {
92 10
                        foreach (array_keys($_POST) as $k) {
93 2
                            $_POST[$k] = $xss->clean($_POST[$k]);
94 2
                        }
95
                    }
96
                }
97 54
            );
98 54
        }
99
    }
100
101
    /**
102
     * Clean the $_GET superglobal.
103
     *
104
     * @param array $enable The option to enable filtering $_GET.
105
     * @param Xss   $xss    The Xss instance.
106
     *
107
     * @return void
108
     */
109
    private function cleanGet(array $enable, Xss $xss): void
110 83
    {
111
        if ($enable['get']) {
112 83
            $this->kernel->setClosure(
113
                'xss_get',
114 54
                function () use ($xss) {
115 54
                    if (!empty($_GET)) {
116 54
                        foreach (array_keys($_GET) as $k) {
117 10
                            $_GET[$k] = $xss->clean($_GET[$k]);
118 1
                        }
119 1
                    }
120
                }
121
            );
122 54
        }
123 54
    }
124
125
    /**
126
     * Clean the $_COOKIE superglobal.
127
     *
128
     * @param array $enable The option to enable filtering $_COOKIE.
129
     * @param Xss   $xss    The Xss instance.
130
     *
131
     * @return void
132
     */
133
    private function cleanCookie(array $enable, Xss $xss): void
134
    {
135 83
        if ($enable['cookie']) {
136
            $this->kernel->setClosure(
137 83
                'xss_cookie',
138
                function () use ($xss) {
139 82
                    if (!empty($_COOKIE)) {
140 82
                        foreach (array_keys($_COOKIE) as $k) {
141 82
                            $_COOKIE[$k] = $xss->clean($_COOKIE[$k]);
142 10
                        }
143 1
                    }
144 1
                }
145
            );
146
        }
147 82
    }
148 82
149
    /**
150
     * Clean the specific protected varibles.
151
     *
152
     * @param array $protectedList The specific variables to be filtered.
153
     * @param Xss   $xss           The Xss instance.
154
     *
155
     * @return void
156
     */
157
    private function cleanProtectedList(array $protectedList, Xss $xss): void
158
    {
159
        if (!empty($protectedList)) {
160 83
            $this->kernel->setClosure(
161
                'xss_protection',
162 83
                function () use ($xss, $protectedList) {
163
                    foreach ($protectedList as $v) {
164 83
                        $k = $v['variable'] ?? 'undefined';
165 83
        
166 83
                        switch ($v['type']) {
167 10
                            case 'get':
168 10
                                if (!empty($_GET[$k])) {
169
                                    $_GET[$k] = $xss->clean($_GET[$k]);
170 10
                                }
171 10
                                break;
172 10
        
173 1
                            case 'post':
174
                                if (!empty($_POST[$k])) {
175 10
                                    $_POST[$k] = $xss->clean($_POST[$k]);
176
                                }
177 10
                                break;
178 10
        
179 1
                            case 'cookie':
180
                                if (!empty($_COOKIE[$k])) {
181 10
                                    $_COOKIE[$k] = $xss->clean($_COOKIE[$k]);
182
                                }
183 10
                                break;
184 10
                        }
185 1
                    }
186
                }
187 10
            );
188
        }
189
    }
190
}
191