Skip to content

EspoCRM – Open Source CRM Application

HTTP Client usage. Support IPv6.

Yurii5223b2a6698f
diff --git a/application/Espo/Core/Utils/Security/HostCheck.php b/application/Espo/Core/Utils/Security/HostCheck.php
index de62140ab9..21d91fc0ec 100644
--- a/application/Espo/Core/Utils/Security/HostCheck.php
+++ b/application/Espo/Core/Utils/Security/HostCheck.php
@@ -103,7 +103,7 @@ class HostCheck
*/
public function getHostIpAddresses(string $host): array
{
- $records = dns_get_record($host, DNS_A);
+ $records = dns_get_record($host, DNS_A + DNS_AAAA);
if (!$records) {
return [];
@@ -112,8 +112,15 @@ class HostCheck
$output = [];
foreach ($records as $record) {
- /** @var ?string $idAddress */
- $idAddress = $record['ip'] ?? null;
+ $type = $record['type'] ?? null;
+
+ if ($type === 'AAAA') {
+ /** @var ?string $idAddress */
+ $idAddress = $record['ipv6'] ?? null;
+ } else {
+ /** @var ?string $idAddress */
+ $idAddress = $record['ip'] ?? null;
+ }
if (!$idAddress) {
continue;
@@ -127,9 +134,14 @@ class HostCheck
/**
* @internal
+ * @noinspection SpellCheckingInspection
*/
public function ipAddressIsNotInternal(string $ipAddress): bool
{
+ if (str_starts_with($ipAddress, '::ffff:')) {
+ $ipAddress = substr($ipAddress, 7);
+ }
+
return (bool) filter_var(
$ipAddress,
FILTER_VALIDATE_IP,
diff --git a/application/Espo/Core/Utils/Security/UrlCheck.php b/application/Espo/Core/Utils/Security/UrlCheck.php
index 8ddd937173..071c1a3ae2 100644
--- a/application/Espo/Core/Utils/Security/UrlCheck.php
+++ b/application/Espo/Core/Utils/Security/UrlCheck.php
@@ -119,15 +119,6 @@ class UrlCheck
return $output;
}
- /**
- * @deprecated Since 9.3.4. Use `isUrlAndNotIternal`.
- * @todo Remove in 9.5.0.
- */
- public function isNotInternalUrl(string $url): bool
- {
- return $this->isUrlAndNotIternal($url);
- }
-
/**
* @param string[] $resolve
* @param string[] $allowed An allowed address list in the `{host}:{port}` format.
diff --git a/application/Espo/Tools/Attachment/UploadUrlService.php b/application/Espo/Tools/Attachment/UploadUrlService.php
index c921a673f6..43b4047ddf 100644
--- a/application/Espo/Tools/Attachment/UploadUrlService.php
+++ b/application/Espo/Tools/Attachment/UploadUrlService.php
@@ -33,35 +33,30 @@ use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\ErrorSilent;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\ForbiddenSilent;
+use Espo\Core\HttpClient\ClientFactory;
+use Espo\Core\HttpClient\Options;
use Espo\Core\Utils\File\MimeType;
use Espo\Core\Utils\Metadata;
use Espo\Core\Utils\Security\UrlCheck;
use Espo\Entities\Attachment as Attachment;
use Espo\ORM\EntityManager;
use Espo\Repositories\Attachment as AttachmentRepository;
+use Espo\Core\HttpClient;
+
+use const PATHINFO_EXTENSION;
class UploadUrlService
{
- private AccessChecker $accessChecker;
- private Metadata $metadata;
- private EntityManager $entityManager;
- private MimeType $mimeType;
- private DetailsObtainer $detailsObtainer;
public function __construct(
- AccessChecker $accessChecker,
- Metadata $metadata,
- EntityManager $entityManager,
- MimeType $mimeType,
- DetailsObtainer $detailsObtainer,
- private UrlCheck $urlCheck
- ) {
- $this->accessChecker = $accessChecker;
- $this->metadata = $metadata;
- $this->entityManager = $entityManager;
- $this->mimeType = $mimeType;
- $this->detailsObtainer = $detailsObtainer;
- }
+ private AccessChecker $accessChecker,
+ private Metadata $metadata,
+ private EntityManager $entityManager,
+ private MimeType $mimeType,
+ private DetailsObtainer $detailsObtainer,
+ private UrlCheck $urlCheck,
+ private ClientFactory $clientFactory,
+ ) {}
/**
* Upload an image from and URL and store as attachment.
@@ -114,86 +109,40 @@ class UploadUrlService
/**
* @param non-empty-string $url
* @return ?array{string, string} A type and contents.
- * @throws ForbiddenSilent
+ * @throws Error
*/
private function getImageDataByUrl(string $url): ?array
{
- $resolve = $this->urlCheck->getCurlResolve($url);
-
- if ($resolve === []) {
- throw new ForbiddenSilent("Could not resolve the host.");
- }
-
- if ($resolve !== null && !$this->urlCheck->validateCurlResolveNotInternal($resolve)) {
- throw new ForbiddenSilent("Forbidden host.");
+ $client = $this->clientFactory->create(
+ new Options(
+ protocols: [HttpClient\Protocol::https, HttpClient\Protocol::http],
+ redirect: new HttpClient\Options\Redirect(
+ allow: false,
+ ),
+ internalHostRestriction: new HttpClient\Options\InternalHostRestriction(
+ restrict: true,
+ ),
+ )
+ );
+
+ $request = HttpClient\RequestCreator::create('GET', $url);
+
+ try {
+ $response = $client->send($request);
+ } catch (HttpClient\Exceptions\SendException $e) {
+ throw new Error(previous: $e);
}
- $type = null;
+ $type = $response->getHeader('Content-Type')[0] ?? null;
- if (!function_exists('curl_init')) {
- return null;
- }
-
- $opts = [];
-
- $httpHeaders = [];
- $httpHeaders[] = 'Expect:';
-
- $opts[\CURLOPT_URL] = $url;
- $opts[\CURLOPT_HTTPHEADER] = $httpHeaders;
- $opts[\CURLOPT_CONNECTTIMEOUT] = 10;
- $opts[\CURLOPT_TIMEOUT] = 10;
- $opts[\CURLOPT_HEADER] = true;
- $opts[\CURLOPT_VERBOSE] = true;
- $opts[\CURLOPT_SSL_VERIFYPEER] = true;
- $opts[\CURLOPT_SSL_VERIFYHOST] = 2;
- $opts[\CURLOPT_RETURNTRANSFER] = true;
- // Prevents Server Side Request Forgery by redirecting to an internal host.
- $opts[\CURLOPT_FOLLOWLOCATION] = false;
- $opts[\CURLOPT_MAXREDIRS] = 2;
- $opts[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V4;
- $opts[\CURLOPT_PROTOCOLS] = \CURLPROTO_HTTPS | \CURLPROTO_HTTP;
- $opts[\CURLOPT_REDIR_PROTOCOLS] = \CURLPROTO_HTTPS;
-
- if ($resolve) {
- $opts[CURLOPT_RESOLVE] = $resolve;
+ if ($type) {
+ $type = trim(explode(';', $type)[0]);
}
- $ch = curl_init();
-
- curl_setopt_array($ch, $opts);
-
- /** @var string|false $response */
- $response = curl_exec($ch);
-
- if ($response === false) {
- return null;
- }
-
- $headerSize = curl_getinfo($ch, \CURLINFO_HEADER_SIZE);
-
- $header = substr($response, 0, $headerSize);
- $body = substr($response, $headerSize);
-
- $headLineList = explode("\n", $header);
-
- foreach ($headLineList as $i => $line) {
- if ($i === 0) {
- continue;
- }
-
- if (strpos(strtolower($line), strtolower('Content-Type:')) === 0) {
- $part = trim(substr($line, 13));
-
- if ($part) {
- $type = trim(explode(";", $part)[0]);
- }
- }
- }
if (!$type) {
/** @var string $extension */
- $extension = preg_replace('#\?.*#', '', pathinfo($url, \PATHINFO_EXTENSION));
+ $extension = preg_replace('#\?.*#', '', pathinfo($url, PATHINFO_EXTENSION));
$type = $this->mimeType->getMimeTypeByExtension($extension);
}
@@ -203,13 +152,13 @@ class UploadUrlService
}
/** @var string[] $imageTypeList */
- $imageTypeList = $this->metadata->get(['app', 'image', 'allowedFileTypeList']) ?? [];
+ $imageTypeList = $this->metadata->get('app.image.allowedFileTypeList') ?? [];
if (!in_array($type, $imageTypeList)) {
return null;
}
- return [$type, $body];
+ return [$type, (string) $response->getBody()];
}
private function getAttachmentRepository(): AttachmentRepository
diff --git a/tests/unit/Espo/Core/Utils/Security/HostCheckTest.php b/tests/unit/Espo/Core/Utils/Security/HostCheckTest.php
index d0500e310a..6ac3cb18ac 100644
--- a/tests/unit/Espo/Core/Utils/Security/HostCheckTest.php
+++ b/tests/unit/Espo/Core/Utils/Security/HostCheckTest.php
@@ -70,4 +70,24 @@ class HostCheckTest extends TestCase
$hostCheck->isHostAndNotInternal('0x7f.1')
);
}
+
+ /**
+ * @noinspection SpellCheckingInspection
+ */
+ public function testIpAddress()
+ {
+ $hostCheck = new HostCheck();
+
+ $this->assertFalse(
+ $hostCheck->ipAddressIsNotInternal('172.20.0.1')
+ );
+
+ $this->assertTrue(
+ $hostCheck->ipAddressIsNotInternal('2606:2800:220:1:248:1893:25c8:1946')
+ );
+
+ $this->assertFalse(
+ $hostCheck->ipAddressIsNotInternal('::ffff:127.0.0.1')
+ );
+ }
}