ContentSecurityPolicyManagerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Security\CSP;
  22. use OC\Security\CSP\ContentSecurityPolicyManager;
  23. use OCP\EventDispatcher\IEventDispatcher;
  24. use OCP\Security\CSP\AddContentSecurityPolicyEvent;
  25. use PHPUnit\Framework\MockObject\MockObject;
  26. use Symfony\Component\EventDispatcher\EventDispatcher;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. use Test\TestCase;
  29. class ContentSecurityPolicyManagerTest extends TestCase {
  30. /** @var EventDispatcherInterface */
  31. private $dispatcher;
  32. /** @var ContentSecurityPolicyManager */
  33. private $contentSecurityPolicyManager;
  34. public function setUp() {
  35. parent::setUp();
  36. $this->dispatcher = \OC::$server->query(IEventDispatcher::class);
  37. $this->contentSecurityPolicyManager = new ContentSecurityPolicyManager($this->dispatcher);
  38. }
  39. public function testAddDefaultPolicy() {
  40. $this->contentSecurityPolicyManager->addDefaultPolicy(new \OCP\AppFramework\Http\ContentSecurityPolicy());
  41. $this->addToAssertionCount(1);
  42. }
  43. public function testGetDefaultPolicyWithPolicies() {
  44. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  45. $policy->addAllowedFontDomain('mydomain.com');
  46. $policy->addAllowedImageDomain('anotherdomain.de');
  47. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  48. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  49. $policy->addAllowedFontDomain('example.com');
  50. $policy->addAllowedImageDomain('example.org');
  51. $policy->allowInlineScript(true);
  52. $policy->allowEvalScript(true);
  53. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  54. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  55. $policy->addAllowedChildSrcDomain('childdomain');
  56. $policy->addAllowedFontDomain('anotherFontDomain');
  57. $policy->addAllowedFormActionDomain('thirdDomain');
  58. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  59. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  60. $expected->allowInlineScript(true);
  61. $expected->allowEvalScript(true);
  62. $expected->addAllowedFontDomain('mydomain.com');
  63. $expected->addAllowedFontDomain('example.com');
  64. $expected->addAllowedFontDomain('anotherFontDomain');
  65. $expected->addAllowedFormActionDomain('thirdDomain');
  66. $expected->addAllowedImageDomain('anotherdomain.de');
  67. $expected->addAllowedImageDomain('example.org');
  68. $expected->addAllowedChildSrcDomain('childdomain');
  69. $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";
  70. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  71. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  72. }
  73. public function testGetDefaultPolicyWithPoliciesViaEvent() {
  74. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) {
  75. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  76. $policy->addAllowedFontDomain('mydomain.com');
  77. $policy->addAllowedImageDomain('anotherdomain.de');
  78. $e->addPolicy($policy);
  79. });
  80. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) {
  81. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  82. $policy->addAllowedFontDomain('example.com');
  83. $policy->addAllowedImageDomain('example.org');
  84. $policy->allowInlineScript(true);
  85. $policy->allowEvalScript(true);
  86. $e->addPolicy($policy);
  87. });
  88. $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) {
  89. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  90. $policy->addAllowedChildSrcDomain('childdomain');
  91. $policy->addAllowedFontDomain('anotherFontDomain');
  92. $policy->addAllowedFormActionDomain('thirdDomain');
  93. $e->addPolicy($policy);
  94. });
  95. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  96. $expected->allowInlineScript(true);
  97. $expected->allowEvalScript(true);
  98. $expected->addAllowedFontDomain('mydomain.com');
  99. $expected->addAllowedFontDomain('example.com');
  100. $expected->addAllowedFontDomain('anotherFontDomain');
  101. $expected->addAllowedImageDomain('anotherdomain.de');
  102. $expected->addAllowedImageDomain('example.org');
  103. $expected->addAllowedChildSrcDomain('childdomain');
  104. $expected->addAllowedFormActionDomain('thirdDomain');
  105. $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";
  106. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  107. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  108. }
  109. }