Created
March 15, 2022 11:04
-
-
Save lchrusciel/16d86303a912828172e1e0fe6ba4eb0e to your computer and use it in GitHub Desktop.
Missing HTTP headers to avoid login forms clickjacking security bug fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/services.yaml | |
services: | |
# ... | |
App\EventListener\XFrameOptionsSubscriber: | |
tags: ['kernel.event_subscriber'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/EventListener/XFrameOptionsSubscriber.php | |
namespace App\EventListener | |
final class XFrameOptionsSubscriber implements EventSubscriberInterface | |
{ | |
public static function getSubscribedEvents(): array | |
{ | |
return [ | |
KernelEvents::RESPONSE => 'onKernelResponse', | |
]; | |
} | |
public function onKernelResponse(ResponseEvent $event): void | |
{ | |
if (!$this->isMainRequest($event)) { | |
return; | |
} | |
$response = $event->getResponse(); | |
$response->headers->set('X-Frame-Options', 'sameorigin'); | |
} | |
private function isMainRequest(ResponseEvent $event): bool | |
{ | |
if (\method_exists($event, 'isMainRequest')) { | |
return $event->isMainRequest(); | |
} | |
return $event->isMasterRequest(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment