Unverified Commit 841cd847 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3566351 Throwing AccessDeniedException without a route causes PHP errors

By: rgpublic
By: sourav_paul
By: smustgrave
(cherry picked from commit 051f4387)
parent 1400d62c
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ public function on403(ExceptionEvent $event): void {
    $request = $event->getRequest();
    $routeMatch = RouteMatch::createFromRequest($request);
    $route = $routeMatch->getRouteObject();
    if (!$route->hasRequirement('_csrf_token') || empty($route->getOption('_csrf_confirm_form_route'))) {
    if (is_null($route) || !$route->hasRequirement('_csrf_token') || empty($route->getOption('_csrf_confirm_form_route'))) {
      return;
    }
    $event->setResponse(new RedirectResponse(Url::fromRoute($route->getOption('_csrf_confirm_form_route'))->toString()));
+40 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\Core\EventSubscriber;

use Drupal\Core\EventSubscriber\CsrfExceptionSubscriber;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * Tests Drupal\Core\EventSubscriber\CsrfExceptionSubscriber.
 */
#[CoversClass(CsrfExceptionSubscriber::class)]
#[Group('EventSubscriber')]
class CsrfExceptionSubscriberTest extends UnitTestCase {

  /**
   * Tests on403() with no matched route.
   */
  public function testOn403WithNullRouteDoesNothing(): void {
    $subscriber = new CsrfExceptionSubscriber();

    $request = new Request();
    $kernel = $this->createStub(HttpKernelInterface::class);
    $exception = new AccessDeniedHttpException();

    $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception);

    $subscriber->on403($event);

    $this->assertNull($event->getResponse());
  }

}