forked from PHORAX/formhandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveXSS.php
More file actions
190 lines (169 loc) · 6.99 KB
/
RemoveXSS.php
File metadata and controls
190 lines (169 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Typoheads\Formhandler\Interceptor;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/* *
* This script is part of the TYPO3 project - inspiring people to share! *
* *
* TYPO3 is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License version 2 as published by *
* the Free Software Foundation. *
* *
* This script is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
* Public License for more details. *
* */
/**
* An interceptor doing XSS checking on GET/POST parameters
*/
class RemoveXSS extends AbstractInterceptor
{
/**
* The main method called by the controller
*
* @return array The probably modified GET/POST parameters
*/
public function process()
{
$this->removeChars = [];
//search for a global setting for character removal
if (isset($this->settings['fieldConf.']) && isset($this->settings['fieldConf.']['global.'])) {
$globalSetting = $this->settings['fieldConf.']['global.'];
} else {
$globalSetting = [];
}
if (isset($globalSetting['removeChars'])) {
$sep = ',';
//user set custom rules via cObject
$cObjSettings = $globalSetting['removeChars.'];
if (is_array($cObjSettings)) {
$list = $this->utilityFuncs->getSingle($globalSetting, 'removeChars');
//user set custom separator
if ($globalSetting['separator']) {
$sep = $this->utilityFuncs->getSingle($globalSetting, 'separator');
}
} else {
//user entered a comma seperated list
$list = $globalSetting['removeChars'];
}
$this->removeChars = GeneralUtility::trimExplode($sep, $list);
} elseif ((int)($this->utilityFuncs->getSingle($globalSetting['removeChars.'] ?? null, 'disable')) === 1) {
//user disabled removal globally
$this->removeChars = [];
}
$this->gp = $this->sanitizeValues($this->gp);
return $this->gp;
}
/**
* This method does XSS checks and escapes malicious data
*
* @param array $values The GET/POST parameters
* @return array The sanitized GET/POST parameters
*/
public function sanitizeValues($values)
{
if (!is_array($values)) {
return [];
}
foreach ($values as $key => $value) {
if (!in_array($key, $this->doNotSanitizeFields) && is_array($value)) {
$sanitizedArray[$key] = $this->sanitizeValues($value);
} elseif (!in_array($key, $this->doNotSanitizeFields) && strlen(trim($value)) > 0) {
$removeChars = $this->removeChars;
//search for a specific setting for this field
$fieldConfig = [];
if (isset($this->settings['fieldConf.']) && isset($this->settings['fieldConf.'][$key . '.'])) {
$fieldSetting = $this->settings['fieldConf.'][$key . '.'];
}
if (isset($fieldSetting['removeChars'])) {
$sep = ',';
//user set custom rules via cObject
$cObjSettings = $fieldSetting['removeChars.'] ?? null;
if (is_array($cObjSettings)) {
$list = $this->utilityFuncs->getSingle($fieldSetting, 'removeChars');
//user set custom separator
if (isset($fieldSetting['separator'])) {
$sep = $this->utilityFuncs->getSingle($fieldSetting, 'separator');
}
} else {
//user entered a comma seperated list
$list = $fieldSetting['removeChars'] ?? null;
}
$removeChars = GeneralUtility::trimExplode($sep, $list);
} elseif ((int)($this->utilityFuncs->getSingle($fieldSetting['removeChars.'] ?? null, 'disable')) === 1) {
//user disabled removal for this field
$removeChars = [];
}
$value = str_replace("\t", '', $value);
$value = str_replace($removeChars, ' ', $value);
$isUTF8 = $this->isUTF8($value);
if (!$isUTF8) {
$value = utf8_encode($value);
}
$value = str_replace('&', '&', htmlspecialchars($value, ENT_NOQUOTES | ENT_SUBSTITUTE | ENT_HTML401));
if (!$isUTF8) {
$value = utf8_decode($value);
}
$sanitizedArray[$key] = $value;
} else {
$sanitizedArray[$key] = $value;
}
}
return $sanitizedArray;
}
/**
* This method detects if a given input string if valid UTF-8.
*
* @author hmdker <hmdker(at)gmail(dot)com>
* @param string
* @return bool is UTF-8
*/
protected function isUTF8($str)
{
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$c = ord($str[$i]);
if ($c > 128) {
if (($c >= 254)) {
return false;
}
if ($c >= 252) {
$bits = 6;
} elseif ($c >= 248) {
$bits = 5;
} elseif ($c >= 240) {
$bits = 4;
} elseif ($c >= 224) {
$bits = 3;
} elseif ($c >= 192) {
$bits = 2;
} else {
return false;
}
if (($i + $bits) > $len) {
return false;
}
while ($bits > 1) {
$i++;
$b = ord($str[$i]);
if ($b < 128 || $b > 191) {
return false;
}
$bits--;
}
}
}
return true;
}
/* (non-PHPdoc)
* @see Classes/Component/\Typoheads\Formhandler\Component\AbstractComponent#init($gp, $settings)
*/
public function init($gp, $settings)
{
parent::init($gp, $settings);
$this->doNotSanitizeFields = [];
if (isset($this->settings['doNotSanitizeFields'])) {
$this->doNotSanitizeFields = GeneralUtility::trimExplode(',', $this->utilityFuncs->getSingle($this->settings, 'doNotSanitizeFields'));
}
}
}