Skip to content

Commit b7a918e

Browse files
committed
refactor: add property $directives
1 parent 9e3839f commit b7a918e

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

system/HTTP/ContentSecurityPolicy.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@
2626
*/
2727
class ContentSecurityPolicy
2828
{
29+
/**
30+
* CSP directives
31+
*
32+
* @var array<string, string>
33+
*/
34+
protected array $directives = [
35+
'base-uri' => 'baseURI',
36+
'child-src' => 'childSrc',
37+
'connect-src' => 'connectSrc',
38+
'default-src' => 'defaultSrc',
39+
'font-src' => 'fontSrc',
40+
'form-action' => 'formAction',
41+
'frame-ancestors' => 'frameAncestors',
42+
'frame-src' => 'frameSrc',
43+
'img-src' => 'imageSrc',
44+
'media-src' => 'mediaSrc',
45+
'object-src' => 'objectSrc',
46+
'plugin-types' => 'pluginTypes',
47+
'script-src' => 'scriptSrc',
48+
'style-src' => 'styleSrc',
49+
'manifest-src' => 'manifestSrc',
50+
'sandbox' => 'sandbox',
51+
'report-uri' => 'reportURI',
52+
];
53+
2954
/**
3055
* Used for security enforcement
3156
*
@@ -704,26 +729,6 @@ protected function buildHeaders(ResponseInterface $response)
704729
$response->setHeader('Content-Security-Policy', []);
705730
$response->setHeader('Content-Security-Policy-Report-Only', []);
706731

707-
$directives = [
708-
'base-uri' => 'baseURI',
709-
'child-src' => 'childSrc',
710-
'connect-src' => 'connectSrc',
711-
'default-src' => 'defaultSrc',
712-
'font-src' => 'fontSrc',
713-
'form-action' => 'formAction',
714-
'frame-ancestors' => 'frameAncestors',
715-
'frame-src' => 'frameSrc',
716-
'img-src' => 'imageSrc',
717-
'media-src' => 'mediaSrc',
718-
'object-src' => 'objectSrc',
719-
'plugin-types' => 'pluginTypes',
720-
'script-src' => 'scriptSrc',
721-
'style-src' => 'styleSrc',
722-
'manifest-src' => 'manifestSrc',
723-
'sandbox' => 'sandbox',
724-
'report-uri' => 'reportURI',
725-
];
726-
727732
// inject default base & default URIs if needed
728733
if (empty($this->baseURI)) {
729734
$this->baseURI = 'self';
@@ -733,7 +738,7 @@ protected function buildHeaders(ResponseInterface $response)
733738
$this->defaultSrc = 'self';
734739
}
735740

736-
foreach ($directives as $name => $property) {
741+
foreach ($this->directives as $name => $property) {
737742
if (! empty($this->{$property})) {
738743
$this->addToHeader($name, $this->{$property});
739744
}

0 commit comments

Comments
 (0)