/
CustomAssertionTrait.php
241 lines (204 loc) · 12 KB
/
CustomAssertionTrait.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
<?php
declare(strict_types=1);
namespace SimpleSAML\Assert;
use DateTimeImmutable;
use InvalidArgumentException;
use function array_map;
use function base64_decode;
use function base64_encode;
use function call_user_func_array;
use function filter_var;
use function implode;
use function in_array;
use function reset;
use function sprintf;
/**
* @package simplesamlphp/assert
*/
trait CustomAssertionTrait
{
private static string $duration_regex = '/^(-?)P(?=.)((\d+)Y)?((\d+)M)?((\d+)D)?(T(?=.)((\d+)H)?((\d+)M)?(\d*(\.\d+)?S)?)?$/i';
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/';
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/';
private static string $base64_regex = '/^(?:[a-z0-9+\/]{4})*(?:[a-z0-9+\/]{2}==|[a-z0-9+\/]{3}=)?$/i';
private static string $uri_same_document_regex = '#^(?:\#([A-Za-z][A-Za-z0-9+\-.]*:(?:\/\/(?:(?:[A-Za-z0-9\-._~!$&\'()*+,;=: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:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)|[Vv][0-9A-Fa-f]+\.[A-Za-z0-9\-._~!$&\'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|(?:[A-Za-z0-9\-._~!$&\'()*+,;=]|%[0-9A-Fa-f]{2})*)(?::[0-9]*)?(?:\/(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*|\/(?:(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)?|(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})+(?:\/(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*|)(?:\?(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})*)?(?:\#(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})*)?#';
private static string $hostname_regex = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/';
/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `private` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/
/**
* @param string $value
*/
private static function validDuration(string $value, string $message = ''): void
{
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$duration_regex]]) === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid xs:duration',
$value
));
}
}
/**
* Note: This test is not bullet-proof but prevents a string containing illegal characters
* from being passed and ensures the string roughly follows the correct format for a Base64 encoded string
*
* @param string $value
* @param string $message
*/
private static function stringPlausibleBase64(string $value, string $message = ''): void
{
$result = true;
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$base64_regex]]) === false) {
$result = false;
} elseif (strlen($value) % 4 !== 0) {
$result = false;
} else {
$decoded = base64_decode($value, true);
if ($decoded === false) {
$result = false;
} elseif (base64_encode($decoded) !== $value) {
$result = false;
}
}
if ($result === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid Base64 encoded string',
$value
));
}
}
/**
* @param string $value
* @param string $message
*/
private static function validDateTime(string $value, string $message = ''): void
{
if (
DateTimeImmutable::createFromFormat(DateTimeImmutable::ISO8601, $value) === false &&
DateTimeImmutable::createFromFormat(DateTimeImmutable::RFC3339_EXTENDED, $value) === false
) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid DateTime',
$value
));
}
}
/**
* @param string $value
* @param string $message
*/
private static function validDateTimeZulu(string $value, string $message = ''): void
{
$dateTime1 = DateTimeImmutable::createFromFormat(DateTimeImmutable::ISO8601, $value);
$dateTime2 = DateTimeImmutable::createFromFormat(DateTimeImmutable::RFC3339_EXTENDED, $value);
$dateTime = $dateTime1 ?: $dateTime2;
if ($dateTime === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid DateTime',
$value
));
} elseif ($dateTime->getTimezone()->getName() !== 'Z') {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
$value
));
}
}
/**
* @param mixed $value
* @param array $values
* @param string $message
*/
private static function notInArray($value, array $values, string $message = ''): void
{
if (in_array($value, $values, true)) {
$callable = /** @param mixed $val */function ($val) {
return self::valueToString($val);
};
throw new InvalidArgumentException(sprintf(
$message ?: 'Expected none of: %2$s. Got: %s',
self::valueToString($value),
implode(', ', array_map($callable, $values)),
));
}
}
/**
* @param string $value
* @param string $message
*/
private static function validURN(string $value, string $message = ''): void
{
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$urn_regex]]) === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid RFC8141 compliant URN',
$value
));
}
}
/**
* @param string $value
* @param string $message
*/
private static function validURL(string $value, string $message = ''): void
{
if (filter_var($value, FILTER_VALIDATE_URL) === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid RFC2396 compliant URL',
$value
));
}
}
/**
* @param string $value
* @param string $message
*/
private static function validURI(string $value, string $message = ''): void
{
if (
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$uri_regex]]) === false &&
// We're very lenient here to accept DNS hostnames without a scheme
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$hostname_regex]]) === false &&
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$uri_same_document_regex]]) === false
) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid RFC3986 compliant URI',
$value
));
}
}
/**
* @param string $value
* @param string $message
*/
private static function validNCName(string $value, string $message = ''): void
{
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$ncname_regex]]) === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid non-colonized name (NCName)',
$value
));
}
}
/**
* @param string $value
* @param string $message
*/
private static function validQName(string $value, string $message = ''): void
{
if (
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$qname_regex]]) === false &&
filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$ncname_regex]]) === false
) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid qualified name (QName)',
$value
));
}
}
}