ContentSecurityPolicyManagerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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 Test\Security\CSP;
  23. use OC\Security\CSP\ContentSecurityPolicyManager;
  24. use OCP\EventDispatcher\IEventDispatcher;
  25. use OCP\Security\CSP\AddContentSecurityPolicyEvent;
  26. use Test\TestCase;
  27. class ContentSecurityPolicyManagerTest extends TestCase {
  28. /** @var IEventDispatcher */
  29. private $dispatcher;
  30. /** @var ContentSecurityPolicyManager */
  31. private $contentSecurityPolicyManager;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->dispatcher = \OC::$server->query(IEventDispatcher::class);
  35. $this->contentSecurityPolicyManager = new ContentSecurityPolicyManager($this->dispatcher);
  36. }
  37. public function testAddDefaultPolicy() {
  38. $this->contentSecurityPolicyManager->addDefaultPolicy(new \OCP\AppFramework\Http\ContentSecurityPolicy());
  39. $this->addToAssertionCount(1);
  40. }
  41. public function testGetDefaultPolicyWithPolicies() {
  42. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  43. $policy->addAllowedFontDomain('mydomain.com');
  44. $policy->addAllowedImageDomain('anotherdomain.de');
  45. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  46. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  47. $policy->addAllowedFontDomain('example.com');
  48. $policy->addAllowedImageDomain('example.org');
  49. $policy->allowEvalScript(true);
  50. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  51. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  52. $policy->addAllowedChildSrcDomain('childdomain');
  53. $policy->addAllowedFontDomain('anotherFontDomain');
  54. $policy->addAllowedFormActionDomain('thirdDomain');
  55. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  56. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  57. $expected->allowEvalScript(true);
  58. $expected->addAllowedFontDomain('mydomain.com');
  59. $expected->addAllowedFontDomain('example.com');
  60. $expected->addAllowedFontDomain('anotherFontDomain');
  61. $expected->addAllowedFormActionDomain('thirdDomain');
  62. $expected->addAllowedImageDomain('anotherdomain.de');
  63. $expected->addAllowedImageDomain('example.org');
  64. $expected->addAllowedChildSrcDomain('childdomain');
  65. $expectedStringPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob: anotherdomain.de example.org;font-src 'self' data: mydomain.com example.com anotherFontDomain;connect-src 'self';media-src 'self';child-src childdomain;frame-ancestors 'self';form-action 'self' thirdDomain";
  66. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  67. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  68. }
  69. public function testGetDefaultPolicyWithPoliciesViaEvent() {
  70. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  71. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  72. $policy->addAllowedFontDomain('mydomain.com');
  73. $policy->addAllowedImageDomain('anotherdomain.de');
  74. $policy->useStrictDynamic(true);
  75. $policy->allowEvalScript(true);
  76. $e->addPolicy($policy);
  77. });
  78. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  79. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  80. $policy->addAllowedFontDomain('example.com');
  81. $policy->addAllowedImageDomain('example.org');
  82. $policy->allowEvalScript(false);
  83. $e->addPolicy($policy);
  84. });
  85. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  86. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  87. $policy->addAllowedChildSrcDomain('childdomain');
  88. $policy->addAllowedFontDomain('anotherFontDomain');
  89. $policy->addAllowedFormActionDomain('thirdDomain');
  90. $e->addPolicy($policy);
  91. });
  92. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  93. $expected->allowEvalScript(true);
  94. $expected->addAllowedFontDomain('mydomain.com');
  95. $expected->addAllowedFontDomain('example.com');
  96. $expected->addAllowedFontDomain('anotherFontDomain');
  97. $expected->addAllowedImageDomain('anotherdomain.de');
  98. $expected->addAllowedImageDomain('example.org');
  99. $expected->addAllowedChildSrcDomain('childdomain');
  100. $expected->addAllowedFormActionDomain('thirdDomain');
  101. $expected->useStrictDynamic(true);
  102. $expectedStringPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob: anotherdomain.de example.org;font-src 'self' data: mydomain.com example.com anotherFontDomain;connect-src 'self';media-src 'self';child-src childdomain;frame-ancestors 'self';form-action 'self' thirdDomain";
  103. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  104. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  105. }
  106. }