Issues (41)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Handlers/Sanitise.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace devtoolboxuk\soteria\handlers;
4
5
6
use devtoolboxuk\soteria\classes\Filters;
7
use devtoolboxuk\soteria\classes\Strings;
8
use devtoolboxuk\soteria\classes\Url;
9
use devtoolboxuk\soteria\models\SoteriaModel;
10
11
class Sanitise
12
{
13
14
    private $is_valid = null;
15
    private $_sanitised = null;
16
    private $filters;
17
    private $input;
18
    private $output;
19
    private $strings;
20
    private $urlService;
21
22
    function __construct()
0 ignored issues
show
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...
23
    {
24
        $this->filters = new Filters();
25
        $this->strings = new Strings();
26
        $this->urlService = new Url();
27
    }
28
29
    /**
30
     *
31
     * Removes URLs from strings
32
     *
33
     * @param array|string $data
34
     * @return array|string|string[]|null
35
     */
36
    public function removeUrl($data)
37
    {
38
        $this->_sanitised = null;
39
40
        if (is_array($data)) {
41
            foreach ($data as $key => $value) {
42
                $data[$key] = $this->removeUrl($value);
43
            }
44
            return $data;
45
        }
46
47
        $this->input = $data = trim($data);
48
        $data = $this->urlService->remove($data);
49
        $data = trim($data);
50
51
        if ($this->input != $data) {
52
            $this->_sanitised = true;
53
        }
54
        $this->is_valid = true;
55
56
        $this->output = $data;
57
        return $data;
58
59
    }
60
61
    /**
62
     * @param $data
63
     * @param string $toEncoding
64
     * @param string $fromEncoding
65
     * @return array|false|string|string[]|null
66
     */
67
    public function cleanse($data, $toEncoding = 'utf-8', $fromEncoding = 'auto')
68
    {
69
70
        if (is_array($data)) {
71
            foreach ($data as $key => $value) {
72
                $data[$key] = $this->cleanse($value, $toEncoding, $fromEncoding);
73
            }
74
            return $data;
75
        }
76
77
        $this->input = $data = trim($data);
78
        $data = $this->strings->clean($data);
79
        $data = mb_convert_encoding($data, $toEncoding, $fromEncoding);
80
        $data = htmlspecialchars_decode($data);
81
        $data = $this->strings->clean($data);
82
        if ($this->input != $data) {
83
            $this->_sanitised = true;
84
        }
85
        $this->output = $data;
86
        return $data;
87
    }
88
89
    /**
90
     * @param $string
91
     * @param string $delimiter
92
     * @return string
93
     */
94
    public function cleanseCsv($string, $delimiter = "|")
95
    {
96
        return trim(str_replace([$delimiter, "\n", "\r", "\t"], " ", $string));
97
    }
98
99
    /**
100
     * @param $data
101
     * @param string $type
102
     * @param int $stringLength
103
     * @return mixed|string
104
     */
105 4
    public function disinfect($data, $type = 'special_chars', $stringLength = -1)
106
    {
107
108 4
        $this->_sanitised = null;
109
110 4
        if (is_array($data)) {
111
            foreach ($data as $key => $value) {
112
                $data[$key] = $this->disinfect($value, $type, $stringLength);
113
            }
114
            return $data;
115
        }
116
117 4
        $this->input = $data = trim($data);
118
119 4
        $data = $this->strings->clean($data);
120 4
        $data = $this->strings->stringLength($data, $stringLength);
121
122
        switch ($type) {
123 4
            case "email":
124
                $filterResult = $this->filters->filterEmail($data);
125
                break;
126
127 4
            case "encoded":
128
                $filterResult = $this->filters->filterEncoded($data);
129
                break;
130
131 4
            case "number_float":
132 4
            case "float":
133
                $filterResult = $this->filters->filterFloat($data);
134
                break;
135
136 4
            case "number_int":
137 4
            case "int":
138
                $filterResult = $this->filters->filterInt($data);
139
                break;
140
141 4
            case "full_special_chars":
142
                $filterResult = $this->filters->filterFullSpecialChar($data);
143
                break;
144
145 4
            case "url":
146
                $filterResult = $this->filters->filterUrl($data);
147
                break;
148
149 4
            case "string":
150 3
                $filterResult = $this->filters->filterString($data);
151 3
                break;
152
153 1
            default:
154 1
            case "special_chars":
0 ignored issues
show
case 'special_chars': ...cial($data); break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
155 1
                $filterResult = $this->filters->filterSpecial($data);
156 1
                break;
157 1
        }
158
159 4
        if ($this->input != $filterResult->getResult()) {
160 3
            $this->_sanitised = true;
161 3
        }
162
163 4
        $this->is_valid = $filterResult->isValid();
164 4
        $this->output = $filterResult->getResult();
165 4
        return $this->output;
166
    }
167
168
169
    /**
170
     * @param $str
171
     * @return mixed|string
172
     */
173
    public function decodeHtmlEntity($str)
174
    {
175
        $ret = $ret = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8');
176
177
        try {
178
179
            $p2 = -1;
180
            for (; ;) {
181
                $p = strpos($ret, '&#', $p2 + 1);
182
                if ($p === false) {
183
                    break;
184
                }
185
                $p2 = strpos($ret, ';', $p);
186
                if ($p2 === false) {
187
                    break;
188
                }
189
                if (substr($ret, $p + 2, 1) == 'x') {
190
                    $char = hexdec(substr($ret, $p + 3, $p2 - $p - 3));
191
                } else {
192
                    $char = intval(substr($ret, $p + 2, $p2 - $p - 2));
193
                }
194
                $newchar = iconv(
195
                    'UCS-4', 'UTF-8',
196
                    chr(($char >> 24) & 0xFF) . chr(($char >> 16) & 0xFF) . chr(($char >> 8) & 0xFF) . chr($char & 0xFF)
197
                );
198
                $ret = substr_replace($ret, $newchar, $p, 1 + $p2 - $p);
199
                $p2 = $p + strlen($newchar);
200
            }
201
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
202
203
        }
204
205
        return $ret;
206
    }
207
208
209
    /**
210
     * @return null
211
     */
212
    public function isSanitised()
213
    {
214
        return $this->_sanitised;
215
    }
216
217
    /**
218
     * Returns true if the data is valid
219
     * @return null
220
     */
221
    public function isValid()
222
    {
223
        return $this->is_valid;
224
    }
225
226 4
    function result()
0 ignored issues
show
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...
227
    {
228 4
        $valid = false;
229 4
        if (!$this->_sanitised && $this->is_valid) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_sanitised of type null|boolean is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
230 4
            $valid = true;
231 4
        }
232 4
        return new SoteriaModel($this->input, $this->output, $valid);
233
    }
234
235
}