Verified Commit 98084d0c authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3626006 DefaultExceptionHtmlSubscriber leaves the router with a detached...

fix: #3626006 DefaultExceptionHtmlSubscriber leaves the router with a detached GET request context after error-page subrequests

By: ptmkenny
By: smustgrave
(cherry picked from commit 3e1711eb)
parent 6d257baa
Loading
Loading
Loading
Loading
Loading
+6 −1
Changes for core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php: 6 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -111,6 +111,7 @@ public function on404(ExceptionEvent $event) {
  protected function makeSubrequest(ExceptionEvent $event, $url, $status_code) {
    $request = $event->getRequest();
    $exception = $event->getThrowable();
    $original_request_context = $this->accessUnawareRouter->getContext();

    try {
      // Reuse the exact same request (so keep the same URL, keep the access
@@ -126,7 +127,7 @@ protected function makeSubrequest(ExceptionEvent $event, $url, $status_code) {
      // The routing to the 404 page should be done as GET request because it is
      // restricted to GET and POST requests only. Otherwise a DELETE request
      // would for example trigger a method not allowed exception.
      $request_context = clone ($this->accessUnawareRouter->getContext());
      $request_context = clone $original_request_context;
      $request_context->setMethod('GET');
      $this->accessUnawareRouter->setContext($request_context);

@@ -168,6 +169,10 @@ protected function makeSubrequest(ExceptionEvent $event, $url, $status_code) {
      $error = Error::decodeException($e);
      $this->logger->log($error['severity_level'], Error::DEFAULT_ERROR_MESSAGE, $error);
    }
    finally {
      // Restore the shared context for later requests.
      $this->accessUnawareRouter->setContext($original_request_context);
    }
  }

}
+8
Changes for core/modules/system/tests/modules/router_test_directory/router_test.routing.yml: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -264,6 +264,14 @@ router_test.string_method_scheme:
  methods: 'GET'
  schemes: 'https'

router_test.post_only:
  path: '/router_test/post-only'
  defaults:
    _controller: '\Drupal\router_test\TestControllers::test1'
  requirements:
    _access: 'TRUE'
  methods: [POST]

router_test.alias:
  alias: 'router_test.1'

+22
Changes for core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -124,6 +124,28 @@ public function testHtml404(): void {
    $this->assertEquals('text/html; charset=utf-8', $response->headers->get('Content-type'));
  }

  /**
   * Tests the router context is restored after showing an HTML error page.
   */
  public function testRouterContextRestoredAfterErrorPage(): void {
    /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
    $kernel = \Drupal::getContainer()->get('http_kernel');

    $request = Request::create('/not-found', 'POST');
    $request->setFormat('html', ['text/html']);
    $this->assertSame(Response::HTTP_NOT_FOUND, $kernel->handle($request)->getStatusCode());

    // The router must reuse the shared context, not the error page's clone.
    $this->assertSame(
      $this->container->get('router.request_context'),
      $this->container->get('router.no_access_checks')->getContext(),
    );

    // POST-only routes should not fall back to GET.
    $response = $kernel->handle(Request::create('/router_test/post-only', 'POST'));
    $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
  }

  /**
   * Tests that the exception response is executed in the original context.
   */