-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSanitize.hh
236 lines (202 loc) · 7.62 KB
/
Sanitize.hh
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
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Utility;
/**
* Makes dirty values clean! Sanitize will process an input and return a safe output depending on the scope of the cleaner.
*
* @package Titon\Utility
*/
class Sanitize {
/**
* Sanitize an email by removing all characters except letters, digits and !#$%&'*+-/=?^_`{|}~@.[].
*
* @param string $value
* @param mixed $options
* @return string
*/
public static function email(string $value, mixed $options = []): string {
return (string) filter_var($value, FILTER_SANITIZE_EMAIL, $options);
}
/**
* Escape a string using the apps encoding.
*
* @param string $value
* @param \Titon\Utility\OptionMap $options {
* @var string $encoding Character encoding set; defaults to UTF-8
* @var int $flags Encoding flags; defaults to ENT_QUOTES
* @var bool $double Will double escape existing entities
* }
* @return string
*/
public static function escape(string $value, OptionMap $options = Map {}): string {
$options = (Map {
'encoding' => 'UTF-8',
'flags' => ENT_QUOTES,
'double' => false
})->setAll($options);
return htmlentities($value, $options['flags'], $options['encoding'], $options['double']);
}
/**
* Sanitize a float by removing all characters except digits, +- and .,eE.
*
* @param string $value
* @return float
*/
public static function float(string $value): float {
return (float) filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_SCIENTIFIC);
}
/**
* Sanitize a string by removing xor escaping HTML characters and entities.
*
* @param string $value
* @param \Titon\Utility\OptionMap $options {
* @var bool $strip Will remove HTML tags
* @var string $whitelist List of tags to not strip
* }
* @return string
*/
public static function html(string $value, OptionMap $options = Map {}): string {
$options = (Map {
'strip' => true,
'whitelist' => ''
})->setAll($options);
if ($options['strip']) {
$value = strip_tags($value, (string) $options['whitelist']);
}
return static::escape($value, $options);
}
/**
* Sanitize an integer by removing all characters except digits, plus and minus sign.
*
* @param string $value
* @param mixed $options
* @return int
*/
public static function integer(string $value, mixed $options = []): int {
return (int) filter_var($value, FILTER_SANITIZE_NUMBER_INT, $options);
}
/**
* Sanitize a string by removing excess CRLF characters.
*
* @param string $value
* @param \Titon\Utility\OptionMap $options {
* @var bool $cr Will remove carriage returns \r
* @var bool $lf Will remove line feeds \n
* @var bool $crlf Will remove CRLF \r\n
* @var bool $trim Will remove whitespace and newlines around the edges
* @var int $limit The start limit to remove extraneous characters
* }
* @return string
*/
public static function newlines(string $value, OptionMap $options = Map {}): string {
$options = (Map {
'cr' => true,
'lf' => true,
'crlf' => true,
'limit' => 2,
'trim' => true
})->setAll($options);
if ($options['limit']) {
$pattern = '/(?:{pattern}){' . (string) $options['limit'] . ',}/u';
$replace = null;
} else {
$pattern = '/(?:{pattern})+/u';
$replace = '';
}
if ($options['crlf']) {
$value = preg_replace(Str::insert($pattern, Map {'pattern' => '\r\n'}), is_null($replace) ? "\r\n" : $replace, $value);
}
if ($options['cr']) {
$value = preg_replace(Str::insert($pattern, Map {'pattern' => '\r'}), is_null($replace) ? "\r" : $replace, $value);
}
if ($options['lf']) {
$value = preg_replace(Str::insert($pattern, Map {'pattern' => '\n'}), is_null($replace) ? "\n" : $replace, $value);
}
if ($options['trim']) {
$value = trim($value);
}
return $value;
}
/**
* Sanitize a URL by removing all characters except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=.
*
* @param string $value
* @param mixed $options
* @return string
*/
public static function url(string $value, mixed $options = []): string {
return (string) filter_var($value, FILTER_SANITIZE_URL, $options);
}
/**
* Sanitize a string by removing excess whitespace and tab characters.
*
* @param string $value
* @param \Titon\Utility\OptionMap $options {
* @var bool $space Will remove white space
* @var bool $tab Will remove tabs
* @var bool $strip Will remove non-standard white space character
* @var bool $trim Will remove whitespace and newlines around the edges
* @var int $limit The start limit to remove extraneous characters
* }
* @return string
*/
public static function whitespace(string $value, OptionMap $options = Map {}): string {
$options = (Map {
'space' => true,
'tab' => true,
'limit' => 2,
'strip' => true,
'trim' => true
})->setAll($options);
if ($options['limit']) {
$pattern = '/{pattern}{' . (string) $options['limit'] . ',}/u';
$replace = null;
} else {
$pattern = '/{pattern}+/u';
$replace = '';
}
if ($options['tab']) {
$value = preg_replace(Str::insert($pattern, Map {'pattern' => '\t'}), is_null($replace) ? "\t" : $replace, $value);
}
if ($options['space']) {
$value = preg_replace(Str::insert($pattern, Map {'pattern' => ' '}), is_null($replace) ? ' ' : $replace, $value); // \s replaces other whitespace characters
}
if ($options['strip']) {
$value = str_replace(chr(0xCA), ' ', $value);
}
if ($options['trim']) {
$value = trim($value);
}
return $value;
}
/**
* Sanitize a string by removing any XSS attack vectors.
* Will bubble up to html() and escape().
*
* @param string $value
* @param \Titon\Utility\OptionMap $options {
* @var bool $strip Remove HTML tags
* }
* @return string
*/
public static function xss(string $value, OptionMap $options = Map {}): string {
$options = (Map {'strip' => true})->setAll($options);
$value = str_replace("\0", '', $value);
if (!$options['strip']) {
// Remove any attribute starting with on or xmlns
$value = preg_replace('/\s?(?:on[a-z]+|xmlns)\s?=\s?"(?:.*?)"/isu', '', $value);
// Remove namespaced elements
$value = preg_replace('/<\/?\w+:\w+(?:.*?)>/isu', '', $value);
// Remove really unwanted tags
do {
$old = $value;
$value = preg_replace('/<\/?(?:applet|base|bgsound|embed|frame|frameset|iframe|layer|link|meta|object|script|style|title|xml|audio|video)(?:.*?)>/isu', '', $value);
} while ($old !== $value);
}
return static::html($value, $options);
}
}