Skip to content

Commit

Permalink
Add an event to edit the CSP
Browse files Browse the repository at this point in the history
This introduces and event that can be listend to when we actually use
the CSP. This means that apps no longer have to always inject their CSP
but only do so when it is required. Yay for being lazy.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
  • Loading branch information
rullzer authored and ChristophWurst committed Jun 17, 2019
1 parent 1bbb26e commit 0518ff1
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@
'OCP\\Search\\PagedProvider' => $baseDir . '/lib/public/Search/PagedProvider.php',
'OCP\\Search\\Provider' => $baseDir . '/lib/public/Search/Provider.php',
'OCP\\Search\\Result' => $baseDir . '/lib/public/Search/Result.php',
'OCP\\Security\\CSP\\AddContentSecurityPolicyEvent' => $baseDir . '/lib/public/Security/CSP/AddContentSecurityPolicyEvent.php',
'OCP\\Security\\IContentSecurityPolicyManager' => $baseDir . '/lib/public/Security/IContentSecurityPolicyManager.php',
'OCP\\Security\\ICredentialsManager' => $baseDir . '/lib/public/Security/ICredentialsManager.php',
'OCP\\Security\\ICrypto' => $baseDir . '/lib/public/Security/ICrypto.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Search\\PagedProvider' => __DIR__ . '/../../..' . '/lib/public/Search/PagedProvider.php',
'OCP\\Search\\Provider' => __DIR__ . '/../../..' . '/lib/public/Search/Provider.php',
'OCP\\Search\\Result' => __DIR__ . '/../../..' . '/lib/public/Search/Result.php',
'OCP\\Security\\CSP\\AddContentSecurityPolicyEvent' => __DIR__ . '/../../..' . '/lib/public/Security/CSP/AddContentSecurityPolicyEvent.php',
'OCP\\Security\\IContentSecurityPolicyManager' => __DIR__ . '/../../..' . '/lib/public/Security/IContentSecurityPolicyManager.php',
'OCP\\Security\\ICredentialsManager' => __DIR__ . '/../../..' . '/lib/public/Security/ICredentialsManager.php',
'OCP\\Security\\ICrypto' => __DIR__ . '/../../..' . '/lib/public/Security/ICrypto.php',
Expand Down
12 changes: 12 additions & 0 deletions lib/private/Security/CSP/ContentSecurityPolicyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@

