BedrockStreaming/Firewall

ip2long in 32 bit allow all Ip

Adel-E opened this issue · 3 comments

hello

we have encountered an issue with your library.
On a 32bit installation when we try to run the following code. The Ip is always allowed.

use M6Web\Component\Firewall\Firewall;

$whiteList = array( 
    '10.68.112.0/24'
);

$firewall = new Firewall();

$connAllowed = $firewall
    ->setDefaultState(false)
    ->addList($whiteList, 'local', true)
    ->setIpAddress('10.68.20.205')
    ->handle()
;

if (!$connAllowed) {
    echo "Not allowed";
} else {
    echo "Allowed";
}

This is caused by Ip2Long that doesnt work properly on 32bit

Because PHP's integer type is signed, and many IP addresses will result in negative 
integers on 32-bit architectures, you need to use the "%u" formatter of sprintf() or printf() 
to get the string representation of the unsigned IP address.

To solve this issue we need to change in https://github.com/M6Web/Firewall/blob/master/Entry/AbstractIP.php#L68

    return strval(ip2long($long));

//By

    return  sprintf('%u', ip2long($long));

Thank you for this issue. Could you make a pull request ? 😉

Done :)

这个修改之后long2ip() expects parameter 1 to be integer, string given

protected function long2ip($long, $abbr = true) { switch(static::NB_BITS) { case 128: return $this->long2ip6($long, $abbr); default: return strval(long2ip($long)); } }

还原这个修改后正常!~~