Problem/Motivation
Contrib modules might want to throw an AccessDeniedException to deny access to certain pages depending on certain conditions. This exception frequently takes place in situations where no route has been established (e.g. in a kernel event subscriber etc.). Right now, this leads to an error message "Call to a member function hasRequirement() on null" due to the following code in core/lib/Drupal/Core/EventSubscriber/CsrfExceptionSubscriber.php, on403 method:
if (!$route->hasRequirement('_csrf_token') || empty($route->getOption('_csrf_confirm_form_route'))) {
return;
}
The solution is as simple as this little change:
if (is_null($route) || !$route->hasRequirement('_csrf_token') || empty($route->getOption('_csrf_confirm_form_route'))) {
return;
}
See also: https://www.drupal.org/project/drupal/issues/3511584
Over at this issue, the "ban" module is affected, but the ban module is about to be moved out of core and this feature request is IMHO absolut valid independent of this module. That's why I'm filing a new issue here to make it independent of the ban module and to specifiy that it's not strictly speaking a bug in core, because there are probably no test cases in core after the ban module is removed.
Proposed resolution
See above. Just add "is_null($route) ||"
Remaining tasks
User interface changes
None
Introduced terminology
None
API changes
None
Data model changes
None
Release notes snippet
Don't know if necessary. If at all perhaps the fact that you can now throw AccessDeniedException everywhere. But that's IMHO the expectation anyway.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | core-issue_500_instead_of_404-3464798-4.patch | 907 bytes | pifagor |
Comments
Comment #2
pifagor commentedComment #3
pifagor commentedAdded patch
Comment #4
pifagor commentedComment #5
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #6
cilefen commentedI can't reproduce this bug as written so we need improved steps to reproduce.
/admin/modules.Comment #7
cilefen commentedComment #8
quietone commentedSteps to reproduce have not been supplied in 6 months and @cilefen was not able to reproduce this. Therefore, closing.
If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue starting from "Install Drupal core".
Comment #9
pifagor commentedIt is fixed for Drupal 11 here https://www.drupal.org/project/drupal/issues/3566351 (I note that my solution was 2.5 years before the patch that was made into the Drupal 11 core.), so for version 10 this is still relevant https://git.drupalcode.org/issue/drupal-3566351/-/blob/main/core/lib/Dru...
Comment #10
pifagor commentedComment #11
pifagor commentedComment #12
pifagor commentedComment #13
quietone commentedThis is being resolved in #3566351: Throwing AccessDeniedException without a route causes PHP errors. So, that makes this a duplicate.
In short, that issue will be backported to 10.6.x.