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.

Comments

pifagor created an issue. See original summary.

pifagor’s picture

Issue summary: View changes
pifagor’s picture

Status: Active » Needs review
StatusFileSize
new0 bytes

Added patch

pifagor’s picture

Version: 10.3.x-dev » 10.4.x-dev
StatusFileSize
new907 bytes
needs-review-queue-bot’s picture

Status: Needs review » Needs work

The 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.)

cilefen’s picture

I can't reproduce this bug as written so we need improved steps to reproduce.

  1. I built a 10.3 site on https://simplytest.me.
  2. I created a non-admin user.
  3. I visited /admin/modules.
  4. The HTTP code is 403.
cilefen’s picture

Status: Needs work » Postponed (maintainer needs more info)
quietone’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)
Issue tags: -Needs merge request

Steps 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".

pifagor’s picture

Version: 10.4.x-dev » 10.6.x-dev
Status: Closed (outdated) » Needs work

It 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...

pifagor’s picture

Issue summary: View changes
pifagor’s picture

Issue summary: View changes
pifagor’s picture

Issue summary: View changes
quietone’s picture

Status: Needs work » Closed (duplicate)

This 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.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.