1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupU2fBundle\Validator\Constraints; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupU2fBundle\Exception\LogicException; |
22
|
|
|
use Symfony\Component\Validator\Constraint; |
23
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
24
|
|
|
|
25
|
|
|
final class ClientDataConstraintValidator extends ConstraintValidator |
26
|
|
|
{ |
27
|
|
|
const STRICT = true; |
28
|
|
|
|
29
|
|
|
public function validate($value, Constraint $constraint) |
30
|
|
|
{ |
31
|
|
|
if (!$constraint instanceof ClientDataConstraint) { |
32
|
|
|
throw new LogicException('ClientDataConstraintValidator can only validate ClientDataConstraints'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (!is_string($value)) { |
36
|
|
|
$this->context->addViolation($constraint->message, ['%reason%' => 'Not a string'], $value); |
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$decoded = base64_decode(strtr($value, '-_', '+/'), self::STRICT); |
41
|
|
|
|
42
|
|
View Code Duplication |
if ($decoded === false) { |
|
|
|
|
43
|
|
|
$this->context->addViolation($constraint->message, ['%reason%' => 'Not base64 decodable'], $value); |
|
|
|
|
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$clientData = json_decode($decoded); |
48
|
|
|
$jsonLastError = json_last_error(); |
49
|
|
|
|
50
|
|
|
if ($jsonLastError) { |
51
|
|
|
$jsonErrorReason = $this->getJsonErrorMessage($jsonLastError); |
52
|
|
|
$this->context->addViolation($constraint->message, ['%reason%' => $jsonErrorReason], $value); |
|
|
|
|
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!isset($clientData->challenge)) { |
57
|
|
|
$this->context->addViolation( |
58
|
|
|
$constraint->message, |
59
|
|
|
['%reason%' => 'Challenge not present on client data'], |
60
|
|
|
$value |
|
|
|
|
61
|
|
|
); |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int $errorCode |
68
|
|
|
* @return string |
69
|
|
|
* |
70
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
71
|
|
|
*/ |
72
|
|
|
private function getJsonErrorMessage($errorCode) |
73
|
|
|
{ |
74
|
|
|
switch ($errorCode) { |
75
|
|
|
case JSON_ERROR_NONE: |
76
|
|
|
return 'JSON_ERROR_NONE'; |
77
|
|
|
case JSON_ERROR_DEPTH: |
78
|
|
|
return 'JSON_ERROR_DEPTH'; |
79
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
80
|
|
|
return 'JSON_ERROR_STATE_MISMATCH'; |
81
|
|
|
case JSON_ERROR_CTRL_CHAR: |
82
|
|
|
return 'JSON_ERROR_CTRL_CHAR'; |
83
|
|
|
case JSON_ERROR_SYNTAX: |
84
|
|
|
return 'JSON_ERROR_SYNTAX'; |
85
|
|
|
case JSON_ERROR_UTF8: |
86
|
|
|
return 'JSON_ERROR_UTF8'; |
87
|
|
|
case constant('JSON_ERROR_RECURSION'): |
88
|
|
|
return 'JSON_ERROR_RECURSION'; |
89
|
|
|
case constant('JSON_ERROR_INF_OR_NAN'): |
90
|
|
|
return 'JSON_ERROR_INF_OR_NAN'; |
91
|
|
|
case constant('JSON_ERROR_UNSUPPORTED_TYPE'): |
92
|
|
|
return 'JSON_ERROR_UNSUPPORTED_TYPE'; |
93
|
|
|
default: |
94
|
|
|
throw new LogicException(sprintf('Unknown JSON decoding error code %d encountered', $errorCode)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.