vendor/guzzlehttp/guzzle/src/Handler/CurlVersion.php
322 lines · 11446 bytes
1<?php
2
3namespace GuzzleHttp\Handler;
4
5/**
6 * @internal
7 */
8final 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 private const CONNECTION_CAP_VERSION = '7.30.0';
17
18 // CURLOPT_PIPEWAIT exists since libcurl 7.43.0, and multi handles have
19 // multiplexed by default since 7.62.0 - but a 7.65.0-7.65.1 regression
20 // dropped that default, which 7.65.2 restored, so 7.65.2 is the floor at
21 // which PIPEWAIT is reliably effective.
22 private const MULTIPLEX_VERSION = '7.65.2';
23
24 // libcurl's connection matcher refuses to hand a transfer wanting
25 // HTTP/1.x a pooled connection that already negotiated HTTP/2 or newer
26 // from 7.77.0: ConnectionExists() in lib/url.c gained the check between
27 // the 7.76.0 and 7.77.0 releases. The HTTP/2 branch of the check
28 // regressed to a debug log in 8.11.0 (curl commit 433d730) and was
29 // restored in 8.13.0 via the negotiation mask (curl commit db72b8d), so
30 // 8.11.0 through 8.12.1 are vulnerable again.
31 private const HTTP_VERSION_REUSE_MATCH_VERSION = '7.77.0';
32
33 private const HTTP_VERSION_REUSE_MATCH_REGRESSION = '8.11.0';
34
35 private const HTTP_VERSION_REUSE_MATCH_RESTORED = '8.13.0';
36
37 // CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE restricts the ALPN offer to h2 only
38 // since libcurl 8.10.0, and connection reuse matching stopped handing
39 // lower-version connections to prior-knowledge transfers in 8.14.0; below
40 // that, a required request could silently be sent over a reused HTTP/1.1
41 // connection.
42 private const REQUIRED_MULTIPLEX_VERSION = '8.14.0';
43
44 // curl 7.52.0 introduced HTTPS proxy support, advertised by a feature bit
45 // (a build can meet the version yet lack the feature). Earlier libcurl
46 // mishandles an https:// proxy: before 7.50.2 it silently downgrades to a
47 // plaintext HTTP proxy, and 7.50.2 through 7.51 reject it at connect time.
48 private const HTTPS_PROXY_VERSION = '7.52.0';
49
50 private const HANDLER_SHARING_VERSION = '7.35.0';
51
52 private const SSL_SESSION_SHARING_VERSION = '8.6.0';
53
54 // curl 7.57.0 added share-handle connection caches through
55 // CURL_LOCK_DATA_CONNECT; older share objects can only hold DNS, TLS
56 // session, and cookie data, never connections.
57 private const SHARE_CONNECTION_CACHE_VERSION = '7.57.0';
58
59 // curl 7.83.1 added proxy TLS-SRP to the connection-reuse match
60 // (CVE-2022-27782); the proxy client certificate was matched from 7.52.0,
61 // so proxy TLS credentials are trusted from 7.83.1 onwards.
62 private const PROXY_TLS_CREDENTIAL_REUSE_VERSION = '7.83.1';
63
64 // curl 8.19.0 fixed proxy tunnel reuse after credential changes
65 // (CVE-2026-3784), but related proxy credential leak flaws were only
66 // fixed in 8.20.0, so connection reuse is trusted from 8.20.0 onwards.
67 private const PROXY_CREDENTIAL_REUSE_VERSION = '8.20.0';
68
69 // curl 7.69.0 started comparing SOCKS proxy credentials when matching
70 // connections for reuse (curl #4835); older libcurl matches a SOCKS proxy
71 // by type, host, and port only.
72 private const SOCKS_PROXY_CREDENTIAL_REUSE_VERSION = '7.69.0';
73
74 private const PROXY_HEADER_SEPARATION_VERSION = '7.37.0';
75
76 /**
77 * @var array{version: string, features: int}|false|null
78 */
79 private static $versionInfo;
80
81 private function __construct()
82 {
83 }
84
85 public static function supportsCurlHandler(): bool
86 {
87 $version = self::getVersion();
88
89 return $version !== null && \version_compare($version, self::MIN_VERSION, '>=');
90 }
91
92 public static function supportsTls12(): bool
93 {
94 $version = self::getVersion();
95
96 return self::supportsSsl()
97 && \defined('CURL_SSLVERSION_TLSv1_2')
98 && $version !== null
99 && \version_compare($version, self::TLS_12_VERSION, '>=');
100 }
101
102 public static function supportsTls13(): bool
103 {
104 $version = self::getVersion();
105
106 return self::supportsSsl()
107 && \defined('CURL_SSLVERSION_TLSv1_3')
108 && $version !== null
109 && \version_compare($version, self::TLS_13_VERSION, '>=');
110 }
111
112 public static function supportsHttp2(): bool
113 {
114 $versionInfo = self::getVersionInfo();
115
116 return self::supportsTls12()
117 && \defined('CURL_VERSION_HTTP2')
118 && $versionInfo !== null
119 && 0 !== (\CURL_VERSION_HTTP2 & $versionInfo['features']);
120 }
121
122 public static function supportsMultiplex(): bool
123 {
124 $version = self::getVersion();
125
126 return \defined('CURLOPT_PIPEWAIT')
127 && $version !== null
128 && \version_compare($version, self::MULTIPLEX_VERSION, '>=');
129 }
130
131 public static function supportsHttpVersionReuseMatching(): bool
132 {
133 $version = self::getVersion();
134
135 if ($version === null || \version_compare($version, self::HTTP_VERSION_REUSE_MATCH_VERSION, '<')) {
136 return false;
137 }
138
139 return \version_compare($version, self::HTTP_VERSION_REUSE_MATCH_REGRESSION, '<')
140 || \version_compare($version, self::HTTP_VERSION_REUSE_MATCH_RESTORED, '>=');
141 }
142
143 public static function supportsConnectionCaps(): bool
144 {
145 $version = self::getVersion();
146
147 return \defined('CURLMOPT_MAX_HOST_CONNECTIONS')
148 && \defined('CURLMOPT_MAX_TOTAL_CONNECTIONS')
149 && $version !== null
150 && \version_compare($version, self::CONNECTION_CAP_VERSION, '>=');
151 }
152
153 public static function ensureConnectionCapsSupported(string $option): void
154 {
155 if (self::supportsConnectionCaps()) {
156 return;
157 }
158
159 throw new \InvalidArgumentException(\sprintf(
160 'The "%s" option requires PHP cURL support for CURLMOPT_MAX_HOST_CONNECTIONS and CURLMOPT_MAX_TOTAL_CONNECTIONS with libcurl %s or newer.',
161 $option,
162 self::CONNECTION_CAP_VERSION
163 ));
164 }
165
166 public static function supportsRequiredMultiplex(): bool
167 {
168 $version = self::getVersion();
169
170 return \defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE')
171 && $version !== null
172 && self::supportsHttp2()
173 && \version_compare($version, self::REQUIRED_MULTIPLEX_VERSION, '>=');
174 }
175
176 public static function supportsHttpsProxy(): bool
177 {
178 $versionInfo = self::getVersionInfo();
179
180 // CURL_VERSION_HTTPS_PROXY is not defined on every supported PHP
181 // version; fall back to the curl.h bit value.
182 $httpsProxyFeature = \defined('CURL_VERSION_HTTPS_PROXY') ? \CURL_VERSION_HTTPS_PROXY : (1 << 21);
183
184 return $versionInfo !== null
185 && \version_compare($versionInfo['version'], self::HTTPS_PROXY_VERSION, '>=')
186 && 0 !== ($httpsProxyFeature & $versionInfo['features']);
187 }
188
189 public static function supportsNtlm(): bool
190 {
191 $versionInfo = self::getVersionInfo();
192
193 // CURL_VERSION_NTLM is not defined on every supported PHP version; fall
194 // back to the curl.h bit value.
195 $ntlmFeature = \defined('CURL_VERSION_NTLM') ? \CURL_VERSION_NTLM : (1 << 4);
196
197 return \defined('CURLAUTH_NTLM')
198 && $versionInfo !== null
199 && 0 !== ($ntlmFeature & $versionInfo['features']);
200 }
201
202 public static function supportsHandlerSharing(): bool
203 {
204 $version = self::getVersion();
205
206 return $version !== null && \version_compare($version, self::HANDLER_SHARING_VERSION, '>=');
207 }
208
209 public static function ensureHandlerSharingSupported(): void
210 {
211 if (!self::supportsHandlerSharing()) {
212 throw new \InvalidArgumentException(\sprintf(
213 'The "transport_sharing" option requires libcurl %s or higher for cURL share handles.',
214 self::HANDLER_SHARING_VERSION
215 ));
216 }
217 }
218
219 public static function supportsSslSessionSharing(): bool
220 {
221 $version = self::getVersion();
222
223 return self::supportsSsl()
224 && $version !== null
225 && \version_compare($version, self::SSL_SESSION_SHARING_VERSION, '>=');
226 }
227
228 public static function ensureSslSessionSharingSupported(): void
229 {
230 if (!self::supportsSslSessionSharing()) {
231 throw new \InvalidArgumentException(\sprintf(
232 'The "transport_sharing" option requires libcurl %s or higher with SSL support for SSL session sharing.',
233 self::SSL_SESSION_SHARING_VERSION
234 ));
235 }
236 }
237
238 public static function supportsShareConnectionCaches(): bool
239 {
240 $version = self::getVersion();
241
242 // An undetectable libcurl version is treated as capable so the
243 // opaque share safeguards fail closed.
244 return $version === null || \version_compare($version, self::SHARE_CONNECTION_CACHE_VERSION, '>=');
245 }
246
247 public static function supportsProxyTlsCredentialAwareConnectionReuse(): bool
248 {
249 $version = self::getVersion();
250
251 return $version !== null
252 && \version_compare($version, self::PROXY_TLS_CREDENTIAL_REUSE_VERSION, '>=');
253 }
254
255 public static function supportsProxyCredentialAwareConnectionReuse(): bool
256 {
257 $version = self::getVersion();
258
259 return $version !== null
260 && \version_compare($version, self::PROXY_CREDENTIAL_REUSE_VERSION, '>=');
261 }
262
263 public static function supportsSocksProxyCredentialAwareConnectionReuse(): bool
264 {
265 $version = self::getVersion();
266
267 return $version !== null
268 && \version_compare($version, self::SOCKS_PROXY_CREDENTIAL_REUSE_VERSION, '>=');
269 }
270
271 public static function supportsProxyHeaderSeparation(): bool
272 {
273 $version = self::getVersion();
274
275 return $version !== null
276 && \version_compare($version, self::PROXY_HEADER_SEPARATION_VERSION, '>=')
277 && \defined('CURLOPT_PROXYHEADER')
278 && \defined('CURLOPT_HEADEROPT')
279 && \defined('CURLHEADER_SEPARATE');
280 }
281
282 private static function supportsSsl(): bool
283 {
284 $versionInfo = self::getVersionInfo();
285
286 return \defined('CURL_VERSION_SSL')
287 && $versionInfo !== null
288 && 0 !== (\CURL_VERSION_SSL & $versionInfo['features']);
289 }
290
291 public static function getVersion(): ?string
292 {
293 $versionInfo = self::getVersionInfo();
294
295 return $versionInfo === null ? null : $versionInfo['version'];
296 }
297
298 /**
299 * @return array{version: string, features: int}|null
300 */
301 private static function getVersionInfo(): ?array
302 {
303 if (self::$versionInfo === null) {
304 if (!\function_exists('curl_version')) {
305 self::$versionInfo = false;
306 } else {
307 $versionInfo = \curl_version();
308 self::$versionInfo = \is_array($versionInfo)
309 && isset($versionInfo['version'], $versionInfo['features'])
310 && \is_string($versionInfo['version'])
311 && \is_int($versionInfo['features'])
312 ? [
313 'version' => $versionInfo['version'],
314 'features' => $versionInfo['features'],
315 ]
316 : false;
317 }
318 }
319
320 return self::$versionInfo === false ? null : self::$versionInfo;
321 }
322}