Skip to content

Commit

Permalink
Add csp in a better way
Browse files Browse the repository at this point in the history
Signed-off-by: Micke Nordin <[email protected]>
  • Loading branch information
mickenordin committed Aug 19, 2024
1 parent a849f4d commit 7d0751d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project_dir=$(CURDIR)/$(app_name)
build_dir=$(CURDIR)/build/artifacts
sign_dir=$(build_dir)/sign
package_name=$(app_name)
version+=0.1.1
version+=0.1.2

all: appstore
release: appstore
Expand All @@ -31,7 +31,7 @@ sign: package
docker cp nextcloud:/var/www/html/custom_apps/$(app_name)-$(version).tar.gz $(build_dir)/$(app_name)-$(version).tar.gz
sleep 3
docker kill nextcloud
openssl dgst -sha512 -sign $(cert_dir)/$(app_name).key $(build_dir)/$(app_name)-0.1.1.tar.gz | openssl base64
openssl dgst -sha512 -sign $(cert_dir)/$(app_name).key $(build_dir)/$(app_name)-$(version).tar.gz | openssl base64

appstore: sign

Expand Down
2 changes: 1 addition & 1 deletion integration_jupyterhub/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<description>
<![CDATA[Integrate Jupyther Hub into Nextcloud]]>
</description>
<version>0.1.1</version>
<version>0.1.2</version>
<licence>agpl</licence>
<author mail="[email protected]" homepage="https://github.com/SUNET/nextcloud-jupyter">Mikael Nordin</author>
<namespace>Jupyter</namespace>
Expand Down
24 changes: 19 additions & 5 deletions integration_jupyterhub/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
<?php

declare(strict_types=1);
// SPDX-FileCopyrightText: Mikael Nordin <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\Jupyter\AppInfo;

use OCA\Jupyter\Listener\CSPListener;

use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;

class Application extends App {
public const APP_ID = 'integration_jupyterhub';
class Application extends App implements IBootstrap
{
public const APP_ID = 'integration_jupyterhub';

public function __construct() {
parent::__construct(self::APP_ID);
}
public function __construct()
{
parent::__construct(self::APP_ID);
}
public function register(IRegistrationContext $context): void
{
$context->registerEventListener(AddContentSecurityPolicyEvent::class, CSPListener::class);
}
public function boot(IBootContext $context): void {}
}
11 changes: 0 additions & 11 deletions integration_jupyterhub/lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ public function __construct(
*/
public function index(): TemplateResponse
{
//Util::addScript(Application::APP_ID, 'jupyter-main');
$policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();

$parsed_url = parse_url($this->jupyter_url);

$http = $parsed_url["scheme"] . "://" . $parsed_url["host"];
$policy->addAllowedConnectDomain($http);
$policy->addAllowedScriptDomain($http);
$policy->addAllowedFrameDomain($http);
\OC::$server->getContentSecurityPolicyManager()->addDefaultPolicy($policy);


$params = [
'user_id' => $this->userId,
Expand Down
45 changes: 45 additions & 0 deletions integration_jupyterhub/lib/Listener/CSPListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Jupyter\Listener;

use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use Psr\Log\LoggerInterface;
use OCP\IConfig;

class CSPListener implements IEventListener
{
protected string $appName;
private string $jupyter_url;
public function __construct(
private IConfig $config,
private LoggerInterface $logger
) {
$this->jupyter_url = $config->getAppValue($this->appName, 'jupyter_url') . '/hub/home';
$this->appName = "integration_jupyterhub";
}

public function handle(Event $event): void
{
$this->logger->debug('Adding CSP for Jupyter', ['app' => 'integration_jupyterhub']);
if (!($event instanceof AddContentSecurityPolicyEvent)) {
return;
}
$csp = new ContentSecurityPolicy();
$url = parse_url($this->jupyter_url);
$http = $url["scheme"] . "://" . $url["host"];
$csp->addAllowedConnectDomain($http);
$csp->addAllowedScriptDomain($http);
$csp->addAllowedFrameDomain($http);

$event->addPolicy($csp);
}
}
2 changes: 1 addition & 1 deletion integration_jupyterhub/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "integration_jupyterhub",
"description": "Integrate Jupyther Hub into Nextcloud",
"version": "0.1.1",
"version": "0.1.2",
"author": "Micke Nordin <[email protected]>",
"contributors": [],
"bugs": {
Expand Down

0 comments on commit 7d0751d

Please sign in to comment.