mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-15 01:20:59 +00:00
listener added to finish response with X-Frame-Options sameorigin header
This commit is contained in:
parent
9460236563
commit
0886078036
3 changed files with 81 additions and 0 deletions
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Sylius\Bundle\CoreBundle\EventListener;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
|
||||
final class FinishResponseListener implements EventSubscriberInterface
|
||||
{
|
||||
public function onKernelResponse(ResponseEvent $event): void
|
||||
{
|
||||
if (!$this->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();
|
||||
}
|
||||
}
|
||||
|
|
@ -95,6 +95,10 @@
|
|||
<argument type="service" id="Sylius\Bundle\CoreBundle\EventListener\LocaleAwareListener.inner" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\CoreBundle\EventListener\FinishResponseListener">
|
||||
<tag name="kernel.event_subscriber" />
|
||||
</service>
|
||||
|
||||
<service id="sylius.listener.taxon_deletion" class="Sylius\Bundle\CoreBundle\EventListener\TaxonDeletionListener">
|
||||
<argument type="service" id="session" />
|
||||
<argument type="service" id="sylius.repository.channel" />
|
||||
|
|
|
|||
29
tests/Controller/FinishResponseTest.php
Normal file
29
tests/Controller/FinishResponseTest.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Sylius\Tests\Controller;
|
||||
|
||||
use ApiTestCase\JsonApiTestCase;
|
||||
|
||||
final class FinishResponseTest extends JsonApiTestCase
|
||||
{
|
||||
/** @test */
|
||||
public function it_sets_frame_options_header(): void
|
||||
{
|
||||
$this->client->request('GET', '/');
|
||||
|
||||
$response = $this->client->getResponse();
|
||||
|
||||
$this->assertSame('sameorigin', $response->headers->get('X-Frame-Options'));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue