|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\security\csp; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Manage Content Security Policies. |
|
6
|
|
|
* Ubiquity\security\csp$ContentSecurityManager |
|
7
|
|
|
* This class is part of Ubiquity |
|
8
|
|
|
* |
|
9
|
|
|
* @author jc |
|
10
|
|
|
* @version 1.0.0 |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
class ContentSecurityManager { |
|
14
|
|
|
|
|
15
|
|
|
private static NonceGenerator $nonceGenerator; |
|
16
|
|
|
|
|
17
|
|
|
private static array $csp = []; |
|
18
|
|
|
|
|
19
|
|
|
private static bool $reportOnly; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Starts the Content Security Policies manager. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string|null $nonceGeneratorClass |
|
25
|
|
|
* The class used for generating nonces. |
|
26
|
|
|
* @param bool $reportOnly |
|
27
|
|
|
* @param callable|null $onNonce |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function start(string $nonceGeneratorClass = null, bool $reportOnly = false, ?callable $onNonce = null): void { |
|
30
|
|
|
$nonceGeneratorClass ??= NonceGenerator::class; |
|
31
|
|
|
self::$nonceGenerator = new $nonceGeneratorClass($onNonce); |
|
32
|
|
|
self::$reportOnly = $reportOnly; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns a new or an existing nonce. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* The nonce to create |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function getNonce(string $name): string { |
|
43
|
|
|
return self::$nonceGenerator->getNonce($name); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Checks if the manager is started. |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function isStarted(): bool { |
|
52
|
|
|
return isset(self::$nonceGenerator); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Creates and returns a new ContentSecurity object. |
|
57
|
|
|
* |
|
58
|
|
|
* @param bool|null $reportOnly |
|
59
|
|
|
* @return ContentSecurity |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function addCsp(?bool $reportOnly = null): ContentSecurity { |
|
62
|
|
|
return self::$csp[] = new ContentSecurity($reportOnly ?? self::$reportOnly); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Removes all CSP objects. |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function clearCsp(): void { |
|
69
|
|
|
self::$csp = []; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Creates a new ContentSecurity object for Ubiquity Webtools. |
|
74
|
|
|
* |
|
75
|
|
|
* @param bool|null $reportOnly |
|
76
|
|
|
* @return ContentSecurity |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function defaultUbiquity(?bool $reportOnly = null): ContentSecurity { |
|
79
|
|
|
return self::$csp[] = ContentSecurity::defaultUbiquity()->reportOnly($reportOnly); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Creates a new ContentSecurity object for Ubiquity Webtools in debug mode. |
|
84
|
|
|
* |
|
85
|
|
|
* @param bool|null $reportOnly |
|
86
|
|
|
* @param string $livereloadServer |
|
87
|
|
|
* @return ContentSecurity |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function defaultUbiquityDebug(?bool $reportOnly = null,string $livereloadServer='127.0.0.1:35729'): ContentSecurity { |
|
90
|
|
|
return self::$csp[] = ContentSecurity::defaultUbiquityDebug($livereloadServer)->reportOnly($reportOnly); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Adds all Content security policies to headers. |
|
95
|
|
|
* |
|
96
|
|
|
* @param bool|null $reportOnly |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function addHeadersToResponse(?bool $reportOnly = null): void { |
|
99
|
|
|
$reportOnly ??= self::$reportOnly; |
|
100
|
|
|
foreach (self::$csp as $csp) { |
|
101
|
|
|
$csp->addHeaderToResponse($reportOnly); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the NonceGenerator instance. |
|
107
|
|
|
* |
|
108
|
|
|
* @return NonceGenerator |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function getNonceGenerator(): NonceGenerator { |
|
111
|
|
|
return self::$nonceGenerator; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function getCsp(): array { |
|
119
|
|
|
return self::$csp; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns true if reportOnly header is activated. |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function isReportOnly(): bool { |
|
128
|
|
|
return self::$reportOnly; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|