2016-01-28 08:33:02 -05:00
|
|
|
<?php
|
2021-04-19 09:50:30 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-01-28 08:33:02 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-01-28 08:33:02 -05:00
|
|
|
*/
|
|
|
|
|
|
2016-05-19 03:02:58 -04:00
|
|
|
namespace Test\Security\CSP;
|
|
|
|
|
|
2016-01-28 08:33:02 -05:00
|
|
|
use OC\Security\CSP\ContentSecurityPolicyManager;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
|
|
|
|
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
|
2019-05-24 15:42:37 -04:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
|
|
|
|
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Server;
|
2019-05-24 15:42:37 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class ContentSecurityPolicyManagerTest extends TestCase {
|
2023-07-26 15:56:36 -04:00
|
|
|
/** @var IEventDispatcher */
|
2019-05-24 15:42:37 -04:00
|
|
|
private $dispatcher;
|
2016-01-28 08:33:02 -05:00
|
|
|
|
|
|
|
|
/** @var ContentSecurityPolicyManager */
|
|
|
|
|
private $contentSecurityPolicyManager;
|
|
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-01-28 08:33:02 -05:00
|
|
|
parent::setUp();
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->dispatcher = Server::get(IEventDispatcher::class);
|
2019-05-24 15:42:37 -04:00
|
|
|
$this->contentSecurityPolicyManager = new ContentSecurityPolicyManager($this->dispatcher);
|
2016-01-28 08:33:02 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testAddDefaultPolicy(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->contentSecurityPolicyManager->addDefaultPolicy(new ContentSecurityPolicy());
|
2018-01-25 05:23:12 -05:00
|
|
|
$this->addToAssertionCount(1);
|
2016-01-28 08:33:02 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetDefaultPolicyWithPolicies(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$policy = new ContentSecurityPolicy();
|
2016-01-28 08:33:02 -05:00
|
|
|
$policy->addAllowedFontDomain('mydomain.com');
|
|
|
|
|
$policy->addAllowedImageDomain('anotherdomain.de');
|
|
|
|
|
$this->contentSecurityPolicyManager->addDefaultPolicy($policy);
|
2025-06-12 12:31:58 -04:00
|
|
|
$policy = new ContentSecurityPolicy();
|
2016-01-28 08:33:02 -05:00
|
|
|
$policy->addAllowedFontDomain('example.com');
|
|
|
|
|
$policy->addAllowedImageDomain('example.org');
|
2018-09-03 09:28:37 -04:00
|
|
|
$policy->allowEvalScript(true);
|
2016-01-28 08:33:02 -05:00
|
|
|
$this->contentSecurityPolicyManager->addDefaultPolicy($policy);
|
2025-06-12 12:31:58 -04:00
|
|
|
$policy = new EmptyContentSecurityPolicy();
|
2016-01-28 08:33:02 -05:00
|
|
|
$policy->addAllowedChildSrcDomain('childdomain');
|
|
|
|
|
$policy->addAllowedFontDomain('anotherFontDomain');
|
2019-07-31 03:03:33 -04:00
|
|
|
$policy->addAllowedFormActionDomain('thirdDomain');
|
2016-01-28 08:33:02 -05:00
|
|
|
$this->contentSecurityPolicyManager->addDefaultPolicy($policy);
|
|
|
|
|
|
|
|
|
|
$expected = new \OC\Security\CSP\ContentSecurityPolicy();
|
2019-05-24 15:42:37 -04:00
|
|
|
$expected->allowEvalScript(true);
|
|
|
|
|
$expected->addAllowedFontDomain('mydomain.com');
|
|
|
|
|
$expected->addAllowedFontDomain('example.com');
|
|
|
|
|
$expected->addAllowedFontDomain('anotherFontDomain');
|
2019-07-31 03:03:33 -04:00
|
|
|
$expected->addAllowedFormActionDomain('thirdDomain');
|
2019-05-24 15:42:37 -04:00
|
|
|
$expected->addAllowedImageDomain('anotherdomain.de');
|
|
|
|
|
$expected->addAllowedImageDomain('example.org');
|
|
|
|
|
$expected->addAllowedChildSrcDomain('childdomain');
|
2023-11-17 16:01:02 -05:00
|
|
|
$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";
|
2019-05-24 15:42:37 -04:00
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
|
|
|
|
|
$this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetDefaultPolicyWithPoliciesViaEvent(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e): void {
|
|
|
|
|
$policy = new ContentSecurityPolicy();
|
2019-05-24 15:42:37 -04:00
|
|
|
$policy->addAllowedFontDomain('mydomain.com');
|
|
|
|
|
$policy->addAllowedImageDomain('anotherdomain.de');
|
2022-03-09 08:25:36 -05:00
|
|
|
$policy->useStrictDynamic(true);
|
2022-04-01 07:56:15 -04:00
|
|
|
$policy->allowEvalScript(true);
|
2019-05-24 15:42:37 -04:00
|
|
|
|
|
|
|
|
$e->addPolicy($policy);
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e): void {
|
|
|
|
|
$policy = new ContentSecurityPolicy();
|
2019-05-24 15:42:37 -04:00
|
|
|
$policy->addAllowedFontDomain('example.com');
|
|
|
|
|
$policy->addAllowedImageDomain('example.org');
|
2022-04-01 07:56:15 -04:00
|
|
|
$policy->allowEvalScript(false);
|
2019-05-24 15:42:37 -04:00
|
|
|
$e->addPolicy($policy);
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e): void {
|
|
|
|
|
$policy = new EmptyContentSecurityPolicy();
|
2019-05-24 15:42:37 -04:00
|
|
|
$policy->addAllowedChildSrcDomain('childdomain');
|
|
|
|
|
$policy->addAllowedFontDomain('anotherFontDomain');
|
2019-07-31 03:03:33 -04:00
|
|
|
$policy->addAllowedFormActionDomain('thirdDomain');
|
2019-05-24 15:42:37 -04:00
|
|
|
$e->addPolicy($policy);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$expected = new \OC\Security\CSP\ContentSecurityPolicy();
|
2018-09-03 09:28:37 -04:00
|
|
|
$expected->allowEvalScript(true);
|
2016-01-28 08:33:02 -05:00
|
|
|
$expected->addAllowedFontDomain('mydomain.com');
|
|
|
|
|
$expected->addAllowedFontDomain('example.com');
|
|
|
|
|
$expected->addAllowedFontDomain('anotherFontDomain');
|
|
|
|
|
$expected->addAllowedImageDomain('anotherdomain.de');
|
|
|
|
|
$expected->addAllowedImageDomain('example.org');
|
|
|
|
|
$expected->addAllowedChildSrcDomain('childdomain');
|
2019-07-31 03:03:33 -04:00
|
|
|
$expected->addAllowedFormActionDomain('thirdDomain');
|
2022-03-09 08:25:36 -05:00
|
|
|
$expected->useStrictDynamic(true);
|
2023-11-17 16:01:02 -05:00
|
|
|
$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";
|
2016-01-28 08:33:02 -05:00
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
|
|
|
|
|
$this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
|
|
|
|
|
}
|
|
|
|
|
}
|