Repository navigation
Expand file tree
/
Copy pathSanitizeHelper.php
More file actions
173 lines (160 loc) · 4.84 KB
/
Copy pathSanitizeHelper.php
File metadata and controls
173 lines (160 loc) · 4.84 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
<?php
namespace ProcessMaker;
use Illuminate\Support\Facades\Validator;
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);
$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;
}
/**
* 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);
if (isset($data['_DO_NOT_SANITIZE'])) {
$except = array_unique(array_merge(json_decode($data['_DO_NOT_SANITIZE']), $except));
}
$data['_DO_NOT_SANITIZE'] = json_encode($except);
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;
}
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);
}
}