-
Notifications
You must be signed in to change notification settings - Fork 1
/
Taint.php
467 lines (434 loc) · 9.96 KB
/
Taint.php
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
namespace core;
use prj\ProjectValidators;
/**
* Hide GET/POST to enforce:
* - input validation
* - encoding checks
*/
trait TaintValidators
{
/* prevent: derp@@derp.com
* prevent: derp@derp.com.
* prevent: derp.@derp.com
* prevent: derp..@derp.com
* allow: mark+tag@gmail.com
*/
private static function email($val)
{
return false !== filter_var($val, FILTER_VALIDATE_EMAIL);
}
private static function cmp($val)
{
return 1 === preg_match("/^[a-z0-9_]{2,}\/[a-z0-9_]{2,}$/i", $val);
}
private static function uuid($val)
{
return 1 === preg_match("/^[a-z0-9\-]{2,}$/i", $val);
}
private static function slug($val)
{
return 1 === preg_match("/^[a-z0-9_\-]{2,}$/i", $val);
}
private static function date($val)
{
return 1 === preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/i", $val);
}
private static function datetime($val)
{
return 1 === preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$/i", $val);
}
private static function datetimesec($val)
{
return 1 === preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/i", $val);
}
private static function uint($val)
{
return 1 === preg_match("/^[0-9]+$/i", $val);
}
private static function int($val)
{
return 1 === preg_match("/^-?[0-9]+$/i", $val);
}
private static function ip($val)
{
return false !== filter_var($val, FILTER_VALIDATE_IP);
}
private static function money($val)
{
return 1 === preg_match("/^[0-9]+\.[0-9]+$/i", $val);
}
private static function url($val)
{
return false !== filter_var($val, FILTER_VALIDATE_URL);
}
private static function iso2($val)
{
return 1 === preg_match("/^[a-zA-Z]{2}$/", $val);
}
private static function fragment($val)
{
return is_array($val);
}
private static function text($val)
{
return !is_array($val) && !is_object($val);
}
private static function bytes($val)
{
return 1 === preg_match("/^[0-9]+\.*[0-9]* (B|KB|GB|TB)$/i", $val);
}
private static function ascii($val)
{
return 1 === preg_match("%^[ -~]+$%", $val);
}
// Loosy name validator
// https://stackoverflow.com/questions/888838/regular-expression-for-validating-names-and-surnames
private static function name($txt)
{
$pattern = "/^[\p{L} \.'\-]+$/u";
return 1 === preg_match($pattern, $txt);
}
}
trait ArrayValidators
{
private static function min(array $val, $count)
{
return count($val) >= $count;
}
private static function max(array $val, $count)
{
return count($val) <= $count;
}
}
/**
* Validate user input.
*/
class Taint
{
use TaintValidators;
use ProjectValidators;
use ArrayValidators;
private static $get = [];
private static $post = [];
public static function init()
{
self::$get = $_GET;
self::$post = $_POST;
unset($_GET);
unset($_POST);
unset($_REQUEST);
}
private static function encodingOk($val)
{
return mb_check_encoding($val);
}
private static function opt($val)
{
return 1;
}
private static function check_subarray(array $val, array $rules, $prefix="")
{
$errors = ["type" => "err"];
$output = ["type" => "ok"];
// Check against rules
foreach ($rules as $rule) {
if ($rule === "subarray") {
continue;
}
$idx = mb_strpos($rule, "=");
if ($idx !== false) {
// Key=value
$k = mb_substr($rule, 0, $idx);
$v = mb_substr($rule, $idx+1);
if ($k !== "subtype") {
if (! self::$k($val, $v)) {
$errors[] = "$prefix.subtype.$k.$v";
}
continue;
}
if (! class_exists($v)) {
$errors[] = "$prefix.subtype.nosuchclass.$v";
continue;
}
if (! is_array($val)) {
$errors[] = "$prefix.subtype.notsubarray.$v";
continue;
}
$res = self::check(new $v, $val);
if (is_array($res)) {
$errors = array_merge($errors, $res);
} else {
$output["val"] = $res;
}
} else {
var_dump($data);
user_error("Only supporting array with subtype validation");
}
}
if (count($errors) > 1) {
return $errors;
}
return $output;
}
private static function check_fragment(array $val, array $rules, $prefix="")
{
$errors = ["type" => "err"];
$output = ["type" => "ok"];
// Check against rules
foreach ($rules as $rule) {
if ($rule === "fragment") {
continue;
}
if ($rule === "opt") {
continue;
}
$idx = mb_strpos($rule, "=");
if ($idx !== false) {
// Key=value
$k = mb_substr($rule, 0, $idx);
$v = mb_substr($rule, $idx+1);
if ($k !== "subtype") {
if (! self::$k($val, $v)) {
$errors[] = "$prefix.subtype.$k.$v";
}
continue;
}
if (! class_exists($v)) {
$errors[] = "$prefix.subtype.nosuchclass.$v";
continue;
}
// Start recursion
foreach ($val as $idx => $line) {
if (! is_array($line)) {
$errors[] = "$prefix.subtype.notfragment.$v";
continue;
}
$res = self::check(new $v, $line, "[$idx]");
if (is_array($res)) {
$errors = array_merge($errors, $res);
} else {
$output[] = $res;
}
}
} else {
var_dump($val);
user_error("Only supporting array with subtype validation");
}
}
if (count($errors) > 1) {
return $errors;
}
return $output;
}
private static function check_array(array $vals, array $rules, $prefix="")
{
$errors = ["type" => "err"];
$output = ["type" => "ok"];
foreach ($vals as $idx => $val) {
$ok = true;
// Check against rules
foreach ($rules as $rule) {
if ($rule === "array") {
continue;
}
$v = null;
if (! self::$rule($val, null)) {
$errors[] = "$prefix.array[$idx].$rule";
$ok = false;
}
}
if ($ok) {
$output["val"][$idx] = $val;
}
}
if (count($errors) > 1) {
return $errors;
}
return $output;
}
private static function check($out, array $data, $prefix="")
{
$fields = array_keys(get_object_vars($out));
$errors = [];
$rules = $out->rules();
// Replace any -minus fields to _
$d2 = [];
foreach ($data as $k => $v) {
if (strpos($k, "-") !== false) {
$k = str_replace("-", "_", $k);
}
$d2[$k] = $v;
}
$data = $d2;
$count = 0;
foreach ($fields as $field) {
if ($field === "ignore_extra") continue;
// Check if field exists
if (! isset($data[ $field ])) {
if (in_array("opt", $rules[$field])) {
// Optional field and no value, skip
continue;
}
$errors[] = "none.$field$prefix";
continue;
}
$count++;
$val = $data[ $field ];
if (is_array($val)) {
if (in_array("subarray", $rules[$field])) {
$val = self::check_subarray($val, $rules[$field], $field);
$type = $val["type"];
unset($val["type"]);
if ($type === "err") {
$errors = array_merge($errors, $val);
continue;
}
$val = $val["val"];
} elseif (in_array("fragment", $rules[$field])) {
$val = self::check_fragment($val, $rules[$field], $field);
$type = $val["type"];
unset($val["type"]);
if ($type === "err") {
$errors = array_merge($errors, $val);
continue;
}
} else {
if (in_array("array", $rules[$field])) {
$val = self::check_array($val, $rules[$field], $field);
$type = $val["type"];
unset($val["type"]);
if ($type === "err") {
$errors = array_merge($errors, $val);
continue;
}
$val = $val["val"];
} else {
// cannot handle input
$errors[] = "notarray.$field";
continue;
}
}
} else {
if ($val === "" && in_array("opt", $rules[$field])) {
// Optional field and no value, skip
continue;
}
if (in_array("fragment", $rules[$field]) || in_array("subarray", $rules[$field]) || in_array("array", $rules[$field])) {
$errors[] = "notarray.$field";
continue;
}
// Check if encoding is OK
if (! self::encodingOk($val)) {
$errors[] = "encoding.$field$prefix";
continue;
}
// Check if we need to 'truncate' the value?
if (in_array("trim", $rules[$field])) {
$val = str_replace(" ", "", trim($val));
}
// Check against rules
foreach ($rules[ $field ] as $rule) {
if ($rule === "trim") {
// Ignore
continue;
}
if (! self::$rule($val)) {
$errors[] = "check.$field$prefix.$rule";
}
}
}
if (count($errors) > 0) {
continue;
}
// Value is safe, pass on!
$out->$field = $val;
}
$ignore = $out->ignore_extra ?? false;
if ($count !== count($data) && !$ignore) {
// More fields than $out, find out which
foreach (array_keys($data) as $field) {
if (! in_array($field, $fields, true)) {
$errors[] = "extra.$field$prefix";
}
}
}
if (count($errors) > 0) {
return $errors;
}
return $out;
}
public static function post_count()
{
return count(self::$post);
}
public static function get_count()
{
return count(self::$get);
}
public static function post($out)
{
return self::check($out, self::$post);
}
public static function get($out)
{
return self::check($out, self::$get);
}
public static function json($out)
{
$input = json_decode(file_get_contents('php://input'), true);
if (! is_array($input)) {
return [];
}
return self::check($out, $input);
}
public static function raw($out, $input)
{
return self::check($out, $input);
}
/** Return field if valid by rules (also unset value) */
public static function getField($name, array $rules)
{
if (! isset(self::$get[$name])) {
return false;
}
$val = self::$get[$name];
$ok = 1;
foreach ($rules as $rule) {
$ok &= self::$rule($val);
}
if ($ok === 0) {
return false;
}
unset(self::$get[$name]);
return $val;
}
public static function postField($name, array $rules)
{
if (! isset(self::$post[$name])) {
return false;
}
$val = self::$post[$name];
$ok = 1;
foreach ($rules as $rule) {
$ok &= self::$rule($val);
}
if ($ok === 0) {
return false;
}
unset(self::$post[$name]);
return $val;
}
/** Return field if valid by rules (also unset value) */
public static function rawField($val, array $rules)
{
$ok = 1;
foreach ($rules as $rule) {
$ok &= self::$rule($val);
}
if ($ok === 0) {
return false;
}
return $val;
}
}