Skip to content

Commit cad6482

Browse files
committed
[HttpClient] Don't store response with authentication headers in shared mode
1 parent 1d99639 commit cad6482

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

CachingHttpClient.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,16 +687,21 @@ private function isServerResponseCacheable(int $statusCode, array $requestHeader
687687
return false;
688688
}
689689

690-
if (
691-
$this->sharedCache
692-
&& !isset($cacheControl['public']) && !isset($cacheControl['s-maxage']) && !isset($cacheControl['must-revalidate'])
693-
&& isset($requestHeaders['authorization'])
694-
) {
695-
return false;
696-
}
690+
if ($this->sharedCache) {
691+
if (
692+
!isset($cacheControl['public']) && !isset($cacheControl['s-maxage']) && !isset($cacheControl['must-revalidate'])
693+
&& isset($requestHeaders['authorization'])
694+
) {
695+
return false;
696+
}
697697

698-
if ($this->sharedCache && isset($cacheControl['private'])) {
699-
return false;
698+
if (isset($cacheControl['private'])) {
699+
return false;
700+
}
701+
702+
if (isset($responseHeaders['authentication-info']) || isset($responseHeaders['set-cookie']) || isset($responseHeaders['www-authenticate'])) {
703+
return false;
704+
}
700705
}
701706

702707
// Conditionals require an explicit expiration

Tests/CachingHttpClientTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,62 @@ public function testAPrivateCacheStoresAResponseWithPrivateDirective()
473473
self::assertSame('foo', $response->getContent());
474474
}
475475

476+
public function testASharedCacheDoesntStoreAResponseWithAuthenticationHeader()
477+
{
478+
$mockClient = new MockHttpClient([
479+
new MockResponse('foo', [
480+
'http_code' => 200,
481+
'response_headers' => [
482+
'Cache-Control' => 'max-age=300',
483+
'Set-Cookie' => 'foo=bar',
484+
],
485+
]),
486+
new MockResponse('bar'),
487+
]);
488+
489+
$client = new CachingHttpClient(
490+
$mockClient,
491+
$this->cacheAdapter,
492+
sharedCache: true,
493+
);
494+
495+
$response = $client->request('GET', 'http://example.com/foo-bar');
496+
self::assertSame(200, $response->getStatusCode());
497+
self::assertSame('foo', $response->getContent());
498+
499+
$response = $client->request('GET', 'http://example.com/foo-bar');
500+
self::assertSame(200, $response->getStatusCode());
501+
self::assertSame('bar', $response->getContent());
502+
}
503+
504+
public function testAPrivateCacheStoresAResponseWithAuthenticationHeader()
505+
{
506+
$mockClient = new MockHttpClient([
507+
new MockResponse('foo', [
508+
'http_code' => 200,
509+
'response_headers' => [
510+
'Cache-Control' => 'max-age=300',
511+
'Set-Cookie' => 'foo=bar',
512+
],
513+
]),
514+
new MockResponse('should not be served'),
515+
]);
516+
517+
$client = new CachingHttpClient(
518+
$mockClient,
519+
$this->cacheAdapter,
520+
sharedCache: false,
521+
);
522+
523+
$response = $client->request('GET', 'http://example.com/foo-bar');
524+
self::assertSame(200, $response->getStatusCode());
525+
self::assertSame('foo', $response->getContent());
526+
527+
$response = $client->request('GET', 'http://example.com/foo-bar');
528+
self::assertSame(200, $response->getStatusCode());
529+
self::assertSame('foo', $response->getContent());
530+
}
531+
476532
public function testCacheMissAfterInvalidation()
477533
{
478534
$mockClient = new MockHttpClient([

0 commit comments

Comments
 (0)