Passed
Push — 2.x ( f5b171...c3afe0 )
by Terry
02:06
created

XssProtectionTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 18

1 Method

Rating   Name   Duplication   Size   Complexity  
D setXssProtection() 0 69 18
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
     * Set XSS protection.
26
     *
27
     * @return void
28
     */
29
    protected function setXssProtection(): void
30
    {
31
        $xssProtectionOptions = $this->getOption('xss_protection');
0 ignored issues
show
Bug introduced by
It seems like getOption() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        /** @scrutinizer ignore-call */ 
32
        $xssProtectionOptions = $this->getOption('xss_protection');
Loading history...
32
33
        $xssFilter = new Xss();
34
35
        if ($xssProtectionOptions['post']) {
36
            $this->kernel->setClosure('xss_post', function() use ($xssFilter) {
37
                if (!empty($_POST)) {
38
                    foreach (array_keys($_POST) as $k) {
39
                        $_POST[$k] = $xssFilter->clean($_POST[$k]);
40
                    }
41
                }
42
            });
43
        }
44
45
        if ($xssProtectionOptions['get']) {
46
            $this->kernel->setClosure('xss_get', function() use ($xssFilter) {
47
                if (!empty($_GET)) {
48
                    foreach (array_keys($_GET) as $k) {
49
                        $_GET[$k] = $xssFilter->clean($_GET[$k]);
50
                    }
51
                }
52
            });
53
        }
54
55
        if ($xssProtectionOptions['cookie']) {
56
            $this->kernel->setClosure('xss_cookie', function() use ($xssFilter) {
57
                if (!empty($_COOKIE)) {
58
                    foreach (array_keys($_COOKIE) as $k) {
59
                        $_COOKIE[$k] = $xssFilter->clean($_COOKIE[$k]);
60
                    }
61
                }
62
            });
63
        }
64
65
        $xssProtectedList = $this->getOption('xss_protected_list');
66
67
        if (!empty($xssProtectedList)) {
68
        
69
            $this->kernel->setClosure('xss_protection', function() use ($xssFilter, $xssProtectedList) {
70
71
                foreach ($xssProtectedList as $v) {
72
                    $k = $v['variable'] ?? 'undefined';
73
    
74
                    switch ($v['type']) {
75
76
                        case 'get':
77
78
                            if (!empty($_GET[$k])) {
79
                                $_GET[$k] = $xssFilter->clean($_GET[$k]);
80
                            }
81
                            break;
82
    
83
                        case 'post':
84
    
85
                            if (!empty($_POST[$k])) {
86
                                $_POST[$k] = $xssFilter->clean($_POST[$k]);
87
                            }
88
                            break;
89
    
90
                        case 'cookie':
91
92
                            if (!empty($_COOKIE[$k])) {
93
                                $_COOKIE[$k] = $xssFilter->clean($_COOKIE[$k]);
94
                            }
95
                            break;
96
    
97
                        default:
98
                    }
99
                }
100
            });
101
        }
102
    }
103
}
104