1. Go to this page and download the library: Download opctim/symfony-csp-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
opctim / symfony-csp-bundle example snippets
if (random_int(0, 99) < $chance) {
$someService->addReportHeaders();
}
# src/EventSubscriber/ModifyCspHeaderEventSubscriber.php
declare(strict_types=1);
namespace App\EventSubscriber;
use Opctim\CspBundle\Event\AddCspHeaderEvent;
use Opctim\CspBundle\Service\CspHeaderBuilderService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ModifyCspHeaderEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private CspHeaderBuilderService $cspHeaderBuilderService
)
{}
public static function getSubscribedEvents(): array
{
return [
AddCspHeaderEvent::NAME => 'modifyCspHeader'
];
}
public function modifyCspHeader(AddCspHeaderEvent $event): void
{
// Use the request if you like
$request = $event->getRequest();
$cspHeader = $this->cspHeaderBuilderService->build(
[ // alwaysAdd options
...$this->cspHeaderBuilderService->getAlwaysAdd(), // Merge the existing ones...
'some-conditional-always-to-be-added-origin'
],
[ // directive options
...$this->cspHeaderBuilderService->getDirectives(), // Merge the existing ones...
'script-src' => [ // Override something here
'some-conditional-origin'
]
]
);
$cspHeader = str_replace('foo', 'bar', $cspHeader); // Maybe some string transformations here...
$event->setCspHeaderValue($cspHeader); // Set the newly crafted csp header.
// On response, the bundle will add your new CSP header!
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.