19 private static array
$csp = [];
38 self::$nonceGenerator =
new $nonceGeneratorClass(
$onGenerate);
49 public static function getNonce(
string $name): string {
50 return self::$nonceGenerator->getNonce($name);
62 public static function getHash(
string $name,
string $code,
string $algo =
'sha256'): string {
63 $code = \preg_replace(
'/\r\n/',
'\n', $code);
64 $hash = \base64_encode(\hash($algo, $code,
true));
65 $hash =
"$algo-$hash";
66 if (isset(self::$onGenerate) && ! URequest::isAjax()) {
67 $onG = self::$onGenerate;
68 $onG($name, $hash, $algo);
78 public static function hasNonce(
string $name): bool {
79 if (isset(self::$nonceGenerator)) {
80 return self::$nonceGenerator->hasNonce($name);
91 return isset(self::$nonceGenerator);
101 return self::$csp[] = new
ContentSecurity($reportOnly ?? self::$reportOnly);
111 return self::$csp[
'default'] ??= new
ContentSecurity($reportOnly ?? self::$reportOnly);
128 return self::$csp[
'defaultUbiquity'] ??=
ContentSecurity::defaultUbiquity()->reportOnly($reportOnly);
139 return self::$csp[
'defaultUbiquity'] ??=
ContentSecurity::defaultUbiquityDebug($livereloadServer)->reportOnly($reportOnly);
148 $reportOnly ??= self::$reportOnly;
149 foreach (self::$csp as $csp) {
150 $csp->addHeaderToResponse($reportOnly);
160 return self::$nonceGenerator;
177 return self::$reportOnly;
193 ContentSecurityManager::$hashAlgo = $hashAlgo;
201 ContentSecurityManager::$onGenerate = $onGenerate;
Creates a Content Security Policy object.
Manage Content Security Policies.
static addCsp(?bool $reportOnly=null)
Creates and returns a new ContentSecurity object.
static getHash(string $name, string $code, string $algo='sha256')
Generates a hash and add it to a directive.
static isReportOnly()
Returns true if reportOnly header is activated.
static getNonceGenerator()
Returns the NonceGenerator instance.
static setOnGenerate(callable $onGenerate)
static hasNonce(string $name)
static addHeadersToResponse(?bool $reportOnly=null)
Adds all Content security policies to headers.
static defaultUbiquityDebug(?bool $reportOnly=null, string $livereloadServer='127.0.0.1:35729')
Creates a new ContentSecurity object for Ubiquity Webtools in debug mode.
static defaultUbiquity(?bool $reportOnly=null)
Creates a new ContentSecurity object for Ubiquity Webtools.
static getNonce(string $name)
Returns a new or an existing nonce.
static NonceGenerator $nonceGenerator
static start(string $nonceGeneratorClass=null, bool $reportOnly=false, ?callable $onGenerate=null)
Starts the Content Security Policies manager.
static clearCsp()
Removes all CSP objects.
static defaultCsp(?bool $reportOnly=null)
Returns a default ContentSecurity object.
static isStarted()
Checks if the manager is started.
static setHashAlgo(string $hashAlgo)
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.