Skip to content

EspoCRM – Open Source CRM Application

CURL resolve drop IPv6 if IPv4 is available

Yuriiecc4bdff54db
diff --git a/application/Espo/Core/Utils/Security/UrlCheck.php b/application/Espo/Core/Utils/Security/UrlCheck.php
index 071c1a3ae2..c5b7d0ed75 100644
--- a/application/Espo/Core/Utils/Security/UrlCheck.php
+++ b/application/Espo/Core/Utils/Security/UrlCheck.php
@@ -106,10 +106,24 @@ class UrlCheck
$output = [];
+ $hasIpV4 = false;
+
+ foreach ($ipAddresses as $ipAddress) {
+ if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
+ $hasIpV4 = true;
+
+ break;
+ }
+ }
+
foreach ($ipAddresses as $ipAddress) {
$ipPart = $ipAddress;
if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
+ if ($hasIpV4) {
+ continue;
+ }
+
$ipPart = "[$ipPart]";
}
diff --git a/tests/unit/Espo/Core/Utils/Security/UrlCheckTest.php b/tests/unit/Espo/Core/Utils/Security/UrlCheckTest.php
index 95881e112c..dbc76ed7e1 100644
--- a/tests/unit/Espo/Core/Utils/Security/UrlCheckTest.php
+++ b/tests/unit/Espo/Core/Utils/Security/UrlCheckTest.php
@@ -35,7 +35,7 @@ use PHPUnit\Framework\TestCase;
class UrlCheckTest extends TestCase
{
- public function testGetCurlResolve(): void
+ public function testGetCurlResolveDual(): void
{
$hostCheck = $this->createMock(HostCheck::class);
@@ -51,11 +51,35 @@ class UrlCheckTest extends TestCase
->with('test.com')
->willReturn([
'10.0.0.1',
+ '10.0.0.2',
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
]);
$this->assertEquals([
'test.com:443:10.0.0.1',
+ 'test.com:443:10.0.0.2',
+ ], $urlCheck->getCurlResolve($url));
+ }
+
+ public function testGetCurlResolveIpV6Only(): void
+ {
+ $hostCheck = $this->createMock(HostCheck::class);
+
+ $urlCheck = new UrlCheck($hostCheck);
+
+ $url = 'https://test.com';
+
+ $hostCheck->method('isDomainHost')
+ ->with('test.com')
+ ->willReturn(true);
+
+ $hostCheck->method('getHostIpAddresses')
+ ->with('test.com')
+ ->willReturn([
+ '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
+ ]);
+
+ $this->assertEquals([
'test.com:443:[2001:0db8:85a3:0000:0000:8a2e:0370:7334]',
], $urlCheck->getCurlResolve($url));
}