Complex classes like EmptyContentSecurityPolicy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EmptyContentSecurityPolicy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class EmptyContentSecurityPolicy { |
||
40 | /** @var bool Whether inline JS snippets are allowed */ |
||
41 | protected $inlineScriptAllowed = null; |
||
42 | /** @var string Whether JS nonces should be used */ |
||
43 | protected $useJsNonce = null; |
||
44 | /** |
||
45 | * @var bool Whether eval in JS scripts is allowed |
||
46 | * TODO: Disallow per default |
||
47 | * @link https://github.com/owncloud/core/issues/11925 |
||
48 | */ |
||
49 | protected $evalScriptAllowed = null; |
||
50 | /** @var array Domains from which scripts can get loaded */ |
||
51 | protected $allowedScriptDomains = null; |
||
52 | /** |
||
53 | * @var bool Whether inline CSS is allowed |
||
54 | * TODO: Disallow per default |
||
55 | * @link https://github.com/owncloud/core/issues/13458 |
||
56 | */ |
||
57 | protected $inlineStyleAllowed = null; |
||
58 | /** @var array Domains from which CSS can get loaded */ |
||
59 | protected $allowedStyleDomains = null; |
||
60 | /** @var array Domains from which images can get loaded */ |
||
61 | protected $allowedImageDomains = null; |
||
62 | /** @var array Domains to which connections can be done */ |
||
63 | protected $allowedConnectDomains = null; |
||
64 | /** @var array Domains from which media elements can be loaded */ |
||
65 | protected $allowedMediaDomains = null; |
||
66 | /** @var array Domains from which object elements can be loaded */ |
||
67 | protected $allowedObjectDomains = null; |
||
68 | /** @var array Domains from which iframes can be loaded */ |
||
69 | protected $allowedFrameDomains = null; |
||
70 | /** @var array Domains from which fonts can be loaded */ |
||
71 | protected $allowedFontDomains = null; |
||
72 | /** @var array Domains from which web-workers and nested browsing content can load elements */ |
||
73 | protected $allowedChildSrcDomains = null; |
||
74 | /** @var array Domains which can embed this Nextcloud instance */ |
||
75 | protected $allowedFrameAncestors = null; |
||
76 | /** @var array Domains from which web-workers can be loaded */ |
||
77 | protected $allowedWorkerSrcDomains = null; |
||
78 | |||
79 | /** @var array Locations to report violations to */ |
||
80 | protected $reportTo = null; |
||
81 | |||
82 | /** |
||
83 | * Whether inline JavaScript snippets are allowed or forbidden |
||
84 | * @param bool $state |
||
85 | * @return $this |
||
86 | * @since 8.1.0 |
||
87 | * @deprecated 10.0 CSP tokens are now used |
||
88 | */ |
||
89 | public function allowInlineScript($state = false) { |
||
93 | |||
94 | /** |
||
95 | * Use the according JS nonce |
||
96 | * |
||
97 | * @param string $nonce |
||
98 | * @return $this |
||
99 | * @since 11.0.0 |
||
100 | */ |
||
101 | public function useJsNonce($nonce) { |
||
105 | |||
106 | /** |
||
107 | * Whether eval in JavaScript is allowed or forbidden |
||
108 | * @param bool $state |
||
109 | * @return $this |
||
110 | * @since 8.1.0 |
||
111 | */ |
||
112 | public function allowEvalScript($state = true) { |
||
116 | |||
117 | /** |
||
118 | * Allows to execute JavaScript files from a specific domain. Use * to |
||
119 | * allow JavaScript from all domains. |
||
120 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
121 | * @return $this |
||
122 | * @since 8.1.0 |
||
123 | */ |
||
124 | public function addAllowedScriptDomain($domain) { |
||
128 | |||
129 | /** |
||
130 | * Remove the specified allowed script domain from the allowed domains. |
||
131 | * |
||
132 | * @param string $domain |
||
133 | * @return $this |
||
134 | * @since 8.1.0 |
||
135 | */ |
||
136 | public function disallowScriptDomain($domain) { |
||
140 | |||
141 | /** |
||
142 | * Whether inline CSS snippets are allowed or forbidden |
||
143 | * @param bool $state |
||
144 | * @return $this |
||
145 | * @since 8.1.0 |
||
146 | */ |
||
147 | public function allowInlineStyle($state = true) { |
||
151 | |||
152 | /** |
||
153 | * Allows to execute CSS files from a specific domain. Use * to allow |
||
154 | * CSS from all domains. |
||
155 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
156 | * @return $this |
||
157 | * @since 8.1.0 |
||
158 | */ |
||
159 | public function addAllowedStyleDomain($domain) { |
||
163 | |||
164 | /** |
||
165 | * Remove the specified allowed style domain from the allowed domains. |
||
166 | * |
||
167 | * @param string $domain |
||
168 | * @return $this |
||
169 | * @since 8.1.0 |
||
170 | */ |
||
171 | public function disallowStyleDomain($domain) { |
||
175 | |||
176 | /** |
||
177 | * Allows using fonts from a specific domain. Use * to allow |
||
178 | * fonts from all domains. |
||
179 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
180 | * @return $this |
||
181 | * @since 8.1.0 |
||
182 | */ |
||
183 | public function addAllowedFontDomain($domain) { |
||
187 | |||
188 | /** |
||
189 | * Remove the specified allowed font domain from the allowed domains. |
||
190 | * |
||
191 | * @param string $domain |
||
192 | * @return $this |
||
193 | * @since 8.1.0 |
||
194 | */ |
||
195 | public function disallowFontDomain($domain) { |
||
199 | |||
200 | /** |
||
201 | * Allows embedding images from a specific domain. Use * to allow |
||
202 | * images from all domains. |
||
203 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
204 | * @return $this |
||
205 | * @since 8.1.0 |
||
206 | */ |
||
207 | public function addAllowedImageDomain($domain) { |
||
211 | |||
212 | /** |
||
213 | * Remove the specified allowed image domain from the allowed domains. |
||
214 | * |
||
215 | * @param string $domain |
||
216 | * @return $this |
||
217 | * @since 8.1.0 |
||
218 | */ |
||
219 | public function disallowImageDomain($domain) { |
||
223 | |||
224 | /** |
||
225 | * To which remote domains the JS connect to. |
||
226 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
227 | * @return $this |
||
228 | * @since 8.1.0 |
||
229 | */ |
||
230 | public function addAllowedConnectDomain($domain) { |
||
234 | |||
235 | /** |
||
236 | * Remove the specified allowed connect domain from the allowed domains. |
||
237 | * |
||
238 | * @param string $domain |
||
239 | * @return $this |
||
240 | * @since 8.1.0 |
||
241 | */ |
||
242 | public function disallowConnectDomain($domain) { |
||
246 | |||
247 | /** |
||
248 | * From which domains media elements can be embedded. |
||
249 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
250 | * @return $this |
||
251 | * @since 8.1.0 |
||
252 | */ |
||
253 | public function addAllowedMediaDomain($domain) { |
||
257 | |||
258 | /** |
||
259 | * Remove the specified allowed media domain from the allowed domains. |
||
260 | * |
||
261 | * @param string $domain |
||
262 | * @return $this |
||
263 | * @since 8.1.0 |
||
264 | */ |
||
265 | public function disallowMediaDomain($domain) { |
||
269 | |||
270 | /** |
||
271 | * From which domains objects such as <object>, <embed> or <applet> are executed |
||
272 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
273 | * @return $this |
||
274 | * @since 8.1.0 |
||
275 | */ |
||
276 | public function addAllowedObjectDomain($domain) { |
||
280 | |||
281 | /** |
||
282 | * Remove the specified allowed object domain from the allowed domains. |
||
283 | * |
||
284 | * @param string $domain |
||
285 | * @return $this |
||
286 | * @since 8.1.0 |
||
287 | */ |
||
288 | public function disallowObjectDomain($domain) { |
||
292 | |||
293 | /** |
||
294 | * Which domains can be embedded in an iframe |
||
295 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
296 | * @return $this |
||
297 | * @since 8.1.0 |
||
298 | */ |
||
299 | public function addAllowedFrameDomain($domain) { |
||
303 | |||
304 | /** |
||
305 | * Remove the specified allowed frame domain from the allowed domains. |
||
306 | * |
||
307 | * @param string $domain |
||
308 | * @return $this |
||
309 | * @since 8.1.0 |
||
310 | */ |
||
311 | public function disallowFrameDomain($domain) { |
||
315 | |||
316 | /** |
||
317 | * Domains from which web-workers and nested browsing content can load elements |
||
318 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
319 | * @return $this |
||
320 | * @since 8.1.0 |
||
321 | * @deprecated 15.0.0 use addAllowedWorkerSrcDomains or addAllowedFrameDomain |
||
322 | */ |
||
323 | public function addAllowedChildSrcDomain($domain) { |
||
327 | |||
328 | /** |
||
329 | * Remove the specified allowed child src domain from the allowed domains. |
||
330 | * |
||
331 | * @param string $domain |
||
332 | * @return $this |
||
333 | * @since 8.1.0 |
||
334 | * @deprecated 15.0.0 use the WorkerSrcDomains or FrameDomain |
||
335 | */ |
||
336 | public function disallowChildSrcDomain($domain) { |
||
340 | |||
341 | /** |
||
342 | * Domains which can embed an iFrame of the Nextcloud instance |
||
343 | * |
||
344 | * @param string $domain |
||
345 | * @return $this |
||
346 | * @since 13.0.0 |
||
347 | */ |
||
348 | public function addAllowedFrameAncestorDomain($domain) { |
||
352 | |||
353 | /** |
||
354 | * Domains which can embed an iFrame of the Nextcloud instance |
||
355 | * |
||
356 | * @param string $domain |
||
357 | * @return $this |
||
358 | * @since 13.0.0 |
||
359 | */ |
||
360 | public function disallowFrameAncestorDomain($domain) { |
||
364 | |||
365 | /** |
||
366 | * Domain from which workers can be loaded |
||
367 | * |
||
368 | * @param string $domain |
||
369 | * @return $this |
||
370 | * @since 15.0.0 |
||
371 | */ |
||
372 | public function addAllowedWorkerSrcDomain(string $domain) { |
||
376 | |||
377 | /** |
||
378 | * Remove domain from which workers can be loaded |
||
379 | * |
||
380 | * @param string $domain |
||
381 | * @return $this |
||
382 | * @since 15.0.0 |
||
383 | */ |
||
384 | public function disallowWorkerSrcDomain(string $domain) { |
||
388 | |||
389 | /** |
||
390 | * Add location to report CSP violations to |
||
391 | * |
||
392 | * @param string $location |
||
393 | * @return $this |
||
394 | * @since 15.0.0 |
||
395 | */ |
||
396 | public function addReportTo(string $location) { |
||
400 | |||
401 | /** |
||
402 | * Get the generated Content-Security-Policy as a string |
||
403 | * @return string |
||
404 | * @since 8.1.0 |
||
405 | */ |
||
406 | public function buildPolicy() { |
||
497 | } |
||
498 |