EmptyContentSecurityPolicyTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 testGetPolicyScriptAllowInline() {
  56. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'unsafe-inline';frame-ancestors 'none'";
  57. $this->contentSecurityPolicy->allowInlineScript(true);
  58. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  59. }
  60. public function testGetPolicyScriptAllowInlineWithDomain() {
  61. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src www.owncloud.com 'unsafe-inline';frame-ancestors 'none'";
  62. $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
  63. $this->contentSecurityPolicy->allowInlineScript(true);
  64. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  65. }
  66. public function testGetPolicyScriptAllowInlineAndEval() {
  67. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'unsafe-inline' 'unsafe-eval';frame-ancestors 'none'";
  68. $this->contentSecurityPolicy->allowInlineScript(true);
  69. $this->contentSecurityPolicy->allowEvalScript(true);
  70. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  71. }
  72. public function testGetPolicyStyleDomainValid() {
  73. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com;frame-ancestors 'none'";
  74. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  75. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  76. }
  77. public function testGetPolicyStyleDomainValidMultiple() {
  78. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  79. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  80. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.org');
  81. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  82. }
  83. public function testGetPolicyDisallowStyleDomain() {
  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.com');
  87. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  88. }
  89. public function testGetPolicyDisallowStyleDomainMultiple() {
  90. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com;frame-ancestors 'none'";
  91. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  92. $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.org');
  93. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  94. }
  95. public function testGetPolicyDisallowStyleDomainMultipleStacked() {
  96. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  97. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  98. $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.org')->disallowStyleDomain('www.owncloud.com');
  99. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  100. }
  101. public function testGetPolicyStyleAllowInline() {
  102. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src 'unsafe-inline';frame-ancestors 'none'";
  103. $this->contentSecurityPolicy->allowInlineStyle(true);
  104. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  105. }
  106. public function testGetPolicyStyleAllowInlineWithDomain() {
  107. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';style-src www.owncloud.com 'unsafe-inline';frame-ancestors 'none'";
  108. $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
  109. $this->contentSecurityPolicy->allowInlineStyle(true);
  110. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  111. }
  112. public function testGetPolicyStyleDisallowInline() {
  113. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  114. $this->contentSecurityPolicy->allowInlineStyle(false);
  115. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  116. }
  117. public function testGetPolicyImageDomainValid() {
  118. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';img-src www.owncloud.com;frame-ancestors 'none'";
  119. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  120. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  121. }
  122. public function testGetPolicyImageDomainValidMultiple() {
  123. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';img-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  124. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  125. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.org');
  126. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  127. }
  128. public function testGetPolicyDisallowImageDomain() {
  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.com');
  132. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  133. }
  134. public function testGetPolicyDisallowImageDomainMultiple() {
  135. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';img-src www.owncloud.com;frame-ancestors 'none'";
  136. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  137. $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.org');
  138. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  139. }
  140. public function testGetPolicyDisallowImageDomainMultipleStakes() {
  141. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  142. $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
  143. $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.org')->disallowImageDomain('www.owncloud.com');
  144. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  145. }
  146. public function testGetPolicyFontDomainValid() {
  147. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';font-src www.owncloud.com;frame-ancestors 'none'";
  148. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  149. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  150. }
  151. public function testGetPolicyFontDomainValidMultiple() {
  152. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';font-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  153. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  154. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.org');
  155. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  156. }
  157. public function testGetPolicyDisallowFontDomain() {
  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.com');
  161. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  162. }
  163. public function testGetPolicyDisallowFontDomainMultiple() {
  164. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';font-src www.owncloud.com;frame-ancestors 'none'";
  165. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  166. $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.org');
  167. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  168. }
  169. public function testGetPolicyDisallowFontDomainMultipleStakes() {
  170. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  171. $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
  172. $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.org')->disallowFontDomain('www.owncloud.com');
  173. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  174. }
  175. public function testGetPolicyConnectDomainValid() {
  176. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';connect-src www.owncloud.com;frame-ancestors 'none'";
  177. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  178. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  179. }
  180. public function testGetPolicyConnectDomainValidMultiple() {
  181. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';connect-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  182. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  183. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.org');
  184. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  185. }
  186. public function testGetPolicyDisallowConnectDomain() {
  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.com');
  190. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  191. }
  192. public function testGetPolicyDisallowConnectDomainMultiple() {
  193. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';connect-src www.owncloud.com;frame-ancestors 'none'";
  194. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  195. $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.org');
  196. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  197. }
  198. public function testGetPolicyDisallowConnectDomainMultipleStakes() {
  199. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  200. $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
  201. $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.org')->disallowConnectDomain('www.owncloud.com');
  202. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  203. }
  204. public function testGetPolicyMediaDomainValid() {
  205. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';media-src www.owncloud.com;frame-ancestors 'none'";
  206. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  207. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  208. }
  209. public function testGetPolicyMediaDomainValidMultiple() {
  210. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';media-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  211. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  212. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.org');
  213. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  214. }
  215. public function testGetPolicyDisallowMediaDomain() {
  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.com');
  219. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  220. }
  221. public function testGetPolicyDisallowMediaDomainMultiple() {
  222. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';media-src www.owncloud.com;frame-ancestors 'none'";
  223. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  224. $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.org');
  225. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  226. }
  227. public function testGetPolicyDisallowMediaDomainMultipleStakes() {
  228. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  229. $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
  230. $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.org')->disallowMediaDomain('www.owncloud.com');
  231. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  232. }
  233. public function testGetPolicyObjectDomainValid() {
  234. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';object-src www.owncloud.com;frame-ancestors 'none'";
  235. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  236. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  237. }
  238. public function testGetPolicyObjectDomainValidMultiple() {
  239. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';object-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  240. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  241. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.org');
  242. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  243. }
  244. public function testGetPolicyDisallowObjectDomain() {
  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.com');
  248. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  249. }
  250. public function testGetPolicyDisallowObjectDomainMultiple() {
  251. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';object-src www.owncloud.com;frame-ancestors 'none'";
  252. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  253. $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.org');
  254. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  255. }
  256. public function testGetPolicyDisallowObjectDomainMultipleStakes() {
  257. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  258. $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
  259. $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.org')->disallowObjectDomain('www.owncloud.com');
  260. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  261. }
  262. public function testGetAllowedFrameDomain() {
  263. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-src www.owncloud.com;frame-ancestors 'none'";
  264. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  265. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  266. }
  267. public function testGetPolicyFrameDomainValidMultiple() {
  268. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-src www.owncloud.com www.owncloud.org;frame-ancestors 'none'";
  269. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  270. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.org');
  271. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  272. }
  273. public function testGetPolicyDisallowFrameDomain() {
  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.com');
  277. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  278. }
  279. public function testGetPolicyDisallowFrameDomainMultiple() {
  280. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-src www.owncloud.com;frame-ancestors 'none'";
  281. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  282. $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.org');
  283. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  284. }
  285. public function testGetPolicyDisallowFrameDomainMultipleStakes() {
  286. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  287. $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
  288. $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.org')->disallowFrameDomain('www.owncloud.com');
  289. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  290. }
  291. public function testGetAllowedChildSrcDomain() {
  292. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';child-src child.owncloud.com;frame-ancestors 'none'";
  293. $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.com');
  294. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  295. }
  296. public function testGetPolicyChildSrcValidMultiple() {
  297. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';child-src child.owncloud.com child.owncloud.org;frame-ancestors 'none'";
  298. $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.com');
  299. $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.org');
  300. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  301. }
  302. public function testGetPolicyDisallowChildSrcDomain() {
  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.com');
  306. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  307. }
  308. public function testGetPolicyDisallowChildSrcDomainMultiple() {
  309. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';child-src www.owncloud.com;frame-ancestors 'none'";
  310. $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
  311. $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.org');
  312. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  313. }
  314. public function testGetPolicyDisallowChildSrcDomainMultipleStakes() {
  315. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'";
  316. $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
  317. $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.org')->disallowChildSrcDomain('www.owncloud.com');
  318. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  319. }
  320. public function testGetPolicyWithJsNonceAndScriptDomains() {
  321. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'nonce-TXlKc05vbmNl' www.nextcloud.com www.nextcloud.org;frame-ancestors 'none'";
  322. $this->contentSecurityPolicy->addAllowedScriptDomain('www.nextcloud.com');
  323. $this->contentSecurityPolicy->useJsNonce('MyJsNonce');
  324. $this->contentSecurityPolicy->addAllowedScriptDomain('www.nextcloud.org');
  325. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  326. }
  327. public function testGetPolicyWithJsNonceAndSelfScriptDomain() {
  328. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'nonce-TXlKc05vbmNl';frame-ancestors 'none'";
  329. $this->contentSecurityPolicy->useJsNonce('MyJsNonce');
  330. $this->contentSecurityPolicy->addAllowedScriptDomain("'self'");
  331. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  332. }
  333. public function testGetPolicyWithoutJsNonceAndSelfScriptDomain() {
  334. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self';frame-ancestors 'none'";
  335. $this->contentSecurityPolicy->addAllowedScriptDomain("'self'");
  336. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  337. }
  338. public function testGetPolicyWithReportUri() {
  339. $expectedPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none';report-uri https://my-report-uri.com";
  340. $this->contentSecurityPolicy->addReportTo("https://my-report-uri.com");
  341. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  342. }
  343. public function testGetPolicyWithMultipleReportUri() {
  344. $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";
  345. $this->contentSecurityPolicy->addReportTo("https://my-report-uri.com");
  346. $this->contentSecurityPolicy->addReportTo("https://my-other-report-uri.com");
  347. $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
  348. }
  349. }