use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use OCP\Security\IContentSecurityPolicyManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class ContentSecurityPolicyManager implements IContentSecurityPolicyManager {
/** @var ContentSecurityPolicy[] */
private $policies = [];

/** @var EventDispatcherInterface */
private $dispatcher;

public function __construct(EventDispatcherInterface $dispatcher) {
$this->dispatcher = $dispatcher;
}

/** {@inheritdoc} */
public function addDefaultPolicy(EmptyContentSecurityPolicy $policy) {
$this->policies[] = $policy;
Expand All @@ -43,6 +52,9 @@ public function addDefaultPolicy(EmptyContentSecurityPolicy $policy) {
* @return ContentSecurityPolicy
*/
public function getDefaultPolicy(): ContentSecurityPolicy {
$event = new AddContentSecurityPolicyEvent($this);
$this->dispatcher->dispatch(AddContentSecurityPolicyEvent::class, $event);

$defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy();
foreach($this->policies as $policy) {
$defaultPolicy = $this->mergePolicies($defaultPolicy, $policy);
Expand Down
6 changes: 2 additions & 4 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,8 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerService(SessionStorage::class, function (Server $c) {
return new SessionStorage($c->getSession());
});
$this->registerService(\OCP\Security\IContentSecurityPolicyManager::class, function (Server $c) {
return new ContentSecurityPolicyManager();
});
$this->registerAlias('ContentSecurityPolicyManager', \OCP\Security\IContentSecurityPolicyManager::class);
$this->registerAlias(\OCP\Security\IContentSecurityPolicyManager::class, \OC\Security\CSP\ContentSecurityPolicyManager::class);
$this->registerAlias('ContentSecurityPolicyManager', \OC\Security\CSP\ContentSecurityPolicyManager::class);

$this->registerService('ContentSecurityPolicyNonceManager', function (Server $c) {
return new ContentSecurityPolicyNonceManager(
Expand Down
1 change: 1 addition & 0 deletions lib/public/IServerContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ public function getShareManager();
/**
* @return IContentSecurityPolicyManager
* @since 9.0.0
* @deprecated 17.0.0 Use the AddContentSecurityPolicyEvent
*/
public function getContentSecurityPolicyManager();

Expand Down
52 changes: 52 additions & 0 deletions lib/public/Security/CSP/AddContentSecurityPolicyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\Security\CSP;

use OC\Security\CSP\ContentSecurityPolicyManager;
use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
use Symfony\Component\EventDispatcher\Event;

/**
* @since 17.0.0
*/
class AddContentSecurityPolicyEvent extends Event {

/** @var ContentSecurityPolicyManager */
private $policyManager;

/**
* @since 17.0.0
*/
public function __construct(ContentSecurityPolicyManager $policyManager) {
$this->policyManager = $policyManager;
}

/**
* @since 17.0.0
*/
public function addPolicy(EmptyContentSecurityPolicy $csp): void {
$this->policyManager->addDefaultPolicy($csp);
}
}
2 changes: 2 additions & 0 deletions lib/public/Security/IContentSecurityPolicyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
* @package OCP\Security
* @since 9.0.0
* @deprecated 17.0.0 listen to the AddContentSecurityPolicyEvent to add a policy
*/
interface IContentSecurityPolicyManager {
/**
Expand All @@ -46,6 +47,7 @@ interface IContentSecurityPolicyManager {
*
* @param EmptyContentSecurityPolicy $policy
* @since 9.0.0
* @deprecated 17.0.0 listen to the AddContentSecurityPolicyEvent to add a policy
*/
public function addDefaultPolicy(EmptyContentSecurityPolicy $policy);
}
44 changes: 44 additions & 0 deletions tests/lib/Security/CSP/AddContentSecurityPolicyEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Test\Security\CSP;

use OC\Security\CSP\ContentSecurityPolicyManager;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use Test\TestCase;

class AddContentSecurityPolicyEventTest extends TestCase {
public function testAddEvent() {
$cspManager = $this->createMock(ContentSecurityPolicyManager::class);
$policy = $this->createMock(ContentSecurityPolicy::class);
$event = new AddContentSecurityPolicyEvent($cspManager);

$cspManager->expects($this->once())
->method('addDefaultPolicy')
->with($policy);

$event->addPolicy($policy);
}
}
53 changes: 51 additions & 2 deletions tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@


use OC\Security\CSP\ContentSecurityPolicyManager;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase;

class ContentSecurityPolicyManagerTest extends TestCase {
/** @var EventDispatcherInterface */
private $dispatcher;

class ContentSecurityPolicyManagerTest extends \Test\TestCase {
/** @var ContentSecurityPolicyManager */
private $contentSecurityPolicyManager;

public function setUp() {
parent::setUp();
$this->contentSecurityPolicyManager = new ContentSecurityPolicyManager();
$this->dispatcher = new EventDispatcher();
$this->contentSecurityPolicyManager = new ContentSecurityPolicyManager($this->dispatcher);
}

public function testAddDefaultPolicy() {
Expand Down Expand Up @@ -69,4 +78,44 @@ public function testGetDefaultPolicyWithPolicies() {
$this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
}

public function testGetDefaultPolicyWithPoliciesViaEvent() {
$this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) {
$policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
$policy->addAllowedFontDomain('mydomain.com');
$policy->addAllowedImageDomain('anotherdomain.de');

$e->addPolicy($policy);
});

$this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) {
$policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
$policy->addAllowedFontDomain('example.com');
$policy->addAllowedImageDomain('example.org');
$policy->allowInlineScript(true);
$policy->allowEvalScript(true);
$e->addPolicy($policy);
});

$this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function(AddContentSecurityPolicyEvent $e) {
$policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
$policy->addAllowedChildSrcDomain('childdomain');
$policy->addAllowedFontDomain('anotherFontDomain');
$e->addPolicy($policy);
});

$expected = new \OC\Security\CSP\ContentSecurityPolicy();
$expected->allowInlineScript(true);
$expected->allowEvalScript(true);
$expected->addAllowedFontDomain('mydomain.com');
$expected->addAllowedFontDomain('example.com');
$expected->addAllowedFontDomain('anotherFontDomain');
$expected->addAllowedImageDomain('anotherdomain.de');
$expected->addAllowedImageDomain('example.org');
$expected->addAllowedChildSrcDomain('childdomain');
$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'";

$this->assertEquals($expected, $this->contentSecurityPolicyManager->getDefaultPolicy());
$this->assertSame($expectedStringPolicy, $this->contentSecurityPolicyManager->getDefaultPolicy()->buildPolicy());
}

}

0 comments on commit 0518ff1

Please sign in to comment.