Skip to content

Commit 67de9e8

Browse files
authored
bug #14 [Security] Clickjacking vulnerability fixed (ernestWarwas)
This PR was merged into the 1.9 branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? | 1.9 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | License | MIT There was a possibility to load a page within an iframe which is enabling to the possibility to perform a clickjacking attack. <!-- - Bug fixes must be submitted against the 1.10 or 1.11 branch(the lowest possible) - Features and deprecations must be submitted against the master branch - Make sure that the correct base branch is set To be sure you are not breaking any Backward Compatibilities, check the documentation: https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html --> Commits ------- 0886078 listener added to finish response with X-Frame-Options sameorigin header c236431 suggested review changes
2 parents 9460236 + c236431 commit 67de9e8

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\CoreBundle\EventListener;
15+
16+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
18+
use Symfony\Component\HttpKernel\KernelEvents;
19+
20+
final class XFrameOptionsSubscriber implements EventSubscriberInterface
21+
{
22+
public static function getSubscribedEvents(): array
23+
{
24+
return [
25+
KernelEvents::RESPONSE => 'onKernelResponse',
26+
];
27+
}
28+
29+
public function onKernelResponse(ResponseEvent $event): void
30+
{
31+
if (!$this->isMainRequest($event)) {
32+
return;
33+
}
34+
35+
$response = $event->getResponse();
36+
37+
$response->headers->set('X-Frame-Options', 'sameorigin');
38+
}
39+
40+
private function isMainRequest(ResponseEvent $event): bool
41+
{
42+
if (\method_exists($event, 'isMainRequest')) {
43+
return $event->isMainRequest();
44+
}
45+
46+
return $event->isMasterRequest();
47+
}
48+
}

src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
<argument type="service" id="Sylius\Bundle\CoreBundle\EventListener\LocaleAwareListener.inner" />
9696
</service>
9797

98+
<service id="Sylius\Bundle\CoreBundle\EventListener\XFrameOptionsSubscriber">
99+
<tag name="kernel.event_subscriber" />
100+
</service>
101+
98102
<service id="sylius.listener.taxon_deletion" class="Sylius\Bundle\CoreBundle\EventListener\TaxonDeletionListener">
99103
<argument type="service" id="session" />
100104
<argument type="service" id="sylius.repository.channel" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Tests\Controller;
15+
16+
use ApiTestCase\JsonApiTestCase;
17+
18+
final class XFrameOptionsTest extends JsonApiTestCase
19+
{
20+
/** @test */
21+
public function it_sets_frame_options_header(): void
22+
{
23+
$this->client->request('GET', '/');
24+
25+
$response = $this->client->getResponse();
26+
27+
$this->assertSame('sameorigin', $response->headers->get('X-Frame-Options'));
28+
}
29+
}

0 commit comments

Comments
 (0)