Commit afdbe0da authored by alexpott's avatar alexpott

Issue #2472281 by pwolanin: 404/403 responses for non-existing nodes are...

Issue #2472281 by pwolanin: 404/403 responses for non-existing nodes are cached in Page Cache/reverse proxy, are not invalidated when the node is created
parent 8f072976
......@@ -196,6 +196,10 @@ services:
factory_method: get
factory_service: cache_factory
arguments: [discovery]
cache_router_rebuild_subscriber:
class: Drupal\Core\EventSubscriber\CacheRouterRebuildSubscriber
tags:
- { name: event_subscriber }
page_cache_request_policy:
class: Drupal\Core\PageCache\DefaultRequestPolicy
arguments: ['@session_configuration']
......
......@@ -504,6 +504,10 @@ protected function invalidateTagsOnSave($update) {
// listing's filtering requirements. A newly created entity may start to
// appear in listings because it did not exist before.)
$tags = $this->getEntityType()->getListCacheTags();
if ($this->hasLinkTemplate('canonical')) {
// Creating or updating an entity may change a cached 403 or 404 response.
$tags = Cache::mergeTags($tags, ['4xx-response']);
}
if ($update) {
// An existing entity was updated, also invalidate its unique cache tag.
$tags = Cache::mergeTags($tags, $this->getCacheTags());
......
<?php
/**
* @file
* Contains \Drupal\Core\EventSubscriber\CacheRouterRebuildSubscriber.
*/
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Clear cache tags when the router is rebuilt.
*/
class CacheRouterRebuildSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public function onRouterFinished() {
// Requested URLs that formerly gave a 403/404 may now be valid.
Cache::invalidateTags(['4xx-response']);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [];
// Act only when the router rebuild is finished.
$events[RoutingEvents::FINISHED][] = ['onRouterFinished'];
return $events;
}
}
......@@ -138,6 +138,15 @@ public function onRespond(FilterResponseEvent $event) {
if ($access_result instanceof CacheableDependencyInterface) {
$this->updateDrupalCacheHeaders($response, $access_result);
}
// Add a cache tag to any 4xx response.
if ($response->isClientError()) {
$cache_tags = ['4xx-response'];
if ($response->headers->has('X-Drupal-Cache-Tags')) {
$existing_cache_tags = explode(' ', $response->headers->get('X-Drupal-Cache-Tags'));
$cache_tags = Cache::mergeTags($existing_cache_tags, $cache_tags);
}
$response->headers->set('X-Drupal-Cache-Tags', implode(' ', $cache_tags));
}
$is_cacheable = ($this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) && ($this->responsePolicy->check($response, $request) !== ResponsePolicyInterface::DENY);
......
......@@ -10,6 +10,7 @@
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Cache\Cache;
use Drupal\user\Entity\Role;
......@@ -29,7 +30,7 @@ class PageCacheTest extends WebTestBase {
*
* @var array
*/
public static $modules = array('test_page_test', 'system_test');
public static $modules = array('test_page_test', 'system_test', 'entity_test');
protected function setUp() {
parent::setUp();
......@@ -307,6 +308,52 @@ function testPageCacheAnonymousRolePermissions() {
$this->assertNoCacheTag('config:user.role.authenticated');
}
/**
* Tests the 4xx-response cache tag is added and invalidated.
*/
function testPageCacheAnonymous403404() {
$admin_url = Url::fromRoute('system.admin');
$invalid_url = 'foo/' . $this->randomString();
$tests = [
403 => $admin_url,
404 => $invalid_url,
];
foreach ($tests as $code => $content_url) {
// Anonymous user, without permissions.
$this->drupalGet($content_url);
$this->assertResponse($code);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this->assertCacheTag('4xx-response');
$this->drupalGet($content_url);
$this->assertResponse($code);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$entity_values = array(
'name' => $this->randomMachineName(),
'user_id' => 1,
'field_test_text' => array(
0 => array(
'value' => $this->randomString(),
'format' => 'plain_text',
)
),
);
$entity = EntityTest::create($entity_values);
$entity->save();
// Saving an entity clears 4xx cache tag.
$this->drupalGet($content_url);
$this->assertResponse($code);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this->drupalGet($content_url);
$this->assertResponse($code);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
// Rebuilding the router should invalidate the 4xx cache tag.
$this->container->get('router.builder')->rebuild();
$this->drupalGet($content_url);
$this->assertResponse($code);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
}
}
/**
* Tests the omit_vary_cookie setting.
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment