|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* antibot |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\Antibot |
|
8
|
|
|
* @subpackage Jkphl\Antibot\Ports\Validators |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Jkphl\Antibot\Ports\Validators; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\Antibot\Domain\Antibot; |
|
40
|
|
|
use Jkphl\Antibot\Infrastructure\Exceptions\HoneypotValidationException; |
|
41
|
|
|
use Jkphl\Antibot\Infrastructure\Model\AbstractValidator; |
|
42
|
|
|
use Jkphl\Antibot\Infrastructure\Model\InputElement; |
|
43
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Honeypot Validator |
|
47
|
|
|
* |
|
48
|
|
|
* @package Jkphl\Antibot |
|
49
|
|
|
* @subpackage Jkphl\Antibot\Ports\Validators |
|
50
|
|
|
*/ |
|
51
|
|
|
class HoneypotValidator extends AbstractValidator |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Honeypot field names & structure |
|
55
|
|
|
* |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $honeypots; |
|
59
|
|
|
/** |
|
60
|
|
|
* Field renderer |
|
61
|
|
|
* |
|
62
|
|
|
* @var \Closure |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $renderer; |
|
65
|
|
|
/** |
|
66
|
|
|
* Validation order position |
|
67
|
|
|
* |
|
68
|
|
|
* @var int |
|
69
|
|
|
*/ |
|
70
|
|
|
const POSITION = 50; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Constructor |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $honeypots Honeypot field names & structure |
|
76
|
|
|
* @param \Closure $renderer Field renderer |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function __construct(array $honeypots, \Closure $renderer = null) |
|
79
|
|
|
{ |
|
80
|
1 |
|
$this->honeypots = $honeypots; |
|
81
|
1 |
|
$this->renderer = $renderer; |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Validate a request |
|
86
|
|
|
* |
|
87
|
|
|
* @param ServerRequestInterface $request Request |
|
88
|
|
|
* @param Antibot $antibot Antibot instance |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool Success |
|
91
|
|
|
* @throws HoneypotValidationException If a honeypot was triggered |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function validate(ServerRequestInterface $request, Antibot $antibot): bool |
|
94
|
|
|
{ |
|
95
|
1 |
|
$data = array_merge($request->getQueryParams(), $request->getParsedBody()); |
|
96
|
|
|
|
|
97
|
1 |
|
return $this->validateHoneypotsRecursive($this->honeypots, $data, $antibot); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Recursively validate honeypots |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $honeypots Honeypot configuration |
|
104
|
|
|
* @param array $data Submitted data |
|
105
|
|
|
* @param Antibot $antibot Antibot instance |
|
106
|
|
|
* @param null $prefix Variable prefix |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool Success |
|
109
|
|
|
* @throws HoneypotValidationException If a honeypot was triggered |
|
110
|
|
|
*/ |
|
111
|
1 |
|
protected function validateHoneypotsRecursive(array $honeypots, array $data, Antibot $antibot, $prefix = null): bool |
|
112
|
|
|
{ |
|
113
|
|
|
// Run through the honeypot configuration |
|
114
|
1 |
|
foreach ($honeypots as $name => $config) { |
|
115
|
1 |
|
if (is_array($config)) { |
|
116
|
1 |
|
$honeypotPrefix = $prefix ? $prefix.'['.htmlspecialchars($name).']' : htmlspecialchars($name); |
|
117
|
1 |
|
if (array_key_exists($name, $data)) { |
|
118
|
1 |
|
$this->validateHoneypotsRecursive($config, $data[$name], $antibot, $honeypotPrefix); |
|
119
|
|
|
} |
|
120
|
1 |
|
continue; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// If the honeypot was submitted empty (or not submitted at all): Succeed |
|
124
|
1 |
|
if (!array_key_exists($name, $data) || !strlen($data[$name])) { |
|
125
|
1 |
|
continue; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
$honeypotName = $prefix ? $prefix.'['.htmlspecialchars($name).']' : htmlspecialchars($name); |
|
129
|
1 |
|
$antibot->getLogger()->debug('[HNPT] Triggered honeypot "'.$honeypotName.'"'); |
|
130
|
1 |
|
throw new HoneypotValidationException( |
|
131
|
1 |
|
sprintf(HoneypotValidationException::TRIGGERED_HONEYPOT_STR, $honeypotName), |
|
132
|
1 |
|
HoneypotValidationException::TRIGGERED_HONEYPOT |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Create protective form HTML |
|
141
|
|
|
* |
|
142
|
|
|
* @param ServerRequestInterface $request Request |
|
143
|
|
|
* @param Antibot $antibot Antibot instance |
|
144
|
|
|
* |
|
145
|
|
|
* @return InputElement[] HMTL input elements |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function armor(ServerRequestInterface $request, Antibot $antibot): array |
|
148
|
|
|
{ |
|
149
|
1 |
|
$armor = []; |
|
150
|
1 |
|
$this->createHoneypotsRecursive($this->honeypots, $armor); |
|
151
|
|
|
|
|
152
|
1 |
|
return $armor; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Recursively create honeypot input elements |
|
157
|
|
|
* |
|
158
|
|
|
* @param array $honeypots Honeypot configuration |
|
159
|
|
|
* @param array $armor Armor input elements |
|
160
|
|
|
* @param null $prefix Variable prefix |
|
161
|
|
|
*/ |
|
162
|
1 |
|
protected function createHoneypotsRecursive(array $honeypots, array &$armor, $prefix = null): void |
|
163
|
|
|
{ |
|
164
|
|
|
// Run through the honeypot configuration |
|
165
|
1 |
|
foreach ($honeypots as $name => $config) { |
|
166
|
1 |
|
if (is_array($config)) { |
|
167
|
1 |
|
$honeypotPrefix = $prefix ? $prefix.'['.htmlspecialchars($name).']' : htmlspecialchars($name); |
|
168
|
1 |
|
$this->createHoneypotsRecursive($config, $armor, $honeypotPrefix); |
|
169
|
1 |
|
continue; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
$honeypotName = $prefix ? $prefix.'['.htmlspecialchars($name).']' : htmlspecialchars($name); |
|
173
|
1 |
|
$armor[] = new InputElement([ |
|
174
|
1 |
|
'type' => $config ?: 'text', |
|
175
|
1 |
|
'name' => $honeypotName, |
|
176
|
1 |
|
'value' => '' |
|
177
|
1 |
|
], $this->renderer); |
|
178
|
|
|
} |
|
179
|
1 |
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Return all serializable properties |
|
183
|
|
|
* |
|
184
|
|
|
* The renderer closure must be omitted in order to make the validator serializable |
|
185
|
|
|
* |
|
186
|
|
|
* @return array Serializable properties |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function __sleep() |
|
189
|
|
|
{ |
|
190
|
1 |
|
return ['honeypots']; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|