ContentSecurityPolicyManagerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
  27. use Test\TestCase;
  28. class ContentSecurityPolicyManagerTest extends TestCase {
  29. /** @var EventDispatcherInterface */
  30. private $dispatcher;
  31. /** @var ContentSecurityPolicyManager */
  32. private $contentSecurityPolicyManager;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->dispatcher = \OC::$server->query(IEventDispatcher::class);
  36. $this->contentSecurityPolicyManager = new ContentSecurityPolicyManager($this->dispatcher);
  37. }
  38. public function testAddDefaultPolicy() {
  39. $this->contentSecurityPolicyManager->addDefaultPolicy(new \OCP\AppFramework\Http\ContentSecurityPolicy());
  40. $this->addToAssertionCount(1);
  41. }
  42. public function testGetDefaultPolicyWithPolicies() {
  43. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  44. $policy->addAllowedFontDomain('mydomain.com');
  45. $policy->addAllowedImageDomain('anotherdomain.de');
  46. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  47. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  48. $policy->addAllowedFontDomain('example.com');
  49. $policy->addAllowedImageDomain('example.org');
  50. $policy->allowInlineScript(true);
  51. $policy->allowEvalScript(true);
  52. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  53. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  54. $policy->addAllowedChildSrcDomain('childdomain');
  55. $policy->addAllowedFontDomain('anotherFontDomain');
  56. $policy->addAllowedFormActionDomain('thirdDomain');
  57. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  58. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  59. $expected->allowInlineScript(true);
  60. $expected->allowEvalScript(true);
  61. $expected->addAllowedFontDomain('mydomain.com');
  62. $expected->addAllowedFontDomain('example.com');
  63. $expected->addAllowedFontDomain('anotherFontDomain');
  64. $expected->addAllowedFormActionDomain('thirdDomain');
  65. $expected->addAllowedImageDomain('anotherdomain.de');
  66. $expected->addAllowedImageDomain('example.org');
  67. $expected->addAllowedChildSrcDomain('childdomain');
  68. $expectedStringPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-inline' '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";
  69. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  70. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  71. }
  72. public function testGetDefaultPolicyWithPoliciesViaEvent() {
  73. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  74. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  75. $policy->addAllowedFontDomain('mydomain.com');
  76. $policy->addAllowedImageDomain('anotherdomain.de');
  77. $policy->useStrictDynamic(true);
  78. $policy->allowEvalScript(true);
  79. $e->addPolicy($policy);
  80. });
  81. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  82. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  83. $policy->addAllowedFontDomain('example.com');
  84. $policy->addAllowedImageDomain('example.org');
  85. $policy->allowInlineScript(true);
  86. $policy->allowEvalScript(false);
  87. $e->addPolicy($policy);
  88. });
  89. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  90. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  91. $policy->addAllowedChildSrcDomain('childdomain');
  92. $policy->addAllowedFontDomain('anotherFontDomain');
  93. $policy->addAllowedFormActionDomain('thirdDomain');
  94. $e->addPolicy($policy);
  95. });
  96. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  97. $expected->allowInlineScript(true);
  98. $expected->allowEvalScript(true);
  99. $expected->addAllowedFontDomain('mydomain.com');
  100. $expected->addAllowedFontDomain('example.com');
  101. $expected->addAllowedFontDomain('anotherFontDomain');
  102. $expected->addAllowedImageDomain('anotherdomain.de');
  103. $expected->addAllowedImageDomain('example.org');
  104. $expected->addAllowedChildSrcDomain('childdomain');
  105. $expected->addAllowedFormActionDomain('thirdDomain');
  106. $expected->useStrictDynamic(true);
  107. $expectedStringPolicy = "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-inline' '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";
  108. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  109. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  110. }
  111. }