forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSanitizeHelper.php
More file actions
135 lines (125 loc) · 3.77 KB
/
SanitizeHelper.php
File metadata and controls
135 lines (125 loc) · 3.77 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
<?php
namespace ProcessMaker;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\Screen;
use function GuzzleHttp\json_decode;
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 boolean $strip_tags
* @return mixed
*/
public static function sanitize($value, $strip_tags = true)
{
if (is_string($value) && $strip_tags) {
// Remove most injectable code
$value = strip_tags($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;
}
/**
* 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 string $tag
*
* @return string
*/
public static function sanitizeData($data, $screen)
{
$except = self::getExceptions($screen);
return self::sanitizeWithExceptions($data, $except);
}
private static function sanitizeWithExceptions(Array $data, Array $except, $level = 0)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = self::sanitizeWithExceptions($value, $except, $level + 1);
} else {
// Only allow skipping on top-level data for now
$strip_tags = $level !== 0 || !in_array($key, $except);
$data[$key] = self::sanitize($value, $strip_tags);
}
}
return $data;
}
private static function getExceptions($screen)
{
$except = [];
if (!$screen) {
return $except;
}
$config = $screen->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)
{
$elements = [];
foreach ($items as $item) {
if (isset($item['items']) && is_array($item['items'])) {
// Inside a table
foreach ($item['items'] as $cell) {
if (is_array($cell)) {
$elements = array_merge($elements, self::getRichTextElements($cell));
}
}
} else {
if (
isset($item['component']) &&
$item['component'] === 'FormTextArea' &&
isset($item['config']['richtext']) &&
$item['config']['richtext'] === true
) {
$elements[] = $item['config']['name'];
}
}
}
return $elements;
}
}