From 0acc3ea58623188b191924d4dddc8fa2ca75b85f Mon Sep 17 00:00:00 2001
From: Pascal Woerde
Date: Thu, 28 Jul 2022 10:28:49 +0200
Subject: [PATCH] [HttpKernel] Use xxh128 algorithm instead of sha256 for http
cache store key
---
.../Component/HttpFoundation/CHANGELOG.md | 5 +++++
.../Component/HttpKernel/HttpCache/Store.php | 2 +-
.../HttpKernel/Tests/HttpCache/StoreTest.php | 20 +++++++++----------
3 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md
index d78710a3e43f2..8c69bc1dea4a1 100644
--- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md
+++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========
+6.2
+---
+
+ * The HTTP cache store uses the `xxh128` algorithm
+
6.1
---
diff --git a/src/Symfony/Component/HttpKernel/HttpCache/Store.php b/src/Symfony/Component/HttpKernel/HttpCache/Store.php
index c3c662c11d2fd..0309db21a0dd8 100644
--- a/src/Symfony/Component/HttpKernel/HttpCache/Store.php
+++ b/src/Symfony/Component/HttpKernel/HttpCache/Store.php
@@ -226,7 +226,7 @@ public function write(Request $request, Response $response): string
*/
protected function generateContentDigest(Response $response): string
{
- return 'en'.hash('sha256', $response->getContent());
+ return 'en'.hash('xxh128', $response->getContent());
}
/**
diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/StoreTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/StoreTest.php
index 151b32bdd0deb..e215b1ee79843 100644
--- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/StoreTest.php
+++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/StoreTest.php
@@ -94,7 +94,7 @@ public function testSetsTheXContentDigestResponseHeaderBeforeStoring()
$entries = $this->getStoreMetadata($cacheKey);
[, $res] = $entries[0];
- $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
+ $this->assertEquals('en6c78e0e3bd51d358d01e758642b85fb8', $res['x-content-digest'][0]);
}
public function testDoesNotTrustXContentDigestFromUpstream()
@@ -105,8 +105,8 @@ public function testDoesNotTrustXContentDigestFromUpstream()
$entries = $this->getStoreMetadata($cacheKey);
[, $res] = $entries[0];
- $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
- $this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $response->headers->get('X-Content-Digest'));
+ $this->assertEquals('en6c78e0e3bd51d358d01e758642b85fb8', $res['x-content-digest'][0]);
+ $this->assertEquals('en6c78e0e3bd51d358d01e758642b85fb8', $response->headers->get('X-Content-Digest'));
}
public function testWritesResponseEvenIfXContentDigestIsPresent()
@@ -198,7 +198,7 @@ public function testRestoresResponseContentFromEntityStoreWithLookup()
{
$this->storeSimpleEntry();
$response = $this->store->lookup($this->request);
- $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test')), $response->getContent());
+ $this->assertEquals($this->getStorePath('en'.hash('xxh128', 'test')), $response->getContent());
}
public function testInvalidatesMetaAndEntityStoreEntriesWithInvalidate()
@@ -251,9 +251,9 @@ public function testStoresMultipleResponsesForEachVaryCombination()
$res3 = new Response('test 3', 200, ['Vary' => 'Foo Bar']);
$this->store->write($req3, $res3);
- $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
- $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
- $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
+ $this->assertEquals($this->getStorePath('en'.hash('xxh128', 'test 3')), $this->store->lookup($req3)->getContent());
+ $this->assertEquals($this->getStorePath('en'.hash('xxh128', 'test 2')), $this->store->lookup($req2)->getContent());
+ $this->assertEquals($this->getStorePath('en'.hash('xxh128', 'test 1')), $this->store->lookup($req1)->getContent());
$this->assertCount(3, $this->getStoreMetadata($key));
}
@@ -263,17 +263,17 @@ public function testOverwritesNonVaryingResponseWithStore()
$req1 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
$res1 = new Response('test 1', 200, ['Vary' => 'Foo Bar']);
$this->store->write($req1, $res1);
- $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 1')), $this->store->lookup($req1)->getContent());
+ $this->assertEquals($this->getStorePath('en'.hash('xxh128', 'test 1')), $this->store->lookup($req1)->getContent());
$req2 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Bling', 'HTTP_BAR' => 'Bam']);
$res2 = new Response('test 2', 200, ['Vary' => 'Foo Bar']);
$this->store->write($req2, $res2);
- $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 2')), $this->store->lookup($req2)->getContent());
+ $this->assertEquals($this->getStorePath('en'.hash('xxh128', 'test 2')), $this->store->lookup($req2)->getContent());
$req3 = Request::create('/test', 'get', [], [], [], ['HTTP_FOO' => 'Foo', 'HTTP_BAR' => 'Bar']);
$res3 = new Response('test 3', 200, ['Vary' => 'Foo Bar']);
$key = $this->store->write($req3, $res3);
- $this->assertEquals($this->getStorePath('en'.hash('sha256', 'test 3')), $this->store->lookup($req3)->getContent());
+ $this->assertEquals($this->getStorePath('en'.hash('xxh128', 'test 3')), $this->store->lookup($req3)->getContent());
$this->assertCount(2, $this->getStoreMetadata($key));
}