ExceptionHandlingTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Drupal\KernelTests\Core\Routing;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\KernelTests\KernelTestBase;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. /**
  8. * Tests the exception handling for various cases.
  9. *
  10. * @group Routing
  11. */
  12. class ExceptionHandlingTest extends KernelTestBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public static $modules = ['system', 'router_test'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function setUp() {
  21. parent::setUp();
  22. $this->installEntitySchema('date_format');
  23. }
  24. /**
  25. * Tests on a route with a non-supported HTTP method.
  26. */
  27. public function test405() {
  28. $request = Request::create('/router_test/test15', 'PATCH');
  29. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  30. $kernel = \Drupal::getContainer()->get('http_kernel');
  31. $response = $kernel->handle($request);
  32. $this->assertEqual(Response::HTTP_METHOD_NOT_ALLOWED, $response->getStatusCode());
  33. }
  34. /**
  35. * Tests the exception handling for json and 403 status code.
  36. */
  37. public function testJson403() {
  38. $request = Request::create('/router_test/test15');
  39. $request->query->set('_format', 'json');
  40. $request->setRequestFormat('json');
  41. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  42. $kernel = \Drupal::getContainer()->get('http_kernel');
  43. $response = $kernel->handle($request);
  44. $this->assertEqual($response->getStatusCode(), Response::HTTP_FORBIDDEN);
  45. $this->assertEqual($response->headers->get('Content-type'), 'application/json');
  46. $this->assertEqual('{"message":""}', $response->getContent());
  47. }
  48. /**
  49. * Tests the exception handling for json and 404 status code.
  50. */
  51. public function testJson404() {
  52. $request = Request::create('/not-found');
  53. $request->query->set('_format', 'json');
  54. $request->setRequestFormat('json');
  55. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  56. $kernel = \Drupal::getContainer()->get('http_kernel');
  57. $response = $kernel->handle($request);
  58. $this->assertEqual($response->getStatusCode(), Response::HTTP_NOT_FOUND);
  59. $this->assertEqual($response->headers->get('Content-type'), 'application/json');
  60. $this->assertEqual('{"message":"No route found for \\u0022GET \\/not-found\\u0022"}', $response->getContent());
  61. }
  62. /**
  63. * Tests the exception handling for HTML and 403 status code.
  64. */
  65. public function testHtml403() {
  66. $request = Request::create('/router_test/test15');
  67. $request->setFormat('html', ['text/html']);
  68. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  69. $kernel = \Drupal::getContainer()->get('http_kernel');
  70. $response = $kernel->handle($request)->prepare($request);
  71. $this->assertEqual($response->getStatusCode(), Response::HTTP_FORBIDDEN);
  72. $this->assertEqual($response->headers->get('Content-type'), 'text/html; charset=UTF-8');
  73. }
  74. /**
  75. * Tests the exception handling for HTML and 404 status code.
  76. */
  77. public function testHtml404() {
  78. $request = Request::create('/not-found');
  79. $request->setFormat('html', ['text/html']);
  80. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  81. $kernel = \Drupal::getContainer()->get('http_kernel');
  82. $response = $kernel->handle($request)->prepare($request);
  83. $this->assertEqual($response->getStatusCode(), Response::HTTP_NOT_FOUND);
  84. $this->assertEqual($response->headers->get('Content-type'), 'text/html; charset=UTF-8');
  85. }
  86. /**
  87. * Tests that the exception response is executed in the original context.
  88. */
  89. public function testExceptionResponseGeneratedForOriginalRequest() {
  90. // Test with 404 path pointing to a route that uses '_controller'.
  91. $response = $this->doTest404Route('/router_test/test25');
  92. $this->assertTrue(strpos($response->getContent(), '/not-found') !== FALSE);
  93. // Test with 404 path pointing to a route that uses '_form'.
  94. $response = $this->doTest404Route('/router_test/test26');
  95. $this->assertTrue(strpos($response->getContent(), '<form class="system-logging-settings"') !== FALSE);
  96. // Test with 404 path pointing to a route that uses '_entity_form'.
  97. $response = $this->doTest404Route('/router_test/test27');
  98. $this->assertTrue(strpos($response->getContent(), '<form class="date-format-add-form date-format-form"') !== FALSE);
  99. }
  100. /**
  101. * Sets the given path to use as the 404 page and triggers a 404.
  102. *
  103. * @param string $path
  104. * @return \Drupal\Core\Render\HtmlResponse
  105. *
  106. * @see \Drupal\system\Tests\Routing\ExceptionHandlingTest::testExceptionResponseGeneratedForOriginalRequest()
  107. */
  108. protected function doTest404Route($path) {
  109. $this->config('system.site')->set('page.404', $path)->save();
  110. $request = Request::create('/not-found');
  111. $request->setFormat('html', ['text/html']);
  112. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  113. $kernel = \Drupal::getContainer()->get('http_kernel');
  114. return $kernel->handle($request)->prepare($request);
  115. }
  116. /**
  117. * Tests if exception backtraces are properly escaped when output to HTML.
  118. */
  119. public function testBacktraceEscaping() {
  120. // Enable verbose error logging.
  121. $this->config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->save();
  122. $request = Request::create('/router_test/test17');
  123. $request->setFormat('html', ['text/html']);
  124. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  125. $kernel = \Drupal::getContainer()->get('http_kernel');
  126. $response = $kernel->handle($request)->prepare($request);
  127. $this->assertEqual($response->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
  128. $this->assertEqual($response->headers->get('Content-type'), 'text/plain; charset=UTF-8');
  129. // Test both that the backtrace is properly escaped, and that the unescaped
  130. // string is not output at all.
  131. $this->assertTrue(strpos($response->getContent(), Html::escape('<script>alert(\'xss\')</script>')) !== FALSE);
  132. $this->assertTrue(strpos($response->getContent(), '<script>alert(\'xss\')</script>') === FALSE);
  133. }
  134. /**
  135. * Tests exception message escaping.
  136. */
  137. public function testExceptionEscaping() {
  138. // Enable verbose error logging.
  139. $this->config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)->save();
  140. // Using SafeMarkup::format().
  141. $request = Request::create('/router_test/test24');
  142. $request->setFormat('html', ['text/html']);
  143. /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  144. $kernel = \Drupal::getContainer()->get('http_kernel');
  145. $response = $kernel->handle($request)->prepare($request);
  146. $this->assertEqual($response->getStatusCode(), Response::HTTP_INTERNAL_SERVER_ERROR);
  147. $this->assertEqual($response->headers->get('Content-type'), 'text/plain; charset=UTF-8');
  148. // Test message is properly escaped, and that the unescaped string is not
  149. // output at all.
  150. $this->setRawContent($response->getContent());
  151. $this->assertRaw(Html::escape('Escaped content: <p> <br> <h3>'));
  152. $this->assertNoRaw('<p> <br> <h3>');
  153. $string = '<script>alert(123);</script>';
  154. $request = Request::create('/router_test/test2?_format=json' . urlencode($string), 'GET');
  155. $kernel = \Drupal::getContainer()->get('http_kernel');
  156. $response = $kernel->handle($request)->prepare($request);
  157. // As the Content-type is text/plain the fact that the raw string is
  158. // contained in the output would not matter, but because it is output by the
  159. // final exception subscriber, it is printed as partial HTML, and hence
  160. // escaped.
  161. $this->assertEqual($response->headers->get('Content-type'), 'text/plain; charset=UTF-8');
  162. $this->assertStringStartsWith('The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException</em>: Not acceptable format: json&lt;script&gt;alert(123);&lt;/script&gt; in <em class="placeholder">', $response->getContent());
  163. }
  164. }