EmptyContentSecurityPolicyTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Lukas Reschke lukas@owncloud.com
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\AppFramework\Http;
  9. use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
  10. /**
  11. * Class ContentSecurityPolicyTest
  12. *
  13. * @package OC\AppFramework\Http
  14. */
  15. class EmptyContentSecurityPolicyTest extends \Test\TestCase {
  16. /** @var EmptyContentSecurityPolicy */
  17. private $contentSecurityPolicy;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->contentSecurityPolicy = new EmptyContentSecurityPolicy();
  21. }
  22. public function testGetPolicyDefault() {
  23. $defaultPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  24. $this->assertSame($defaultPolicy, $this->contentSecurityPolicy->buildPolicy());
  25. }
  26. public function testGetPolicyScriptDomainValid() {
  27. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src www.owncloud.com;frame-ancestors 'none'";
  28. $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
  29. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  30. }
  31. public function testGetPolicyScriptDomainValidMultiple() {
  32. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  33. $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
  34. $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.org');
  35. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  36. }
  37. public function testGetPolicyDisallowScriptDomain() {
  38. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  39. $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
  40. $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.com');
  41. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  42. }
  43. public function testGetPolicyDisallowScriptDomainMultiple() {
  44. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src www.owncloud.com;frame-ancestors 'none'";
  45. $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
  46. $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.org');
  47. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  48. }
  49. public function testGetPolicyDisallowScriptDomainMultipleStacked() {
  50. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  51. $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
  52. $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.org')->disallowScriptDomain('www.owncloud.com');
  53. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  54. }
  55. public function testGetPolicyScriptAllowEval() {
  56. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'unsafe-eval';frame-ancestors 'none'";
  57. $this->contentSecurityPolicy->allowEvalScript(true);
  58. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  59. }
  60. public function testGetPolicyStyleDomainValid() {
  61. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com;frame-ancestors 'none'";
  62. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  63. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  64. }
  65. public function testGetPolicyStyleDomainValidMultiple() {
  66. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  67. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  68. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.org');
  69. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  70. }
  71. public function testGetPolicyDisallowStyleDomain() {
  72. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  73. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  74. $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.com');
  75. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  76. }
  77. public function testGetPolicyDisallowStyleDomainMultiple() {
  78. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com;frame-ancestors 'none'";
  79. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  80. $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.org');
  81. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  82. }
  83. public function testGetPolicyDisallowStyleDomainMultipleStacked() {
  84. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  85. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  86. $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.org')->disallowStyleDomain('www.owncloud.com');
  87. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  88. }
  89. public function testGetPolicyStyleAllowInline() {
  90. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src 'unsafe-inline';frame-ancestors 'none'";
  91. $this->contentSecurityPolicy->allowInlineStyle(true);
  92. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  93. }
  94. public function testGetPolicyStyleAllowInlineWithDomain() {
  95. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com 'unsafe-inline';frame-ancestors 'none'";
  96. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  97. $this->contentSecurityPolicy->allowInlineStyle(true);
  98. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  99. }
  100. public function testGetPolicyStyleDisallowInline() {
  101. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  102. $this->contentSecurityPolicy->allowInlineStyle(false);
  103. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  104. }
  105. public function testGetPolicyImageDomainValid() {
  106. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';img-src www.owncloud.com;frame-ancestors 'none'";
  107. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  108. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  109. }
  110. public function testGetPolicyImageDomainValidMultiple() {
  111. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';img-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  112. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  113. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.org');
  114. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  115. }
  116. public function testGetPolicyDisallowImageDomain() {
  117. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  118. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  119. $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.com');
  120. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  121. }
  122. public function testGetPolicyDisallowImageDomainMultiple() {
  123. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';img-src www.owncloud.com;frame-ancestors 'none'";
  124. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  125. $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.org');
  126. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  127. }
  128. public function testGetPolicyDisallowImageDomainMultipleStakes() {
  129. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  130. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  131. $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.org')->disallowImageDomain('www.owncloud.com');
  132. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  133. }
  134. public function testGetPolicyFontDomainValid() {
  135. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';font-src www.owncloud.com;frame-ancestors 'none'";
  136. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  137. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  138. }
  139. public function testGetPolicyFontDomainValidMultiple() {
  140. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';font-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  141. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  142. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.org');
  143. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  144. }
  145. public function testGetPolicyDisallowFontDomain() {
  146. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  147. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  148. $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.com');
  149. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  150. }
  151. public function testGetPolicyDisallowFontDomainMultiple() {
  152. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';font-src www.owncloud.com;frame-ancestors 'none'";
  153. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  154. $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.org');
  155. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  156. }
  157. public function testGetPolicyDisallowFontDomainMultipleStakes() {
  158. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  159. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  160. $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.org')->disallowFontDomain('www.owncloud.com');
  161. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  162. }
  163. public function testGetPolicyConnectDomainValid() {
  164. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';connect-src www.owncloud.com;frame-ancestors 'none'";
  165. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  166. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  167. }
  168. public function testGetPolicyConnectDomainValidMultiple() {
  169. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';connect-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  170. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  171. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.org');
  172. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  173. }
  174. public function testGetPolicyDisallowConnectDomain() {
  175. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  176. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  177. $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.com');
  178. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  179. }
  180. public function testGetPolicyDisallowConnectDomainMultiple() {
  181. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';connect-src www.owncloud.com;frame-ancestors 'none'";
  182. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  183. $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.org');
  184. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  185. }
  186. public function testGetPolicyDisallowConnectDomainMultipleStakes() {
  187. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  188. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  189. $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.org')->disallowConnectDomain('www.owncloud.com');
  190. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  191. }
  192. public function testGetPolicyMediaDomainValid() {
  193. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';media-src www.owncloud.com;frame-ancestors 'none'";
  194. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  195. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  196. }
  197. public function testGetPolicyMediaDomainValidMultiple() {
  198. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';media-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  199. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  200. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.org');
  201. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  202. }
  203. public function testGetPolicyDisallowMediaDomain() {
  204. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  205. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  206. $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.com');
  207. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  208. }
  209. public function testGetPolicyDisallowMediaDomainMultiple() {
  210. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';media-src www.owncloud.com;frame-ancestors 'none'";
  211. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  212. $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.org');
  213. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  214. }
  215. public function testGetPolicyDisallowMediaDomainMultipleStakes() {
  216. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  217. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  218. $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.org')->disallowMediaDomain('www.owncloud.com');
  219. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  220. }
  221. public function testGetPolicyObjectDomainValid() {
  222. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';object-src www.owncloud.com;frame-ancestors 'none'";
  223. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  224. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  225. }
  226. public function testGetPolicyObjectDomainValidMultiple() {
  227. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';object-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  228. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  229. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.org');
  230. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  231. }
  232. public function testGetPolicyDisallowObjectDomain() {
  233. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  234. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  235. $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.com');
  236. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  237. }
  238. public function testGetPolicyDisallowObjectDomainMultiple() {
  239. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';object-src www.owncloud.com;frame-ancestors 'none'";
  240. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  241. $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.org');
  242. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  243. }
  244. public function testGetPolicyDisallowObjectDomainMultipleStakes() {
  245. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  246. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  247. $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.org')->disallowObjectDomain('www.owncloud.com');
  248. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  249. }
  250. public function testGetAllowedFrameDomain() {
  251. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-src www.owncloud.com;frame-ancestors 'none'";
  252. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  253. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  254. }
  255. public function testGetPolicyFrameDomainValidMultiple() {
  256. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  257. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  258. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.org');
  259. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  260. }
  261. public function testGetPolicyDisallowFrameDomain() {
  262. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  263. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  264. $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.com');
  265. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  266. }
  267. public function testGetPolicyDisallowFrameDomainMultiple() {
  268. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-src www.owncloud.com;frame-ancestors 'none'";
  269. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  270. $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.org');
  271. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  272. }
  273. public function testGetPolicyDisallowFrameDomainMultipleStakes() {
  274. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  275. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  276. $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.org')->disallowFrameDomain('www.owncloud.com');
  277. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  278. }
  279. public function testGetAllowedChildSrcDomain() {
  280. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';child-src child.owncloud.com;frame-ancestors 'none'";
  281. $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.com');
  282. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  283. }
  284. public function testGetPolicyChildSrcValidMultiple() {
  285. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';child-src child.owncloud.com child.owncloud.org;frame-ancestors 'none'";
  286. $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.com');
  287. $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.org');
  288. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  289. }
  290. public function testGetPolicyDisallowChildSrcDomain() {
  291. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  292. $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
  293. $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.com');
  294. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  295. }
  296. public function testGetPolicyDisallowChildSrcDomainMultiple() {
  297. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';child-src www.owncloud.com;frame-ancestors 'none'";
  298. $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
  299. $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.org');
  300. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  301. }
  302. public function testGetPolicyDisallowChildSrcDomainMultipleStakes() {
  303. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  304. $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
  305. $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.org')->disallowChildSrcDomain('www.owncloud.com');
  306. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  307. }
  308. public function testGetPolicyWithJsNonceAndScriptDomains() {
  309. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'nonce-TXlKc05vbmNl' www.nextcloud.com www.nextcloud.org;frame-ancestors 'none'";
  310. $this->contentSecurityPolicy->addAllowedScriptDomain('www.nextcloud.com');
  311. $this->contentSecurityPolicy->useJsNonce('MyJsNonce');
  312. $this->contentSecurityPolicy->addAllowedScriptDomain('www.nextcloud.org');
  313. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  314. }
  315. public function testGetPolicyWithJsNonceAndSelfScriptDomain() {
  316. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'nonce-TXlKc05vbmNl';frame-ancestors 'none'";
  317. $this->contentSecurityPolicy->useJsNonce('MyJsNonce');
  318. $this->contentSecurityPolicy->addAllowedScriptDomain("'self'");
  319. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  320. }
  321. public function testGetPolicyWithoutJsNonceAndSelfScriptDomain() {
  322. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self';frame-ancestors 'none'";
  323. $this->contentSecurityPolicy->addAllowedScriptDomain("'self'");
  324. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  325. }
  326. public function testGetPolicyWithReportUri() {
  327. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none';report-uri https://my-report-uri.com";
  328. $this->contentSecurityPolicy->addReportTo("https://my-report-uri.com");
  329. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  330. }
  331. public function testGetPolicyWithMultipleReportUri() {
  332. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none';report-uri https://my-report-uri.com https://my-other-report-uri.com";
  333. $this->contentSecurityPolicy->addReportTo("https://my-report-uri.com");
  334. $this->contentSecurityPolicy->addReportTo("https://my-other-report-uri.com");
  335. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  336. }
  337. }