Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ContentSecurity.php
Go to the documentation of this file.
1<?php
3
6
17
18 const HEADER = 'Content-Security-Policy';
19
20 const DEBUG_HEADER = 'Content-Security-Policy-Report-Only';
21
22 private array $policies = [];
23
25
31 public function __construct(?bool $reportOnly = null) {
32 if (isset($reportOnly)) {
33 $this->reportOnly($reportOnly);
34 }
35 }
36
44 public function addPolicy(string $directive, string ...$values): self {
45 $policies = $this->policies[$directive] ?? [];
46 foreach ($values as $v) {
47 if (\in_array($v, CspValues::QUOTED)) {
48 $v = "'$v'";
49 }
50 $policies[$v] = true;
51 }
52 $this->policies[$directive] = $policies;
53 return $this;
54 }
55
56 public function removePolicy(string $directive, string ...$values): self {
57 $policies = $this->policies[$directive] ?? [];
58 foreach ($values as $v) {
59 if (\in_array($v, CspValues::QUOTED)) {
60 $v = "'$v'";
61 }
62 if (isset($this->policies[$directive][$v])) {
63 unset($this->policies[$directive][$v]);
64 }
65 }
66 return $this;
67 }
68
76 public function addPolicyDefault(string $directive, string ...$values): self {
77 $default = \array_keys($this->policies[CspDirectives::DEFAULT_SRC] ?? []);
78 $values = \array_merge($default, $values);
79 $this->addPolicy($directive, ...$values);
80 return $this;
81 }
82
90 public function addNonce(string $nonce, string ...$directives): self {
91 foreach ($directives as $directive) {
92 $this->addPolicy($directive, "'nonce-$nonce'", CspValues::STRICT_DYNAMIC);
93 }
94 return $this;
95 }
96
104 public function addHash(string $hash, string ...$directives): self {
105 foreach ($directives as $directive) {
106 $this->addPolicy($directive, "'$hash'");
107 }
108 return $this;
109 }
110
118 public function addNonceDefault(string $nonce, string ...$directives): self {
119 foreach ($directives as $directive) {
120 $this->addPolicyDefault($directive, "'nonce-$nonce'", CspValues::STRICT_DYNAMIC);
121 }
122 return $this;
123 }
124
132 public function addHashDefault(string $hash, string ...$directives): self {
133 foreach ($directives as $directive) {
134 $this->addPolicyDefault($directive, "'$hash'");
135 }
136 return $this;
137 }
138
145 public function setDefaultSrc(string ...$policies): self {
146 return $this->addPolicy(CspDirectives::DEFAULT_SRC, ...$policies);
147 }
148
154 public function generate(): string {
155 $strs = '';
156 foreach ($this->policies as $directive => $policy) {
157 $policies = \array_keys($policy);
158 $strs .= $directive . ' ' . \implode(' ', $policies) . ';';
159 }
160 return $strs;
161 }
162
170 public function display(callable $directiveCall, callable $policyCall): string {
171 $strs = '';
172 foreach ($this->policies as $directive => $policy) {
173 $policies = \array_keys($policy);
174 $strs .= $directiveCall($directive) . $policyCall(\implode(' ', $policies));
175 }
176 return $strs;
177 }
178
185 public function reportOnly(?bool $reportOnly = true): self {
186 if (isset($reportOnly)) {
187 $this->header = $reportOnly ? self::DEBUG_HEADER : self::HEADER;
188 }
189 return $this;
190 }
191
197 public function addHeaderToResponse(?bool $reportOnly = null): void {
198 if (isset($reportOnly)) {
199 $this->reportOnly($reportOnly);
200 }
201 UResponse::header($this->header, $this->generate(), false);
202 }
203
212 public static function nonce($nonce, string ...$directives): ContentSecurity {
213 $csp = new self();
214 return $csp->addNonce($nonce, ...$directives);
215 }
216
222 public static function all(): ContentSecurity {
223 $csp = new self();
224 return $csp->addPolicy(CspDirectives::DEFAULT_SRC, CspValues::SELF);
225 }
226
232 public function getPolicies(): array {
233 return $this->policies;
234 }
235
241 public static function defaultUbiquity(): ContentSecurity {
242 $csp = new self();
243 $csp->addPolicyDefault(CspDirectives::CONNECT_SRC, CspValues::SELF);
244 $csp->addPolicy(CspDirectives::IMG_SRC, 'data:');
245 return $csp;
246 }
247
254 public static function defaultUbiquityDebug(string $livereloadServer = '127.0.0.1:35729'): ContentSecurity {
255 $csp = self::defaultUbiquity();
256 $config = Startup::$config;
257 if ($config['debug'] && \Ubiquity\debug\LiveReload::hasLiveReload()) {
258 $csp->addPolicyDefault(CspDirectives::CONNECT_SRC, "ws://$livereloadServer");
259 }
260 return $csp;
261 }
262}
Starts the framework.
Definition Startup.php:19
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.
Http Response utilities.
Definition UResponse.php:17
Class Configuration \config.