Passed
Push — master ( 564935...7d8694 )
by Christoph
02:36 queued 59s
created

Application::createCsp()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 18
rs 9.2222
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\SentryReporterAdapter;
28
use OCP\AppFramework\App;
29
use OCP\AppFramework\Http\ContentSecurityPolicy;
30
use OCP\EventDispatcher\IEventDispatcher;
31
use OCP\IConfig;
32
use OCP\IInitialStateService;
33
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
34
use OCP\Security\IContentSecurityPolicyManager;
35
use OCP\Support\CrashReport\IRegistry;
36
use function Sentry\init as initSentry;
37
38
class Application extends App {
39
40
	/**
41
	 * @param array $urlParams
42
	 */
43
	public function __construct($urlParams = []) {
44
		parent::__construct('sentry', $urlParams);
45
46
		$container = $this->getContainer();
47
48
		/* @var $config IConfig */
49
		$config = $container->query(IConfig::class);
50
		$dsn = $config->getSystemValue('sentry.dsn', null);
51
		$reportUrl = $config->getSystemValue('sentry.csp-report-url', null);
52
53
		if ($dsn !== '') {
54
			$this->registerClient($dsn);
55
		}
56
		$publicDsn = $config->getSystemValueString('sentry.public-dsn', '');
57
		$this->setInitialState($publicDsn);
58
59
		/** @var IEventDispatcher $dispatcher */
60
		$dispatcher = $container->query(IEventDispatcher::class);
61
		$dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $event) use ($reportUrl, $publicDsn) {
62
			$event->addPolicy($this->createCsp($publicDsn, $reportUrl));
63
		});
64
	}
65
66
	/**
67
	 * @param string $dsn
68
	 */
69
	private function registerClient(string $dsn): void {
70
		$container = $this->getContainer();
71
		/* @var $config IConfig */
72
		$config = $container->query(IConfig::class);
73
74
		initSentry([
75
			'dsn' => $dsn,
76
			'release' => $config->getSystemValue('version', '0.0.0'),
77
		]);
78
79
		/* @var $registry IRegistry */
80
		$registry = $container->query(IRegistry::class);
81
		$reporter = $container->query(SentryReporterAdapter::class);
82
		$registry->register($reporter);
83
	}
84
85
	private function createCsp(?string $publicDsn, ?string $reportUrl): ContentSecurityPolicy {
86
		$csp = new ContentSecurityPolicy();
87
		if ($publicDsn === null && $reportUrl === null) {
88
			// Don't add any custom CSP
89
			return $csp;
90
		}
91
92
		if ($publicDsn !== null) {
93
			$parsedUrl = parse_url($publicDsn);
94
			if (isset($parsedUrl['scheme'], $parsedUrl['host'])) {
95
				$domain = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
96
				$csp->addAllowedConnectDomain($domain);
97
			}
98
		}
99
		if ($reportUrl !== null) {
100
			$csp->addReportTo($reportUrl);
101
		}
102
		return $csp;
103
	}
104
105
	private function setInitialState(string $dsn): void {
106
		$container = $this->getContainer();
107
108
		/** @var IInitialStateService $stateService */
109
		$stateService = $container->query(IInitialStateService::class);
110
111
		$stateService->provideLazyInitialState('sentry', 'dsn', function () use ($dsn) {
112
			return [
113
				'dsn' => $dsn === '' ? null : $dsn,
114
			];
115
		});
116
	}
117
118
}
119