Skip to content

Commit 4d3247c

Browse files
Merge branch '3.4' into 4.4
* 3.4: [Validator] Add missing translations of nn locale [HttpKernel] Fix that the `Store` would not save responses with the X-Content-Digest header present [Intl] bump icu 67.1
2 parents 1f23d4d + e7b59be commit 4d3247c

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

HttpCache/Store.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,15 @@ public function write(Request $request, Response $response)
175175
$key = $this->getCacheKey($request);
176176
$storedEnv = $this->persistRequest($request);
177177

178-
// write the response body to the entity store if this is the original response
179-
if (!$response->headers->has('X-Content-Digest')) {
180-
$digest = $this->generateContentDigest($response);
178+
$digest = $this->generateContentDigest($response);
179+
$response->headers->set('X-Content-Digest', $digest);
181180

182-
if (!$this->save($digest, $response->getContent())) {
183-
throw new \RuntimeException('Unable to store the entity.');
184-
}
185-
186-
$response->headers->set('X-Content-Digest', $digest);
181+
if (!$this->save($digest, $response->getContent(), false)) {
182+
throw new \RuntimeException('Unable to store the entity.');
183+
}
187184

188-
if (!$response->headers->has('Transfer-Encoding')) {
189-
$response->headers->set('Content-Length', \strlen($response->getContent()));
190-
}
185+
if (!$response->headers->has('Transfer-Encoding')) {
186+
$response->headers->set('Content-Length', \strlen($response->getContent()));
191187
}
192188

193189
// read existing cache entries, remove non-varying, and add this one to the list
@@ -346,10 +342,14 @@ private function load(string $key): ?string
346342
/**
347343
* Save data for the given key.
348344
*/
349-
private function save(string $key, string $data): bool
345+
private function save(string $key, string $data, bool $overwrite = true): bool
350346
{
351347
$path = $this->getPath($key);
352348

349+
if (!$overwrite && file_exists($path)) {
350+
return true;
351+
}
352+
353353
if (isset($this->locks[$key])) {
354354
$fp = $this->locks[$key];
355355
@ftruncate($fp, 0);

Tests/HttpCache/StoreTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ public function testSetsTheXContentDigestResponseHeaderBeforeStoring()
9797
$this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
9898
}
9999

100+
public function testDoesNotTrustXContentDigestFromUpstream()
101+
{
102+
$response = new Response('test', 200, ['X-Content-Digest' => 'untrusted-from-elsewhere']);
103+
104+
$cacheKey = $this->store->write($this->request, $response);
105+
$entries = $this->getStoreMetadata($cacheKey);
106+
list(, $res) = $entries[0];
107+
108+
$this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
109+
$this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $response->headers->get('X-Content-Digest'));
110+
}
111+
112+
public function testWritesResponseEvenIfXContentDigestIsPresent()
113+
{
114+
// Prime the store
115+
$this->store->write($this->request, new Response('test', 200, ['X-Content-Digest' => 'untrusted-from-elsewhere']));
116+
117+
$response = $this->store->lookup($this->request);
118+
$this->assertNotNull($response);
119+
}
120+
100121
public function testFindsAStoredEntryWithLookup()
101122
{
102123
$this->storeSimpleEntry();

0 commit comments

Comments
 (0)