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