Passed
Push — 2.x ( e966a7...a2237c )
by Terry
02:13
created

XssProtectionTrait   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 132
rs 10
wmc 24

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanCookie() 0 7 4
B cleanProtectedList() 0 26 9
A cleanGet() 0 7 4
A cleanPost() 0 7 4
A setXssProtection() 0 16 3
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
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Firewall;
14
15
use Shieldon\Firewall\Security\Xss;
16
17
use function array_keys;
18
19
/*
20
 * Xss Protection Trait is loaded in Firewall instance only.
21
 */
22
trait XssProtectionTrait
23
{
24
    /**
25
     * Fetch value from configuration.
26
     *
27
     * @param string $option
28
     * @param string $section
29
     *
30
     * @return mixed
31
     */
32
    abstract function getOption(string $option, string $section = '');
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
34
    /**
35
     * Set XSS protection.
36
     *
37
     * @return void
38
     */
39
    protected function setXssProtection(): void
40
    {
41
        $enable = $this->getOption('xss_protection');
42
        $protectedList = $this->getOption('xss_protected_list');
43
        $key = array_search(true, $enable);
44
45
        if (empty($key) && empty($protectedList)) {
46
            return;
47
        }
48
49
        $xss = new Xss();
50
51
        $this->cleanPost($enable, $xss);
52
        $this->cleanGet($enable, $xss);
53
        $this->cleanCookie($enable, $xss);
54
        $this->cleanProtectedList($protectedList, $xss);
55
    }
56
57
    /**
58
     * Clean the $_POST superglobal.
59
     *
60
     * @param array $enable
61
     * @param Xss   $xss
62
     *
63
     * @return void
64
     */
65
    private function cleanPost(array $enable, Xss $xss): void
66
    {
67
        if ($enable['post']) {
68
            $this->kernel->setClosure('xss_post', function() use ($xss) {
69
                if (!empty($_POST)) {
70
                    foreach (array_keys($_POST) as $k) {
71
                        $_POST[$k] = $xss->clean($_POST[$k]);
72
                    }
73
                }
74
            });
75
        }
76
    }
77
78
    /**
79
     * Clean the $_GET superglobal.
80
     *
81
     * @param array $enable
82
     * @param Xss   $xss
83
     *
84
     * @return void
85
     */
86
    private function cleanGet(array $enable, Xss $xss): void
87
    {
88
        if ($enable['get']) {
89
            $this->kernel->setClosure('xss_get', function() use ($xss) {
90
                if (!empty($_GET)) {
91
                    foreach (array_keys($_GET) as $k) {
92
                        $_GET[$k] = $xss->clean($_GET[$k]);
93
                    }
94
                }
95
            });
96
        }
97
    }
98
99
    /**
100
     * Clean the $_COOKIE superglobal.
101
     *
102
     * @param array $enable
103
     * @param Xss   $xss
104
     *
105
     * @return void
106
     */
107
    private function cleanCookie(array $enable, Xss $xss): void
108
    {
109
        if ($enable['cookie']) {
110
            $this->kernel->setClosure('xss_cookie', function() use ($xss) {
111
                if (!empty($_COOKIE)) {
112
                    foreach (array_keys($_COOKIE) as $k) {
113
                        $_COOKIE[$k] = $xss->clean($_COOKIE[$k]);
114
                    }
115
                }
116
            });
117
        }
118
    }
119
120
    /**
121
     * Clean the specific protected varibles.
122
     *
123
     * @param array $protectedLis
124
     * @param Xss   $xss
125
     *
126
     * @return void
127
     */
128
    private function cleanProtectedList(array $protectedList, Xss $xss): void
129
    {
130
        if (!empty($protectedList)) {
131
            $this->kernel->setClosure('xss_protection', 
132
                function() use ($xss, $protectedList) {
133
                    foreach ($protectedList as $v) {
134
                        $k = $v['variable'] ?? 'undefined';
135
        
136
                        switch ($v['type']) {
137
                            case 'get':
138
                                if (!empty($_GET[$k])) {
139
                                    $_GET[$k] = $xss->clean($_GET[$k]);
140
                                }
141
                                break;
142
        
143
                            case 'post':
144
                                if (!empty($_POST[$k])) {
145
                                    $_POST[$k] = $xss->clean($_POST[$k]);
146
                                }
147
                                break;
148
        
149
                            case 'cookie':
150
                                if (!empty($_COOKIE[$k])) {
151
                                    $_COOKIE[$k] = $xss->clean($_COOKIE[$k]);
152
                                }
153
                                break;
154
                        }
155
                    }
156
                }
157
            );
158
        }
159
    }
160
}
161