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\Factory\HmacFactory; |
41
|
|
|
use Jkphl\Antibot\Infrastructure\Model\AbstractValidator; |
42
|
|
|
use Jkphl\Antibot\Ports\Exceptions\InvalidArgumentException; |
43
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* HMAC Validator |
47
|
|
|
* |
48
|
|
|
* @package Jkphl\Antibot |
49
|
|
|
* @subpackage Jkphl\Antibot\Ports\Validators |
50
|
|
|
*/ |
51
|
|
|
class HmacValidator extends AbstractValidator |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Request method vector |
55
|
|
|
* |
56
|
|
|
* @var string[] |
57
|
|
|
*/ |
58
|
|
|
protected $methodVector = null; |
59
|
|
|
/** |
60
|
|
|
* Request submission times |
61
|
|
|
* |
62
|
|
|
* @var float[] |
63
|
|
|
*/ |
64
|
|
|
protected $submissionTimes = null; |
65
|
|
|
/** |
66
|
|
|
* Validation order position |
67
|
|
|
* |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
const POSITION = 100; |
71
|
|
|
/** |
72
|
|
|
* GET request |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
const METHOD_GET = 'GET'; |
77
|
|
|
/** |
78
|
|
|
* POST request |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
const METHOD_POST = 'POST'; |
83
|
|
|
/** |
84
|
|
|
* Minimum submission time |
85
|
|
|
* |
86
|
|
|
* @var float |
87
|
|
|
*/ |
88
|
|
|
const MINIMUM_SUBMISSION = 10; |
89
|
|
|
/** |
90
|
|
|
* Minimum submission time for follow-up submissions |
91
|
|
|
* |
92
|
|
|
* @var float |
93
|
|
|
*/ |
94
|
|
|
const MINIMUM_FOLLOWUP_SUBMISSION = 3; |
95
|
|
|
/** |
96
|
|
|
* Maximum submission time |
97
|
|
|
* |
98
|
|
|
* @var float |
99
|
|
|
*/ |
100
|
|
|
const MAXIMUM_SUBMISSION = 3600; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the request method vector |
104
|
|
|
* |
105
|
|
|
* @param string $previous Previous request |
106
|
|
|
* @param string $current Current request |
107
|
|
|
*/ |
108
|
1 |
|
public function setMethodVector(string $previous = null, string $current = null): void |
109
|
|
|
{ |
110
|
|
|
// If the request method vector should be unset |
111
|
1 |
|
if ($previous === null) { |
112
|
|
|
$this->methodVector = null; |
|
|
|
|
113
|
|
|
|
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$this->methodVector = [$this->validateRequestMethod($previous), $this->validateRequestMethod($current)]; |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sanitize and validate a request method |
122
|
|
|
* |
123
|
|
|
* @param string $method Request method |
124
|
|
|
* |
125
|
|
|
* @return string Validated request method |
126
|
|
|
* @throws InvalidArgumentException If the request method is invalid |
127
|
|
|
*/ |
128
|
1 |
|
protected function validateRequestMethod(string $method): string |
129
|
|
|
{ |
130
|
1 |
|
$method = strtoupper($method); |
131
|
1 |
|
if ($method !== static::METHOD_GET && $method !== static::METHOD_POST) { |
132
|
|
|
throw new InvalidArgumentException( |
133
|
|
|
sprintf(InvalidArgumentException::INVALID_REQUEST_METHOD_STR, $method), |
134
|
|
|
InvalidArgumentException::INVALID_REQUEST_METHOD |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return $method; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Sanitize and set the submission times |
143
|
|
|
* |
144
|
|
|
* @param float $max Maximum submission time |
145
|
|
|
* @param float $min Minimum submission time |
146
|
|
|
* @param float|null $minFollowUp Minimum submission time for follow-up submissions |
147
|
|
|
*/ |
148
|
1 |
|
public function setSubmissionTimes(float $max = null, float $min = null, float $minFollowUp = null): void |
149
|
|
|
{ |
150
|
|
|
// If the submission times should be unset |
151
|
1 |
|
if ($max === null) { |
152
|
|
|
$this->submissionTimes = null; |
|
|
|
|
153
|
|
|
|
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
$max = min(floatval($max), static::MAXIMUM_SUBMISSION); |
158
|
1 |
|
$min = max(floatval($min), static::MINIMUM_SUBMISSION); |
159
|
1 |
|
$minFollowUp = ($minFollowUp === null) |
160
|
1 |
|
? $min : max(floatval($minFollowUp), static::MINIMUM_FOLLOWUP_SUBMISSION); |
161
|
1 |
|
$this->submissionTimes = [$min, $minFollowUp, $max]; |
|
|
|
|
162
|
1 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Validate a request |
166
|
|
|
* |
167
|
|
|
* @param ServerRequestInterface $request Request |
168
|
|
|
* @param Antibot $antibot Antibot instance |
169
|
|
|
* |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public function validate(ServerRequestInterface $request, Antibot $antibot): bool |
173
|
|
|
{ |
174
|
|
|
$data = $antibot->getData(); |
175
|
|
|
|
176
|
|
|
// If Antibot data has been submitted |
177
|
|
|
if ($data !== null) { |
178
|
|
|
// If no HMAC was submitted |
179
|
|
|
if (empty($data['hmac'])) { |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Create protective form HTML |
191
|
|
|
* |
192
|
|
|
* @param ServerRequestInterface $request Request |
193
|
|
|
* @param Antibot $antibot Antibot instance |
194
|
|
|
* |
195
|
|
|
* @return string Form HTML |
196
|
|
|
*/ |
197
|
1 |
|
public function armor(ServerRequestInterface $request, Antibot $antibot): string |
198
|
|
|
{ |
199
|
1 |
|
$now = null; |
200
|
1 |
|
$hmac = $this->calculateHmac($request, $antibot, $now); |
201
|
1 |
|
$armor = '<input type="hidden" name="'.htmlspecialchars($antibot->getParameterPrefix()).'[hmac]" value="'.htmlspecialchars($hmac).'">'; |
202
|
|
|
|
203
|
1 |
|
return $armor; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function _decryptHmac($hmac) |
207
|
|
|
{ |
208
|
|
|
$decrypted = false; |
209
|
|
|
$previousMethod = null; |
210
|
|
|
$hmacParams = array($this->_token); |
|
|
|
|
211
|
|
|
// If session token checks are enabled |
212
|
|
|
if ($this->_sessionTokenEnabled()) { |
|
|
|
|
213
|
|
|
$hmacParams[] = session_id(); |
214
|
|
|
} |
215
|
|
|
// Short-circuit blocked HMAC |
216
|
|
|
$hmacBlock = $hmacParams; |
217
|
|
|
$hmacBlock[] = self::BLOCK; |
218
|
|
|
if (\TYPO3\CMS\Core\Utility\GeneralUtility::hmac(serialize($hmacBlock)) == $hmac) { |
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
// If submission time checks are enabled |
222
|
|
|
if ($this->_submissionMethodOrderEnabled()) { |
|
|
|
|
223
|
|
|
list($previousMethod, $currentMethod) = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('-', |
224
|
|
|
$this->_settings['order']['method'], true); |
|
|
|
|
225
|
|
|
// If the current request method doesn't match |
226
|
|
|
if ($currentMethod != strtoupper($_SERVER['REQUEST_METHOD'])) { |
227
|
|
|
throw new Exception\InvalidRequestMethodOrderException(strtoupper($_SERVER['REQUEST_METHOD'])); |
228
|
|
|
} |
229
|
|
|
$hmacParams[] = $previousMethod; |
230
|
|
|
} |
231
|
|
|
// If submission time checks are enabled |
232
|
|
|
if ($this->_submissionTimeEnabled()) { |
|
|
|
|
233
|
|
|
$minimum = intval($this->_settings['time']['minimum']); |
234
|
|
|
$maximium = intval($this->_settings['time']['maximum']); |
235
|
|
|
$first = max($minimum, intval($this->_settings['time']['first'])); |
236
|
|
|
$now = time(); |
237
|
|
|
$initial = $now - $first; |
238
|
|
|
// If a timestamp hint has been submitted: Probe this first |
239
|
|
|
if ($this->_timestamp && (($this->_timestamp + $minimum) <= $now) && (($this->_timestamp + $maximium) >= $now) && $this->_info('Probing timestamp hint first') && ( |
|
|
|
|
240
|
|
|
$this->_probeTimedHMAC($hmac, $hmacParams, $this->_timestamp, $this->_timestamp > $initial) || |
|
|
|
|
241
|
|
|
(($this->_timestamp <= $initial) ? $this->_probeTimedHMAC($hmac, $hmacParams, $this->_timestamp, |
|
|
|
|
242
|
|
|
true) : false)) |
|
|
|
|
243
|
|
|
) { |
244
|
|
|
$this->_delay = $now - $this->_timestamp; |
|
|
|
|
245
|
|
|
$decrypted = true; |
246
|
|
|
// Else (or if decryption failed for some reason: Probe the valid time range |
247
|
|
|
} else { |
248
|
|
|
// Run through the valid seconds range |
249
|
|
|
for ($time = $now - $minimum; $time >= $now - $maximium; --$time) { |
250
|
|
|
// Probe the current timestamp |
251
|
|
|
if ($this->_probeTimedHMAC($hmac, $hmacParams, $time, |
|
|
|
|
252
|
|
|
$time > $initial) || (($time <= $initial) && $this->_probeTimedHMAC($hmac, $hmacParams, |
|
|
|
|
253
|
|
|
$time, true)) |
|
|
|
|
254
|
|
|
) { |
255
|
|
|
$this->_delay = $now - $time; |
256
|
|
|
$decrypted = true; |
257
|
|
|
break; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
// Else: Check for HMAC match |
262
|
|
|
} else { |
263
|
|
|
$currentHMAC = \TYPO3\CMS\Core\Utility\GeneralUtility::hmac(serialize($hmacParams)); |
264
|
|
|
$decrypted = $hmac == $currentHMAC; |
265
|
|
|
$this->_debug('Probing HMAC with parameters', $hmacParams); |
|
|
|
|
266
|
|
|
$this->_debug('Current HMAC:', $currentHMAC); |
|
|
|
|
267
|
|
|
} |
268
|
|
|
// Register the initial HTTP method in case decryption was successfull |
269
|
|
|
if ($decrypted && $previousMethod) { |
270
|
|
|
$this->_method = $previousMethod; |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $decrypted; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Probe a set of HMAC parameters with timestamp (for both initial or follow-up requests) |
278
|
|
|
* |
279
|
|
|
* @param \string $hmac HMAC |
280
|
|
|
* @param \array $hmacParams HMAC parameters |
281
|
|
|
* @param \int $timestamp Timestamp |
282
|
|
|
* @param \boolean $followUp Follow-up request |
283
|
|
|
* |
284
|
|
|
* @return \boolean HMAC matches |
285
|
|
|
*/ |
286
|
|
|
protected function _probeTimedHMAC($hmac, array $hmacParams, $timestamp, $followUp = false) |
287
|
|
|
{ |
288
|
|
|
if ($followUp) { |
289
|
|
|
$hmacParams[] = true; |
290
|
|
|
} |
291
|
|
|
$hmacParams[] = $timestamp; |
292
|
|
|
$currentHMAC = \TYPO3\CMS\Core\Utility\GeneralUtility::hmac(serialize($hmacParams)); |
293
|
|
|
$this->_debug('Probing HMAC with parameters', $hmacParams); |
|
|
|
|
294
|
|
|
$this->_debug('Current HMAC:', $currentHMAC); |
|
|
|
|
295
|
|
|
|
296
|
|
|
return $currentHMAC == $hmac; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Create and return the submission HMAC |
301
|
|
|
* |
302
|
|
|
* @param \int $now Current timestamp |
303
|
|
|
* |
304
|
|
|
* @return \string Submission HMAC |
305
|
|
|
*/ |
306
|
|
|
protected function _hmac(&$now = null) |
307
|
|
|
{ |
308
|
|
|
$hmacParams = array($this->_token); |
309
|
|
|
// If session token checks are enabled |
310
|
|
|
if ($this->_sessionTokenEnabled()) { |
|
|
|
|
311
|
|
|
$hmacParams[] = session_id(); |
312
|
|
|
} |
313
|
|
|
// If there is an invalid current HMAC |
314
|
|
|
if ($this->_valid === false) { |
|
|
|
|
315
|
|
|
$hmacParams[] = self::BLOCK; |
316
|
|
|
// Else |
317
|
|
|
} else { |
318
|
|
|
// If submission time checks are enabled |
319
|
|
|
if ($this->_submissionMethodOrderEnabled()) { |
|
|
|
|
320
|
|
|
$hmacParams[] = $this->_method ?: strtoupper($_SERVER['REQUEST_METHOD']); |
321
|
|
|
} |
322
|
|
|
// If submission time checks are enabled |
323
|
|
|
if ($this->_submissionTimeEnabled()) { |
|
|
|
|
324
|
|
|
if ($this->_data) { |
|
|
|
|
325
|
|
|
$hmacParams[] = true; |
326
|
|
|
} |
327
|
|
|
$hmacParams[] = |
328
|
|
|
$now = time(); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
$hmac = \TYPO3\CMS\Core\Utility\GeneralUtility::hmac(serialize($hmacParams)); |
332
|
|
|
$this->_debug('Creating HMAC for parameters', $hmacParams); |
|
|
|
|
333
|
|
|
$this->_debug('HMAC:', $hmac); |
|
|
|
|
334
|
|
|
|
335
|
|
|
return $hmac; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Calculate the HMAC |
340
|
|
|
* |
341
|
|
|
* @param ServerRequestInterface $request Request |
342
|
|
|
* @param Antibot $antibot Antibot instance |
343
|
|
|
* @param int|null $now Current timestamp |
344
|
|
|
* |
345
|
|
|
* @return string HMAC |
346
|
|
|
*/ |
347
|
1 |
|
protected function calculateHmac(ServerRequestInterface $request, Antibot $antibot, int &$now = null): string |
348
|
|
|
{ |
349
|
1 |
|
$hmacParams = [$antibot->getUnique()]; |
350
|
1 |
|
$now = null; |
351
|
|
|
|
352
|
|
|
// Invalidate the HMAC if there's a current, invalid one |
353
|
1 |
|
if (false) { |
|
|
|
|
354
|
|
|
|
355
|
|
|
} else { |
356
|
1 |
|
$serverParams = $request->getServerParams(); |
357
|
|
|
|
358
|
|
|
// If the request method vector should be used |
359
|
1 |
|
if (!empty($this->methodVector)) { |
360
|
1 |
|
$requestMethod = empty($serverParams['REQUEST_METHOD']) ? '' : $serverParams['REQUEST_METHOD']; |
361
|
1 |
|
$hmacParams[] = $this->validateRequestMethod($requestMethod); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
// If submission time checks are enabled |
365
|
1 |
|
if (!empty($this->submissionTimes)) { |
366
|
1 |
|
if (!empty($antibot->getData())) { |
367
|
|
|
$hmacParams[] = true; |
368
|
|
|
} |
369
|
1 |
|
$hmacParams[] = $now = time(); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
// print_r($hmacParams); |
374
|
|
|
|
375
|
1 |
|
$hmac = HmacFactory::createFromString(serialize($hmacParams), $antibot->getUnique()); |
376
|
|
|
|
377
|
1 |
|
return $hmac; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..