|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Lukas Reschke <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license AGPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
12
|
|
|
* as published by the Free Software Foundation. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OC\Security\CSP; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy; |
|
|
|
|
|
|
27
|
|
|
use OCP\AppFramework\Http\EmptyContentSecurityPolicy; |
|
28
|
|
|
use OCP\EventDispatcher\IEventDispatcher; |
|
29
|
|
|
use OCP\Security\CSP\AddContentSecurityPolicyEvent; |
|
30
|
|
|
use OCP\Security\IContentSecurityPolicyManager; |
|
31
|
|
|
|
|
32
|
|
|
class ContentSecurityPolicyManager implements IContentSecurityPolicyManager { |
|
|
|
|
|
|
33
|
|
|
/** @var ContentSecurityPolicy[] */ |
|
34
|
|
|
private $policies = []; |
|
35
|
|
|
|
|
36
|
|
|
/** @var IEventDispatcher */ |
|
37
|
|
|
private $dispatcher; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct(IEventDispatcher $dispatcher) { |
|
40
|
|
|
$this->dispatcher = $dispatcher; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** {@inheritdoc} */ |
|
44
|
|
|
public function addDefaultPolicy(EmptyContentSecurityPolicy $policy) { |
|
45
|
|
|
$this->policies[] = $policy; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the configured default policy. This is not in the public namespace |
|
50
|
|
|
* as it is only supposed to be used by core itself. |
|
51
|
|
|
* |
|
52
|
|
|
* @return ContentSecurityPolicy |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getDefaultPolicy(): ContentSecurityPolicy { |
|
55
|
|
|
$event = new AddContentSecurityPolicyEvent($this); |
|
56
|
|
|
$this->dispatcher->dispatch(AddContentSecurityPolicyEvent::class, $event); |
|
57
|
|
|
|
|
58
|
|
|
$defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy(); |
|
59
|
|
|
foreach($this->policies as $policy) { |
|
60
|
|
|
$defaultPolicy = $this->mergePolicies($defaultPolicy, $policy); |
|
61
|
|
|
} |
|
62
|
|
|
return $defaultPolicy; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Merges the first given policy with the second one |
|
67
|
|
|
* |
|
68
|
|
|
* @param ContentSecurityPolicy $defaultPolicy |
|
69
|
|
|
* @param EmptyContentSecurityPolicy $originalPolicy |
|
70
|
|
|
* @return ContentSecurityPolicy |
|
71
|
|
|
*/ |
|
72
|
|
|
public function mergePolicies(ContentSecurityPolicy $defaultPolicy, |
|
73
|
|
|
EmptyContentSecurityPolicy $originalPolicy): ContentSecurityPolicy { |
|
74
|
|
|
foreach((object)(array)$originalPolicy as $name => $value) { |
|
75
|
|
|
$setter = 'set'.ucfirst($name); |
|
76
|
|
|
if(\is_array($value)) { |
|
77
|
|
|
$getter = 'get'.ucfirst($name); |
|
78
|
|
|
$currentValues = \is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : []; |
|
79
|
|
|
$defaultPolicy->$setter(array_values(array_unique(array_merge($currentValues, $value)))); |
|
80
|
|
|
} elseif (\is_bool($value)) { |
|
81
|
|
|
$defaultPolicy->$setter($value); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $defaultPolicy; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: