From 5f8efda4e3b255b3b4e075022ff25e219ca22d3c Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 00:17:36 +0100 Subject: [PATCH] Do not write credentials into the caller's URI in the stream transport With the userauth and passwordauth options set, the stream transport called setUser() and setPass() on the Uri object it was given, so the caller's object carried the password afterwards and leaked it into logs, later requests and exception messages. An immutable URI got no credentials at all. Send the credentials as an Authorization header instead, unless the request already has one. Co-Authored-By: Claude Opus 5.5 --- Tests/Transport/StreamCredentialsTest.php | 70 +++++++++++++++++++++++ docs/classes/TransportInterface.md | 2 +- src/Transport/Stream.php | 12 ++-- 3 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 Tests/Transport/StreamCredentialsTest.php diff --git a/Tests/Transport/StreamCredentialsTest.php b/Tests/Transport/StreamCredentialsTest.php new file mode 100644 index 00000000..8873e4c1 --- /dev/null +++ b/Tests/Transport/StreamCredentialsTest.php @@ -0,0 +1,70 @@ +markTestSkipped('The stream transport is not supported in this environment.'); + } + + if (!\defined('JTEST_HTTP_STUB') && getenv('JTEST_HTTP_STUB') == '') { + $this->markTestSkipped('The Transport test stub has not been configured'); + } + + $this->stubUrl = \defined('JTEST_HTTP_STUB') ? JTEST_HTTP_STUB : getenv('JTEST_HTTP_STUB'); + } + + public function testCredentialsAreSentWithoutChangingTheUri(): void + { + $uri = new Uri($this->stubUrl); + $transport = new Stream(['userauth' => 'username', 'passwordauth' => 'secret']); + + $body = json_decode((string) $transport->request('GET', $uri)->getBody()); + + $this->assertSame('username', $body->username); + $this->assertSame('secret', $body->password); + $this->assertSame($this->stubUrl, (string) $uri, 'The credentials must not be written into the URI object.'); + } + + public function testCredentialsAreSentForAnImmutableUri(): void + { + $transport = new Stream(['userauth' => 'username', 'passwordauth' => 'secret']); + + $body = json_decode((string) $transport->request('GET', new UriImmutable($this->stubUrl))->getBody()); + + $this->assertSame('username', $body->username); + $this->assertSame('secret', $body->password); + } +} diff --git a/docs/classes/TransportInterface.md b/docs/classes/TransportInterface.md index ffb364a5..7d49537e 100644 --- a/docs/classes/TransportInterface.md +++ b/docs/classes/TransportInterface.md @@ -104,7 +104,7 @@ with `getOption($key, $default)` and are transport-specific; the commonly used o | Option | Used by | Meaning | |---|---|---| -| `userauth` / `passwordauth` | Curl | HTTP Basic credentials | +| `userauth` / `passwordauth` | Curl, Stream, Socket | HTTP Basic credentials; the stream transport sends them as an `Authorization` header and leaves the URI object unchanged | | `follow_location` | Curl | Follow redirects, default `true` | | `transport.curl` | Curl | Raw `CURLOPT_*` overrides, applied last | | `stream.certpath` | Stream | CA bundle file or directory | diff --git a/src/Transport/Stream.php b/src/Transport/Stream.php index ef468ba0..55c9522c 100644 --- a/src/Transport/Stream.php +++ b/src/Transport/Stream.php @@ -13,7 +13,6 @@ use Joomla\Http\AbstractTransport; use Joomla\Http\Exception\InvalidResponseCodeException; use Joomla\Http\Response; -use Joomla\Uri\Uri; use Joomla\Uri\UriInterface; use Laminas\Diactoros\Stream as StreamResponse; @@ -128,10 +127,13 @@ public function request( $options['header'] = trim($headerString, "\r\n"); } - // Authentication, if needed - if ($uri instanceof Uri && $this->getOption('userauth') && $this->getOption('passwordauth')) { - $uri->setUser($this->getOption('userauth')); - $uri->setPass($this->getOption('passwordauth')); + // Authentication, if needed. Sent as a header, so the credentials do not end up in the caller's URI object. + if ( + $this->getOption('userauth') && $this->getOption('passwordauth') + && !isset(array_change_key_case($headers)['authorization']) + ) { + $authorization = 'Authorization: Basic ' . base64_encode($this->getOption('userauth') . ':' . $this->getOption('passwordauth')); + $options['header'] = isset($options['header']) ? $options['header'] . "\r\n" . $authorization : $authorization; } // Set any custom transport options