Passed
Push — master ( 5fdeac...5cef89 )
by Roeland
19:23 queued 08:48
created

ContentSecurityPolicyManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OC\Security\CSP\ContentSecurityPolicy. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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 {
0 ignored issues
show
Deprecated Code introduced by
The interface OCP\Security\IContentSecurityPolicyManager has been deprecated: 17.0.0 listen to the AddContentSecurityPolicyEvent to add a policy ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

32
class ContentSecurityPolicyManager implements /** @scrutinizer ignore-deprecated */ IContentSecurityPolicyManager {

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
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