1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yiisoft\Validator\Rule; |
4
|
|
|
|
5
|
|
|
use Yiisoft\NetworkUtilities\IpHelper; |
6
|
|
|
use Yiisoft\Validator\DataSetInterface; |
7
|
|
|
use Yiisoft\Validator\Result; |
8
|
|
|
use Yiisoft\Validator\Rule; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The validator checks if the attribute value is a valid IPv4/IPv6 address or subnet. |
12
|
|
|
* |
13
|
|
|
* It also may change attribute's value if normalization of IPv6 expansion is enabled. |
14
|
|
|
* |
15
|
|
|
* The following are examples of validation rules using this validator: |
16
|
|
|
* |
17
|
|
|
* ```php |
18
|
|
|
* ['ip_address', 'ip'], // IPv4 or IPv6 address |
19
|
|
|
* ['ip_address', 'ip', 'ipv6' => false], // IPv4 address (IPv6 is disabled) |
20
|
|
|
* ['ip_address', 'ip', 'subnet' => true], // requires a CIDR prefix (like 10.0.0.1/24) for the IP address |
21
|
|
|
* ['ip_address', 'ip', 'subnet' => null], // CIDR prefix is optional |
22
|
|
|
* ['ip_address', 'ip', 'subnet' => null, 'normalize' => true], // CIDR prefix is optional and will be added when missing |
23
|
|
|
* ['ip_address', 'ip', 'ranges' => ['192.168.0.0/24']], // only IP addresses from the specified subnet are allowed |
24
|
|
|
* ['ip_address', 'ip', 'ranges' => ['!192.168.0.0/24', 'any']], // any IP is allowed except IP in the specified subnet |
25
|
|
|
* ['ip_address', 'ip', 'expandIPv6' => true], // expands IPv6 address to a full notation format |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* @property array $ranges The IPv4 or IPv6 ranges that are allowed or forbidden. See [[setRanges()]] for |
29
|
|
|
* detailed description. |
30
|
|
|
*/ |
31
|
|
|
class Ip extends Rule |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Negation char. |
35
|
|
|
* |
36
|
|
|
* Used to negate [[ranges]] or [[networks]] or to negate validating value when [[negation]] is set to `true`. |
37
|
|
|
* |
38
|
|
|
* @see allowNegation |
39
|
|
|
* @see networks |
40
|
|
|
* @see ranges |
41
|
|
|
*/ |
42
|
|
|
private const NEGATION_CHAR = '!'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array The network aliases, that can be used in [[ranges]]. |
46
|
|
|
* - key - alias name |
47
|
|
|
* - value - array of strings. String can be an IP range, IP address or another alias. String can be |
48
|
|
|
* negated with [[NEGATION_CHAR]] (independent of `negation` option). |
49
|
|
|
* |
50
|
|
|
* The following aliases are defined by default: |
51
|
|
|
* - `*`: `any` |
52
|
|
|
* - `any`: `0.0.0.0/0, ::/0` |
53
|
|
|
* - `private`: `10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8` |
54
|
|
|
* - `multicast`: `224.0.0.0/4, ff00::/8` |
55
|
|
|
* - `linklocal`: `169.254.0.0/16, fe80::/10` |
56
|
|
|
* - `localhost`: `127.0.0.0/8', ::1` |
57
|
|
|
* - `documentation`: `192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32` |
58
|
|
|
* - `system`: `multicast, linklocal, localhost, documentation` |
59
|
|
|
*/ |
60
|
|
|
private array $networks = [ |
61
|
|
|
'*' => ['any'], |
62
|
|
|
'any' => ['0.0.0.0/0', '::/0'], |
63
|
|
|
'private' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fd00::/8'], |
64
|
|
|
'multicast' => ['224.0.0.0/4', 'ff00::/8'], |
65
|
|
|
'linklocal' => ['169.254.0.0/16', 'fe80::/10'], |
66
|
|
|
'localhost' => ['127.0.0.0/8', '::1'], |
67
|
|
|
'documentation' => ['192.0.2.0/24', '198.51.100.0/24', '203.0.113.0/24', '2001:db8::/32'], |
68
|
|
|
'system' => ['multicast', 'linklocal', 'localhost', 'documentation'], |
69
|
|
|
]; |
70
|
|
|
/** |
71
|
|
|
* @var bool whether the validating value can be an IPv6 address. Defaults to `true`. |
72
|
|
|
*/ |
73
|
|
|
private bool $allowIpv6 = true; |
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 address can be an IP with CIDR subnet, like `192.168.10.0/24`. |
80
|
|
|
* The following values are possible: |
81
|
|
|
* |
82
|
|
|
* - `false` - the address must not have a subnet (default). |
83
|
|
|
* - `true` - specifying a subnet is optional. |
84
|
|
|
*/ |
85
|
|
|
private bool $allowSubnet = false; |
86
|
|
|
/** |
87
|
|
|
* @var bool |
88
|
|
|
*/ |
89
|
|
|
private bool $requireSubnet = false; |
90
|
|
|
/** |
91
|
|
|
* @var bool whether address may have a [[NEGATION_CHAR]] character at the beginning. |
92
|
|
|
* Defaults to `false`. |
93
|
|
|
*/ |
94
|
|
|
private bool $allowNegation = false; |
95
|
|
|
|
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 IPv6 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 allowIpv6 |
114
|
|
|
*/ |
115
|
|
|
private string $ipv6NotAllowed = 'Must not be an IPv6 address.'; |
116
|
|
|
/** |
117
|
|
|
* @var string user-defined error message is used when validation fails due to the disabled IPv4 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 allowIpv4 |
125
|
|
|
*/ |
126
|
|
|
private string $ipv4NotAllowed = 'Must not be an IPv4 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
|
|
|
* @see allowSubnet |
135
|
|
|
*/ |
136
|
|
|
private string $wrongCidr = 'Contains wrong subnet mask.'; |
137
|
|
|
/** |
138
|
|
|
* @var string user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', |
139
|
|
|
* but the CIDR prefix is not set. |
140
|
|
|
* |
141
|
|
|
* You may use the following placeholders in the message: |
142
|
|
|
* |
143
|
|
|
* - `{attribute}`: the label of the attribute being validated |
144
|
|
|
* - `{value}`: the value of the attribute being validated |
145
|
|
|
* |
146
|
|
|
* @see allowSubnet |
147
|
|
|
*/ |
148
|
|
|
private string $noSubnet = 'Must be an IP address with specified subnet.'; |
149
|
|
|
/** |
150
|
|
|
* @var string user-defined error message is used when validation fails |
151
|
|
|
* due to [[subnet]] is false, but CIDR prefix is present. |
152
|
|
|
* |
153
|
|
|
* You may use the following placeholders in the message: |
154
|
|
|
* |
155
|
|
|
* - `{attribute}`: the label of the attribute being validated |
156
|
|
|
* - `{value}`: the value of the attribute being validated |
157
|
|
|
* |
158
|
|
|
* @see allowSubnet |
159
|
|
|
*/ |
160
|
|
|
private string $hasSubnet = 'Must not be a subnet.'; |
161
|
|
|
/** |
162
|
|
|
* @var string user-defined error message is used when validation fails due to IP address |
163
|
|
|
* is not not allowed by [[ranges]] check. |
164
|
|
|
* |
165
|
|
|
* You may use the following placeholders in the message: |
166
|
|
|
* |
167
|
|
|
* - `{attribute}`: the label of the attribute being validated |
168
|
|
|
* - `{value}`: the value of the attribute being validated |
169
|
|
|
* |
170
|
|
|
* @see ranges |
171
|
|
|
*/ |
172
|
|
|
private string $notInRange = 'Is not in the allowed range.'; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @var array |
176
|
|
|
*/ |
177
|
|
|
private array $ranges = []; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set the IPv4 or IPv6 ranges that are allowed or forbidden. |
181
|
|
|
* |
182
|
|
|
* The following preparation tasks are performed: |
183
|
|
|
* |
184
|
|
|
* - Recursively substitutes aliases (described in [[networks]]) with their values. |
185
|
|
|
* - Removes duplicates |
186
|
|
|
* |
187
|
|
|
* @param array $ranges the IPv4 or IPv6 ranges that are allowed or forbidden. |
188
|
|
|
* |
189
|
|
|
* When the array is empty, or the option not set, all IP addresses are allowed. |
190
|
|
|
* |
191
|
|
|
* Otherwise, the rules are checked sequentially until the first match is found. |
192
|
|
|
* An IP address is forbidden, when it has not matched any of the rules. |
193
|
|
|
* |
194
|
|
|
* Example: |
195
|
|
|
* |
196
|
|
|
* ```php |
197
|
|
|
* (new Ip())->ranges([ |
198
|
|
|
* '192.168.10.128' |
199
|
|
|
* '!192.168.10.0/24', |
200
|
|
|
* 'any' // allows any other IP addresses |
201
|
|
|
* ]); |
202
|
|
|
* ``` |
203
|
|
|
* |
204
|
|
|
* In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the `192.168.10.0/24` subnet. |
205
|
|
|
* IPv4 address `192.168.10.128` is also allowed, because it is listed before the restriction. |
206
|
|
|
*/ |
207
|
8 |
|
public function ranges(array $ranges): self |
208
|
|
|
{ |
209
|
8 |
|
$this->ranges = $this->prepareRanges($ranges); |
210
|
8 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
5 |
|
public function getRanges(): array |
214
|
|
|
{ |
215
|
5 |
|
return $this->ranges; |
216
|
|
|
} |
217
|
|
|
|
218
|
2 |
|
public function allowIpv4(): self |
219
|
|
|
{ |
220
|
2 |
|
$this->allowIpv4 = true; |
221
|
2 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
4 |
|
public function disallowIpv4(): self |
225
|
|
|
{ |
226
|
4 |
|
$this->allowIpv4 = false; |
227
|
4 |
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
public function allowIpv6(): self |
231
|
|
|
{ |
232
|
2 |
|
$this->allowIpv6 = true; |
233
|
2 |
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
2 |
|
public function disallowIpv6(): self |
237
|
|
|
{ |
238
|
2 |
|
$this->allowIpv6 = false; |
239
|
2 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
3 |
|
public function allowNegation(): self |
243
|
|
|
{ |
244
|
3 |
|
$this->allowNegation = true; |
245
|
3 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function disallowNegation(): self |
249
|
|
|
{ |
250
|
|
|
$this->allowNegation = false; |
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
5 |
|
public function allowSubnet(): self |
255
|
|
|
{ |
256
|
5 |
|
$this->allowSubnet = true; |
257
|
5 |
|
$this->requireSubnet = false; |
258
|
5 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
3 |
|
public function requireSubnet(): self |
262
|
|
|
{ |
263
|
3 |
|
$this->allowSubnet = true; |
264
|
3 |
|
$this->requireSubnet = true; |
265
|
3 |
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function disallowSubnet(): self |
269
|
|
|
{ |
270
|
|
|
$this->allowSubnet = false; |
271
|
|
|
$this->requireSubnet = false; |
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
15 |
|
protected function validateValue($value, DataSetInterface $dataSet = null): Result |
276
|
|
|
{ |
277
|
15 |
|
if (!$this->allowIpv4 && !$this->allowIpv6) { |
278
|
1 |
|
throw new \RuntimeException('Both IPv4 and IPv6 checks can not be disabled at the same time'); |
279
|
|
|
} |
280
|
14 |
|
$result = new Result(); |
281
|
14 |
|
if (!is_string($value)) { |
282
|
4 |
|
$result->addError($this->formatMessage($this->message)); |
283
|
4 |
|
return $result; |
284
|
|
|
} |
285
|
|
|
|
286
|
10 |
|
if (preg_match($this->getIpParsePattern(), $value, $matches) === 0) { |
287
|
6 |
|
$result->addError($this->formatMessage($this->message)); |
288
|
6 |
|
return $result; |
289
|
|
|
} |
290
|
7 |
|
$negation = !empty($matches['not'] ?? null); |
291
|
7 |
|
$ip = $matches['ip']; |
292
|
7 |
|
$cidr = $matches['cidr'] ?? null; |
293
|
7 |
|
$ipCidr = $matches['ipCidr']; |
294
|
|
|
|
295
|
|
|
try { |
296
|
7 |
|
$ipVersion = IpHelper::getIpVersion($ip, false); |
297
|
|
|
} catch (\InvalidArgumentException $e) { |
298
|
|
|
$result->addError($this->formatMessage($this->message)); |
299
|
|
|
return $result; |
300
|
|
|
} |
301
|
|
|
|
302
|
7 |
|
if ($this->requireSubnet === true && $cidr === null) { |
303
|
3 |
|
$result->addError($this->formatMessage($this->noSubnet)); |
304
|
3 |
|
return $result; |
305
|
|
|
} |
306
|
7 |
|
if ($this->allowSubnet === false && $cidr !== null) { |
307
|
3 |
|
$result->addError($this->formatMessage($this->hasSubnet)); |
308
|
3 |
|
return $result; |
309
|
|
|
} |
310
|
7 |
|
if ($this->allowNegation === false && $negation) { |
311
|
3 |
|
$result->addError($this->formatMessage($this->message)); |
312
|
3 |
|
return $result; |
313
|
|
|
} |
314
|
7 |
|
if ($ipVersion === IpHelper::IPV6 && !$this->allowIpv6) { |
315
|
1 |
|
$result->addError($this->formatMessage($this->ipv6NotAllowed)); |
316
|
1 |
|
return $result; |
317
|
|
|
} |
318
|
7 |
|
if ($ipVersion === IpHelper::IPV4 && !$this->allowIpv4) { |
319
|
2 |
|
$result->addError($this->formatMessage($this->ipv4NotAllowed)); |
320
|
2 |
|
return $result; |
321
|
|
|
} |
322
|
7 |
|
if (!$result->isValid()) { |
323
|
|
|
return $result; |
324
|
|
|
} |
325
|
7 |
|
if ($cidr !== null) { |
326
|
|
|
try { |
327
|
5 |
|
$cidr = IpHelper::getCidrBits($ipCidr); |
|
|
|
|
328
|
2 |
|
} catch (\InvalidArgumentException $e) { |
329
|
2 |
|
$result->addError($this->formatMessage($this->wrongCidr)); |
330
|
2 |
|
return $result; |
331
|
|
|
} |
332
|
|
|
} |
333
|
7 |
|
if (!$this->isAllowed($ipCidr)) { |
334
|
4 |
|
$result->addError($this->formatMessage($this->notInRange)); |
335
|
4 |
|
return $result; |
336
|
|
|
} |
337
|
|
|
|
338
|
7 |
|
return $result; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* The method checks whether the IP address with specified CIDR is allowed according to the [[ranges]] list. |
343
|
|
|
* |
344
|
|
|
* @see ranges |
345
|
|
|
*/ |
346
|
7 |
|
private function isAllowed(string $ip): bool |
347
|
|
|
{ |
348
|
7 |
|
if (empty($this->ranges)) { |
349
|
3 |
|
return true; |
350
|
|
|
} |
351
|
|
|
|
352
|
4 |
|
foreach ($this->ranges as $string) { |
353
|
4 |
|
[$isNegated, $range] = $this->parseNegatedRange($string); |
354
|
4 |
|
if (IpHelper::inRange($ip, $range)) { |
355
|
4 |
|
return !$isNegated; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
3 |
|
return false; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Parses IP address/range for the negation with [[NEGATION_CHAR]]. |
364
|
|
|
* |
365
|
|
|
* @param $string |
366
|
|
|
* @return array `[0 => bool, 1 => string]` |
367
|
|
|
* - boolean: whether the string is negated |
368
|
|
|
* - string: the string without negation (when the negation were present) |
369
|
|
|
*/ |
370
|
8 |
|
private function parseNegatedRange($string): array |
371
|
|
|
{ |
372
|
8 |
|
$isNegated = strpos($string, static::NEGATION_CHAR) === 0; |
373
|
8 |
|
return [$isNegated, $isNegated ? substr($string, strlen(static::NEGATION_CHAR)) : $string]; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Prepares array to fill in [[ranges]]. |
378
|
|
|
* |
379
|
|
|
* - Recursively substitutes aliases, described in [[networks]] with their values, |
380
|
|
|
* - Removes duplicates. |
381
|
|
|
* |
382
|
|
|
* @param array $ranges |
383
|
|
|
* @return array |
384
|
|
|
* @see networks |
385
|
|
|
*/ |
386
|
8 |
|
private function prepareRanges(array $ranges): array |
387
|
|
|
{ |
388
|
8 |
|
$result = []; |
389
|
8 |
|
foreach ($ranges as $string) { |
390
|
8 |
|
[$isRangeNegated, $range] = $this->parseNegatedRange($string); |
391
|
8 |
|
if (isset($this->networks[$range])) { |
392
|
6 |
|
$replacements = $this->prepareRanges($this->networks[$range]); |
393
|
6 |
|
foreach ($replacements as &$replacement) { |
394
|
6 |
|
[$isReplacementNegated, $replacement] = $this->parseNegatedRange($replacement); |
395
|
6 |
|
$result[] = ($isRangeNegated && !$isReplacementNegated ? static::NEGATION_CHAR : '') . $replacement; |
396
|
|
|
} |
397
|
|
|
} else { |
398
|
8 |
|
$result[] = $string; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
8 |
|
return array_unique($result); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Used to get the Regexp pattern for initial IP address parsing. |
407
|
|
|
* @return string |
408
|
|
|
*/ |
409
|
10 |
|
public function getIpParsePattern(): string |
410
|
|
|
{ |
411
|
10 |
|
return '/^(?<not>' . preg_quote(static::NEGATION_CHAR, '/') . ')?(?<ipCidr>(?<ip>(?:' . IpHelper::IPV4_PATTERN . ')|(?:' . IpHelper::IPV6_PATTERN . '))(?:\/(?<cidr>-?\d+))?)$/'; |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|