1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Yiisoft\NetworkUtilities\IpHelper; |
11
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
|
|
|
|
12
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
13
|
|
|
use Yiisoft\Validator\ParametrizedRuleInterface; |
14
|
|
|
use function array_key_exists; |
15
|
|
|
use function strlen; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Checks if the value is a valid IPv4/IPv6 address or subnet. |
19
|
|
|
* |
20
|
|
|
* It also may change the value if normalization of IPv6 expansion is enabled. |
21
|
|
|
*/ |
22
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
23
|
|
|
final class Ip implements ParametrizedRuleInterface |
24
|
|
|
{ |
25
|
|
|
use HandlerClassNameTrait; |
26
|
|
|
use RuleNameTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Negation char. |
30
|
|
|
* |
31
|
|
|
* Used to negate {@see $ranges} or {@see $network} or to negate validating value when {@see $allowNegation} |
32
|
|
|
* is used. |
33
|
|
|
*/ |
34
|
|
|
private const NEGATION_CHAR = '!'; |
35
|
|
|
/** |
36
|
|
|
* @see $networks |
37
|
|
|
*/ |
38
|
|
|
private array $defaultNetworks = [ |
39
|
|
|
'*' => ['any'], |
40
|
|
|
'any' => ['0.0.0.0/0', '::/0'], |
41
|
|
|
'private' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fd00::/8'], |
42
|
|
|
'multicast' => ['224.0.0.0/4', 'ff00::/8'], |
43
|
|
|
'linklocal' => ['169.254.0.0/16', 'fe80::/10'], |
44
|
|
|
'localhost' => ['127.0.0.0/8', '::1'], |
45
|
|
|
'documentation' => ['192.0.2.0/24', '198.51.100.0/24', '203.0.113.0/24', '2001:db8::/32'], |
46
|
|
|
'system' => ['multicast', 'linklocal', 'localhost', 'documentation'], |
47
|
|
|
]; |
48
|
|
|
|
49
|
6 |
|
public function __construct( |
50
|
|
|
/** |
51
|
|
|
* @var array Custom network aliases, that can be used in {@see $ranges}. |
52
|
|
|
* |
53
|
|
|
* - key - alias name |
54
|
|
|
* - value - array of strings. String can be an IP range, IP address or another alias. String can be |
55
|
|
|
* negated with {@see NEGATION_CHAR} (independent of {@see $allowNegation} option). |
56
|
|
|
* |
57
|
|
|
* The following aliases are defined by default in {@see $defaultNetworks} and will be merged with custom ones: |
58
|
|
|
* |
59
|
|
|
* - `*`: `any` |
60
|
|
|
* - `any`: `0.0.0.0/0, ::/0` |
61
|
|
|
* - `private`: `10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8` |
62
|
|
|
* - `multicast`: `224.0.0.0/4, ff00::/8` |
63
|
|
|
* - `linklocal`: `169.254.0.0/16, fe80::/10` |
64
|
|
|
* - `localhost`: `127.0.0.0/8', ::1` |
65
|
|
|
* - `documentation`: `192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32` |
66
|
|
|
* - `system`: `multicast, linklocal, localhost, documentation` |
67
|
|
|
* |
68
|
|
|
* @see $defaultNetworks |
69
|
|
|
*/ |
70
|
|
|
private array $networks = [], |
71
|
|
|
/** |
72
|
|
|
* @var bool whether the validating value can be an IPv4 address. Defaults to `true`. |
73
|
|
|
*/ |
74
|
|
|
private bool $allowIpv4 = true, |
75
|
|
|
/** |
76
|
|
|
* @var bool whether the validating value can be an IPv6 address. Defaults to `true`. |
77
|
|
|
*/ |
78
|
|
|
private bool $allowIpv6 = true, |
79
|
|
|
/** |
80
|
|
|
* @var bool whether the address can be an IP with CIDR subnet, like `192.168.10.0/24`. |
81
|
|
|
* The following values are possible: |
82
|
|
|
* |
83
|
|
|
* - `false` - the address must not have a subnet (default). |
84
|
|
|
* - `true` - specifying a subnet is optional. |
85
|
|
|
*/ |
86
|
|
|
private bool $allowSubnet = false, |
87
|
|
|
private bool $requireSubnet = false, |
88
|
|
|
/** |
89
|
|
|
* @var bool whether address may have a {@see NEGATION_CHAR} character at the beginning. |
90
|
|
|
* Defaults to `false`. |
91
|
|
|
*/ |
92
|
|
|
private bool $allowNegation = false, |
93
|
|
|
/** |
94
|
|
|
* @var string user-defined error message is used when validation fails due to the wrong IP address format. |
95
|
|
|
* |
96
|
|
|
* You may use the following placeholders in the message: |
97
|
|
|
* |
98
|
|
|
* - `{attribute}`: the label of the attribute being validated |
99
|
|
|
* - `{value}`: the value of the attribute being validated |
100
|
|
|
*/ |
101
|
|
|
private string $message = 'Must be a valid IP address.', |
102
|
|
|
/** |
103
|
|
|
* @var string user-defined error message is used when validation fails due to the disabled IPv4 validation. |
104
|
|
|
* |
105
|
|
|
* You may use the following placeholders in the message: |
106
|
|
|
* |
107
|
|
|
* - `{attribute}`: the label of the attribute being validated |
108
|
|
|
* - `{value}`: the value of the attribute being validated |
109
|
|
|
* |
110
|
|
|
* @see $allowIpv4 |
111
|
|
|
*/ |
112
|
|
|
private string $ipv4NotAllowedMessage = 'Must not be an IPv4 address.', |
113
|
|
|
/** |
114
|
|
|
* @var string user-defined error message is used when validation fails due to the disabled IPv6 validation. |
115
|
|
|
* |
116
|
|
|
* You may use the following placeholders in the message: |
117
|
|
|
* |
118
|
|
|
* - `{attribute}`: the label of the attribute being validated |
119
|
|
|
* - `{value}`: the value of the attribute being validated |
120
|
|
|
* |
121
|
|
|
* @see $allowIpv6 |
122
|
|
|
*/ |
123
|
|
|
private string $ipv6NotAllowedMessage = 'Must not be an IPv6 address.', |
124
|
|
|
/** |
125
|
|
|
* @var string user-defined error message is used when validation fails due to the wrong CIDR. |
126
|
|
|
* |
127
|
|
|
* You may use the following placeholders in the message: |
128
|
|
|
* |
129
|
|
|
* - `{attribute}`: the label of the attribute being validated |
130
|
|
|
* - `{value}`: the value of the attribute being validated |
131
|
|
|
* |
132
|
|
|
* @see $allowSubnet |
133
|
|
|
*/ |
134
|
|
|
private string $wrongCidrMessage = 'Contains wrong subnet mask.', |
135
|
|
|
/** |
136
|
|
|
* @var string user-defined error message is used when validation fails due to subnet {@see $allowSubnet} is |
137
|
|
|
* used, but the CIDR prefix is not set. |
138
|
|
|
* |
139
|
|
|
* You may use the following placeholders in the message: |
140
|
|
|
* |
141
|
|
|
* - `{attribute}`: the label of the attribute being validated |
142
|
|
|
* - `{value}`: the value of the attribute being validated |
143
|
|
|
* |
144
|
|
|
* @see $allowSubnet |
145
|
|
|
*/ |
146
|
|
|
private string $noSubnetMessage = 'Must be an IP address with specified subnet.', |
147
|
|
|
/** |
148
|
|
|
* @var string user-defined error message is used when validation fails |
149
|
|
|
* due to {@see $allowSubnet} is false, but CIDR prefix is present. |
150
|
|
|
* |
151
|
|
|
* You may use the following placeholders in the message: |
152
|
|
|
* |
153
|
|
|
* - `{attribute}`: the label of the attribute being validated |
154
|
|
|
* - `{value}`: the value of the attribute being validated |
155
|
|
|
* |
156
|
|
|
* @see $allowSubnet |
157
|
|
|
*/ |
158
|
|
|
private string $hasSubnetMessage = 'Must not be a subnet.', |
159
|
|
|
/** |
160
|
|
|
* @var string user-defined error message is used when validation fails due to IP address |
161
|
|
|
* is not allowed by {@see $ranges} check. |
162
|
|
|
* |
163
|
|
|
* You may use the following placeholders in the message: |
164
|
|
|
* |
165
|
|
|
* - `{attribute}`: the label of the attribute being validated |
166
|
|
|
* - `{value}`: the value of the attribute being validated |
167
|
|
|
* |
168
|
|
|
* @see $ranges |
169
|
|
|
*/ |
170
|
|
|
private string $notInRangeMessage = 'Is not in the allowed range.', |
171
|
|
|
/** |
172
|
|
|
* @var string[] The IPv4 or IPv6 ranges that are allowed or forbidden. |
173
|
|
|
* |
174
|
|
|
* The following preparation tasks are performed: |
175
|
|
|
* |
176
|
|
|
* - Recursively substitutes aliases (described in {@see $networks}) with their values. |
177
|
|
|
* - Removes duplicates. |
178
|
|
|
* |
179
|
|
|
* When the array is empty, or the option not set, all IP addresses are allowed. |
180
|
|
|
* |
181
|
|
|
* Otherwise, the rules are checked sequentially until the first match is found. |
182
|
|
|
* An IP address is forbidden, when it has not matched any of the rules. |
183
|
|
|
* |
184
|
|
|
* Example: |
185
|
|
|
* |
186
|
|
|
* ```php |
187
|
|
|
* (new Ip(ranges: [ |
188
|
|
|
* '192.168.10.128' |
189
|
|
|
* '!192.168.10.0/24', |
190
|
|
|
* 'any' // allows any other IP addresses |
191
|
|
|
* ]); |
192
|
|
|
* ``` |
193
|
|
|
* |
194
|
|
|
* In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the `192.168.10.0/24` |
195
|
|
|
* subnet. IPv4 address `192.168.10.128` is also allowed, because it is listed before the restriction. |
196
|
|
|
*/ |
197
|
|
|
private array $ranges = [], |
198
|
|
|
private bool $skipOnEmpty = false, |
199
|
|
|
private bool $skipOnError = false, |
200
|
|
|
private ?Closure $when = null, |
201
|
|
|
) { |
202
|
6 |
|
foreach ($networks as $key => $_values) { |
203
|
1 |
|
if (array_key_exists($key, $this->defaultNetworks)) { |
204
|
1 |
|
throw new RuntimeException("Network alias \"{$key}\" already set as default."); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
5 |
|
$this->networks = array_merge($this->defaultNetworks, $this->networks); |
209
|
|
|
|
210
|
5 |
|
if ($requireSubnet) { |
211
|
|
|
$this->allowSubnet = true; |
212
|
|
|
} |
213
|
|
|
|
214
|
5 |
|
$this->ranges = $this->prepareRanges($ranges); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return array |
219
|
|
|
*/ |
220
|
|
|
public function getNetworks(): array |
221
|
|
|
{ |
222
|
|
|
return $this->networks; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
117 |
|
public function isAllowIpv4(): bool |
229
|
|
|
{ |
230
|
117 |
|
return $this->allowIpv4; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
35 |
|
public function isAllowIpv6(): bool |
237
|
|
|
{ |
238
|
35 |
|
return $this->allowIpv6; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
34 |
|
public function isAllowSubnet(): bool |
245
|
|
|
{ |
246
|
34 |
|
return $this->allowSubnet; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
52 |
|
public function isRequireSubnet(): bool |
253
|
|
|
{ |
254
|
52 |
|
return $this->requireSubnet; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
8 |
|
public function isAllowNegation(): bool |
261
|
|
|
{ |
262
|
8 |
|
return $this->allowNegation; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
116 |
|
public function getMessage(): string |
269
|
|
|
{ |
270
|
116 |
|
return $this->message; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
2 |
|
public function getIpv4NotAllowedMessage(): string |
277
|
|
|
{ |
278
|
2 |
|
return $this->ipv4NotAllowedMessage; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return string |
283
|
|
|
*/ |
284
|
1 |
|
public function getIpv6NotAllowedMessage(): string |
285
|
|
|
{ |
286
|
1 |
|
return $this->ipv6NotAllowedMessage; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
2 |
|
public function getWrongCidrMessage(): string |
293
|
|
|
{ |
294
|
2 |
|
return $this->wrongCidrMessage; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
4 |
|
public function getNoSubnetMessage(): string |
301
|
|
|
{ |
302
|
4 |
|
return $this->noSubnetMessage; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
4 |
|
public function getHasSubnetMessage(): string |
309
|
|
|
{ |
310
|
4 |
|
return $this->hasSubnetMessage; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
16 |
|
public function getNotInRangeMessage(): string |
317
|
|
|
{ |
318
|
16 |
|
return $this->notInRangeMessage; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return bool |
323
|
|
|
*/ |
324
|
|
|
public function isSkipOnEmpty(): bool |
325
|
|
|
{ |
326
|
|
|
return $this->skipOnEmpty; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @return bool |
331
|
|
|
*/ |
332
|
|
|
public function isSkipOnError(): bool |
333
|
|
|
{ |
334
|
|
|
return $this->skipOnError; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @return Closure|null |
339
|
|
|
*/ |
340
|
|
|
public function getWhen(): ?Closure |
341
|
|
|
{ |
342
|
|
|
return $this->when; |
343
|
|
|
} |
344
|
|
|
|
345
|
4 |
|
public function getRanges(): array |
346
|
|
|
{ |
347
|
4 |
|
return $this->ranges; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Parses IP address/range for the negation with {@see NEGATION_CHAR}. |
352
|
|
|
* |
353
|
|
|
* @param $string |
354
|
|
|
* |
355
|
|
|
* @return array `[0 => bool, 1 => string]` |
356
|
|
|
* - boolean: whether the string is negated |
357
|
|
|
* - string: the string without negation (when the negation were present) |
358
|
|
|
*/ |
359
|
42 |
|
private function parseNegatedRange($string): array |
360
|
|
|
{ |
361
|
42 |
|
$isNegated = str_starts_with($string, self::NEGATION_CHAR); |
362
|
42 |
|
return [$isNegated, $isNegated ? substr($string, strlen(self::NEGATION_CHAR)) : $string]; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Prepares array to fill in {@see $ranges}. |
367
|
|
|
* |
368
|
|
|
* - Recursively substitutes aliases, described in {@see $networks} with their values, |
369
|
|
|
* - Removes duplicates. |
370
|
|
|
* |
371
|
|
|
* @see $networks |
372
|
|
|
*/ |
373
|
5 |
|
private function prepareRanges(array $ranges): array |
374
|
|
|
{ |
375
|
5 |
|
$result = []; |
376
|
5 |
|
foreach ($ranges as $string) { |
377
|
4 |
|
[$isRangeNegated, $range] = $this->parseNegatedRange($string); |
378
|
4 |
|
if (isset($this->networks[$range])) { |
379
|
3 |
|
$replacements = $this->prepareRanges($this->networks[$range]); |
380
|
3 |
|
foreach ($replacements as &$replacement) { |
381
|
3 |
|
[$isReplacementNegated, $replacement] = $this->parseNegatedRange($replacement); |
382
|
3 |
|
$result[] = ($isRangeNegated && !$isReplacementNegated ? self::NEGATION_CHAR : '') . $replacement; |
383
|
|
|
} |
384
|
|
|
} else { |
385
|
4 |
|
$result[] = $string; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
5 |
|
return array_unique($result); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* The method checks whether the IP address with specified CIDR is allowed according to the {@see $ranges} list. |
394
|
|
|
*/ |
395
|
69 |
|
public function isAllowed(string $ip): bool |
396
|
|
|
{ |
397
|
69 |
|
if (empty($this->ranges)) { |
398
|
31 |
|
return true; |
399
|
|
|
} |
400
|
|
|
|
401
|
38 |
|
foreach ($this->ranges as $string) { |
402
|
38 |
|
[$isNegated, $range] = $this->parseNegatedRange($string); |
403
|
38 |
|
if (IpHelper::inRange($ip, $range)) { |
404
|
32 |
|
return !$isNegated; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
408
|
6 |
|
return false; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Used to get the Regexp pattern for initial IP address parsing. |
413
|
|
|
*/ |
414
|
112 |
|
public function getIpParsePattern(): string |
415
|
|
|
{ |
416
|
112 |
|
return '/^(?<not>' . preg_quote( |
417
|
|
|
self::NEGATION_CHAR, |
418
|
|
|
'/' |
419
|
|
|
) . ')?(?<ipCidr>(?<ip>(?:' . IpHelper::IPV4_PATTERN . ')|(?:' . IpHelper::IPV6_PATTERN . '))(?:\/(?<cidr>-?\d+))?)$/'; |
420
|
|
|
} |
421
|
|
|
|
422
|
7 |
|
public function getOptions(): array |
423
|
|
|
{ |
424
|
|
|
return [ |
425
|
7 |
|
'networks' => $this->networks, |
426
|
7 |
|
'allowIpv4' => $this->allowIpv4, |
427
|
7 |
|
'allowIpv6' => $this->allowIpv6, |
428
|
7 |
|
'allowSubnet' => $this->allowSubnet, |
429
|
7 |
|
'requireSubnet' => $this->requireSubnet, |
430
|
7 |
|
'allowNegation' => $this->allowNegation, |
431
|
|
|
'message' => [ |
432
|
7 |
|
'message' => $this->message, |
433
|
|
|
], |
434
|
|
|
'ipv4NotAllowedMessage' => [ |
435
|
7 |
|
'message' => $this->ipv4NotAllowedMessage, |
436
|
|
|
], |
437
|
|
|
'ipv6NotAllowedMessage' => [ |
438
|
7 |
|
'message' => $this->ipv6NotAllowedMessage, |
439
|
|
|
], |
440
|
|
|
'wrongCidrMessage' => [ |
441
|
7 |
|
'message' => $this->wrongCidrMessage, |
442
|
|
|
], |
443
|
|
|
'noSubnetMessage' => [ |
444
|
7 |
|
'message' => $this->noSubnetMessage, |
445
|
|
|
], |
446
|
|
|
'hasSubnetMessage' => [ |
447
|
7 |
|
'message' => $this->hasSubnetMessage, |
448
|
|
|
], |
449
|
|
|
'notInRangeMessage' => [ |
450
|
7 |
|
'message' => $this->notInRangeMessage, |
451
|
|
|
], |
452
|
7 |
|
'ranges' => $this->ranges, |
453
|
7 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
454
|
7 |
|
'skipOnError' => $this->skipOnError, |
455
|
|
|
]; |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
|