diff --git a/core/lib/Drupal/Core/PageCache/RequestPolicyInterface.php b/core/lib/Drupal/Core/PageCache/RequestPolicyInterface.php index b287e60..3a4ef21 100644 --- a/core/lib/Drupal/Core/PageCache/RequestPolicyInterface.php +++ b/core/lib/Drupal/Core/PageCache/RequestPolicyInterface.php @@ -13,7 +13,7 @@ * Defines the interface for request policy implementations. * * The request policy is evaluated in order to determine whether delivery of a - * cached page should be attempted. Calling should do so if static::ALLOW is + * cached page should be attempted. The caller should do so if static::ALLOW is * returned from the check() method. */ interface RequestPolicyInterface { @@ -31,6 +31,16 @@ /** * Determines whether delivery of a cached page should be attempted. * + * Note that the request-policy check runs very early. In particular it is + * not possible to determine the logged in user. Also the current route match + * is not yet present when the check runs. Therefore, request-policy checks + * need to be designed in a way such that they do not depend on any other + * service and only take in account the information present on the incoming + * request. + * + * When matching against the request path, special attention is needed to + * support path prefixes which are often used on multilingual sites. + * * @param \Symfony\Component\HttpFoundation\Request $request * The incoming request object. * diff --git a/core/modules/toolbar/src/PageCache/AllowToolbarPath.php b/core/modules/toolbar/src/PageCache/AllowToolbarPath.php index 47a4989..81d5734 100644 --- a/core/modules/toolbar/src/PageCache/AllowToolbarPath.php +++ b/core/modules/toolbar/src/PageCache/AllowToolbarPath.php @@ -22,15 +22,10 @@ class AllowToolbarPath implements RequestPolicyInterface { * {@inheritdoc} */ public function check(Request $request) { - $path = $request->getPathInfo(); - $toolbar_path_offset = strpos($path, '/toolbar/subtrees/'); - if ($toolbar_path_offset !== FALSE) { - // Also accept paths matching at the first slash in order to cater for - // sites using language prefixes. - $second_slash = strpos($path, '/', 1); - if ($toolbar_path_offset === 0 || $toolbar_path_offset === $second_slash) { - return static::ALLOW; - } + // Note that this regular expression matches the end of pathinfo in order to + // support multilingual sites using path prefixes. + if (preg_match('#/toolbar/subtrees/[^/]+(/[^/]+)?$#', $request->getPathInfo())) { + return static::ALLOW; } } diff --git a/core/modules/toolbar/tests/src/Unit/PageCache/AllowToolbarPathTest.php b/core/modules/toolbar/tests/src/Unit/PageCache/AllowToolbarPathTest.php index a783c71..aef4ec8 100644 --- a/core/modules/toolbar/tests/src/Unit/PageCache/AllowToolbarPathTest.php +++ b/core/modules/toolbar/tests/src/Unit/PageCache/AllowToolbarPathTest.php @@ -51,9 +51,13 @@ public function providerTestAllowToolbarPath() { return [ [NULL, '/'], [NULL, '/other-path?q=/toolbar/subtrees/'], + [NULL, '/toolbar/subtrees/'], + [NULL, '/toolbar/subtrees/some-hash/langcode/additional-stuff'], [RequestPolicyInterface::ALLOW, '/de/toolbar/subtrees/abcd'], - [RequestPolicyInterface::ALLOW, '/en-us/toolbar/subtrees/'], + [RequestPolicyInterface::ALLOW, '/en-us/toolbar/subtrees/xyz'], + [RequestPolicyInterface::ALLOW, '/en-us/toolbar/subtrees/xyz/de'], [RequestPolicyInterface::ALLOW, '/toolbar/subtrees/some-hash'], + [RequestPolicyInterface::ALLOW, '/toolbar/subtrees/some-hash/en'], ]; }