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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [12.1.1] - UPCOMING

* Add `PRIVATE-TOKEN`, `JOB-TOKEN`, and `SUDO` to the default cache key headers
* Fix authentication headers being sent to origins other than the base URL (GHSA-9hx4-j425-r37q)
* Stop authenticating requests redirected from an `http` base URL to `https`
* Disable redirect following in Symfony's `Psr18Client` and `HttplugClient`
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function removePlugin(string $fqcn): void
public function addCache(CacheItemPoolInterface $cachePool, array $config = []): void
{
if (!isset($config['cache_key_generator'])) {
$config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'Cookie', 'Accept', 'Content-type']);
$config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'PRIVATE-TOKEN', 'JOB-TOKEN', 'SUDO', 'Cookie', 'Accept', 'Content-type']);
}

$this->cachePlugin = CachePlugin::clientCache($cachePool, $this->streamFactory, $config);
Expand Down
45 changes: 45 additions & 0 deletions tests/HttpClient/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

namespace Gitlab\Tests\HttpClient;

use Generator;
use Gitlab\HttpClient\Builder;
use GuzzleHttp\Psr7\Response;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
Expand Down Expand Up @@ -64,6 +69,46 @@ public function testRemovePluginShouldInvalidateHttpClient(): void
$this->assertNotSame($client, $this->subject->getHttpClient());
}

#[DataProvider('credentialHeaderProvider')]
public function testCacheKeyShouldIncludeCredentialHeaders(string $header): void
{
$keys = [];

$cachePool = $this->createMock(CacheItemPoolInterface::class);
$cachePool
->expects($this->exactly(2))
->method('getItem')
->willReturnCallback(function (string $key) use (&$keys): CacheItemInterface {
$keys[] = $key;

$cacheItem = $this->createMock(CacheItemInterface::class);
$cacheItem->method('expiresAfter')->willReturnSelf();

return $cacheItem;
});

$httpClient = $this->createMock(ClientInterface::class);
$httpClient
->method('sendRequest')
->willReturnCallback(static function (): Response {
return new Response(200, ['Cache-Control' => 'max-age=60, private']);
});

$builder = new Builder($httpClient);
$builder->addCache($cachePool);
$builder->getHttpClient()->get('https://gitlab.example.com/api/v4/user', [$header => 'first']);
$builder->getHttpClient()->get('https://gitlab.example.com/api/v4/user', [$header => 'second']);

$this->assertNotSame($keys[0], $keys[1]);
}

public static function credentialHeaderProvider(): Generator
{
yield 'PRIVATE-TOKEN' => ['PRIVATE-TOKEN'];
yield 'JOB-TOKEN' => ['JOB-TOKEN'];
yield 'SUDO' => ['SUDO'];
}

public function testHttpClientShouldBeAnHttpMethodsClient(): void
{
$this->assertInstanceOf(HttpMethodsClientInterface::class, $this->subject->getHttpClient());
Expand Down