antonioribeiro / firewall

Firewall package for Laravel applications
BSD 3-Clause "New" or "Revised" License
1.39k stars 163 forks source link

Notice: Undefined offset: 1 in IpAddress::cidrToRange #137

Closed jordanade closed 4 years ago

jordanade commented 5 years ago

/var/app/current/vendor/pragmarx/support/src/IpAddress.php in PragmaRX\Support\IpAddress::cidrToRange at line 232

jordanade commented 5 years ago

Anyone know a solution? This is filling up my logs.

lk77 commented 5 years ago

I have the same error :

ErrorException: Notice: Undefined offset: 1
#68 /vendor/pragmarx/support/src/IpAddress.php(232): PragmaRX\Support\IpAddress::cidrToRange
#67 /vendor/pragmarx/support/src/IpAddress.php(283): PragmaRX\Support\IpAddress::ipV4Valid

And there is no solution, you will have a notice no matter what, this needs to be fixed in the package code.

the problem is here (PragmaRX\Support\IpAddress::ipV4Valid):

try
{
    // if ip is 1.2.3.4/32 then Warning: inet_pton(): Unrecognized  address 1.2.3.4/32
    $isIpAddress = inet_pton($ip) !== false;
}
catch(\Exception $e) {}

try
{
    // if ip is 1.2.3.4 then ErrorException: Notice: Undefined offset: 1
    $isRange = static::cidrToRange($ip);
}
catch(\Exception $e) {}
rydje commented 5 years ago

I had the same issue. To fix it I turned the flag 'enable_range_search' to false in the config file. It's used in the validRange method called from checkSecondaryLists method.

I'm not using cidr notation on my addresses I don't need it for now so I does work for me.

rdpascua commented 5 years ago

I got the same issue above even though I disabled the enable_range_search. The $cidr exploded values only contains one value in array.

/**
 * method cidrToRange.
 * Returns an array of only two IPv4 addresses that have the lowest ip
 * address as the first entry. If you need to check to see if an IPv4
 * address is within range please use the IPisWithinCIDR method above.
 * Usage:
 *     CIDR::cidrToRange("127.0.0.128/25");
 * Result:
 *     array(2) {
 *       "127.0.0.128",
 *       "127.0.0.255",
 *     }
 * @param $cidr string CIDR block
 * @return Array low end of range then high end of range.
 */
public static function cidrToRange($cidr)
{
    if (! preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\/[0-9]{1,2})?$/", $cidr)) {
        return false;
    }

    $range = array();

    $cidr = explode('/', $cidr);

    $range[0] = long2ip((ip2long($cidr[0])) & ((-1 << (32 - (int)$cidr[1]))));

    $range[1] = long2ip((ip2long($cidr[0])) + pow(2, (32 - (int)$cidr[1])) - 1);

    return $range;
}
decowboy commented 5 years ago

I'm experiencing the same issue

decowboy commented 5 years ago

What seems to fix this issue for me is to not use any wildcards. I changed my only firewall entry from 10.*.*.* to 10.0.0.0/8, and I haven't gotten the warning since. I haven't tested it enough to be sure though.

What I do know is my stacktrace shows the warning occurred in: /vendor/pragmarx/support/src/IpAddress.php in PragmaRX\Support\IpAddress::cidrToRange at line 232

$cidr = explode('/', $cidr);
$range[0] = long2ip((ip2long($cidr[0])) & ((-1 << (32 - (int)$cidr[1]))));
rdpascua commented 5 years ago

We'll im not using any wildcards :'( I'm using filter by country

decowboy commented 5 years ago

I created a pull request that should take care of the issue.

nachete1987 commented 5 years ago

I have configured the 'enable_range_search' => false, and continue with this error :(

ErrorException: Notice: Undefined offset: 1

84 /vendor/pragmarx/support/src/IpAddress.php(232): PragmaRX\Support\IpAddress::cidrToRange

83 /vendor/pragmarx/support/src/IpAddress.php(283): PragmaRX\Support\IpAddress::ipV4Valid

82 /vendor/pragmarx/firewall/src/Support/IpAddress.php(20): PragmaRX\Firewall\Support\IpAddress::isValid

I try with ip blacklist, country blacklist and without no blacklist record and always obtain the same error.

nachete1987 commented 5 years ago

I think that the problem is support dependency version. When firewall package is downloaded, old Support version is downloaded because 857e250 commit content is not downloaded.

antonioribeiro commented 4 years ago

It was merged, sorry for the delay.

nachete1987 commented 4 years ago

I dont know what I'm doing wrong but, after "composer update" and with all updated, the error continues. In my code, the cidrToRange function doesn't have the if (if (count($cidr) !== 2) {) statement yet

what I do wrong?

antonioribeiro commented 4 years ago

Unfortunately I cannot reproduce your error. I have just created a new app and it works fine with 'enable_range_search' => false

nachete1987 commented 4 years ago

I think that the problem is composer update doesnt update support package, I dont know why, and the class is inside this package.