From 088607803652d77ea80881890f044002d737c84e Mon Sep 17 00:00:00 2001 From: Ernest Warwas Date: Wed, 2 Mar 2022 11:36:57 +0100 Subject: [PATCH] listener added to finish response with X-Frame-Options sameorigin header --- .../EventListener/FinishResponseListener.php | 48 +++++++++++++++++++ .../Resources/config/services/listeners.xml | 4 ++ tests/Controller/FinishResponseTest.php | 29 +++++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/Sylius/Bundle/CoreBundle/EventListener/FinishResponseListener.php create mode 100644 tests/Controller/FinishResponseTest.php diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/FinishResponseListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/FinishResponseListener.php new file mode 100644 index 00000000000..735ff4a88a7 --- /dev/null +++ b/src/Sylius/Bundle/CoreBundle/EventListener/FinishResponseListener.php @@ -0,0 +1,48 @@ +isMainRequest($event)) { + return; + } + + $response = $event->getResponse(); + + $response->headers->set('X-Frame-Options', 'sameorigin'); + } + + public static function getSubscribedEvents() + { + return [ + KernelEvents::RESPONSE => [['onKernelResponse']], + ]; + } + + private function isMainRequest(ResponseEvent $event): bool + { + if (\method_exists($event, 'isMainRequest')) { + return $event->isMainRequest(); + } + + return $event->isMasterRequest(); + } +} diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml index ef39f9b6ba5..9b1b97520b6 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml @@ -95,6 +95,10 @@ + + + + diff --git a/tests/Controller/FinishResponseTest.php b/tests/Controller/FinishResponseTest.php new file mode 100644 index 00000000000..513780469f3 --- /dev/null +++ b/tests/Controller/FinishResponseTest.php @@ -0,0 +1,29 @@ +client->request('GET', '/'); + + $response = $this->client->getResponse(); + + $this->assertSame('sameorigin', $response->headers->get('X-Frame-Options')); + } +}