EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Better IPv6 host checking
Yuriibae7c8cada69
diff --git a/application/Espo/Core/Utils/Security/HostCheck.php b/application/Espo/Core/Utils/Security/HostCheck.phpindex c3b30b4060..427ce8c531 100644--- a/application/Espo/Core/Utils/Security/HostCheck.php+++ b/application/Espo/Core/Utils/Security/HostCheck.php@@ -141,6 +141,23 @@ class HostCheck$ipAddress = substr($ipAddress, 7);}+ if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {+ $packed = inet_pton($ipAddress);++ if ($packed !== false && strlen($packed) === 16) {+ if (str_starts_with($packed, "\x00\x64\xff\x9b\x00\x00\x00\x00\x00\x00\x00\x00")) {+ // NAT64 64:ff9b::/96+ $ipAddress = inet_ntop(substr($packed, 12, 4));+ } else if ($packed[0] === "\x20" && $packed[1] === "\x02") {+ // 6to4 2002::/16+ $ipAddress = inet_ntop(substr($packed, 2, 4));+ } else if (str_starts_with($packed, "\x20\x01\x00\x00")) {+ // Teredo 2001:0000::/32+ $ipAddress = inet_ntop(substr($packed, 12, 4) ^ "\xff\xff\xff\xff");+ }+ }+ }+return (bool) filter_var($ipAddress,FILTER_VALIDATE_IP,diff --git a/tests/unit/Espo/Core/Utils/Security/HostCheckTest.php b/tests/unit/Espo/Core/Utils/Security/HostCheckTest.phpindex 6b302e0c45..86c4fc7ca9 100644--- a/tests/unit/Espo/Core/Utils/Security/HostCheckTest.php+++ b/tests/unit/Espo/Core/Utils/Security/HostCheckTest.php@@ -30,6 +30,7 @@namespace tests\unit\Espo\Core\Utils\Security;use Espo\Core\Utils\Security\HostCheck;+use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;class HostCheckTest extends TestCase@@ -71,20 +72,36 @@ class HostCheckTest extends TestCase);}- public function testIpAddress()+ public static function ipAddressTestProvider(): array{- $hostCheck = new HostCheck();-- $this->assertFalse(- $hostCheck->ipAddressIsNotInternal('172.20.0.1')- );+ return [+ ['200.1.1.1', true],+ ['172.20.0.1', false],+ ['2606:2800:220:1:248:1893:25c8:1946', true],+ ['::ffff:127.0.0.1', false],+ ['64:ff9b::a00:1', false],+ ['2002:0a00:0001::', false],+ ['2001:0000:4136:e378:8000:0000:f5ff:fffe', false],+ ['2001:4860:4860::8888', true],+ ['2606:4700:4700::1111', true],+ ['2620:fe::fe', true],+ ['2404:6800:4004:80c::200e', true],+ ['2a09:bac1:1234::1', true],+ // documentation+ ['2001:db8:abcd:1234::1', false],+ // documentation+ ['2001:db8::1', false],+ ['fe80::1', false],+ ['fc00::1', false],+ ['::1', false],+ ];+ }- $this->assertTrue(- $hostCheck->ipAddressIsNotInternal('2606:2800:220:1:248:1893:25c8:1946')- );+ #[DataProvider('ipAddressTestProvider')]+ public function testIpAddress(string $ipAddress, bool $expected)+ {+ $hostCheck = new HostCheck();- $this->assertFalse(- $hostCheck->ipAddressIsNotInternal('::ffff:127.0.0.1')- );+ $this->assertEquals($hostCheck->ipAddressIsNotInternal($ipAddress), $expected);}}