Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to new bootstrap mechanism #207

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<repository>https://github.com/ChristophWurst/nextcloud_sentry</repository>
<dependencies>
<php min-version="7.2" max-version="7.4" />
<nextcloud min-version="17" max-version="19" />
<nextcloud min-version="20" max-version="20" />
</dependencies>
<commands>
<command>OCA\Sentry\Command\Test</command>
Expand Down
111 changes: 43 additions & 68 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

/**
Expand All @@ -23,95 +24,69 @@

namespace OCA\Sentry\AppInfo;

use OCA\Sentry\Config;
use OCA\Sentry\Reporter\RecursionAwareReporter;
use OCA\Sentry\Reporter\SentryReporterAdapter;
use OCP\AppFramework\App;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\IConfig;
use OCP\IContainer;
use OCP\IInitialStateService;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use OCP\Support\CrashReport\IRegistry;
use function Sentry\init as initSentry;

class Application extends App {
class Application extends App implements IBootstrap {

/**
* @param array $urlParams
*/
public function __construct($urlParams = []) {
parent::__construct('sentry', $urlParams);
public const APP_ID = 'sentry';

$container = $this->getContainer();
public function __construct() {
parent::__construct(self::APP_ID, []);
}

/* @var $config IConfig */
$config = $container->query(IConfig::class);
$publicDsn = $config->getSystemValueString('sentry.public-dsn', '');
$dsn = $config->getSystemValue('sentry.dsn', $publicDsn);
$reportUrl = $config->getSystemValue('sentry.csp-report-url', null);
public function register(IRegistrationContext $context): void {
$context->registerCrashReporter(RecursionAwareReporter::class);

if ($dsn !== '') {
$this->registerClient($dsn);
}
$this->setInitialState($publicDsn);
$context->registerService(RecursionAwareReporter::class, function (IContainer $c) {
/** @var SentryReporterAdapter $reporter */
$reporter = $c->query(SentryReporterAdapter::class);

/** @var IEventDispatcher $dispatcher */
$dispatcher = $container->query(IEventDispatcher::class);
$dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $event) use ($reportUrl, $publicDsn) {
$event->addPolicy($this->createCsp($publicDsn, $reportUrl));
return new RecursionAwareReporter($reporter);
});
}

/**
* @param string $dsn
*/
private function registerClient(string $dsn): void {
$container = $this->getContainer();
/* @var $config IConfig */
$config = $container->query(IConfig::class);

initSentry([
'dsn' => $dsn,
'release' => $config->getSystemValue('version', '0.0.0'),
]);

/* @var $registry IRegistry */
$registry = $container->query(IRegistry::class);
$reporter = $container->query(SentryReporterAdapter::class);
$registry->register(new RecursionAwareReporter($reporter));
public function boot(IBootContext $context): void {
$this->initSentry(
$context->getAppContainer()->query(IConfig::class),
$context->getAppContainer()->query(Config::class)
);
$this->setInitialState(
$context->getAppContainer()->query(IInitialStateService::class),
$context->getAppContainer()->query(Config::class)
);
}

private function createCsp(?string $publicDsn, ?string $reportUrl): ContentSecurityPolicy {
$csp = new ContentSecurityPolicy();
if ($publicDsn === null && $reportUrl === null) {
// Don't add any custom CSP
return $csp;
}

if ($publicDsn !== null) {
$parsedUrl = parse_url($publicDsn);
if (isset($parsedUrl['scheme'], $parsedUrl['host'])) {
$domain = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
$csp->addAllowedConnectDomain($domain);
}
}
if ($reportUrl !== null) {
$csp->addReportTo($reportUrl);
}
return $csp;
private function setInitialState(IInitialStateService $stateService,
Config $config): void {
$stateService->provideLazyInitialState('sentry', 'dsn', function () use ($config) {
return [
'dsn' => $config->getDsn(),
];
});
}

private function setInitialState(string $dsn): void {
$container = $this->getContainer();
private function initSentry(IConfig $sysConfig,
Config $config) {
$dsn = $config->getDsn();

/** @var IInitialStateService $stateService */
$stateService = $container->query(IInitialStateService::class);
if ($dsn === null) {
return;
}

$stateService->provideLazyInitialState('sentry', 'dsn', function () use ($dsn) {
return [
'dsn' => $dsn === '' ? null : $dsn,
];
});
initSentry([
'dsn' => $dsn,
'release' => $sysConfig->getSystemValue('version', '0.0.0'),
]);
}

}
53 changes: 53 additions & 0 deletions lib/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/**
* @copyright 2020 Christoph Wurst <[email protected]>
*
* @author 2020 Christoph Wurst <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Sentry;

use OCP\IConfig;

class Config {

/** @var IConfig */
private $config;

public function __construct(IConfig $config) {
$this->config = $config;
}

public function getDsn(): ?string {
$dsn = $this->config->getSystemValue('sentry.dsn', $this->getPublicDsn());
return $dsn === '' ? null : $dsn;
}

public function getPublicDsn(): ?string {
$publicDsn = $this->config->getSystemValueString('sentry.public-dsn');
return $publicDsn === '' ? null : $publicDsn;
}

public function getReportUrl(): ?string {
return $this->config->getSystemValue('sentry.csp-report-url', null);
}

}
76 changes: 76 additions & 0 deletions lib/Listener/AddSentryCspListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

/**
* @copyright 2020 Christoph Wurst <[email protected]>
*
* @author 2020 Christoph Wurst <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Sentry\Listener;

use OCA\Sentry\Config;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use function parse_url;

class AddSentryCspListener implements IEventListener {

/** @var Config */
private $config;

public function __construct(Config $config) {
$this->config = $config;
}

public function handle(Event $event): void {
if (!($event instanceof AddContentSecurityPolicyEvent)) {
return;
}

$event->addPolicy(
$this->createCsp(
$this->config->getPublicDsn(),
$this->config->getReportUrl()
)
);
}

private function createCsp(?string $publicDsn, ?string $reportUrl): ContentSecurityPolicy {
$csp = new ContentSecurityPolicy();
if ($publicDsn === null && $reportUrl === null) {
// Don't add any custom CSP
return $csp;
}

if ($publicDsn !== null) {
$parsedUrl = parse_url($publicDsn);
if (isset($parsedUrl['scheme'], $parsedUrl['host'])) {
$domain = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
$csp->addAllowedConnectDomain($domain);
}
}
if ($reportUrl !== null) {
$csp->addReportTo($reportUrl);
}
return $csp;
}
}