diff --git a/CHANGELOG.md b/CHANGELOG.md index 65c6dda3..1289113b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/src/HttpClient/Builder.php b/src/HttpClient/Builder.php index 70e30595..c404784a 100644 --- a/src/HttpClient/Builder.php +++ b/src/HttpClient/Builder.php @@ -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); diff --git a/tests/HttpClient/BuilderTest.php b/tests/HttpClient/BuilderTest.php index 3f083b3a..a6e0b19d 100644 --- a/tests/HttpClient/BuilderTest.php +++ b/tests/HttpClient/BuilderTest.php @@ -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; @@ -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());