|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2015 Lukas Reschke [email protected] |
| 4 | + * This file is licensed under the Affero General Public License version 3 or |
| 5 | + * later. |
| 6 | + * See the COPYING-README file. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCP\AppFramework\Http; |
| 10 | + |
| 11 | +use OCP\AppFramework\Http; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class ContentSecurityPolicyHelper is a simple helper which allows applications |
| 15 | + * to modify the Content-Security-Policy sent by ownCloud. Per default only |
| 16 | + * JavaScript, stylesheets, images, fonts, media and connections from the same |
| 17 | + * domain ('self') are allowed. |
| 18 | + * |
| 19 | + * Even if a value gets modified above defaults will still get appended. Please |
| 20 | + * notice that ownCloud ships already with sensible defaults and those policies |
| 21 | + * should require no modification at all for most use-cases. |
| 22 | + * |
| 23 | + * @package OCP\AppFramework\Http |
| 24 | + */ |
| 25 | +class ContentSecurityPolicyHelper { |
| 26 | + /** @var bool Whether inline JS snippets are allowed */ |
| 27 | + private $inlineScriptAllowed = false; |
| 28 | + /** |
| 29 | + * @var bool Whether eval in JS scripts is allowed |
| 30 | + * TODO: Disallow per default |
| 31 | + * @link https://github.com/owncloud/core/issues/11925 |
| 32 | + */ |
| 33 | + private $evalScriptAllowed = true; |
| 34 | + /** @var array Domains from which scripts can get loaded */ |
| 35 | + private $allowedScriptDomains = [ |
| 36 | + '\'self\'', |
| 37 | + ]; |
| 38 | + /** |
| 39 | + * @var bool Whether inline CSS is allowed |
| 40 | + * TODO: Disallow per default |
| 41 | + * @link https://github.com/owncloud/core/issues/13458 |
| 42 | + */ |
| 43 | + private $inlineStyleAllowed = true; |
| 44 | + /** @var array Domains from which CSS can get loaded */ |
| 45 | + private $allowedStyleDomains = [ |
| 46 | + '\'self\'', |
| 47 | + ]; |
| 48 | + /** @var array Domains from which images can get loaded */ |
| 49 | + private $allowedImageDomains = [ |
| 50 | + '\'self\'', |
| 51 | + ]; |
| 52 | + /** @var array Domains to which connections can be done */ |
| 53 | + private $allowedConnectDomains = [ |
| 54 | + '\'self\'', |
| 55 | + ]; |
| 56 | + /** @var array Domains from which media elements can be loaded */ |
| 57 | + private $allowedMediaDomains = [ |
| 58 | + '\'self\'', |
| 59 | + ]; |
| 60 | + /** @var array Domains from which object elements can be loaded */ |
| 61 | + private $allowedObjectDomains = []; |
| 62 | + /** @var array Domains from which iframes can be loaded */ |
| 63 | + private $allowedFrameDomains = []; |
| 64 | + /** @var array Domains from which fonts can be loaded */ |
| 65 | + private $allowedFontDomains = [ |
| 66 | + '\'self\'', |
| 67 | + ]; |
| 68 | + |
| 69 | + /** |
| 70 | + * Whether inline JavaScript snippets are allowed or forbidden |
| 71 | + * @param bool $state |
| 72 | + * @return $this |
| 73 | + */ |
| 74 | + public function inlineScriptState($state = false) { |
| 75 | + $this->inlineScriptAllowed = $state; |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Whether eval in JavaScript is allowed or forbidden |
| 81 | + * @param bool $state |
| 82 | + * @return $this |
| 83 | + */ |
| 84 | + public function evalScriptState($state = true) { |
| 85 | + $this->evalScriptAllowed= $state; |
| 86 | + return $this; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Allows to execute JavaScript files from a specific domain. Use * to |
| 91 | + * allow JavaScript from all domains. |
| 92 | + * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
| 93 | + * @return $this |
| 94 | + */ |
| 95 | + public function addAllowedScriptDomain($domain) { |
| 96 | + $this->allowedScriptDomains[] = $domain; |
| 97 | + return $this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Whether inline CSS snippets are allowed or forbidden |
| 102 | + * @param bool $state |
| 103 | + * @return $this |
| 104 | + */ |
| 105 | + public function inlineStyleState($state = true) { |
| 106 | + $this->inlineStyleAllowed = $state; |
| 107 | + return $this; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Allows to execute CSS files from a specific domain. Use * to allow |
| 112 | + * CSS from all domains. |
| 113 | + * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
| 114 | + * @return $this |
| 115 | + */ |
| 116 | + public function addAllowedStyleDomain($domain) { |
| 117 | + $this->allowedStyleDomains[] = $domain; |
| 118 | + return $this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Allows using fonts from a specific domain. Use * to allow |
| 123 | + * fonts from all domains. |
| 124 | + * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
| 125 | + * @return $this |
| 126 | + */ |
| 127 | + public function addAllowedFontDomain($domain) { |
| 128 | + $this->allowedFontDomains[] = $domain; |
| 129 | + return $this; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Allows embedding images from a specific domain. Use * to allow |
| 134 | + * images from all domains. |
| 135 | + * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
| 136 | + * @return $this |
| 137 | + */ |
| 138 | + public function addAllowedImageDomain($domain) { |
| 139 | + $this->allowedImageDomains[] = $domain; |
| 140 | + return $this; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * To which remote domains the JS can bind to. |
| 145 | + * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
| 146 | + * @return $this |
| 147 | + */ |
| 148 | + public function addAllowedConnectDomain($domain) { |
| 149 | + $this->allowedConnectDomains[] = $domain; |
| 150 | + return $this; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * To which remote domains the JS can bind to. |
| 155 | + * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
| 156 | + * @return $this |
| 157 | + */ |
| 158 | + public function addAllowedMediaDomain($domain) { |
| 159 | + $this->allowedMediaDomains[] = $domain; |
| 160 | + return $this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * From which domains objects such as <object>, <embed> or <applet> are executed |
| 165 | + * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
| 166 | + * @return $this |
| 167 | + */ |
| 168 | + public function addAllowedObjectDomain($domain) { |
| 169 | + $this->allowedObjectDomains[] = $domain; |
| 170 | + return $this; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Which domains can be embedded in an iframe |
| 175 | + * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized. |
| 176 | + * @return $this |
| 177 | + */ |
| 178 | + public function addAllowedFrameDomain($domain) { |
| 179 | + $this->allowedFrameDomains[] = $domain; |
| 180 | + return $this; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @return string |
| 185 | + */ |
| 186 | + public function getPolicy() { |
| 187 | + $policy = "default-src 'none';"; |
| 188 | + |
| 189 | + if(!empty($this->allowedScriptDomains)) { |
| 190 | + $policy .= 'script-src ' . implode(' ', $this->allowedScriptDomains); |
| 191 | + if($this->inlineScriptAllowed) { |
| 192 | + $policy .= ' \'unsafe-inline\''; |
| 193 | + } |
| 194 | + if($this->evalScriptAllowed) { |
| 195 | + $policy .= ' \'unsafe-eval\''; |
| 196 | + } |
| 197 | + $policy .= ';'; |
| 198 | + } |
| 199 | + |
| 200 | + if(!empty($this->allowedStyleDomains)) { |
| 201 | + $policy .= 'style-src ' . implode(' ', $this->allowedStyleDomains); |
| 202 | + if($this->inlineStyleAllowed) { |
| 203 | + $policy .= ' \'unsafe-inline\''; |
| 204 | + } |
| 205 | + $policy .= ';'; |
| 206 | + } |
| 207 | + |
| 208 | + if(!empty($this->allowedImageDomains)) { |
| 209 | + $policy .= 'img-src ' . implode(' ', $this->allowedImageDomains); |
| 210 | + $policy .= ';'; |
| 211 | + } |
| 212 | + |
| 213 | + if(!empty($this->allowedFontDomains)) { |
| 214 | + $policy .= 'font-src ' . implode(' ', $this->allowedFontDomains); |
| 215 | + $policy .= ';'; |
| 216 | + } |
| 217 | + |
| 218 | + if(!empty($this->allowedConnectDomains)) { |
| 219 | + $policy .= 'connect-src ' . implode(' ', $this->allowedConnectDomains); |
| 220 | + $policy .= ';'; |
| 221 | + } |
| 222 | + |
| 223 | + if(!empty($this->allowedMediaDomains)) { |
| 224 | + $policy .= 'media-src ' . implode(' ', $this->allowedMediaDomains); |
| 225 | + $policy .= ';'; |
| 226 | + } |
| 227 | + |
| 228 | + if(!empty($this->allowedObjectDomains)) { |
| 229 | + $policy .= 'object-src ' . implode(' ', $this->allowedObjectDomains); |
| 230 | + $policy .= ';'; |
| 231 | + } |
| 232 | + |
| 233 | + if(!empty($this->allowedFrameDomains)) { |
| 234 | + $policy .= 'frame-src ' . implode(' ', $this->allowedFrameDomains); |
| 235 | + $policy .= ';'; |
| 236 | + } |
| 237 | + |
| 238 | + return rtrim($policy, ';'); |
| 239 | + } |
| 240 | +} |
0 commit comments