From 1e156160518540bcdac8468107c07320462e35f3 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 18 Aug 2020 13:05:29 +0200 Subject: [PATCH] Add the framesrc csp by default Since we want to iframe the pdfviewer we need to properly set it. Else it might not work on some pages. Signed-off-by: Roeland Jago Douma --- lib/AppInfo/Application.php | 3 +++ lib/Listeners/CSPListener.php | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 lib/Listeners/CSPListener.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 582c72aa..9656e6af 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -27,12 +27,14 @@ namespace OCA\Files_PDFViewer\AppInfo; +use OCA\Files_PDFViewer\Listeners\CSPListener; use OCA\Files_PDFViewer\Listeners\LoadViewerListener; use OCA\Viewer\Event\LoadViewer; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Security\CSP\AddContentSecurityPolicyEvent; use OCP\Util; class Application extends App implements IBootstrap { @@ -44,6 +46,7 @@ public function __construct() { public function register(IRegistrationContext $context): void { $context->registerEventListener(LoadViewer::class, LoadViewerListener::class); + $context->registerEventListener(AddContentSecurityPolicyEvent::class, CSPListener::class); } public function boot(IBootContext $context): void { diff --git a/lib/Listeners/CSPListener.php b/lib/Listeners/CSPListener.php new file mode 100644 index 00000000..8802aaea --- /dev/null +++ b/lib/Listeners/CSPListener.php @@ -0,0 +1,43 @@ + + * + * @author Roeland Jago Douma + * + * @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 . + * + */ + +namespace OCA\Files_PDFViewer\Listeners; + +use OCP\AppFramework\Http\EmptyContentSecurityPolicy; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Security\CSP\AddContentSecurityPolicyEvent; + +class CSPListener implements IEventListener { + public function handle(Event $event): void { + if (!$event instanceof AddContentSecurityPolicyEvent) { + return; + } + + $csp = new EmptyContentSecurityPolicy(); + $csp->addAllowedFrameDomain('\'self\''); + $event->addPolicy($csp); + } +}