From 28fadbb56ddf83ac758cf94b24f02bf874f97ad8 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 00:08:13 +0100 Subject: [PATCH] Use the same proxy options in the cURL and stream transports The cURL transport read the proxy password from proxy.pass and the stream transport from proxy.password, so a configuration that worked with one transport silently sent no proxy credentials with the other. cURL built "host:port" even when either was missing, while the stream transport then silently connected without the proxy. The socket transport ignored the proxy options without any notice. Accept both password option names in both transports, require proxy.host and proxy.port when proxy.enabled is set, and raise a warning in the socket transport, which cannot use a proxy. Co-Authored-By: Claude Opus 5.5 --- Tests/Transport/ProxyOptionsTest.php | 129 +++++++++++++++++++++++++++ Tests/stubs/jhttp_stub.php | 2 + docs/classes/TransportInterface.md | 4 + src/Transport/Curl.php | 9 +- src/Transport/Socket.php | 9 ++ src/Transport/Stream.php | 15 ++-- 6 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 Tests/Transport/ProxyOptionsTest.php diff --git a/Tests/Transport/ProxyOptionsTest.php b/Tests/Transport/ProxyOptionsTest.php new file mode 100644 index 00000000..b3b0da2e --- /dev/null +++ b/Tests/Transport/ProxyOptionsTest.php @@ -0,0 +1,129 @@ +markTestSkipped('The Transport test stub has not been configured'); + } + + return new Uri(\defined('JTEST_HTTP_STUB') ? JTEST_HTTP_STUB : getenv('JTEST_HTTP_STUB')); + } + + public static function proxyCredentialsProvider(): \Generator + { + foreach ([Curl::class, Stream::class] as $transport) { + foreach (['proxy.pass', 'proxy.password'] as $passwordOption) { + yield substr(strrchr($transport, '\\'), 1) . ' with ' . $passwordOption => [$transport, $passwordOption]; + } + } + } + + #[DataProvider('proxyCredentialsProvider')] + public function testRequestIsSentThroughTheProxyWithCredentials(string $transportClass, string $passwordOption): void + { + if (!$transportClass::isSupported()) { + $this->markTestSkipped(sprintf('The "%s" adapter is not supported in this environment.', $transportClass)); + } + + $stub = $this->getStubUri(); + $transport = new $transportClass([ + 'proxy.enabled' => true, + 'proxy.host' => $stub->getHost(), + 'proxy.port' => $stub->getPort() ?: 80, + 'proxy.user' => 'proxyuser', + $passwordOption => 'proxysecret', + ]); + + $target = 'http://proxied.example.invalid' . $stub->getPath(); + $response = $transport->request('GET', new Uri($target)); + $body = json_decode((string) $response->getBody()); + $headers = array_change_key_case((array) $body->headers); + + $this->assertSame($target, $body->request_uri, 'The request did not go through the proxy.'); + $this->assertSame('Basic ' . base64_encode('proxyuser:proxysecret'), $headers['proxy-authorization'] ?? null); + } + + public static function incompleteProxyProvider(): \Generator + { + foreach ([Curl::class, Stream::class] as $transport) { + $name = substr(strrchr($transport, '\\'), 1); + + yield $name . ' without port' => [$transport, ['proxy.enabled' => true, 'proxy.host' => 'proxy.example.invalid']]; + yield $name . ' without host' => [$transport, ['proxy.enabled' => true, 'proxy.port' => 3128]]; + } + } + + #[DataProvider('incompleteProxyProvider')] + public function testIncompleteProxyConfigurationIsRejected(string $transportClass, array $options): void + { + if (!$transportClass::isSupported()) { + $this->markTestSkipped(sprintf('The "%s" adapter is not supported in this environment.', $transportClass)); + } + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The proxy.host and proxy.port options are required when proxy.enabled is set.'); + + (new $transportClass($options))->request('GET', new Uri('http://127.0.0.1:1/')); + } + + public function testSocketTransportWarnsThatItBypassesTheProxy(): void + { + if (!Socket::isSupported()) { + $this->markTestSkipped('The socket transport is not supported in this environment.'); + } + + $stub = $this->getStubUri(); + $warnings = []; + + set_error_handler( + static function (int $severity, string $message) use (&$warnings): bool { + $warnings[] = [$severity, $message]; + + return true; + } + ); + + try { + $response = (new Socket(['proxy.enabled' => true, 'proxy.host' => 'proxy.example.invalid', 'proxy.port' => 3128])) + ->request('GET', $stub); + } finally { + restore_error_handler(); + } + + $this->assertSame(200, $response->getStatusCode()); + $this->assertCount(1, $warnings); + $this->assertSame(E_USER_WARNING, $warnings[0][0]); + $this->assertStringContainsString('does not support proxies', $warnings[0][1]); + } +} diff --git a/Tests/stubs/jhttp_stub.php b/Tests/stubs/jhttp_stub.php index b3b9a98e..e071f92e 100644 --- a/Tests/stubs/jhttp_stub.php +++ b/Tests/stubs/jhttp_stub.php @@ -28,6 +28,8 @@ $response->http_referer = getVar($_SERVER, 'HTTP_REFERER'); +$response->headers = function_exists('getallheaders') ? getallheaders() : []; + $response->get = $_GET; $response->post = $_POST; $response->files = $_FILES; diff --git a/docs/classes/TransportInterface.md b/docs/classes/TransportInterface.md index ffb364a5..e5e25f8f 100644 --- a/docs/classes/TransportInterface.md +++ b/docs/classes/TransportInterface.md @@ -49,6 +49,7 @@ $http = (new HttpFactory())->getHttp([], ['curl', 'stream']); // first suppor > Prefer `curl` or `stream`. `Socket` connects with `fsockopen()`, which accepts no stream context, > so no CA bundle, minimum TLS version or client certificate can be supplied for it. +> It also cannot use a proxy: with `proxy.enabled` set it raises an `E_USER_WARNING` and connects directly. ## Writing a transport @@ -108,6 +109,9 @@ with `getOption($key, $default)` and are transport-specific; the commonly used o | `follow_location` | Curl | Follow redirects, default `true` | | `transport.curl` | Curl | Raw `CURLOPT_*` overrides, applied last | | `stream.certpath` | Stream | CA bundle file or directory | +| `proxy.enabled` | Curl, Stream | Send requests through an HTTP proxy; `proxy.host` and `proxy.port` are then required | +| `proxy.host` / `proxy.port` | Curl, Stream | The proxy; an `InvalidArgumentException` is thrown if either is missing | +| `proxy.user` / `proxy.pass` | Curl, Stream | Basic credentials for the proxy; `proxy.password` is accepted for `proxy.pass` | | `protocolVersion` | Curl | `1.0`, `1.1` or `2.0` | See [the overview](../overview.md#things-to-know-before-you-build-on-this) for the caveats around diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index 88a8b6c8..74077ca2 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -152,10 +152,15 @@ public function request($method, UriInterface $uri, $data = null, array $headers // Proxy configuration if ($this->getOption('proxy.enabled', false)) { - $options[CURLOPT_PROXY] = $this->getOption('proxy.host') . ':' . $this->getOption('proxy.port'); + if (!$this->getOption('proxy.host') || !$this->getOption('proxy.port')) { + throw new \InvalidArgumentException('The proxy.host and proxy.port options are required when proxy.enabled is set.'); + } + + $options[\CURLOPT_PROXY] = $this->getOption('proxy.host') . ':' . (int) $this->getOption('proxy.port'); + // proxy.password is accepted as an alias of proxy.pass if ($user = $this->getOption('proxy.user')) { - $options[CURLOPT_PROXYUSERPWD] = $user . ':' . $this->getOption('proxy.pass'); + $options[\CURLOPT_PROXYUSERPWD] = $user . ':' . $this->getOption('proxy.pass', $this->getOption('proxy.password', '')); } } diff --git a/src/Transport/Socket.php b/src/Transport/Socket.php index 9aedd4c8..c243c082 100644 --- a/src/Transport/Socket.php +++ b/src/Transport/Socket.php @@ -130,6 +130,15 @@ public function request($method, UriInterface $uri, $data = null, array $headers $request[] = $value; } + // This transport cannot use a proxy, so make it visible that the request bypasses the configured one. + if ($this->getOption('proxy.enabled', false)) { + trigger_error( + 'The socket transport does not support proxies; the request is sent without the configured proxy.' + . ' Use the cURL or stream transport.', + E_USER_WARNING + ); + } + // If we have data to send add it to the request payload. if (!empty($data)) { $request[] = null; diff --git a/src/Transport/Stream.php b/src/Transport/Stream.php index ef468ba0..48224e2f 100644 --- a/src/Transport/Stream.php +++ b/src/Transport/Stream.php @@ -96,16 +96,17 @@ public function request( // Add the proxy configuration if enabled if ($this->getOption('proxy.enabled', false)) { - $options['request_fulluri'] = true; - - if ($this->getOption('proxy.host') && $this->getOption('proxy.port')) { - $options['proxy'] = $this->getOption('proxy.host') . ':' . (int)$this->getOption('proxy.port'); + if (!$this->getOption('proxy.host') || !$this->getOption('proxy.port')) { + throw new \InvalidArgumentException('The proxy.host and proxy.port options are required when proxy.enabled is set.'); } - // If authentication details are provided, add those as well - if ($this->getOption('proxy.user') && $this->getOption('proxy.password')) { + $options['request_fulluri'] = true; + $options['proxy'] = 'tcp://' . $this->getOption('proxy.host') . ':' . (int)$this->getOption('proxy.port'); + + // If authentication details are provided, add those as well; proxy.password is accepted as an alias of proxy.pass + if ($user = $this->getOption('proxy.user')) { $headers['Proxy-Authorization'] = 'Basic ' . base64_encode( - $this->getOption('proxy.user') . ':' . $this->getOption('proxy.password') + $user . ':' . $this->getOption('proxy.pass', $this->getOption('proxy.password', '')) ); } }