EmptyContentSecurityPolicy.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Pavel Krasikov <klonishe@gmail.com>
  8. * @author Pierre Rudloff <contact@rudloff.pro>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Citharel <nextcloud@tcit.fr>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCP\AppFramework\Http;
  28. /**
  29. * Class EmptyContentSecurityPolicy is a simple helper which allows applications
  30. * to modify the Content-Security-Policy sent by Nexcloud. Per default the policy
  31. * is forbidding everything.
  32. *
  33. * As alternative with sane exemptions look at ContentSecurityPolicy
  34. *
  35. * @see \OCP\AppFramework\Http\ContentSecurityPolicy
  36. * @since 9.0.0
  37. */
  38. class EmptyContentSecurityPolicy {
  39. /** @var bool Whether inline JS snippets are allowed */
  40. protected $inlineScriptAllowed = null;
  41. /** @var string Whether JS nonces should be used */
  42. protected $useJsNonce = null;
  43. /** @var bool Whether strict-dynamic should be used */
  44. protected $strictDynamicAllowed = null;
  45. /**
  46. * @var bool Whether eval in JS scripts is allowed
  47. * TODO: Disallow per default
  48. * @link https://github.com/owncloud/core/issues/11925
  49. */
  50. protected $evalScriptAllowed = null;
  51. /** @var array Domains from which scripts can get loaded */
  52. protected $allowedScriptDomains = null;
  53. /**
  54. * @var bool Whether inline CSS is allowed
  55. * TODO: Disallow per default
  56. * @link https://github.com/owncloud/core/issues/13458
  57. */
  58. protected $inlineStyleAllowed = null;
  59. /** @var array Domains from which CSS can get loaded */
  60. protected $allowedStyleDomains = null;
  61. /** @var array Domains from which images can get loaded */
  62. protected $allowedImageDomains = null;
  63. /** @var array Domains to which connections can be done */
  64. protected $allowedConnectDomains = null;
  65. /** @var array Domains from which media elements can be loaded */
  66. protected $allowedMediaDomains = null;
  67. /** @var array Domains from which object elements can be loaded */
  68. protected $allowedObjectDomains = null;
  69. /** @var array Domains from which iframes can be loaded */
  70. protected $allowedFrameDomains = null;
  71. /** @var array Domains from which fonts can be loaded */
  72. protected $allowedFontDomains = null;
  73. /** @var array Domains from which web-workers and nested browsing content can load elements */
  74. protected $allowedChildSrcDomains = null;
  75. /** @var array Domains which can embed this Nextcloud instance */
  76. protected $allowedFrameAncestors = null;
  77. /** @var array Domains from which web-workers can be loaded */
  78. protected $allowedWorkerSrcDomains = null;
  79. /** @var array Domains which can be used as target for forms */
  80. protected $allowedFormActionDomains = null;
  81. /** @var array Locations to report violations to */
  82. protected $reportTo = null;
  83. /**
  84. * Whether inline JavaScript snippets are allowed or forbidden
  85. * @param bool $state
  86. * @return $this
  87. * @since 8.1.0
  88. * @deprecated 10.0 CSP tokens are now used
  89. */
  90. public function allowInlineScript($state = false) {
  91. $this->inlineScriptAllowed = $state;
  92. return $this;
  93. }
  94. /**
  95. * @param bool $state
  96. * @return EmptyContentSecurityPolicy
  97. * @since 24.0.0
  98. */
  99. public function useStrictDynamic(bool $state = false): self {
  100. $this->strictDynamicAllowed = $state;
  101. return $this;
  102. }
  103. /**
  104. * Use the according JS nonce
  105. * This method is only for CSPMiddleware, custom values are ignored in mergePolicies of ContentSecurityPolicyManager
  106. *
  107. * @param string $nonce
  108. * @return $this
  109. * @since 11.0.0
  110. */
  111. public function useJsNonce($nonce) {
  112. $this->useJsNonce = $nonce;
  113. return $this;
  114. }
  115. /**
  116. * Whether eval in JavaScript is allowed or forbidden
  117. * @param bool $state
  118. * @return $this
  119. * @since 8.1.0
  120. * @deprecated Eval should not be used anymore. Please update your scripts. This function will stop functioning in a future version of Nextcloud.
  121. */
  122. public function allowEvalScript($state = true) {
  123. $this->evalScriptAllowed = $state;
  124. return $this;
  125. }
  126. /**
  127. * Allows to execute JavaScript files from a specific domain. Use * to
  128. * allow JavaScript from all domains.
  129. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  130. * @return $this
  131. * @since 8.1.0
  132. */
  133. public function addAllowedScriptDomain($domain) {
  134. $this->allowedScriptDomains[] = $domain;
  135. return $this;
  136. }
  137. /**
  138. * Remove the specified allowed script domain from the allowed domains.
  139. *
  140. * @param string $domain
  141. * @return $this
  142. * @since 8.1.0
  143. */
  144. public function disallowScriptDomain($domain) {
  145. $this->allowedScriptDomains = array_diff($this->allowedScriptDomains, [$domain]);
  146. return $this;
  147. }
  148. /**
  149. * Whether inline CSS snippets are allowed or forbidden
  150. * @param bool $state
  151. * @return $this
  152. * @since 8.1.0
  153. */
  154. public function allowInlineStyle($state = true) {
  155. $this->inlineStyleAllowed = $state;
  156. return $this;
  157. }
  158. /**
  159. * Allows to execute CSS files from a specific domain. Use * to allow
  160. * CSS from all domains.
  161. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  162. * @return $this
  163. * @since 8.1.0
  164. */
  165. public function addAllowedStyleDomain($domain) {
  166. $this->allowedStyleDomains[] = $domain;
  167. return $this;
  168. }
  169. /**
  170. * Remove the specified allowed style domain from the allowed domains.
  171. *
  172. * @param string $domain
  173. * @return $this
  174. * @since 8.1.0
  175. */
  176. public function disallowStyleDomain($domain) {
  177. $this->allowedStyleDomains = array_diff($this->allowedStyleDomains, [$domain]);
  178. return $this;
  179. }
  180. /**
  181. * Allows using fonts from a specific domain. Use * to allow
  182. * fonts from all domains.
  183. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  184. * @return $this
  185. * @since 8.1.0
  186. */
  187. public function addAllowedFontDomain($domain) {
  188. $this->allowedFontDomains[] = $domain;
  189. return $this;
  190. }
  191. /**
  192. * Remove the specified allowed font domain from the allowed domains.
  193. *
  194. * @param string $domain
  195. * @return $this
  196. * @since 8.1.0
  197. */
  198. public function disallowFontDomain($domain) {
  199. $this->allowedFontDomains = array_diff($this->allowedFontDomains, [$domain]);
  200. return $this;
  201. }
  202. /**
  203. * Allows embedding images from a specific domain. Use * to allow
  204. * images from all domains.
  205. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  206. * @return $this
  207. * @since 8.1.0
  208. */
  209. public function addAllowedImageDomain($domain) {
  210. $this->allowedImageDomains[] = $domain;
  211. return $this;
  212. }
  213. /**
  214. * Remove the specified allowed image domain from the allowed domains.
  215. *
  216. * @param string $domain
  217. * @return $this
  218. * @since 8.1.0
  219. */
  220. public function disallowImageDomain($domain) {
  221. $this->allowedImageDomains = array_diff($this->allowedImageDomains, [$domain]);
  222. return $this;
  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) {
  231. $this->allowedConnectDomains[] = $domain;
  232. return $this;
  233. }
  234. /**
  235. * Remove the specified allowed connect domain from the allowed domains.
  236. *
  237. * @param string $domain
  238. * @return $this
  239. * @since 8.1.0
  240. */
  241. public function disallowConnectDomain($domain) {
  242. $this->allowedConnectDomains = array_diff($this->allowedConnectDomains, [$domain]);
  243. return $this;
  244. }
  245. /**
  246. * From which domains media elements can be embedded.
  247. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  248. * @return $this
  249. * @since 8.1.0
  250. */
  251. public function addAllowedMediaDomain($domain) {
  252. $this->allowedMediaDomains[] = $domain;
  253. return $this;
  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) {
  263. $this->allowedMediaDomains = array_diff($this->allowedMediaDomains, [$domain]);
  264. return $this;
  265. }
  266. /**
  267. * From which domains objects such as <object>, <embed> or <applet> are executed
  268. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  269. * @return $this
  270. * @since 8.1.0
  271. */
  272. public function addAllowedObjectDomain($domain) {
  273. $this->allowedObjectDomains[] = $domain;
  274. return $this;
  275. }
  276. /**
  277. * Remove the specified allowed object domain from the allowed domains.
  278. *
  279. * @param string $domain
  280. * @return $this
  281. * @since 8.1.0
  282. */
  283. public function disallowObjectDomain($domain) {
  284. $this->allowedObjectDomains = array_diff($this->allowedObjectDomains, [$domain]);
  285. return $this;
  286. }
  287. /**
  288. * Which domains can be embedded in an iframe
  289. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  290. * @return $this
  291. * @since 8.1.0
  292. */
  293. public function addAllowedFrameDomain($domain) {
  294. $this->allowedFrameDomains[] = $domain;
  295. return $this;
  296. }
  297. /**
  298. * Remove the specified allowed frame domain from the allowed domains.
  299. *
  300. * @param string $domain
  301. * @return $this
  302. * @since 8.1.0
  303. */
  304. public function disallowFrameDomain($domain) {
  305. $this->allowedFrameDomains = array_diff($this->allowedFrameDomains, [$domain]);
  306. return $this;
  307. }
  308. /**
  309. * Domains from which web-workers and nested browsing content can load elements
  310. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  311. * @return $this
  312. * @since 8.1.0
  313. * @deprecated 15.0.0 use addAllowedWorkerSrcDomains or addAllowedFrameDomain
  314. */
  315. public function addAllowedChildSrcDomain($domain) {
  316. $this->allowedChildSrcDomains[] = $domain;
  317. return $this;
  318. }
  319. /**
  320. * Remove the specified allowed child src domain from the allowed domains.
  321. *
  322. * @param string $domain
  323. * @return $this
  324. * @since 8.1.0
  325. * @deprecated 15.0.0 use the WorkerSrcDomains or FrameDomain
  326. */
  327. public function disallowChildSrcDomain($domain) {
  328. $this->allowedChildSrcDomains = array_diff($this->allowedChildSrcDomains, [$domain]);
  329. return $this;
  330. }
  331. /**
  332. * Domains which can embed an iFrame of the Nextcloud instance
  333. *
  334. * @param string $domain
  335. * @return $this
  336. * @since 13.0.0
  337. */
  338. public function addAllowedFrameAncestorDomain($domain) {
  339. $this->allowedFrameAncestors[] = $domain;
  340. return $this;
  341. }
  342. /**
  343. * Domains which can embed an iFrame of the Nextcloud instance
  344. *
  345. * @param string $domain
  346. * @return $this
  347. * @since 13.0.0
  348. */
  349. public function disallowFrameAncestorDomain($domain) {
  350. $this->allowedFrameAncestors = array_diff($this->allowedFrameAncestors, [$domain]);
  351. return $this;
  352. }
  353. /**
  354. * Domain from which workers can be loaded
  355. *
  356. * @param string $domain
  357. * @return $this
  358. * @since 15.0.0
  359. */
  360. public function addAllowedWorkerSrcDomain(string $domain) {
  361. $this->allowedWorkerSrcDomains[] = $domain;
  362. return $this;
  363. }
  364. /**
  365. * Remove domain from which workers can be loaded
  366. *
  367. * @param string $domain
  368. * @return $this
  369. * @since 15.0.0
  370. */
  371. public function disallowWorkerSrcDomain(string $domain) {
  372. $this->allowedWorkerSrcDomains = array_diff($this->allowedWorkerSrcDomains, [$domain]);
  373. return $this;
  374. }
  375. /**
  376. * Domain to where forms can submit
  377. *
  378. * @since 17.0.0
  379. *
  380. * @return $this
  381. */
  382. public function addAllowedFormActionDomain(string $domain) {
  383. $this->allowedFormActionDomains[] = $domain;
  384. return $this;
  385. }
  386. /**
  387. * Remove domain to where forms can submit
  388. *
  389. * @return $this
  390. * @since 17.0.0
  391. */
  392. public function disallowFormActionDomain(string $domain) {
  393. $this->allowedFormActionDomains = array_diff($this->allowedFormActionDomains, [$domain]);
  394. return $this;
  395. }
  396. /**
  397. * Add location to report CSP violations to
  398. *
  399. * @param string $location
  400. * @return $this
  401. * @since 15.0.0
  402. */
  403. public function addReportTo(string $location) {
  404. $this->reportTo[] = $location;
  405. return $this;
  406. }
  407. /**
  408. * Get the generated Content-Security-Policy as a string
  409. * @return string
  410. * @since 8.1.0
  411. */
  412. public function buildPolicy() {
  413. $policy = "default-src 'none';";
  414. $policy .= "base-uri 'none';";
  415. $policy .= "manifest-src 'self';";
  416. if (!empty($this->allowedScriptDomains) || $this->inlineScriptAllowed || $this->evalScriptAllowed) {
  417. $policy .= 'script-src ';
  418. if (is_string($this->useJsNonce)) {
  419. if ($this->strictDynamicAllowed) {
  420. $policy .= '\'strict-dynamic\' ';
  421. }
  422. $policy .= '\'nonce-'.base64_encode($this->useJsNonce).'\'';
  423. $allowedScriptDomains = array_flip($this->allowedScriptDomains);
  424. unset($allowedScriptDomains['\'self\'']);
  425. $this->allowedScriptDomains = array_flip($allowedScriptDomains);
  426. if (count($allowedScriptDomains) !== 0) {
  427. $policy .= ' ';
  428. }
  429. }
  430. if (is_array($this->allowedScriptDomains)) {
  431. $policy .= implode(' ', $this->allowedScriptDomains);
  432. }
  433. if ($this->inlineScriptAllowed) {
  434. $policy .= ' \'unsafe-inline\'';
  435. }
  436. if ($this->evalScriptAllowed) {
  437. $policy .= ' \'unsafe-eval\'';
  438. }
  439. $policy .= ';';
  440. }
  441. if (!empty($this->allowedStyleDomains) || $this->inlineStyleAllowed) {
  442. $policy .= 'style-src ';
  443. if (is_array($this->allowedStyleDomains)) {
  444. $policy .= implode(' ', $this->allowedStyleDomains);
  445. }
  446. if ($this->inlineStyleAllowed) {
  447. $policy .= ' \'unsafe-inline\'';
  448. }
  449. $policy .= ';';
  450. }
  451. if (!empty($this->allowedImageDomains)) {
  452. $policy .= 'img-src ' . implode(' ', $this->allowedImageDomains);
  453. $policy .= ';';
  454. }
  455. if (!empty($this->allowedFontDomains)) {
  456. $policy .= 'font-src ' . implode(' ', $this->allowedFontDomains);
  457. $policy .= ';';
  458. }
  459. if (!empty($this->allowedConnectDomains)) {
  460. $policy .= 'connect-src ' . implode(' ', $this->allowedConnectDomains);
  461. $policy .= ';';
  462. }
  463. if (!empty($this->allowedMediaDomains)) {
  464. $policy .= 'media-src ' . implode(' ', $this->allowedMediaDomains);
  465. $policy .= ';';
  466. }
  467. if (!empty($this->allowedObjectDomains)) {
  468. $policy .= 'object-src ' . implode(' ', $this->allowedObjectDomains);
  469. $policy .= ';';
  470. }
  471. if (!empty($this->allowedFrameDomains)) {
  472. $policy .= 'frame-src ';
  473. $policy .= implode(' ', $this->allowedFrameDomains);
  474. $policy .= ';';
  475. }
  476. if (!empty($this->allowedChildSrcDomains)) {
  477. $policy .= 'child-src ' . implode(' ', $this->allowedChildSrcDomains);
  478. $policy .= ';';
  479. }
  480. if (!empty($this->allowedFrameAncestors)) {
  481. $policy .= 'frame-ancestors ' . implode(' ', $this->allowedFrameAncestors);
  482. $policy .= ';';
  483. } else {
  484. $policy .= 'frame-ancestors \'none\';';
  485. }
  486. if (!empty($this->allowedWorkerSrcDomains)) {
  487. $policy .= 'worker-src ' . implode(' ', $this->allowedWorkerSrcDomains);
  488. $policy .= ';';
  489. }
  490. if (!empty($this->allowedFormActionDomains)) {
  491. $policy .= 'form-action ' . implode(' ', $this->allowedFormActionDomains);
  492. $policy .= ';';
  493. }
  494. if (!empty($this->reportTo)) {
  495. $policy .= 'report-uri ' . implode(' ', $this->reportTo);
  496. $policy .= ';';
  497. }
  498. return rtrim($policy, ';');
  499. }
  500. }