Issues (4)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Rules/IpVN.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @link https://github.com/phpviet/validation
4
 *
5
 * @copyright (c) PHP Viet
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace PHPViet\Validation\Rules;
10
11
use IPLib\Factory as IpFactory;
12
use IPLib\Address\Type as IpType;
13
use Respect\Validation\Rules\AbstractRule;
14
use IPLib\Address\AddressInterface as IpInterface;
15
use Respect\Validation\Exceptions\ComponentException;
16
17
/**
18
 * @author Vuong Minh <[email protected]>
19
 * @since 1.0.0
20
 */
21
class IpVN extends AbstractRule
22
{
23
    const IPV4 = IpType::T_IPv4;
24
25
    const IPV6 = IpType::T_IPv6;
26
27
    public $version;
28
29
    public function __construct(?int $version = null)
30
    {
31
        if (null !== $version && ! $this->isSupportedVersion($version)) {
32
            throw new ComponentException(sprintf('Only versions %d, %d are supported: %d given', self::IPV4, self::IPV6, $version));
33
        }
34
35
        $this->version = $version;
36
    }
37
38
    public function validate($input): bool
39
    {
40
        if (! $ip = IpFactory::addressFromString($input)) {
41
            return false;
42
        }
43
44
        if (($version = $ip->getAddressType()) !== $this->version && null !== $this->version) {
45
            return false;
46
        }
47
48
        if (! $ranges = $this->getIpRanges($input, $version)) {
49
            return false;
50
        }
51
52
        return $this->validateIpInRange($ip, $ranges);
53
    }
54
55
    protected function getIpRanges(string $ip, int $version): ?array
56
    {
57
        if (self::IPV4 === $version) {
58
            $keys = explode('.', $ip);
59
            $map = static::getIpV4Range();
60
        } else {
61
            $keys = explode(':', $ip);
62
            $map = static::getIpV6Range();
63
        }
64
65
        while (! is_null($key = array_shift($keys))) {
66
            if (isset($map[$key])) {
67
                $map = $map[$key];
68
69
                continue;
70
            }
71
72
            return $map['range'] ?? null;
73
        }
74
75
        return null;
76
    }
77
78
    protected function validateIpInRange(IpInterface $ip, array $ranges): bool
79
    {
80
        foreach ($ranges as $range) {
81
            [$begin, $end] = $range;
0 ignored issues
show
The variable $begin does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $end does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
82
83
            if (($subnet = IpFactory::rangeFromBoundaries($begin, $end)) && $subnet->contains($ip)) {
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
91
    protected static function getIpV4Range(): array
92
    {
93
        static $range = null;
94
95
        if (null === $range) {
96
            $range = require __DIR__.'/../../resources/ip-v4-range.php';
97
        }
98
99
        return $range;
100
    }
101
102
    protected static function getIpV6Range(): array
103
    {
104
        static $range = null;
105
106
        if (null === $range) {
107
            $range = require __DIR__.'/../../resources/ip-v6-range.php';
108
        }
109
110
        return $range;
111
    }
112
113
    protected function isSupportedVersion(int $version): bool
114
    {
115
        return self::IPV4 === $version || self::IPV6 === $version;
116
    }
117
}
118