Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ CHANGE LOG
==========


## V5.1.1 (UPCOMING)

* Fixed authentication being sent to origins other than the base URL (GHSA-jp92-97q7-5p29)
* Stopped authenticating requests redirected from an `http` base URL to `https`


## V5.1 (06/05/2026)

* Added PHP 8.5 support
Expand Down
20 changes: 18 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;

/**
* The Bitbucket API 2.0 client.
Expand Down Expand Up @@ -80,11 +81,14 @@ class Client

private readonly Builder $httpClientBuilder;
private readonly History $responseHistory;
private UriInterface $url;
private ?Authentication $authentication = null;

public function __construct(?Builder $httpClientBuilder = null)
{
$this->httpClientBuilder = $builder = $httpClientBuilder ?? new Builder();
$this->responseHistory = new History();
$this->url = Psr17FactoryDiscovery::findUriFactory()->createUri(self::BASE_URL);

$builder->addPlugin(new ExceptionThrower());
$builder->addPlugin(new HistoryPlugin($this->responseHistory));
Expand Down Expand Up @@ -153,17 +157,29 @@ public function workspaces(string $workspace): Workspaces
*/
public function authenticate(string $method, #[\SensitiveParameter] string $token, #[\SensitiveParameter] ?string $password = null): void
{
$this->authentication = new Authentication($method, $token, $password, $this->url);

$this->getHttpClientBuilder()->removePlugin(Authentication::class);
$this->getHttpClientBuilder()->addPlugin(new Authentication($method, $token, $password));
$this->getHttpClientBuilder()->addPlugin($this->authentication);
}

/**
* Set the base URL.
*/
public function setUrl(string $url): void
{
$this->url = Psr17FactoryDiscovery::findUriFactory()->createUri($url);

$this->httpClientBuilder->removePlugin(AddHostPlugin::class);
$this->httpClientBuilder->addPlugin(new AddHostPlugin(Psr17FactoryDiscovery::findUriFactory()->createUri($url)));
$this->httpClientBuilder->addPlugin(new AddHostPlugin($this->url));

if (null !== $this->authentication) {
$this->authentication = $this->authentication->withUrl($this->url);

// the authentication plugin must run after the host plugin
$this->httpClientBuilder->removePlugin(Authentication::class);
$this->httpClientBuilder->addPlugin($this->authentication);
}
}

/**
Expand Down
32 changes: 30 additions & 2 deletions src/HttpClient/Plugin/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;

/**
* A plugin to add authentication to the request.
Expand All @@ -31,10 +32,20 @@
final class Authentication implements Plugin
{
private readonly string $header;
private UriInterface $url;

public function __construct(string $method, #[\SensitiveParameter] string $token, #[\SensitiveParameter] ?string $password = null)
public function __construct(string $method, #[\SensitiveParameter] string $token, #[\SensitiveParameter] ?string $password, UriInterface $url)
{
$this->header = self::buildAuthorizationHeader($method, $token, $password);
$this->url = $url;
}

public function withUrl(UriInterface $url): self
{
$plugin = clone $this;
$plugin->url = $url;

return $plugin;
}

/**
Expand All @@ -47,11 +58,28 @@ public function __construct(string $method, #[\SensitiveParameter] string $token
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
$request = $request->withHeader('Authorization', $this->header);
if (self::isSameOrigin($request->getUri(), $this->url)) {
$request = $request->withHeader('Authorization', $this->header);
} elseif ($request->getHeaderLine('Authorization') === $this->header) {
// a redirected request already carries it
$request = $request->withoutHeader('Authorization');
}

return $next($request);
}

private static function isSameOrigin(UriInterface $a, UriInterface $b): bool
{
return 0 === \strcasecmp($a->getScheme(), $b->getScheme())
&& 0 === \strcasecmp($a->getHost(), $b->getHost())
&& self::getPort($a) === self::getPort($b);
}

private static function getPort(UriInterface $uri): ?int
{
return $uri->getPort() ?? ['http' => 80, 'https' => 443][\strtolower($uri->getScheme())] ?? null;
}

/**
* Build the authentication header to be attached to the request.
*
Expand Down
103 changes: 103 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
namespace Bitbucket\Tests;

use Bitbucket\Client;
use Bitbucket\HttpClient\Builder;
use GuzzleHttp\Psr7\Response;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Mock\Client as MockClient;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -29,4 +32,104 @@ public function testCreateClient(): void
self::assertInstanceOf(Client::class, $client);
self::assertInstanceOf(HttpMethodsClientInterface::class, $client->getHttpClient());
}

public function testAuthenticationIsSentToTheBaseUrl(): void
{
$httpClient = new MockClient();

$client = new Client(new Builder($httpClient));
$client->authenticate(Client::AUTH_OAUTH_TOKEN, 'my-token');
$client->getHttpClient()->get('/2.0/user');
$client->getHttpClient()->get('https://API.bitbucket.org:443/2.0/repositories?page=2');

$requests = $httpClient->getRequests();

self::assertSame('Bearer my-token', $requests[0]->getHeaderLine('Authorization'));
self::assertSame('Bearer my-token', $requests[1]->getHeaderLine('Authorization'));
}

public function testAuthenticationIsNotSentToOtherOrigins(): void
{
$httpClient = new MockClient();

$client = new Client(new Builder($httpClient));
$client->authenticate(Client::AUTH_HTTP_PASSWORD, 'my-username', 'my-password');
$client->getHttpClient()->get('https://bitbucket.org/my-workspace/my-repo');
$client->getHttpClient()->get('http://api.bitbucket.org/2.0/user');
$client->getHttpClient()->get('https://api.bitbucket.org:8443/2.0/user');
$client->getHttpClient()->get('https://api.bitbucket.org.example.com/2.0/user');

$requests = $httpClient->getRequests();

self::assertCount(4, $requests);

foreach ($requests as $request) {
self::assertFalse($request->hasHeader('Authorization'), (string) $request->getUri());
}
}

public function testAuthenticationIsRemovedFromRequestsToOtherOrigins(): void
{
$httpClient = new MockClient();

$client = new Client(new Builder($httpClient));
$client->authenticate(Client::AUTH_OAUTH_TOKEN, 'my-token');
$client->getHttpClient()->get('https://bitbucket.example.com/2.0/user', ['Authorization' => 'Bearer my-token']);
$client->getHttpClient()->get('https://bitbucket.example.com/2.0/user', ['Authorization' => 'Bearer other-token']);

$requests = $httpClient->getRequests();

self::assertFalse($requests[0]->hasHeader('Authorization'));
self::assertSame('Bearer other-token', $requests[1]->getHeaderLine('Authorization'));
}

public function testAuthenticationFollowsTheBaseUrl(): void
{
$httpClient = new MockClient();

$client = new Client(new Builder($httpClient));
$client->authenticate(Client::AUTH_OAUTH_TOKEN, 'my-token');
$client->setUrl('https://bitbucket.example.com');
$client->getHttpClient()->get('/2.0/user');
$client->getHttpClient()->get('https://api.bitbucket.org/2.0/user');

$requests = $httpClient->getRequests();

self::assertSame('bitbucket.example.com', $requests[0]->getUri()->getHost());
self::assertSame('Bearer my-token', $requests[0]->getHeaderLine('Authorization'));
self::assertFalse($requests[1]->hasHeader('Authorization'));
}

public function testAuthenticationIsSentToTheBaseUrlOnRedirect(): void
{
$httpClient = new MockClient();

$httpClient->addResponse(new Response(302, ['Location' => 'https://api.bitbucket.org/2.0/repositories/my-workspace/my-repo/diff/abc..def']));

$client = new Client(new Builder($httpClient));
$client->authenticate(Client::AUTH_OAUTH_TOKEN, 'my-token');
$client->repositories()->workspaces('my-workspace')->pullRequests('my-repo')->diff('1')->download();

$requests = $httpClient->getRequests();

self::assertSame('/2.0/repositories/my-workspace/my-repo/diff/abc..def', $requests[1]->getUri()->getPath());
self::assertSame('Bearer my-token', $requests[1]->getHeaderLine('Authorization'));
}

public function testAuthenticationIsNotSentToOtherOriginsOnRedirect(): void
{
$httpClient = new MockClient();

$httpClient->addResponse(new Response(302, ['Location' => 'https://bbuseruploads.s3.amazonaws.com/my-file.zip?Signature=abc']));

$client = new Client(new Builder($httpClient));
$client->authenticate(Client::AUTH_OAUTH_TOKEN, 'my-token');
$client->repositories()->workspaces('my-workspace')->downloads('my-repo')->download('my-file.zip');

$requests = $httpClient->getRequests();

self::assertSame('Bearer my-token', $requests[0]->getHeaderLine('Authorization'));
self::assertSame('bbuseruploads.s3.amazonaws.com', $requests[1]->getUri()->getHost());
self::assertFalse($requests[1]->hasHeader('Authorization'));
}
}