HttpClient: свой файл корневых сертификатов у клиента; ядро 1.5.0

This commit is contained in:
rsgrinko committed 2026-10-29 10:37:31 +03:00
1 parent f3bfef9788
commit 96328aeb94
11 files changed
+288 -10

No files matched your search

+1 -1
View File
@@ -172,7 +172,7 @@
"framework/Support/Exports.php": "a3431e65b8041575aabf51ca2ce0ab35508d55c59cb67ead3de97ca0240729f6",
"framework/Support/Filter.php": "d0e64d46147cb664ee45f9e74d82af19b5e936ed3a69ad4d24f792f524e178d8",
"framework/Support/Filters.php": "d706838b31dca302609e17906c6add2d0ac0231cbe2aadcc567d4da297bfdeb0",
"framework/Support/HttpClient.php": "761c504b902e3ddcd24578af0a53625dcf478e284312e62edcadeaeeab0e3c02",
"framework/Support/HttpClient.php": "b582fb5603142a18858f0c00240e69e4579824f29d740c7ad0302b813413ae3b",
"framework/Support/Import.php": "56d7d0cd17d15220b4a9e0762aafda326f583427e8a74fc1f7baf7a9d99b5be6",
"framework/Support/ImportPlan.php": "990edf2878958010c35ff419b52ae62230b15a7c4501fa66836082bbc735fc68",
"framework/Support/ImportReport.php": "9b2e3712b2be7ac3a10a5147dd8d4456117451c14edb85eb476f3773f7a4a374",
+33 -7
View File
@@ -48,6 +48,16 @@ use Closure;
* сертификатов (обычная история на Windows), путь к cacert.pem задаётся
* настройкой HTTP_CA_BUNDLE — иначе любой https-запрос падает на «unable to
* get local issuer certificate».
*
* Сервису со своим удостоверяющим центром, которого нет в общих наборах
* (корень Минцифры у российских банков, внутренний УЦ компании), корневой
* сертификат передаётся третьим аргументом — файлом из самого проекта:
*
* new HttpClient(30, '', APP_ROOT . '/resources/certs/russian_trusted_root_ca.pem');
*
* Тогда этот клиент проверяет сервер только по нему, а HTTP_CA_BUNDLE
* и системное хранилище не участвуют: ставить сертификат в ОС каждого сервера
* не нужно, и он не начинает подтверждать чужие сайты для остального кода.
*/
class HttpClient
{
@@ -64,17 +74,24 @@ class HttpClient
/**
* Что успели отправить, пока подмена была включена.
*
* @var array<int, array{method: string, url: string, body: string, headers: array<int, string>, proxy: string}>
* @var array<int, array{method: string, url: string, body: string, headers: array<int, string>, proxy: string, ca_bundle: string}>
*/
private static array $recorded = [];
/** @var array{type: string, host: string, port: int, user: string, pass: string}|null Разобранный прокси */
private ?array $proxyParts = null;
public function __construct(private int $timeout = 10, private string $proxy = '')
public function __construct(private int $timeout = 10, private string $proxy = '', private string $caBundle = '')
{
$this->timeout = max(1, $this->timeout);
$this->proxy = trim($this->proxy);
$this->timeout = max(1, $this->timeout);
$this->proxy = trim($this->proxy);
$this->caBundle = trim($this->caBundle);
// Нет файла — ошибка сразу: молча откатиться на общий набор значило бы
// либо не достучаться до сервиса, либо проверять его не тем, что просили
if ($this->caBundle !== '' && !is_file($this->caBundle)) {
throw new ProtonException('Нет файла корневых сертификатов: ' . $this->caBundle);
}
// Разбираем сразу: опечатка в адресе прокси должна всплыть при сборке
// клиента, а не тихо отправить запрос мимо прокси
@@ -91,6 +108,15 @@ class HttpClient
return $this->proxy;
}
/**
* По каким корневым сертификатам проверяется сервер: свой файл клиента,
* иначе общий HTTP_CA_BUNDLE, иначе пусто — хранилище самого PHP.
*/
public function caFile(): string
{
return $this->caBundle !== '' ? $this->caBundle : self::caBundle();
}
/**
* Разбирает адрес прокси. Годятся только http и socks5: у curl есть и
* другие типы, но потоки не умеют даже socks5, а разъезжаться по
@@ -258,7 +284,7 @@ class HttpClient
{
$method = strtoupper($method);
self::$recorded[] = ['method' => $method, 'url' => $url, 'body' => $body, 'headers' => $headers, 'proxy' => $this->proxy];
self::$recorded[] = ['method' => $method, 'url' => $url, 'body' => $body, 'headers' => $headers, 'proxy' => $this->proxy, 'ca_bundle' => $this->caBundle];
foreach (self::$fakeResponses as $pattern => $response) {
$pattern = (string) $pattern;
@@ -336,7 +362,7 @@ class HttpClient
}
}
$ca = self::caBundle();
$ca = $this->caFile();
if ($ca !== '') {
curl_setopt($curl, CURLOPT_CAINFO, $ca);
@@ -394,7 +420,7 @@ class HttpClient
$options['http']['request_fulluri'] = strtolower((string) parse_url($url, PHP_URL_SCHEME)) === 'http';
}
$ca = self::caBundle();
$ca = $this->caFile();
if ($ca !== '') {
$options['ssl']['cafile'] = $ca;
+1 -1
View File
@@ -1 +1 @@
1.4.0
1.5.0