Skip to content

Instantly share code, notes, and snippets.

@lchrusciel
Created March 15, 2022 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lchrusciel/16d86303a912828172e1e0fe6ba4eb0e to your computer and use it in GitHub Desktop.
Save lchrusciel/16d86303a912828172e1e0fe6ba4eb0e to your computer and use it in GitHub Desktop.
Missing HTTP headers to avoid login forms clickjacking security bug fix
# config/services.yaml
services:
# ...
App\EventListener\XFrameOptionsSubscriber:
tags: ['kernel.event_subscriber']
<?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