Debian Codegraph @

vendor/guzzlehttp/guzzle/src/Handler/CurlVersion.php

raw
1 <?php
2
3 namespace GuzzleHttp\Handler;
4
5 /**
6 * @internal
7 */
8 final class CurlVersion
9 {
10 private const MIN_VERSION = '7.21.2';
11
12 private const TLS_12_VERSION = '7.34.0';
13
14 private const TLS_13_VERSION = '7.52.0';
15
16 // curl 7.52.0 introduced HTTPS proxy support, advertised by a feature bit
17 // (a build can meet the version yet lack the feature). Earlier libcurl
18 // mishandles an https:// proxy: before 7.50.2 it silently downgrades to a
19 // plaintext HTTP proxy, and 7.50.2 through 7.51 reject it at connect time.
20 private const HTTPS_PROXY_VERSION = '7.52.0';
21
22 private const HANDLER_SHARING_VERSION = '7.35.0';
23
24 private const SSL_SESSION_SHARING_VERSION = '8.6.0';
25
26 // curl 7.83.1 added proxy TLS-SRP to the connection-reuse match
27 // (CVE-2022-27782); the proxy client certificate was matched from 7.52.0,
28 // so proxy TLS credentials are trusted from 7.83.1 onwards.
29 private const PROXY_TLS_CREDENTIAL_REUSE_VERSION = '7.83.1';
30
31 // curl 8.19.0 fixed proxy tunnel reuse after credential changes
32 // (CVE-2026-3784), but related proxy credential leak flaws were only
33 // fixed in 8.20.0, so connection reuse is trusted from 8.20.0 onwards.
34 private const PROXY_CREDENTIAL_REUSE_VERSION = '8.20.0';
35
36 private const PROXY_HEADER_SEPARATION_VERSION = '7.37.0';
37
38 /**
39 * @var array{version: string, features: int}|false|null
40 */
41 private static $versionInfo;
42
43 private function __construct()
44 {
45 }
46
47 public static function supportsCurlHandler(): bool
48 {
49 $version = self::getVersion();
50
51 return $version !== null && \version_compare($version, self::MIN_VERSION, '>=');
52 }
53
54 public static function supportsTls12(): bool
55 {
56 $version = self::getVersion();
57
58 return self::supportsSsl()
59 && \defined('CURL_SSLVERSION_TLSv1_2')
60 && $version !== null
61 && \version_compare($version, self::TLS_12_VERSION, '>=');
62 }
63
64 public static function supportsTls13(): bool
65 {
66 $version = self::getVersion();
67
68 return self::supportsSsl()
69 && \defined('CURL_SSLVERSION_TLSv1_3')
70 && $version !== null
71 && \version_compare($version, self::TLS_13_VERSION, '>=');
72 }
73
74 public static function supportsHttp2(): bool
75 {
76 $versionInfo = self::getVersionInfo();
77
78 return self::supportsTls12()
79 && \defined('CURL_VERSION_HTTP2')
80 && $versionInfo !== null
81 && 0 !== (\CURL_VERSION_HTTP2 & $versionInfo['features']);
82 }
83
84 public static function supportsHttpsProxy(): bool
85 {
86 $versionInfo = self::getVersionInfo();
87
88 // CURL_VERSION_HTTPS_PROXY is not defined on every supported PHP
89 // version; fall back to the curl.h bit value.
90 $httpsProxyFeature = \defined('CURL_VERSION_HTTPS_PROXY') ? \CURL_VERSION_HTTPS_PROXY : (1 << 21);
91
92 return $versionInfo !== null
93 && \version_compare($versionInfo['version'], self::HTTPS_PROXY_VERSION, '>=')
94 && 0 !== ($httpsProxyFeature & $versionInfo['features']);
95 }
96
97 public static function supportsHandlerSharing(): bool
98 {
99 $version = self::getVersion();
100
101 return $version !== null && \version_compare($version, self::HANDLER_SHARING_VERSION, '>=');
102 }
103
104 public static function ensureHandlerSharingSupported(): void
105 {
106 if (!self::supportsHandlerSharing()) {
107 throw new \InvalidArgumentException(\sprintf(
108 'The "transport_sharing" option requires libcurl %s or higher for cURL share handles.',
109 self::HANDLER_SHARING_VERSION
110 ));
111 }
112 }
113
114 public static function supportsSslSessionSharing(): bool
115 {
116 $version = self::getVersion();
117
118 return self::supportsSsl()
119 && $version !== null
120 && \version_compare($version, self::SSL_SESSION_SHARING_VERSION, '>=');
121 }
122
123 public static function ensureSslSessionSharingSupported(): void
124 {
125 if (!self::supportsSslSessionSharing()) {
126 throw new \InvalidArgumentException(\sprintf(
127 'The "transport_sharing" option requires libcurl %s or higher with SSL support for SSL session sharing.',
128 self::SSL_SESSION_SHARING_VERSION
129 ));
130 }
131 }
132
133 public static function supportsProxyTlsCredentialAwareConnectionReuse(): bool
134 {
135 $version = self::getVersion();
136
137 return $version !== null
138 && \version_compare($version, self::PROXY_TLS_CREDENTIAL_REUSE_VERSION, '>=');
139 }
140
141 public static function supportsProxyCredentialAwareConnectionReuse(): bool
142 {
143 $version = self::getVersion();
144
145 return $version !== null
146 && \version_compare($version, self::PROXY_CREDENTIAL_REUSE_VERSION, '>=');
147 }
148
149 public static function supportsProxyHeaderSeparation(): bool
150 {
151 $version = self::getVersion();
152
153 return $version !== null
154 && \version_compare($version, self::PROXY_HEADER_SEPARATION_VERSION, '>=')
155 && \defined('CURLOPT_PROXYHEADER')
156 && \defined('CURLOPT_HEADEROPT')
157 && \defined('CURLHEADER_SEPARATE');
158 }
159
160 private static function supportsSsl(): bool
161 {
162 $versionInfo = self::getVersionInfo();
163
164 return \defined('CURL_VERSION_SSL')
165 && $versionInfo !== null
166 && 0 !== (\CURL_VERSION_SSL & $versionInfo['features']);
167 }
168
169 public static function getVersion(): ?string
170 {
171 $versionInfo = self::getVersionInfo();
172
173 return $versionInfo === null ? null : $versionInfo['version'];
174 }
175
176 /**
177 * @return array{version: string, features: int}|null
178 */
179 private static function getVersionInfo(): ?array
180 {
181 if (self::$versionInfo === null) {
182 if (!\function_exists('curl_version')) {
183 self::$versionInfo = false;
184 } else {
185 $versionInfo = \curl_version();
186 self::$versionInfo = \is_array($versionInfo)
187 && isset($versionInfo['version'], $versionInfo['features'])
188 && \is_string($versionInfo['version'])
189 && \is_int($versionInfo['features'])
190 ? [
191 'version' => $versionInfo['version'],
192 'features' => $versionInfo['features'],
193 ]
194 : false;
195 }
196 }
197
198 return self::$versionInfo === false ? null : self::$versionInfo;
199 }
200 }