|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tleckie\InjectorDetect; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Tleckie\InjectorDetect\Rules\DefaultRule; |
|
8
|
|
|
use function base64_decode; |
|
9
|
|
|
use function is_array; |
|
10
|
|
|
use function parse_str; |
|
11
|
|
|
use function urldecode; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Detector |
|
15
|
|
|
* |
|
16
|
|
|
* @package Tleckie\InjectorDetect |
|
17
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Detector implements DetectorInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var RulesInterface[] */ |
|
22
|
|
|
private array $rules = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Detector constructor. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->addRule(new DefaultRule()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param RulesInterface $rule |
|
34
|
|
|
* @return DetectorInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
public function addRule(RulesInterface $rule): DetectorInterface |
|
37
|
|
|
{ |
|
38
|
|
|
$this->rules[] = $rule; |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ServerRequestInterface $request |
|
45
|
|
|
* @throws InvalidArgumentException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function check(ServerRequestInterface $request): void |
|
48
|
|
|
{ |
|
49
|
|
|
$requestParams = $this->parseQueryString(urldecode($request->getUri()->getQuery())); |
|
50
|
|
|
|
|
51
|
|
|
$bodyParams = $request->getParsedBody() ?? []; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->rules as $rule) { |
|
54
|
|
|
foreach ($requestParams as $item) { |
|
55
|
|
|
$this->arrayRecursive($rule, $item); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
foreach ($bodyParams as $item) { |
|
59
|
|
|
$this->arrayRecursive($rule, $item); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $queryString |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
private function parseQueryString(string $queryString): array |
|
69
|
|
|
{ |
|
70
|
|
|
$data = []; |
|
71
|
|
|
|
|
72
|
|
|
parse_str($queryString, $data); |
|
73
|
|
|
|
|
74
|
|
|
return $data; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param RulesInterface $rule |
|
79
|
|
|
* @param mixed $valueToCheck |
|
80
|
|
|
* @return DetectorInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
private function arrayRecursive(RulesInterface $rule, mixed $valueToCheck): DetectorInterface |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_array($valueToCheck)) { |
|
85
|
|
|
foreach ($valueToCheck as $value) { |
|
86
|
|
|
return $this->arrayRecursive($rule, $value); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$rule->check(base64_decode($valueToCheck))->check($valueToCheck); |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|