-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partially address Turbo-related CSP errors
- Loading branch information
1 parent
cd29274
commit 67da71c
Showing
5 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
51 changes: 51 additions & 0 deletions
51
src/Infrastructure/Security/ContentSecurityPolicy/NonceGenerator.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Security\ContentSecurityPolicy; | ||
|
||
use Nelmio\SecurityBundle\ContentSecurityPolicy\NonceGenerator as ParentNonceGenerator; | ||
use Nelmio\SecurityBundle\ContentSecurityPolicy\NonceGeneratorInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
class NonceGenerator implements NonceGeneratorInterface, EventSubscriberInterface | ||
{ | ||
private ?string $requestNonce = null; | ||
|
||
public function __construct( | ||
private readonly ParentNonceGenerator $parent, | ||
) { | ||
} | ||
|
||
public function generate(): string | ||
{ | ||
if ($this->requestNonce) { | ||
return $this->requestNonce; | ||
} else { | ||
return $this->parent->generate(); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => ['onKernelRequest', 400], | ||
KernelEvents::RESPONSE => 'onKernelResponse', | ||
]; | ||
} | ||
|
||
public function onKernelRequest(RequestEvent $event): void | ||
{ | ||
if ($event->getRequest()->headers->has('X-CSP-Nonce')) { | ||
$this->requestNonce = $event->getRequest()->headers->get('X-CSP-Nonce'); | ||
} | ||
} | ||
|
||
public function onKernelResponse(ResponseEvent $event): void | ||
{ | ||
$this->requestNonce = null; | ||
} | ||
} |
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