Passed
Pull Request — master (#22)
by Christoph
03:29
created

Application::addCsp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\Sentry\AppInfo;
24
25
use OC;
26
use OCA\Sentry\Reporter\SentryReporterAdapter;
27
use OCP\AppFramework\App;
28
use OCP\AppFramework\Http\ContentSecurityPolicy;
29
use OCP\IConfig;
30
use OCP\Security\IContentSecurityPolicyManager;
31
use OCP\Support\CrashReport\IRegistry;
32
use Raven_Client;
33
use Raven_ErrorHandler;
34
35
class Application extends App {
36
37
	/**
38
	 * @param array $urlParams
39
	 */
40
	public function __construct($urlParams = []) {
41
		parent::__construct('sentry', $urlParams);
42
43
		$container = $this->getContainer();
44
45
		/* @var $config IConfig */
46
		$config = $container->query(IConfig::class);
47
		$dsn = $config->getSystemValue('sentry.dsn', null);
48
		if (!is_null($dsn)) {
49
			$this->registerClient($dsn);
50
		}
51
		$publicDsn = $config->getSystemValue('sentry.public-dsn', null);
52
		if (!is_null($publicDsn)) {
53
			$this->addCsp($publicDsn);
54
		}
55
	}
56
57
	/**
58
	 * @param string $dsn
59
	 */
60
	private function registerClient($dsn) {
61
		$container = $this->getContainer();
62
		/* @var $config IConfig */
63
		$config = $container->query(IConfig::class);
64
65
		$client = new Raven_Client($dsn);
66
		$client->setRelease($config->getSystemValue('version', '0.0.0'));
67
		$container->registerService(Raven_Client::class, function() use ($client) {
68
			return $client;
69
		});
70
71
		/* @var $registry IRegistry */
72
		$registry = $container->query(IRegistry::class);
73
		$reporter = $container->query(SentryReporterAdapter::class);
74
		$registry->register($reporter);
75
76
		$this->registerErrorHandlers($client);
77
	}
78
79
	private function registerErrorHandlers(Raven_Client $client) {
80
		$errorHandler = new Raven_ErrorHandler($client);
81
		$errorHandler->registerExceptionHandler();
82
		$errorHandler->registerErrorHandler();
83
		$errorHandler->registerShutdownFunction();
84
	}
85
86
	public function addCsp($publicDsn) {
87
		$parsedUrl = parse_url($publicDsn);
88
		if (!isset($parsedUrl['scheme']) || !isset($parsedUrl['host'])) {
89
			// Misconfigured setup -> ignore
90
			return;
91
		}
92
93
		$domain = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
94
		$csp = new ContentSecurityPolicy();
95
		$csp->addAllowedConnectDomain($domain);
96
		$cspManager = OC::$server->query(IContentSecurityPolicyManager::class);
97
		$cspManager->addDefaultPolicy($csp);
98
	}
99
100
}
101