Skip to content

Commit 8d6d0a6

Browse files
committed
Fixed issue with wrong vary key when unsetting cookies on the Symfony request instance
1 parent b914ab6 commit 8d6d0a6

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Psr6Store.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,14 +424,27 @@ public function getVaryKey(array $vary, Request $request)
424424
return self::NON_VARYING_KEY;
425425
}
426426

427+
// Normalize
428+
$vary = array_map('strtolower', $vary);
427429
sort($vary);
428430

429431
$hashData = '';
430432

431433
foreach ($vary as $headerName) {
434+
if ('cookie' === $headerName) {
435+
continue;
436+
}
437+
432438
$hashData .= $headerName.':'.$request->headers->get($headerName);
433439
}
434440

441+
if (\in_array('cookie', $vary, true)) {
442+
$hashData .= 'cookies:';
443+
foreach ($request->cookies->all() as $k => $v) {
444+
$hashData .= $k.'='.$v;
445+
}
446+
}
447+
435448
return hash('sha256', $hashData);
436449
}
437450

tests/Psr6StoreTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Cache\Adapter\RedisAdapter;
1919
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
2020
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
21+
use Symfony\Component\HttpFoundation\Cookie;
2122
use Symfony\Component\HttpFoundation\Request;
2223
use Symfony\Component\HttpFoundation\Response;
2324
use Symfony\Component\Lock\Exception\LockReleasingException;
@@ -334,6 +335,26 @@ public function testRegularLookup()
334335
$this->assertSame('whatever', $result->headers->get('Foobar'));
335336
}
336337

338+
public function testLookupWithVaryOnCookies()
339+
{
340+
// Cookies match
341+
$request = Request::create('https://foobar.com/', 'GET', [], ['Foo' => 'Bar'], [], ['HTTP_COOKIE' => 'Foo=Bar']);
342+
$response = new Response('hello world', 200, ['Vary' => 'Cookie']);
343+
$response->headers->setCookie(Cookie::create('Foo', 'Bar'));
344+
345+
$this->store->write($request, $response);
346+
347+
$result = $this->store->lookup($request);
348+
$this->assertInstanceOf(Response::class, $result);
349+
350+
// Cookies do not match (manually removed on request)
351+
$request = Request::create('https://foobar.com/', 'GET', [], ['Foo' => 'Bar'], [], ['HTTP_COOKIE' => 'Foo=Bar']);
352+
$request->cookies->remove('Foo');
353+
354+
$result = $this->store->lookup($request);
355+
$this->assertNull($result);
356+
}
357+
337358
public function testLookupWithEmptyCache()
338359
{
339360
$request = Request::create('https://foobar.com/');

0 commit comments

Comments
 (0)