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