Tagged as "cache system" because it involves caching and I could not find an "access system" component.
Problem/Motivation
AccessManager::checkNamedRoute() is mainly used by menus, local tasks, etc. to run a large amount of routes through a check to see if they are accessible. It has the following logic:
try {
$route = $this->routeProvider->getRouteByName($route_name);
// ParamConverterManager relies on the route name and object being
// available from the parameters array.
$parameters[RouteObjectInterface::ROUTE_NAME] = $route_name;
$parameters[RouteObjectInterface::ROUTE_OBJECT] = $route;
$upcasted_parameters = $this->paramConverterManager->convert($parameters + $route->getDefaults());
$route_match = new RouteMatch($route_name, $route, $upcasted_parameters, $parameters);
return $this->check($route_match, $account, NULL, $return_as_object);
}
catch (RouteNotFoundException) {
// Cacheable until extensions change.
$result = AccessResult::forbidden()->addCacheTags(['config:core.extension']);
return $return_as_object ? $result : $result->isAllowed();
}
catch (ParamNotConvertedException) {
// Uncacheable because conversion of the parameter may not have been
// possible due to dynamic circumstances.
$result = AccessResult::forbidden()->setCacheMaxAge(0);
return $return_as_object ? $result : $result->isAllowed();
}
As you can see, there are some issues with the current implementation:
- It only runs one specific route enhancer: ParamConversionEnhancer; so any other route enhancer does not apply. This was more than likely done for performance reasons, but means that any CacheableNotFoundHttpException (or similar) thrown from other enhancers only works when actually visiting the route and does nothing on any page that is trying to build a menu, local tasks, etc. on a cold cache. When thrown from an access check it even breaks the page building the menu.
- It kills caching when a param could not be converted, even though the converter may actually be able to offer cacheable metadata about why that was the case.
- When a route could not be found, it assumes only the enabling of an extension can alleviate that.
So as it stands, there is no way for a route enhancer or access check to cause a route to 404 with cacheable metadata without either breaking menus or making them uncacheable.
A great example of why you'd want this is only allowing certain node types or nodes with a certain flag to have a given page.
E.g.: node/{node}/only-pages-show-this can already work right now, but only if you specify the bundle property for EntityConverter. As you know by now, converters can throw a 404, but they completely kill caching. See #3617758: Local task routes using bundle option have max-age: 0 for any bundles not in the bundle option
Steps to reproduce
- Create a route enhancer that throws a CacheableNotFoundHttpException see that it works on the route itself but that menu links to the route still show.
- Do the same, but from an access check and see that it works on the route but breaks any page trying to build a link to said page.
Proposed resolution
We should probably not want to run every route enhancer as it may kill performance when trying to build a menu and checking which links to show. But in absence of a complete rework, replacing ::checkNamedRoute(), let's at least give param converters and route access checks a way already to have a route 404 with cacheable metadata other than max age 0.
So I suggest we allow ::checkNamedRoute() to also catch CacheableNotFoundHttpException, document it as such, and bubble up the metadata from the exception.
Alternatively, maybe better, we expand ParamNotConvertedException to carry cacheable metadata. Then access checks are still only for access, but param converters can actually cause a 404 without killing caching.
Remaining tasks
Carry out the work, I'll make a start.
User interface changes
Links and local tasks to routes that would 404 will no longer show, which is a good thing.
- A famous example of this is #2778345: Views argument validators aren't taken into account on access checks - menu links and local task point to 403, but other issues have been filed and they always boil down to the same issue.
- Example where things broke: #3032433: Allow section storages to be loaded via routing without loading from the tempstore
- Example where more code will rely on checkNamedRoute(): #3231247: Prevent inaccessible links from being displayed in LinkFormatter
Introduced terminology
API changes
One of both:
- Route access checks and param converters now support CacheableNotFoundHttpException
- ParamNotConvertedException can carry cacheable metadata
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3633769
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
kristiaanvandeneyndeSeems to be fine now. Last pipeline had one random failure, running it again.
Essentially the changes to EntityResourceTestBase prove that we now get cacheable responses rather than uncacheable ones, so that's awesome. I was hoping to see a performance test show some improvement but I suppose our umami setup does not have any menus or local tasks pointing to 404 links.
Comment #4
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #5
kristiaanvandeneyndeThis approach isn't foolproof as it only allows to pass cacheable metadata when something fails. Ideally we can also pass that when something doesn't fail.