-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathSanitizeHelper.php
More file actions
307 lines (272 loc) · 9.52 KB
/
SanitizeHelper.php
File metadata and controls
307 lines (272 loc) · 9.52 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
namespace ProcessMaker;
use Illuminate\Support\Facades\Validator;
use ProcessMaker\Managers\ExportManager;
use ProcessMaker\Models\Screen;
class SanitizeHelper
{
/**
* The tags that should always be sanitized, even
* when the controller specifies doNotSanitize.
*
* @var array
*/
private static $blacklist = [
'<form>',
'<input>',
'<textarea>',
'<button>',
'<select>',
'<option>',
'<optgroup>',
'<fieldset>',
'<label>',
'<output>',
];
/**
* Sanitize the given value.
*
* @param string $key
* @param mixed $value
* @param bool $strip_tags
* @return mixed
*/
public static function sanitize($value, $strip_tags = true, $sanitizeExpressions = true)
{
if (is_string($value) && $strip_tags) {
// Remove most injectable code
$value = self::strip_tags($value);
if ($sanitizeExpressions) {
$value = self::sanitizeVueExp($value);
}
// Return the sanitized string
return $value;
} elseif (is_string($value)) {
// Remove tags in blacklist, even if $strip_tags is false
foreach (self::$blacklist as $tag) {
$regexp = self::convertTagToRegExp($tag);
$value = preg_replace($regexp, '', $value);
}
return $value;
}
// Return the original value.
return $value;
}
/**
* Strip php and html tags
*
* @param string $string
*
* @return string
*/
public static function strip_tags($string)
{
// strip server side tags
$string = preg_replace('/(<\?((?!\?>)[\w\W])+\?>)/', '', $string);
// strip html comments
$string = preg_replace('/(<!--((?!-->)[\w\W])+-->)/', '', $string);
// strip html tags
$string = preg_replace('/<[a-zA-Z0-9]+[^>]*>/', '', $string);
$string = preg_replace('/<\/[a-zA-Z0-9]+[^>]*>/', '', $string);
$string = preg_replace('/<[a-zA-Z0-9]+[^>]*\/>/', '', $string);
return $string;
}
/**
* Convert a <tag> into a regexp.
*
* @param string $tag
*
* @return string
*/
private static function convertTagToRegExp($tag)
{
return '/' . str_replace(['\<', '\>'], ['<[\s\/]*', '[^>]*>'], preg_quote($tag)) . '/i';
}
/**
* Sanitize each element of an array. Do not sanitize rich text elements
*
* @param array $data
* @param Task $task
* @param array $except
*
* @return array
*/
public static function sanitizeData($data, $task = null, $except = [])
{
if ($task) {
// Get current and nested screens IDs ..
$currentScreenExceptions = [];
$currentScreenAndNestedIds = $task->getScreenAndNestedIds();
foreach ($currentScreenAndNestedIds as $id) {
// Find the screen version ..
$screen = Screen::findOrFail($id);
$screen = $screen->versionFor($task->processRequest)->toArray();
// Get exceptions ..
$exceptions = self::getExceptions((object) $screen);
if (count($exceptions)) {
$currentScreenExceptions = array_unique(array_merge($exceptions, $currentScreenExceptions));
}
}
// Get process request exceptions stored in do_not_sanitize column ..
$processRequestExceptions = $task->processRequest->do_not_sanitize;
if (!$processRequestExceptions) {
$processRequestExceptions = [];
}
// Merge (nestedSreensExceptions and currentScreenExceptions) with processRequestExceptions ..
$exceptTask = array_unique(array_merge($processRequestExceptions, $currentScreenExceptions));
$except = array_unique(array_merge($except, $exceptTask));
}
return self::sanitizeWithExceptions($data, $except);
}
/**
* Get exceptions for a screen. Used for web entry start events when no task exists yet.
*
* @param Screen $screen
*
* @return array
*/
public static function getExceptionsForScreen(Screen $screen)
{
$screens = collect([$screen]);
$nestedScreens = collect($screen->nestedScreenIds())->map(fn ($id) => Screen::findOrFail($id));
$screens = $screens->concat($nestedScreens);
return $screens->flatMap(fn ($screen) => self::getExceptions($screen))->toArray();
}
public static function sanitizeWithExceptions(array $data, array $except, $parent = null)
{
foreach ($data as $key => $value) {
if (!is_int($key)) {
$searchKey = ($parent ? $parent . '.' . $key : $key);
} else {
$searchKey = $parent;
}
if (is_array($value)) {
$data[$key] = self::sanitizeWithExceptions($value, $except, $searchKey);
} else {
// Only allow skipping on top-level data for now
$strip_tags = !in_array($searchKey, $except);
$data[$key] = self::sanitize($value, $strip_tags);
}
}
return $data;
}
private static function getExceptions($screen)
{
$except = [];
if (!$screen) {
return $except;
}
$config = $screen->config;
if ($config) {
foreach ($config as $page) {
if (isset($page['items']) && is_array($page['items'])) {
$except = array_merge($except, self::getRichTextElements($page['items']));
}
}
}
return $except;
}
private static function getRichTextElements($items, $parent = null, &$elements = [])
{
foreach ($items as $item) {
if (isset($item['items']) && is_array($item['items'])) {
// Inside loop ..
if ($item['component'] == 'FormLoop') {
self::getRichTextElements(
$item['items'],
($parent ? $parent . '.' . $item['config']['name'] : $item['config']['name']),
$elements
);
} elseif ($item['component'] == 'FormMultiColumn') {
self::getVariableMultiColumn($item, $parent, $elements);
} else {
self::getVariableExceptions($item, $parent, $elements);
}
} else {
self::getVariableExceptions($item, $parent, $elements);
}
}
return $elements;
}
private static function getVariableMultiColumn($item, $parent, &$elements)
{
foreach ($item['items'] as $cell) {
self::getVariableExceptions($cell, $parent, $elements);
if (is_array($cell)) {
self::getRichTextElements($cell, $parent, $elements);
}
}
}
private static function getVariableExceptions($item, $parent, &$elements)
{
if (!isset($item['component'])) {
return;
}
if (self::renderHtmlIsEnabled($item, 'FormTextArea', 'richtext')) {
$elements[] = ($parent ? $parent . '.' . $item['config']['name'] : $item['config']['name']);
} elseif (self::renderHtmlIsEnabled($item, 'FormHtmlViewer', 'renderVarHtml')) {
preg_match_all("/{{([^{}]*)}}/", $item['config']['content'], $matches);
if ($matches && $matches[1]) {
foreach ($matches[1] as $variable) {
$elements[] = ($parent ? $parent . '.' . $variable : $variable);
}
}
}
}
private static function renderHtmlIsEnabled($item, $type, $field)
{
return isset($item['config'])
&& $item['component'] === $type
&& isset($item['config'][$field])
&& $item['config'][$field] === true;
}
public static function sanitizeEmail($email)
{
$validator = Validator::make(['email' => $email], [
'email' => 'required|email',
]);
if ($validator->fails()) {
return '';
} else {
return $email;
}
}
public static function sanitizePhoneNumber($number)
{
$regexp = "/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/";
if (preg_match($regexp, $number)) {
return $number;
} else {
return '';
}
}
public static function sanitizeVueExp($string)
{
// strip {{, }}
$codes = [
'{{' => '',
'}}' => '',
];
return str_replace(array_keys($codes), array_values($codes), $string);
}
public static function getDoNotSanitizeFields($process)
{
$manager = app(ExportManager::class);
$screenIds = $manager->getDependenciesOfType(Screen::class, $process, []);
$doNotSanitizeFields = [];
foreach ($screenIds as $screenId) {
$doNotSanitizeFieldsForScreen = self::findFieldsInScreen($screenId);
$doNotSanitizeFields = array_unique(array_merge($doNotSanitizeFieldsForScreen, $doNotSanitizeFields));
}
return $doNotSanitizeFields;
}
public static function findFieldsInScreen($screenId)
{
$screen = Screen::find($screenId);
$doNotSanitizeFields = [];
if ($screen) {
$doNotSanitizeFields = self::getExceptions((object) $screen);
}
return $doNotSanitizeFields;
}
}