Passed
Pull Request — master (#60)
by Christoph
05:22 queued 03:11
created

Application::addCsp()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 6
nop 3
dl 0
loc 26
rs 8.8333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * @author Christoph Wurst <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
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
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Sentry\AppInfo;
25
26
use OC;
27
use OCA\Sentry\Reporter\SentryReporterBreadcrumbAdapter;
0 ignored issues
show
Bug introduced by
The type OCA\Sentry\Reporter\Sent...porterBreadcrumbAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use OCA\Sentry\Reporter\SentryReporterAdapter;
29
use OCP\AppFramework\App;
30
use OCP\AppFramework\Http\ContentSecurityPolicy;
31
use OCP\IConfig;
32
use OCP\Security\IContentSecurityPolicyManager;
33
use OCP\Support\CrashReport\IRegistry;
34
use Raven_Client;
35
use Raven_ErrorHandler;
36
37
class Application extends App {
38
39
	/**
40
	 * @param array $urlParams
41
	 */
42
	public function __construct($urlParams = []) {
43
		parent::__construct('sentry', $urlParams);
44
45
		$container = $this->getContainer();
46
47
		/* @var $config IConfig */
48
		$config = $container->query(IConfig::class);
49
		/** @var IContentSecurityPolicyManager $cspManager */
50
		$cspManager = $container->query(IContentSecurityPolicyManager::class);
51
52
		$dsn = $config->getSystemValue('sentry.dsn', null);
53
		$reportUrl = $config->getSystemValue('sentry.csp-report-url', null);
54
		if (!is_null($dsn)) {
55
			$this->registerClient($config, $dsn);
56
		}
57
		$publicDsn = $config->getSystemValue('sentry.public-dsn', null);
58
		$this->addCsp($cspManager, $publicDsn, $reportUrl);
59
	}
60
61
	/**
62
	 * @param string $dsn
63
	 */
64
	private function registerClient(IConfig $config, string $dsn) {
65
		$container = $this->getContainer();
66
67
		$client = new Raven_Client($dsn);
68
		$client->setRelease($config->getSystemValue('version', '0.0.0'));
69
		$container->registerService(Raven_Client::class, function () use ($client) {
70
			return $client;
71
		});
72
73
		/* @var $registry IRegistry */
74
		$registry = $container->query(IRegistry::class);
75
		$reporter = $container->query(SentryReporterAdapter::class);
76
		$registry->register($reporter);
77
78
		$this->registerErrorHandlers($client);
79
	}
80
81
	private function registerErrorHandlers(Raven_Client $client) {
82
		$errorHandler = new Raven_ErrorHandler($client);
83
		$errorHandler->registerExceptionHandler();
84
		$errorHandler->registerErrorHandler();
85
		$errorHandler->registerShutdownFunction();
86
	}
87
88
	public function addCsp(IContentSecurityPolicyManager $cspManager,
89
						   string $publicDsn = null,
90
						   string $reportUrl = null) {
91
		if (is_null($publicDsn) && is_null($reportUrl)) {
92
			// Don't add any custom CSP
93
			return;
94
		}
95
96
		$csp = new ContentSecurityPolicy();
0 ignored issues
show
Deprecated Code introduced by
The class OCP\AppFramework\Http\ContentSecurityPolicy has been deprecated: 14.0.0 Use one of our stricter CSP policies ( Ignorable by Annotation )

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

96
		$csp = /** @scrutinizer ignore-deprecated */ new ContentSecurityPolicy();
Loading history...
97
98
		if (!is_null($publicDsn)) {
99
			$parsedUrl = parse_url($publicDsn);
100
			if (!isset($parsedUrl['scheme']) || !isset($parsedUrl['host'])) {
101
				// Misconfigured setup -> ignore
102
				return;
103
			}
104
105
			$domain = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
106
			$csp->addAllowedConnectDomain($domain);
107
		}
108
109
		if (!is_null($reportUrl)) {
110
			$csp->addReportTo($reportUrl);
0 ignored issues
show
Bug introduced by
The method addReportTo() does not exist on OCP\AppFramework\Http\ContentSecurityPolicy. ( Ignorable by Annotation )

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

110
			$csp->/** @scrutinizer ignore-call */ 
111
         addReportTo($reportUrl);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
		}
112
113
		$cspManager->addDefaultPolicy($csp);
114
	}
115
116
}
117