10000 bug #35304 [HttpKernel] Fix that no-cache MUST revalidate with the or… · symfony/symfony@764c91b · GitHub
[go: up one dir, main page]

Skip to content

Commit 764c91b

Browse files
committed
bug #35304 [HttpKernel] Fix that no-cache MUST revalidate with the origin (mpdude)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpKernel] Fix that no-cache MUST revalidate with the origin | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | From [RFC 7234 Section 5.2.2](https://tools.ietf.org/html/rfc7234#section-5.2.2) > The "no-cache" response directive indicates that the response MUST NOT be used to satisfy a subsequent request without successful validation on the origin server. This allows an origin server to prevent a cache from using it to satisfy a request without contacting it, even by caches that have been configured to send stale responses. This is unconditional – the response must be revalidated right away. (`must-revalidate`, to the contrary, requires revalidation only once the response has become stale.) Commits ------- c8bdcb3 Fix that no-cache requires positive validation with the origin, even for fresh responses
2 parents a0b976f + c8bdcb3 commit 764c91b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ protected function lookup(Request $request, $catch = false)
323323
return $this->validate($request, $entry, $catch);
324324
}
325325

326+
if ($entry->headers->hasCacheControlDirective('no-cache')) {
327+
return $this->validate($request, $entry, $catch);
328+
}
329+
326330
$this->record($request, 'fresh');
327331

328332
$entry->headers->set('Age', $entry->getAge());

src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,22 @@ public function testCachesResponsesWithExplicitNoCacheDirective()
443443
$this->assertTrue($this->response->headers->has('Age'));
444444
}
445445

446+
public function testRevalidatesResponsesWithNoCacheDirectiveEvenIfFresh()
447+
{
448+
$this->setNextResponse(200, ['Cache-Control' => 'public, no-cache, max-age=10', 'ETag' => 'some-etag'], 'OK');
449+
$this->request('GET', '/'); // warm the cache
450+
451+
sleep(5);
452+
453+
$this->setNextResponse(304, ['Cache-Control' => 'public, no-cache, max-age=10', 'ETag' => 'some-etag']);
454+
$this->request('GET', '/');
455+
456+
$this->assertHttpKernelIsCalled(); // no-cache -> MUST have revalidated at origin
457+
$this->assertTraceContains('valid');
458+
$this->assertEquals('OK', $this->response->getContent());
459+
$this->assertEquals(0, $this->response->getAge());
460+
}
461+
446462
public function testCachesResponsesWithAnExpirationHeader()
447463
{
448464
$time = \DateTime::createFromFormat('U', time() + 5);

0 commit comments

Comments
 (0)
0