@@ -22,12 +22,24 @@ class ContentSecurity {
22
22
23
23
private $ header = self ::HEADER ;
24
24
25
+ /**
26
+ * ContentSecurity constructor.
27
+ *
28
+ * @param bool|null $reportOnly
29
+ */
25
30
public function __construct (?bool $ reportOnly = null ) {
26
31
if (isset ($ reportOnly )) {
27
32
$ this ->reportOnly ($ reportOnly );
28
33
}
29
34
}
30
35
36
+ /**
37
+ * Adds new values to a directive.
38
+ *
39
+ * @param string $directive
40
+ * @param string ...$values
41
+ * @return $this
42
+ */
31
43
public function addPolicy (string $ directive , string ...$ values ): self {
32
44
$ policies = $ this ->policies [$ directive ] ?? [];
33
45
foreach ($ values as $ v ) {
@@ -40,31 +52,63 @@ public function addPolicy(string $directive, string ...$values): self {
40
52
return $ this ;
41
53
}
42
54
55
+ /**
56
+ * Adds new values to a directive, re-using default-src actual values.
57
+ *
58
+ * @param string $directive
59
+ * @param string ...$values
60
+ * @return $this
61
+ */
43
62
public function addPolicyDefault (string $ directive , string ...$ values ): self {
44
63
$ default = \array_keys ($ this ->policies [CspDirectives::DEFAULT_SRC ] ?? []);
45
64
$ values = \array_merge ($ default , $ values );
46
65
$ this ->addPolicy ($ directive , ...$ values );
47
66
return $ this ;
48
67
}
49
68
69
+ /**
70
+ * Adds a nonce to the directives.
71
+ *
72
+ * @param string $nonce
73
+ * @param string ...$directives
74
+ * @return $this
75
+ */
50
76
public function addNonce (string $ nonce , string ...$ directives ): self {
51
77
foreach ($ directives as $ directive ) {
52
78
$ this ->addPolicy ($ directive , "'nonce- $ nonce' " , CspValues::STRICT_DYNAMIC );
53
79
}
54
80
return $ this ;
55
81
}
56
82
83
+ /**
84
+ * Adds a nonce to a directive, re-using default-src actual values.
85
+ *
86
+ * @param string $nonce
87
+ * @param string ...$directives
88
+ * @return $this
89
+ */
57
90
public function addNonceDefault (string $ nonce , string ...$ directives ): self {
58
91
foreach ($ directives as $ directive ) {
59
92
$ this ->addPolicyDefault ($ directive , "'nonce- $ nonce' " , CspValues::STRICT_DYNAMIC );
60
93
}
61
94
return $ this ;
62
95
}
63
96
64
- public function setDefaultSrc (string ...$ policies ) {
97
+ /**
98
+ * Defines the policies for default-src directive.
99
+ *
100
+ * @param string ...$policies
101
+ * @return $this
102
+ */
103
+ public function setDefaultSrc (string ...$ policies ): self {
65
104
return $ this ->addPolicy (CspDirectives::DEFAULT_SRC , ...$ policies );
66
105
}
67
106
107
+ /**
108
+ * Generates the header string.
109
+ *
110
+ * @return string
111
+ */
68
112
public function generate (): string {
69
113
$ strs = '' ;
70
114
foreach ($ this ->policies as $ directive => $ policy ) {
@@ -74,37 +118,84 @@ public function generate(): string {
74
118
return $ strs ;
75
119
}
76
120
121
+ /**
122
+ * Display a ContentSecurity object.
123
+ *
124
+ * @param callable $directiveCall
125
+ * @param callable $policyCall
126
+ * @return string
127
+ */
128
+ public function display (callable $ directiveCall , callable $ policyCall ): string {
129
+ $ strs = '' ;
130
+ foreach ($ this ->policies as $ directive => $ policy ) {
131
+ $ policies = \array_keys ($ policy );
132
+ $ strs .= $ directiveCall ($ directive ) . $ policyCall (\implode (' ' , $ policies ));
133
+ }
134
+ return $ strs ;
135
+ }
136
+
137
+ /**
138
+ * Sets reportOnly.
139
+ *
140
+ * @param bool|null $reportOnly
141
+ * @return $this
142
+ */
77
143
public function reportOnly (?bool $ reportOnly = true ): self {
78
144
if (isset ($ reportOnly )) {
79
145
$ this ->header = $ reportOnly ? self ::DEBUG_HEADER : self ::HEADER ;
80
146
}
81
147
return $ this ;
82
148
}
83
149
150
+ /**
151
+ * Adds headers to the response.
152
+ *
153
+ * @param bool|null $reportOnly
154
+ */
84
155
public function addHeaderToResponse (?bool $ reportOnly = null ): void {
85
156
if (isset ($ reportOnly )) {
86
157
$ this ->reportOnly ($ reportOnly );
87
158
}
88
159
UResponse::header ($ this ->header , $ this ->generate (), false );
89
160
}
90
161
162
+ /**
163
+ * Creates a nonce and add it to some directives.
164
+ *
165
+ * @param
166
+ * $nonce
167
+ * @param string ...$directives
168
+ * @return ContentSecurity
169
+ */
91
170
public static function nonce ($ nonce , string ...$ directives ): ContentSecurity {
92
171
$ csp = new self ();
93
172
return $ csp ->addNonce ($ nonce , ...$ directives );
94
173
}
95
174
175
+ /**
176
+ * Creates a new ContentSecurity object, with self in default-src.
177
+ *
178
+ * @return ContentSecurity
179
+ */
96
180
public static function all (): ContentSecurity {
97
181
$ csp = new self ();
98
182
return $ csp ->addPolicy (CspDirectives::DEFAULT_SRC , CspValues::SELF );
99
183
}
100
184
101
185
/**
186
+ * Returns the actual policies.
187
+ *
102
188
* @return array
103
189
*/
104
190
public function getPolicies (): array {
105
191
return $ this ->policies ;
106
192
}
107
193
194
+ /**
195
+ * Creates a new ContentSecurity object for Ubiquity Webtools.
196
+ *
197
+ * @return ContentSecurity
198
+ */
108
199
public static function defaultUbiquity (): ContentSecurity {
109
200
$ csp = new self ();
110
201
$ csp ->addPolicy (CspDirectives::DEFAULT_SRC , 'self ' , 'cdn.jsdelivr.net ' , 'cdnjs.cloudflare.com ' );
@@ -114,12 +205,4 @@ public static function defaultUbiquity(): ContentSecurity {
114
205
$ csp ->addPolicy (CspDirectives::IMG_SRC , 'data: ' );
115
206
return $ csp ;
116
207
}
117
-
118
- /**
119
- * @param array $policies
120
- */
121
- public function setPolicies (array $ policies ): void {
122
- $ this ->policies = $ policies ;
123
- }
124
-
125
208
}
0 commit comments