Skip to content

Commit 554bc24

Browse files
committed
bug symfony#25933 Disable CSP header on exception pages only in debug (ostrolucky)
This PR was merged into the 2.7 branch. Discussion ---------- Disable CSP header on exception pages only in debug | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#24772 | License | MIT | Doc PR | Based on a feedback we received, there are situations on production when it's desired to have CSP header in place even if exception occurred. This uses now same condition that is used by ExceptionController in TwigBridge to evaluate if styled exception template is going to be shown, minus `showException` request attribute which don't make sense in this context, because it's used by PreviewController only and in such case this listener isn't triggered. Overriding CSP header via HTML meta tag unfortunately, but not surprisingly, doesn't work. Commits ------- b77538c Disable CSP header on exception pages only in debug
2 parents ba8fb60 + b77538c commit 554bc24

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<tag name="monolog.logger" channel="request" />
130130
<argument>%twig.exception_listener.controller%</argument>
131131
<argument type="service" id="logger" on-invalid="null" />
132+
<argument>%kernel.debug%</argument>
132133
</service>
133134

134135
<service id="twig.controller.exception" class="%twig.controller.exception.class%">

src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ class ExceptionListener implements EventSubscriberInterface
3232
{
3333
protected $controller;
3434
protected $logger;
35+
protected $debug;
3536

36-
public function __construct($controller, LoggerInterface $logger = null)
37+
public function __construct($controller, LoggerInterface $logger = null, $debug = false)
3738
{
3839
$this->controller = $controller;
3940
$this->logger = $logger;
41+
$this->debug = $debug;
4042
}
4143

4244
public function onKernelException(GetResponseForExceptionEvent $event)
@@ -71,7 +73,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
7173

7274
$event->setResponse($response);
7375

74-
if ($eventDispatcher instanceof EventDispatcherInterface) {
76+
if ($this->debug && $eventDispatcher instanceof EventDispatcherInterface) {
7577
$cspRemovalListener = function (FilterResponseEvent $event) use (&$cspRemovalListener, $eventDispatcher) {
7678
$event->getResponse()->headers->remove('Content-Security-Policy');
7779
$eventDispatcher->removeListener(KernelEvents::RESPONSE, $cspRemovalListener);

src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function testCSPHeaderIsRemoved()
134134
return new Response($request->getRequestFormat());
135135
}));
136136

137-
$listener = new ExceptionListener('foo', $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock());
137+
$listener = new ExceptionListener('foo', $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(), true);
138138

139139
$dispatcher->addSubscriber($listener);
140140

0 commit comments

Comments
 (0)