PHP code example of erickjmenezes / policyman
1. Go to this page and download the library: Download erickjmenezes/policyman 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/ */
erickjmenezes / policyman example snippets
use ErickJMenezes\Policyman\Policyman;
use ErickJMenezes\Policyman\Keyword;
$header = Policyman::builder()
->defaultSrc([Keyword::Self])
->scriptSrc([Keyword::Self, Keyword::UnsafeEval, Keyword::UnsafeInline, 'trusted-cdn.com'])
->styleSrc([Keyword::Self, 'trusted-cdn.com'])
->toString();
// Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' trusted-cdn.com; style-src 'self' trusted-cdn.com
use ErickJMenezes\Policyman\Policyman;
use ErickJMenezes\Policyman\ContentSecurityPolicy;
use ErickJMenezes\Policyman\Policy;
use ErickJMenezes\Policyman\Keyword;
use ErickJMenezes\Policyman\Directive;
// Example header.
$header = "Content-Security-Policy: img-src 'self' data:; object-src 'none'";
// Parsing to an object.
/** @var ContentSecurityPolicy $csp */
$csp = Policyman::parse($header);
// Adding script-src directive.
$csp->add(new Policy(Directive::ScriptSrc, [Keyword::Self, 'example.com']));
$csp->find(Directive::ImgSrc)->add('example.com');
// Convert it back to a string.
$newHeader = Policyman::serialize($csp);
// Content-Security-Policy: img-src 'self' data: example.com; object-src 'none'; script-src 'self' example.com
use ErickJMenezes\Policyman\Policyman;
Policyman::validate("Content-Security-Policy: default_src 'self'"); // false
Policyman::validate("Content-Security-Policy: default-src 'self'"); // true