18 const HEADER =
'Content-Security-Policy';
32 if (isset($reportOnly)) {
44 public function addPolicy(
string $directive,
string ...$values): self {
45 $policies = $this->policies[$directive] ?? [];
46 foreach ($values as $v) {
56 public function removePolicy(
string $directive,
string ...$values): self {
57 $policies = $this->policies[$directive] ?? [];
58 foreach ($values as $v) {
62 if (isset($this->policies[$directive][$v])) {
63 unset($this->policies[$directive][$v]);
77 $default = \array_keys($this->policies[
CspDirectives::DEFAULT_SRC] ?? []);
78 $values = \array_merge($default, $values);
79 $this->addPolicy($directive, ...$values);
90 public function addNonce(
string $nonce,
string ...$directives): self {
91 foreach ($directives as $directive) {
92 $this->addPolicy($directive,
"'nonce-$nonce'", CspValues::STRICT_DYNAMIC);
104 public function addHash(
string $hash,
string ...$directives): self {
105 foreach ($directives as $directive) {
106 $this->addPolicy($directive,
"'$hash'");
119 foreach ($directives as $directive) {
120 $this->addPolicyDefault($directive,
"'nonce-$nonce'", CspValues::STRICT_DYNAMIC);
133 foreach ($directives as $directive) {
134 $this->addPolicyDefault($directive,
"'$hash'");
146 return $this->addPolicy(
CspDirectives::DEFAULT_SRC, ...$policies);
156 foreach ($this->policies as $directive => $policy) {
157 $policies = \array_keys($policy);
158 $strs .= $directive .
' ' . \implode(
' ', $policies) .
';';
170 public function display(callable $directiveCall, callable $policyCall): string {
172 foreach ($this->policies as $directive => $policy) {
173 $policies = \array_keys($policy);
174 $strs .= $directiveCall($directive) . $policyCall(\implode(
' ', $policies));
186 if (isset($reportOnly)) {
187 $this->header = $reportOnly ? self::DEBUG_HEADER : self::HEADER;
198 if (isset($reportOnly)) {
199 $this->reportOnly($reportOnly);
201 UResponse::header($this->header, $this->generate(),
false);
214 return $csp->
addNonce($nonce, ...$directives);
224 return $csp->
addPolicy(CspDirectives::DEFAULT_SRC, CspValues::SELF);
233 return $this->policies;
244 $csp->addPolicy(CspDirectives::IMG_SRC,
'data:');
255 $csp = self::defaultUbiquity();
256 $config = Startup::$config;
257 if ($config[
'debug'] && \
Ubiquity\debug\LiveReload::hasLiveReload()) {
258 $csp->
addPolicyDefault(CspDirectives::CONNECT_SRC,
"ws://$livereloadServer");
Creates a Content Security Policy object.
static all()
Creates a new ContentSecurity object, with self in default-src.
static defaultUbiquityDebug(string $livereloadServer='127.0.0.1:35729')
Creates a new ContentSecurity object for Ubiquity Webtools in debug mode.
addHashDefault(string $hash, string ... $directives)
Adds a hash to a directive, re-using default-src actual values.
getPolicies()
Returns the actual policies.
reportOnly(?bool $reportOnly=true)
Sets reportOnly.
addNonceDefault(string $nonce, string ... $directives)
Adds a nonce to a directive, re-using default-src actual values.
setDefaultSrc(string ... $policies)
Defines the policies for default-src directive.
addHeaderToResponse(?bool $reportOnly=null)
Adds headers to the response.
generate()
Generates the header string.
static nonce($nonce, string ... $directives)
Creates a nonce and add it to some directives.
display(callable $directiveCall, callable $policyCall)
Display a ContentSecurity object.
addPolicyDefault(string $directive, string ... $values)
Adds new values to a directive, re-using default-src actual values.
addNonce(string $nonce, string ... $directives)
Adds a nonce to the directives.
__construct(?bool $reportOnly=null)
ContentSecurity constructor.
addPolicy(string $directive, string ... $values)
Adds new values to a directive.
static defaultUbiquity()
Creates a new ContentSecurity object for Ubiquity Webtools.
removePolicy(string $directive, string ... $values)
addHash(string $hash, string ... $directives)
Adds a hash to the directives.
Ubiquity\security\cspCspDirectives This class is part of Ubiquity.
Class Configuration \config.