ContentSecurityPolicyManagerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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->allowEvalScript(true);
  51. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  52. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  53. $policy->addAllowedChildSrcDomain('childdomain');
  54. $policy->addAllowedFontDomain('anotherFontDomain');
  55. $policy->addAllowedFormActionDomain('thirdDomain');
  56. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  57. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  58. $expected->allowEvalScript(true);
  59. $expected->addAllowedFontDomain('mydomain.com');
  60. $expected->addAllowedFontDomain('example.com');
  61. $expected->addAllowedFontDomain('anotherFontDomain');
  62. $expected->addAllowedFormActionDomain('thirdDomain');
  63. $expected->addAllowedImageDomain('anotherdomain.de');
  64. $expected->addAllowedImageDomain('example.org');
  65. $expected->addAllowedChildSrcDomain('childdomain');
  66. $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";
  67. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  68. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  69. }
  70. public function testGetDefaultPolicyWithPoliciesViaEvent() {
  71. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  72. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  73. $policy->addAllowedFontDomain('mydomain.com');
  74. $policy->addAllowedImageDomain('anotherdomain.de');
  75. $policy->useStrictDynamic(true);
  76. $policy->allowEvalScript(true);
  77. $e->addPolicy($policy);
  78. });
  79. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  80. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  81. $policy->addAllowedFontDomain('example.com');
  82. $policy->addAllowedImageDomain('example.org');
  83. $policy->allowEvalScript(false);
  84. $e->addPolicy($policy);
  85. });
  86. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
  87. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  88. $policy->addAllowedChildSrcDomain('childdomain');
  89. $policy->addAllowedFontDomain('anotherFontDomain');
  90. $policy->addAllowedFormActionDomain('thirdDomain');
  91. $e->addPolicy($policy);
  92. });
  93. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  94. $expected->allowEvalScript(true);
  95. $expected->addAllowedFontDomain('mydomain.com');
  96. $expected->addAllowedFontDomain('example.com');
  97. $expected->addAllowedFontDomain('anotherFontDomain');
  98. $expected->addAllowedImageDomain('anotherdomain.de');
  99. $expected->addAllowedImageDomain('example.org');
  100. $expected->addAllowedChildSrcDomain('childdomain');
  101. $expected->addAllowedFormActionDomain('thirdDomain');
  102. $expected->useStrictDynamic(true);
  103. $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";
  104. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  105. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  106. }
  107. }