PHP code example of magentix / magento-csp

1. Go to this page and download the library: Download magentix/magento-csp 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/ */

    

magentix / magento-csp example snippets




declare(strict_types=1);

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Response\HttpInterface as HttpResponse;

class Csp implements ObserverInterface
{
    public function execute(Observer $observer): void
    {
        /** @var HttpResponse $response */
        $response = $observer->getEvent()->getData('response');

        $response->setHeader(
            'Content-Security-Policy',
            "default-src 'self' *.cloudflare.com; " .
            "script-src 'self' 'unsafe-eval' 'unsafe-inline' *.cloudflare.com; " .
            "style-src 'self' 'unsafe-inline'; " .
            "img-src 'self' *.openstreetmap.org data:"
        );
    }
}