Skip to content

Commit 4a6e804

Browse files
Add GitLab credential headers to the default cache key
1 parent 89fb17f commit 4a6e804

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [12.1.1] - UPCOMING
99

10+
* Add `PRIVATE-TOKEN`, `JOB-TOKEN`, and `SUDO` to the default cache key headers
1011
* Fix authentication headers being sent to origins other than the base URL (GHSA-9hx4-j425-r37q)
1112
* Stop authenticating requests redirected from an `http` base URL to `https`
1213
* Disable redirect following in Symfony's `Psr18Client` and `HttplugClient`

‎src/HttpClient/Builder.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function removePlugin(string $fqcn): void
140140
public function addCache(CacheItemPoolInterface $cachePool, array $config = []): void
141141
{
142142
if (!isset($config['cache_key_generator'])) {
143-
$config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'Cookie', 'Accept', 'Content-type']);
143+
$config['cache_key_generator'] = new HeaderCacheKeyGenerator(['Authorization', 'PRIVATE-TOKEN', 'JOB-TOKEN', 'SUDO', 'Cookie', 'Accept', 'Content-type']);
144144
}
145145

146146
$this->cachePlugin = CachePlugin::clientCache($cachePool, $this->streamFactory, $config);

‎tests/HttpClient/BuilderTest.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414

1515
namespace Gitlab\Tests\HttpClient;
1616

17+
use Generator;
1718
use Gitlab\HttpClient\Builder;
19+
use GuzzleHttp\Psr7\Response;
1820
use Http\Client\Common\HttpMethodsClientInterface;
1921
use Http\Client\Common\Plugin;
2022
use PHPUnit\Framework\Attributes\Before;
23+
use PHPUnit\Framework\Attributes\DataProvider;
2124
use PHPUnit\Framework\TestCase;
25+
use Psr\Cache\CacheItemInterface;
26+
use Psr\Cache\CacheItemPoolInterface;
2227
use Psr\Http\Client\ClientInterface;
2328
use Psr\Http\Message\RequestFactoryInterface;
2429
use Psr\Http\Message\StreamFactoryInterface;
@@ -64,6 +69,46 @@ public function testRemovePluginShouldInvalidateHttpClient(): void
6469
$this->assertNotSame($client, $this->subject->getHttpClient());
6570
}
6671

72+
#[DataProvider('credentialHeaderProvider')]
73+
public function testCacheKeyShouldIncludeCredentialHeaders(string $header): void
74+
{
75+
$keys = [];
76+
77+
$cachePool = $this->createMock(CacheItemPoolInterface::class);
78+
$cachePool
79+
->expects($this->exactly(2))
80+
->method('getItem')
81+
->willReturnCallback(function (string $key) use (&$keys): CacheItemInterface {
82+
$keys[] = $key;
83+
84+
$cacheItem = $this->createMock(CacheItemInterface::class);
85+
$cacheItem->method('expiresAfter')->willReturnSelf();
86+
87+
return $cacheItem;
88+
});
89+
90+
$httpClient = $this->createMock(ClientInterface::class);
91+
$httpClient
92+
->method('sendRequest')
93+
->willReturnCallback(static function (): Response {
94+
return new Response(200, ['Cache-Control' => 'max-age=60, private']);
95+
});
96+
97+
$builder = new Builder($httpClient);
98+
$builder->addCache($cachePool);
99+
$builder->getHttpClient()->get('https://gitlab.example.com/api/v4/user', [$header => 'first']);
100+
$builder->getHttpClient()->get('https://gitlab.example.com/api/v4/user', [$header => 'second']);
101+
102+
$this->assertNotSame($keys[0], $keys[1]);
103+
}
104+
105+
public static function credentialHeaderProvider(): Generator
106+
{
107+
yield 'PRIVATE-TOKEN' => ['PRIVATE-TOKEN'];
108+
yield 'JOB-TOKEN' => ['JOB-TOKEN'];
109+
yield 'SUDO' => ['SUDO'];
110+
}
111+
67112
public function testHttpClientShouldBeAnHttpMethodsClient(): void
68113
{
69114
$this->assertInstanceOf(HttpMethodsClientInterface::class, $this->subject->getHttpClient());

0 commit comments

Comments
 (0)