|
23 | 23 |
|
24 | 24 |
|
25 | 25 | use OC\Security\CSP\ContentSecurityPolicyManager; |
| 26 | +use OCP\Security\CSP\AddContentSecurityPolicyEvent; |
| 27 | +use PHPUnit\Framework\MockObject\MockObject; |
| 28 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 29 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 30 | +use Test\TestCase; |
| 31 | + |
| 32 | +class ContentSecurityPolicyManagerTest extends TestCase { |
| 33 | + /** @var EventDispatcherInterface */ |
| 34 | + private $dispatcher; |
26 | 35 |
|
27 | | -class ContentSecurityPolicyManagerTest extends \Test\TestCase { |
28 | 36 | /** @var ContentSecurityPolicyManager */ |
29 | 37 | private $contentSecurityPolicyManager; |
30 | 38 |
|
31 | 39 | public function setUp() { |
32 | 40 | parent::setUp(); |
33 | | - $this->contentSecurityPolicyManager = new ContentSecurityPolicyManager(); |
| 41 | + $this->dispatcher = new EventDispatcher(); |
| 42 | + $this->contentSecurityPolicyManager = new ContentSecurityPolicyManager($this->dispatcher); |
34 | 43 | } |
35 | 44 |
|
36 | 45 | public function testAddDefaultPolicy() { |
@@ -69,4 +78,44 @@ public function testGetDefaultPolicyWithPolicies() { |
69 | 78 | $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy()); |
70 | 79 | } |
71 | 80 |
|
| 81 | + public function testGetDefaultPolicyWithPoliciesViaEvent() { |
| 82 | + $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) { |
| 83 | + $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
| 84 | + $policy->addAllowedFontDomain('mydomain.com'); |
| 85 | + $policy->addAllowedImageDomain('anotherdomain.de'); |
| 86 | + |
| 87 | + $e->addPolicy($policy); |
| 88 | + }); |
| 89 | + |
| 90 | + $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) { |
| 91 | + $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
| 92 | + $policy->addAllowedFontDomain('example.com'); |
| 93 | + $policy->addAllowedImageDomain('example.org'); |
| 94 | + $policy->allowInlineScript(true); |
| 95 | + $policy->allowEvalScript(true); |
| 96 | + $e->addPolicy($policy); |
| 97 | + }); |
| 98 | + |
| 99 | + $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) { |
| 100 | + $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy(); |
| 101 | + $policy->addAllowedChildSrcDomain('childdomain'); |
| 102 | + $policy->addAllowedFontDomain('anotherFontDomain'); |
| 103 | + $e->addPolicy($policy); |
| 104 | + }); |
| 105 | + |
| 106 | + $expected = new \OC\Security\CSP\ContentSecurityPolicy(); |
| 107 | + $expected->allowInlineScript(true); |
| 108 | + $expected->allowEvalScript(true); |
| 109 | + $expected->addAllowedFontDomain('mydomain.com'); |
| 110 | + $expected->addAllowedFontDomain('example.com'); |
| 111 | + $expected->addAllowedFontDomain('anotherFontDomain'); |
| 112 | + $expected->addAllowedImageDomain('anotherdomain.de'); |
| 113 | + $expected->addAllowedImageDomain('example.org'); |
| 114 | + $expected->addAllowedChildSrcDomain('childdomain'); |
| 115 | + $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'"; |
| 116 | + |
| 117 | + $this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy()); |
| 118 | + $this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy()); |
| 119 | + } |
| 120 | + |
72 | 121 | } |
0 commit comments