8000 [HttpKernel] Make ErrorListener unaware of the event dispatcher. · symfony/symfony@8c8ae6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c8ae6f

Browse files
committed
[HttpKernel] Make ErrorListener unaware of the event dispatcher.
1 parent ecf37dd commit 8c8ae6f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
1616
use Symfony\Component\ErrorHandler\Exception\FlattenException;
17-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1817
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1918
use Symfony\Component\HttpFoundation\Request;
2019
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
2120
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
21+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
2222
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2323
use Symfony\Component\HttpKernel\HttpKernelInterface;
2424
use Symfony\Component\HttpKernel\KernelEvents;
@@ -33,7 +33,7 @@ class ErrorListener implements EventSubscriberInterface
3333
protected $logger;
3434
protected $debug;
3535

36-
public function __construct($controller, LoggerInterface $logger = null, $debug = false)
36+
public function __construct($controller, LoggerInterface $logger = null, bool $debug = false)
3737
{
3838
$this->controller = $controller;
3939
$this->logger = $logger;
@@ -47,7 +47,7 @@ public function logKernelException(ExceptionEvent $event)
4747
$this->logException($event->getThrowable(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
4848
}
4949

50-
public function onKernelException(ExceptionEvent $event, string $eventName = null, EventDispatcherInterface $eventDispatcher = null)
50+
public function onKernelException(ExceptionEvent $event)
5151
{
5252
if (null === $this->controller) {
5353
return;
@@ -79,12 +79,15 @@ public function onKernelException(ExceptionEvent $event, string $eventName = nul
7979

8080
$event->setResponse($response);
8181

82-
if ($this->debug && $eventDispatcher instanceof EventDispatcherInterface) {
83-
$cspRemovalListener = function ($event) use (&$cspRemovalListener, $eventDispatcher) {
84-
$event->getResponse()->headers->remove('Content-Security-Policy');
85-
$eventDispatcher->removeListener(KernelEvents::RESPONSE, $cspRemovalListener);
86-
};
87-
$eventDispatcher->addListener(KernelEvents::RESPONSE, $cspRemovalListener, -128);
82+
if ($this->debug) {
83+
$event->getRequest()->attributes->set('_remove_csp_headers', true);
84+
}
85+
}
86+
87+
public function removeCspHeader(ResponseEvent $event): void
88+
{
89+
if ($event->getRequest()->attributes->get('_remove_csp_headers', false)) {
90+
$event->getResponse()->headers->remove('Content-Security-Policy');
8891
}
8992
}
9093

@@ -114,6 +117,7 @@ public static function getSubscribedEvents(): array
114117
['logKernelException', 0],
115118
['onKernelException', -128],
116119
],
120+
KernelEvents::RESPONSE => ['removeCspHeader', -128],
117121
];
118122
}
119123

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ public function testCSPHeaderIsRemoved()
155155
$dispatcher->dispatch($event, KernelEvents::RESPONSE);
156156

157157
$this->assertFalse($response->headers->has('content-security-policy'), 'CSP header has been removed');
158-
$this->assertFalse($dispatcher->hasListeners(KernelEvents::RESPONSE), 'CSP removal listener has been removed');
159158
}
160159

161160
/**

0 commit comments

Comments
 (0)
0