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 | /** |
||
| 80 | * Whether inline JavaScript snippets are allowed or forbidden |
||
| 81 | * @param bool $state |
||
| 82 | * @return $this |
||
| 83 | * @since 8.1.0 |
||
| 84 | * @deprecated 10.0 CSP tokens are now used |
||
| 85 | */ |
||
| 86 | public function allowInlineScript($state = false) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Use the according JS nonce |
||
| 93 | * |
||
| 94 | * @param string $nonce |
||
| 95 | * @return $this |
||
| 96 | * @since 11.0.0 |
||
| 97 | */ |
||
| 98 | public function useJsNonce($nonce) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Whether eval in JavaScript is allowed or forbidden |
||
| 105 | * @param bool $state |
||
| 106 | * @return $this |
||
| 107 | * @since 8.1.0 |
||
| 108 | */ |
||
| 109 | public function allowEvalScript($state = true) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Allows to execute JavaScript files from a specific domain. Use * to |
||
| 116 | * allow JavaScript from all domains. |
||
| 117 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 118 | * @return $this |
||
| 119 | * @since 8.1.0 |
||
| 120 | */ |
||
| 121 | public function addAllowedScriptDomain($domain) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Remove the specified allowed script domain from the allowed domains. |
||
| 128 | * |
||
| 129 | * @param string $domain |
||
| 130 | * @return $this |
||
| 131 | * @since 8.1.0 |
||
| 132 | */ |
||
| 133 | public function disallowScriptDomain($domain) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Whether inline CSS snippets are allowed or forbidden |
||
| 140 | * @param bool $state |
||
| 141 | * @return $this |
||
| 142 | * @since 8.1.0 |
||
| 143 | */ |
||
| 144 | public function allowInlineStyle($state = true) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Allows to execute CSS files from a specific domain. Use * to allow |
||
| 151 | * CSS from all domains. |
||
| 152 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 153 | * @return $this |
||
| 154 | * @since 8.1.0 |
||
| 155 | */ |
||
| 156 | public function addAllowedStyleDomain($domain) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Remove the specified allowed style domain from the allowed domains. |
||
| 163 | * |
||
| 164 | * @param string $domain |
||
| 165 | * @return $this |
||
| 166 | * @since 8.1.0 |
||
| 167 | */ |
||
| 168 | public function disallowStyleDomain($domain) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Allows using fonts from a specific domain. Use * to allow |
||
| 175 | * fonts from all domains. |
||
| 176 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 177 | * @return $this |
||
| 178 | * @since 8.1.0 |
||
| 179 | */ |
||
| 180 | public function addAllowedFontDomain($domain) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Remove the specified allowed font domain from the allowed domains. |
||
| 187 | * |
||
| 188 | * @param string $domain |
||
| 189 | * @return $this |
||
| 190 | * @since 8.1.0 |
||
| 191 | */ |
||
| 192 | public function disallowFontDomain($domain) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Allows embedding images from a specific domain. Use * to allow |
||
| 199 | * images from all domains. |
||
| 200 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 201 | * @return $this |
||
| 202 | * @since 8.1.0 |
||
| 203 | */ |
||
| 204 | public function addAllowedImageDomain($domain) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Remove the specified allowed image domain from the allowed domains. |
||
| 211 | * |
||
| 212 | * @param string $domain |
||
| 213 | * @return $this |
||
| 214 | * @since 8.1.0 |
||
| 215 | */ |
||
| 216 | public function disallowImageDomain($domain) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * To which remote domains the JS connect to. |
||
| 223 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 224 | * @return $this |
||
| 225 | * @since 8.1.0 |
||
| 226 | */ |
||
| 227 | public function addAllowedConnectDomain($domain) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Remove the specified allowed connect domain from the allowed domains. |
||
| 234 | * |
||
| 235 | * @param string $domain |
||
| 236 | * @return $this |
||
| 237 | * @since 8.1.0 |
||
| 238 | */ |
||
| 239 | public function disallowConnectDomain($domain) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * From which domains media elements can be embedded. |
||
| 246 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 247 | * @return $this |
||
| 248 | * @since 8.1.0 |
||
| 249 | */ |
||
| 250 | public function addAllowedMediaDomain($domain) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Remove the specified allowed media domain from the allowed domains. |
||
| 257 | * |
||
| 258 | * @param string $domain |
||
| 259 | * @return $this |
||
| 260 | * @since 8.1.0 |
||
| 261 | */ |
||
| 262 | public function disallowMediaDomain($domain) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * From which domains objects such as <object>, <embed> or <applet> are executed |
||
| 269 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 270 | * @return $this |
||
| 271 | * @since 8.1.0 |
||
| 272 | */ |
||
| 273 | public function addAllowedObjectDomain($domain) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Remove the specified allowed object domain from the allowed domains. |
||
| 280 | * |
||
| 281 | * @param string $domain |
||
| 282 | * @return $this |
||
| 283 | * @since 8.1.0 |
||
| 284 | */ |
||
| 285 | public function disallowObjectDomain($domain) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Which domains can be embedded in an iframe |
||
| 292 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 293 | * @return $this |
||
| 294 | * @since 8.1.0 |
||
| 295 | */ |
||
| 296 | public function addAllowedFrameDomain($domain) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Remove the specified allowed frame domain from the allowed domains. |
||
| 303 | * |
||
| 304 | * @param string $domain |
||
| 305 | * @return $this |
||
| 306 | * @since 8.1.0 |
||
| 307 | */ |
||
| 308 | public function disallowFrameDomain($domain) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Domains from which web-workers and nested browsing content can load elements |
||
| 315 | * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
||
| 316 | * @return $this |
||
| 317 | * @since 8.1.0 |
||
| 318 | * @deprecated 15.0.0 use addAllowedWorkerSrcDomains or addAllowedFrameDomain |
||
| 319 | */ |
||
| 320 | public function addAllowedChildSrcDomain($domain) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Remove the specified allowed child src domain from the allowed domains. |
||
| 327 | * |
||
| 328 | * @param string $domain |
||
| 329 | * @return $this |
||
| 330 | * @since 8.1.0 |
||
| 331 | * @deprecated 15.0.0 use the WorkerSrcDomains or FrameDomain |
||
| 332 | */ |
||
| 333 | public function disallowChildSrcDomain($domain) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Domains which can embed an iFrame of the Nextcloud instance |
||
| 340 | * |
||
| 341 | * @param string $domain |
||
| 342 | * @return $this |
||
| 343 | * @since 13.0.0 |
||
| 344 | */ |
||
| 345 | public function addAllowedFrameAncestorDomain($domain) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Domains which can embed an iFrame of the Nextcloud instance |
||
| 352 | * |
||
| 353 | * @param string $domain |
||
| 354 | * @return $this |
||
| 355 | * @since 13.0.0 |
||
| 356 | */ |
||
| 357 | public function disallowFrameAncestorDomain($domain) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Domain from which workers can be loaded |
||
| 364 | * |
||
| 365 | * @param string $domain |
||
| 366 | * @return $this |
||
| 367 | * @since 15.0.0 |
||
| 368 | */ |
||
| 369 | public function addAllowedWorkerSrcDomain(string $domain) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Remove domain from which workers can be loaded |
||
| 376 | * |
||
| 377 | * @param string $domain |
||
| 378 | * @return $this |
||
| 379 | * @since 15.0.0 |
||
| 380 | */ |
||
| 381 | public function disallowWorkerSrcDomain(string $domain) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the generated Content-Security-Policy as a string |
||
| 388 | * @return string |
||
| 389 | * @since 8.1.0 |
||
| 390 | */ |
||
| 391 | public function buildPolicy() { |
||
| 477 | } |
||
| 478 |