[core] allow ip_in_range to match exact IPs

This commit is contained in:
Shish 2024-01-04 15:07:07 +00:00
parent c5395df243
commit e49fcfa0c7
2 changed files with 14 additions and 1 deletions

View file

@ -36,7 +36,11 @@ function array_iunique(array $array): array
*/
function ip_in_range(string $IP, string $CIDR): bool
{
list($net, $mask) = explode("/", $CIDR);
$parts = explode("/", $CIDR);
if(count($parts) == 1) {
$parts[1] = "32";
}
list($net, $mask) = $parts;
$ip_net = ip2long($net);
$ip_mask = ~((1 << (32 - (int)$mask)) - 1);

View file

@ -226,4 +226,13 @@ class PolyfillsTest extends TestCase
stringer(["foo" => "bar", "baz" => [1,2,3], "qux" => ["a" => "b"]])
);
}
public function test_ip_in_range()
{
$this->assertTrue(ip_in_range("1.2.3.4", "1.2.0.0/16"));
$this->assertFalse(ip_in_range("4.3.2.1", "1.2.0.0/16"));
// A single IP should be interpreted as a /32
$this->assertTrue(ip_in_range("1.2.3.4", "1.2.3.4"));
}
}