PHP code example of wieni / wmcontent_security_policy

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

    

wieni / wmcontent_security_policy example snippets


\Drupal::service('wmcontent_security_policy.content_security_policy')
    ->addScriptHash("'sha256-n4MwUTyKKCBlMIFkvcS3dkmlRFEcqSm/V0IOhZelzA0='");



use Drupal\wmcontent_security_policy\ContentSecurityPolicyEvents;
use Drupal\wmcontent_security_policy\Event\SourcesAlterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ContentSecurityPolicySourcesSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        $events[ContentSecurityPolicyEvents::SOURCES_ALTER][] = ['onSourcesAlter'];

        return $events;
    }

    public function onSourcesAlter(SourcesAlterEvent $event): void
    {
        if (!empty($_ENV['S3_CNAME'])) {
            $event->addSource('script-src', $_ENV['S3_CNAME']);
            $event->addSource('connect-src', $_ENV['S3_CNAME']);
            $event->addSource('style-src', $_ENV['S3_CNAME']);
        }
    }
}