ContentSecurityPolicyManagerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. class ContentSecurityPolicyManagerTest extends \Test\TestCase {
  24. /** @var ContentSecurityPolicyManager */
  25. private $contentSecurityPolicyManager;
  26. public function setUp() {
  27. parent::setUp();
  28. $this->contentSecurityPolicyManager = new ContentSecurityPolicyManager();
  29. }
  30. public function testAddDefaultPolicy() {
  31. $this->contentSecurityPolicyManager->addDefaultPolicy(new \OCP\AppFramework\Http\ContentSecurityPolicy());
  32. }
  33. public function testGetDefaultPolicyWithPolicies() {
  34. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  35. $policy->addAllowedFontDomain('mydomain.com');
  36. $policy->addAllowedImageDomain('anotherdomain.de');
  37. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  38. $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
  39. $policy->addAllowedFontDomain('example.com');
  40. $policy->addAllowedImageDomain('example.org');
  41. $policy->allowInlineScript(true);
  42. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  43. $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
  44. $policy->addAllowedChildSrcDomain('childdomain');
  45. $policy->addAllowedFontDomain('anotherFontDomain');
  46. $this->contentSecurityPolicyManager->addDefaultPolicy($policy);
  47. $expected = new \OC\Security\CSP\ContentSecurityPolicy();
  48. $expected->allowInlineScript(true);
  49. $expected->addAllowedFontDomain('mydomain.com');
  50. $expected->addAllowedFontDomain('example.com');
  51. $expected->addAllowedFontDomain('anotherFontDomain');
  52. $expected->addAllowedImageDomain('anotherdomain.de');
  53. $expected->addAllowedImageDomain('example.org');
  54. $expected->addAllowedChildSrcDomain('childdomain');
  55. $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' mydomain.com example.com anotherFontDomain;connect-src 'self';media-src 'self';child-src childdomain";
  56. $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
  57. $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
  58. }
  59. }