EmptyContentSecurityPolicy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCP\AppFramework\Http;
  23. use OCP\AppFramework\Http;
  24. /**
  25. * Class EmptyContentSecurityPolicy is a simple helper which allows applications
  26. * to modify the Content-Security-Policy sent by ownCloud. Per default the policy
  27. * is forbidding everything.
  28. *
  29. * As alternative with sane exemptions look at ContentSecurityPolicy
  30. *
  31. * @see \OCP\AppFramework\Http\ContentSecurityPolicy
  32. * @package OCP\AppFramework\Http
  33. * @since 9.0.0
  34. */
  35. class EmptyContentSecurityPolicy {
  36. /** @var bool Whether inline JS snippets are allowed */
  37. protected $inlineScriptAllowed = null;
  38. /** @var string Whether JS nonces should be used */
  39. protected $useJsNonce = null;
  40. /**
  41. * @var bool Whether eval in JS scripts is allowed
  42. * TODO: Disallow per default
  43. * @link https://github.com/owncloud/core/issues/11925
  44. */
  45. protected $evalScriptAllowed = null;
  46. /** @var array Domains from which scripts can get loaded */
  47. protected $allowedScriptDomains = null;
  48. /**
  49. * @var bool Whether inline CSS is allowed
  50. * TODO: Disallow per default
  51. * @link https://github.com/owncloud/core/issues/13458
  52. */
  53. protected $inlineStyleAllowed = null;
  54. /** @var array Domains from which CSS can get loaded */
  55. protected $allowedStyleDomains = null;
  56. /** @var array Domains from which images can get loaded */
  57. protected $allowedImageDomains = null;
  58. /** @var array Domains to which connections can be done */
  59. protected $allowedConnectDomains = null;
  60. /** @var array Domains from which media elements can be loaded */
  61. protected $allowedMediaDomains = null;
  62. /** @var array Domains from which object elements can be loaded */
  63. protected $allowedObjectDomains = null;
  64. /** @var array Domains from which iframes can be loaded */
  65. protected $allowedFrameDomains = null;
  66. /** @var array Domains from which fonts can be loaded */
  67. protected $allowedFontDomains = null;
  68. /** @var array Domains from which web-workers and nested browsing content can load elements */
  69. protected $allowedChildSrcDomains = null;
  70. /**
  71. * Whether inline JavaScript snippets are allowed or forbidden
  72. * @param bool $state
  73. * @return $this
  74. * @since 8.1.0
  75. * @deprecated 10.0 CSP tokens are now used
  76. */
  77. public function allowInlineScript($state = false) {
  78. $this->inlineScriptAllowed = $state;
  79. return $this;
  80. }
  81. /**
  82. * Use the according JS nonce
  83. *
  84. * @param string $nonce
  85. * @return $this
  86. * @since 11.0.0
  87. */
  88. public function useJsNonce($nonce) {
  89. $this->useJsNonce = $nonce;
  90. return $this;
  91. }
  92. /**
  93. * Whether eval in JavaScript is allowed or forbidden
  94. * @param bool $state
  95. * @return $this
  96. * @since 8.1.0
  97. */
  98. public function allowEvalScript($state = true) {
  99. $this->evalScriptAllowed = $state;
  100. return $this;
  101. }
  102. /**
  103. * Allows to execute JavaScript files from a specific domain. Use * to
  104. * allow JavaScript from all domains.
  105. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  106. * @return $this
  107. * @since 8.1.0
  108. */
  109. public function addAllowedScriptDomain($domain) {
  110. $this->allowedScriptDomains[] = $domain;
  111. return $this;
  112. }
  113. /**
  114. * Remove the specified allowed script domain from the allowed domains.
  115. *
  116. * @param string $domain
  117. * @return $this
  118. * @since 8.1.0
  119. */
  120. public function disallowScriptDomain($domain) {
  121. $this->allowedScriptDomains = array_diff($this->allowedScriptDomains, [$domain]);
  122. return $this;
  123. }
  124. /**
  125. * Whether inline CSS snippets are allowed or forbidden
  126. * @param bool $state
  127. * @return $this
  128. * @since 8.1.0
  129. */
  130. public function allowInlineStyle($state = true) {
  131. $this->inlineStyleAllowed = $state;
  132. return $this;
  133. }
  134. /**
  135. * Allows to execute CSS files from a specific domain. Use * to allow
  136. * CSS from all domains.
  137. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  138. * @return $this
  139. * @since 8.1.0
  140. */
  141. public function addAllowedStyleDomain($domain) {
  142. $this->allowedStyleDomains[] = $domain;
  143. return $this;
  144. }
  145. /**
  146. * Remove the specified allowed style domain from the allowed domains.
  147. *
  148. * @param string $domain
  149. * @return $this
  150. * @since 8.1.0
  151. */
  152. public function disallowStyleDomain($domain) {
  153. $this->allowedStyleDomains = array_diff($this->allowedStyleDomains, [$domain]);
  154. return $this;
  155. }
  156. /**
  157. * Allows using fonts from a specific domain. Use * to allow
  158. * fonts from all domains.
  159. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  160. * @return $this
  161. * @since 8.1.0
  162. */
  163. public function addAllowedFontDomain($domain) {
  164. $this->allowedFontDomains[] = $domain;
  165. return $this;
  166. }
  167. /**
  168. * Remove the specified allowed font domain from the allowed domains.
  169. *
  170. * @param string $domain
  171. * @return $this
  172. * @since 8.1.0
  173. */
  174. public function disallowFontDomain($domain) {
  175. $this->allowedFontDomains = array_diff($this->allowedFontDomains, [$domain]);
  176. return $this;
  177. }
  178. /**
  179. * Allows embedding images from a specific domain. Use * to allow
  180. * images from all domains.
  181. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  182. * @return $this
  183. * @since 8.1.0
  184. */
  185. public function addAllowedImageDomain($domain) {
  186. $this->allowedImageDomains[] = $domain;
  187. return $this;
  188. }
  189. /**
  190. * Remove the specified allowed image domain from the allowed domains.
  191. *
  192. * @param string $domain
  193. * @return $this
  194. * @since 8.1.0
  195. */
  196. public function disallowImageDomain($domain) {
  197. $this->allowedImageDomains = array_diff($this->allowedImageDomains, [$domain]);
  198. return $this;
  199. }
  200. /**
  201. * To which remote domains the JS connect to.
  202. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  203. * @return $this
  204. * @since 8.1.0
  205. */
  206. public function addAllowedConnectDomain($domain) {
  207. $this->allowedConnectDomains[] = $domain;
  208. return $this;
  209. }
  210. /**
  211. * Remove the specified allowed connect domain from the allowed domains.
  212. *
  213. * @param string $domain
  214. * @return $this
  215. * @since 8.1.0
  216. */
  217. public function disallowConnectDomain($domain) {
  218. $this->allowedConnectDomains = array_diff($this->allowedConnectDomains, [$domain]);
  219. return $this;
  220. }
  221. /**
  222. * From which domains media elements can be embedded.
  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 addAllowedMediaDomain($domain) {
  228. $this->allowedMediaDomains[] = $domain;
  229. return $this;
  230. }
  231. /**
  232. * Remove the specified allowed media domain from the allowed domains.
  233. *
  234. * @param string $domain
  235. * @return $this
  236. * @since 8.1.0
  237. */
  238. public function disallowMediaDomain($domain) {
  239. $this->allowedMediaDomains = array_diff($this->allowedMediaDomains, [$domain]);
  240. return $this;
  241. }
  242. /**
  243. * From which domains objects such as <object>, <embed> or <applet> are executed
  244. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  245. * @return $this
  246. * @since 8.1.0
  247. */
  248. public function addAllowedObjectDomain($domain) {
  249. $this->allowedObjectDomains[] = $domain;
  250. return $this;
  251. }
  252. /**
  253. * Remove the specified allowed object domain from the allowed domains.
  254. *
  255. * @param string $domain
  256. * @return $this
  257. * @since 8.1.0
  258. */
  259. public function disallowObjectDomain($domain) {
  260. $this->allowedObjectDomains = array_diff($this->allowedObjectDomains, [$domain]);
  261. return $this;
  262. }
  263. /**
  264. * Which domains can be embedded in an iframe
  265. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  266. * @return $this
  267. * @since 8.1.0
  268. */
  269. public function addAllowedFrameDomain($domain) {
  270. $this->allowedFrameDomains[] = $domain;
  271. return $this;
  272. }
  273. /**
  274. * Remove the specified allowed frame domain from the allowed domains.
  275. *
  276. * @param string $domain
  277. * @return $this
  278. * @since 8.1.0
  279. */
  280. public function disallowFrameDomain($domain) {
  281. $this->allowedFrameDomains = array_diff($this->allowedFrameDomains, [$domain]);
  282. return $this;
  283. }
  284. /**
  285. * Domains from which web-workers and nested browsing content can load elements
  286. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  287. * @return $this
  288. * @since 8.1.0
  289. */
  290. public function addAllowedChildSrcDomain($domain) {
  291. $this->allowedChildSrcDomains[] = $domain;
  292. return $this;
  293. }
  294. /**
  295. * Remove the specified allowed child src domain from the allowed domains.
  296. *
  297. * @param string $domain
  298. * @return $this
  299. * @since 8.1.0
  300. */
  301. public function disallowChildSrcDomain($domain) {
  302. $this->allowedChildSrcDomains = array_diff($this->allowedChildSrcDomains, [$domain]);
  303. return $this;
  304. }
  305. /**
  306. * Get the generated Content-Security-Policy as a string
  307. * @return string
  308. * @since 8.1.0
  309. */
  310. public function buildPolicy() {
  311. $policy = "default-src 'none';";
  312. $policy .= "base-uri 'none';";
  313. if(!empty($this->allowedScriptDomains) || $this->inlineScriptAllowed || $this->evalScriptAllowed) {
  314. $policy .= 'script-src ';
  315. if(is_string($this->useJsNonce)) {
  316. $policy .= '\'nonce-'.base64_encode($this->useJsNonce).'\'';
  317. $allowedScriptDomains = array_flip($this->allowedScriptDomains);
  318. unset($allowedScriptDomains['\'self\'']);
  319. $this->allowedScriptDomains = array_flip($allowedScriptDomains);
  320. if(count($allowedScriptDomains) !== 0) {
  321. $policy .= ' ';
  322. }
  323. }
  324. if(is_array($this->allowedScriptDomains)) {
  325. $policy .= implode(' ', $this->allowedScriptDomains);
  326. }
  327. if($this->inlineScriptAllowed) {
  328. $policy .= ' \'unsafe-inline\'';
  329. }
  330. if($this->evalScriptAllowed) {
  331. $policy .= ' \'unsafe-eval\'';
  332. }
  333. $policy .= ';';
  334. }
  335. if(!empty($this->allowedStyleDomains) || $this->inlineStyleAllowed) {
  336. $policy .= 'style-src ';
  337. if(is_array($this->allowedStyleDomains)) {
  338. $policy .= implode(' ', $this->allowedStyleDomains);
  339. }
  340. if($this->inlineStyleAllowed) {
  341. $policy .= ' \'unsafe-inline\'';
  342. }
  343. $policy .= ';';
  344. }
  345. if(!empty($this->allowedImageDomains)) {
  346. $policy .= 'img-src ' . implode(' ', $this->allowedImageDomains);
  347. $policy .= ';';
  348. }
  349. if(!empty($this->allowedFontDomains)) {
  350. $policy .= 'font-src ' . implode(' ', $this->allowedFontDomains);
  351. $policy .= ';';
  352. }
  353. if(!empty($this->allowedConnectDomains)) {
  354. $policy .= 'connect-src ' . implode(' ', $this->allowedConnectDomains);
  355. $policy .= ';';
  356. }
  357. if(!empty($this->allowedMediaDomains)) {
  358. $policy .= 'media-src ' . implode(' ', $this->allowedMediaDomains);
  359. $policy .= ';';
  360. }
  361. if(!empty($this->allowedObjectDomains)) {
  362. $policy .= 'object-src ' . implode(' ', $this->allowedObjectDomains);
  363. $policy .= ';';
  364. }
  365. if(!empty($this->allowedFrameDomains)) {
  366. $policy .= 'frame-src ' . implode(' ', $this->allowedFrameDomains);
  367. $policy .= ';';
  368. }
  369. if(!empty($this->allowedChildSrcDomains)) {
  370. $policy .= 'child-src ' . implode(' ', $this->allowedChildSrcDomains);
  371. $policy .= ';';
  372. }
  373. return rtrim($policy, ';');
  374. }
  375. }