Skip to content

Commit 9466237

Browse files
committed
bug symfony#22154 [WebProfilerBundle] Normalize whitespace in exceptions passed in headers (curry684)
This PR was merged into the 2.7 branch. Discussion ---------- [WebProfilerBundle] Normalize whitespace in exceptions passed in headers | Q | A | ------------- | --- | Branch? | 2.7 upwards | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#22072 | License | MIT If an exception was thrown with line separators in its message the WebProfiler would cause an exception by passing it through unsanitized into the X-Debug-Error HTTP header. This commit fixes that by replacing all whitespace sequences with a single space in the header. Commits ------- d646790 [WebProfilerBundle] Normalize whitespace in exceptions passed in headers
2 parents dd37126 + d646790 commit 9466237

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function onKernelResponse(FilterResponseEvent $event)
6868
$this->urlGenerator->generate('_profiler', array('token' => $response->headers->get('X-Debug-Token')), UrlGeneratorInterface::ABSOLUTE_URL)
6969
);
7070
} catch (\Exception $e) {
71-
$response->headers->set('X-Debug-Error', get_class($e).': '.$e->getMessage());
71+
$response->headers->set('X-Debug-Error', get_class($e).': '.preg_replace('/\s+/', ' ', $e->getMessage()));
7272
}
7373
}
7474

src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,27 @@ public function testThrowingUrlGenerator()
245245
$this->assertEquals('Exception: foo', $response->headers->get('X-Debug-Error'));
246246
}
247247

248+
public function testThrowingErrorCleanup()
249+
{
250+
$response = new Response();
251+
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
252+
253+
$urlGenerator = $this->getUrlGeneratorMock();
254+
$urlGenerator
255+
->expects($this->once())
256+
->method('generate')
257+
->with('_profiler', array('token' => 'xxxxxxxx'))
258+
->will($this->throwException(new \Exception("This\nmultiline\r\ntabbed text should\tcome out\r on\n \ta single plain\r\nline")))
259+
;
260+
261+
$event = new FilterResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
262+
263+
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, 'bottom', $urlGenerator);
264+
$listener->onKernelResponse($event);
265+
266+
$this->assertEquals('Exception: This multiline tabbed text should come out on a single plain line', $response->headers->get('X-Debug-Error'));
267+
}
268+
248269
protected function getRequestMock($isXmlHttpRequest = false, $requestFormat = 'html', $hasSession = true)
249270
{
250271
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->setMethods(array('getSession', 'isXmlHttpRequest', 'getRequestFormat'))->disableOriginalConstructor()->getMock();

0 commit comments

Comments
 (0)