refactor: page controller csp together

pull/363/head
Varun Patil 2023-01-17 19:09:02 -08:00
parent 24a3b8c638
commit 8bd1834214
3 changed files with 27 additions and 52 deletions

View File

@ -90,12 +90,18 @@ class PageController extends Controller
$this->initialState->provideInitialState('facerecognitionEnabled', \OCA\Memories\Util::facerecognitionIsEnabled($this->config, $uid)); $this->initialState->provideInitialState('facerecognitionEnabled', \OCA\Memories\Util::facerecognitionIsEnabled($this->config, $uid));
$this->initialState->provideInitialState('albums', \OCA\Memories\Util::albumsIsEnabled($this->appManager)); $this->initialState->provideInitialState('albums', \OCA\Memories\Util::albumsIsEnabled($this->appManager));
// App version // Common state
$this->initialState->provideInitialState('version', $this->appManager->getAppInfo('memories')['version']); self::provideCommonInitialState($this->initialState);
// Video configuration $response = new TemplateResponse($this->appName, 'main');
$this->initialState->provideInitialState('notranscode', $this->config->getSystemValue('memories.no_transcode', 'UNSET')); $response->setContentSecurityPolicy(self::getCSP());
return $response;
}
/** Get the common content security policy */
public static function getCSP()
{
$policy = new ContentSecurityPolicy(); $policy = new ContentSecurityPolicy();
$policy->addAllowedWorkerSrcDomain("'self'"); $policy->addAllowedWorkerSrcDomain("'self'");
$policy->addAllowedScriptDomain("'self'"); $policy->addAllowedScriptDomain("'self'");
@ -112,10 +118,20 @@ class PageController extends Controller
$policy->addAllowedConnectDomain('nominatim.openstreetmap.org'); $policy->addAllowedConnectDomain('nominatim.openstreetmap.org');
$policy->addAllowedFrameDomain('www.openstreetmap.org'); $policy->addAllowedFrameDomain('www.openstreetmap.org');
$response = new TemplateResponse($this->appName, 'main'); return $policy;
$response->setContentSecurityPolicy($policy); }
return $response; /** Provide initial state for all pages */
public static function provideCommonInitialState(IInitialState &$initialState)
{
$appManager = \OC::$server->get(\OCP\App\IAppManager::class);
$config = \OC::$server->get(\OCP\IConfig::class);
// App version
$initialState->provideInitialState('version', $appManager->getAppInfo('memories')['version']);
// Video configuration
$initialState->provideInitialState('notranscode', $config->getSystemValue('memories.no_transcode', 'UNSET'));
} }
/** /**

View File

@ -6,7 +6,6 @@ use OCA\Files\Event\LoadSidebar;
use OCA\Memories\Db\TimelineQuery; use OCA\Memories\Db\TimelineQuery;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\Template\PublicTemplateResponse; use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState; use OCP\AppFramework\Services\IInitialState;
@ -59,30 +58,12 @@ class PublicAlbumController extends Controller
// Scripts // Scripts
Util::addScript($this->appName, 'memories-main'); Util::addScript($this->appName, 'memories-main');
$this->eventDispatcher->dispatchTyped(new LoadSidebar()); $this->eventDispatcher->dispatchTyped(new LoadSidebar());
PageController::provideCommonInitialState($this->initialState);
$this->initialState->provideInitialState('version', $this->appManager->getAppInfo('memories')['version']);
$this->initialState->provideInitialState('notranscode', $this->config->getSystemValue('memories.no_transcode', 'UNSET'));
$policy = new ContentSecurityPolicy();
$policy->addAllowedWorkerSrcDomain("'self'");
$policy->addAllowedScriptDomain("'self'");
// Video player
$policy->addAllowedWorkerSrcDomain('blob:');
$policy->addAllowedScriptDomain('blob:');
$policy->addAllowedMediaDomain('blob:');
// Image editor
$policy->addAllowedConnectDomain('data:');
// Allow nominatim for metadata
$policy->addAllowedConnectDomain('nominatim.openstreetmap.org');
$policy->addAllowedFrameDomain('www.openstreetmap.org');
$response = new PublicTemplateResponse($this->appName, 'main'); $response = new PublicTemplateResponse($this->appName, 'main');
$response->setHeaderTitle($album['name']); $response->setHeaderTitle($album['name']);
$response->setFooterVisible(false); // wth is that anyway? $response->setFooterVisible(false); // wth is that anyway?
$response->setContentSecurityPolicy($policy); $response->setContentSecurityPolicy(PageController::getCSP());
return $response; return $response;
} }

View File

@ -5,7 +5,6 @@ namespace OCA\Memories\Controller;
use OCA\Files\Event\LoadSidebar; use OCA\Files\Event\LoadSidebar;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\AppFramework\AuthPublicShareController; use OCP\AppFramework\AuthPublicShareController;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\Template\PublicTemplateResponse; use OCP\AppFramework\Http\Template\PublicTemplateResponse;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState; use OCP\AppFramework\Services\IInitialState;
@ -102,36 +101,15 @@ class PublicController extends AuthPublicShareController
// Scripts // Scripts
Util::addScript($this->appName, 'memories-main'); Util::addScript($this->appName, 'memories-main');
$this->eventDispatcher->dispatchTyped(new LoadSidebar()); $this->eventDispatcher->dispatchTyped(new LoadSidebar());
PageController::provideCommonInitialState($this->initialState);
// App version
$this->initialState->provideInitialState('version', $this->appManager->getAppInfo('memories')['version']);
// Video configuration
$this->initialState->provideInitialState('notranscode', $this->config->getSystemValue('memories.no_transcode', 'UNSET'));
// Share info // Share info
$this->initialState->provideInitialState('no_download', $share->getHideDownload()); $this->initialState->provideInitialState('no_download', $share->getHideDownload());
$policy = new ContentSecurityPolicy();
$policy->addAllowedWorkerSrcDomain("'self'");
$policy->addAllowedScriptDomain("'self'");
// Video player
$policy->addAllowedWorkerSrcDomain('blob:');
$policy->addAllowedScriptDomain('blob:');
$policy->addAllowedMediaDomain('blob:');
// Image editor
$policy->addAllowedConnectDomain('data:');
// Allow nominatim for metadata
$policy->addAllowedConnectDomain('nominatim.openstreetmap.org');
$policy->addAllowedFrameDomain('www.openstreetmap.org');
$response = new PublicTemplateResponse($this->appName, 'main'); $response = new PublicTemplateResponse($this->appName, 'main');
$response->setHeaderTitle($share->getNode()->getName()); $response->setHeaderTitle($share->getNode()->getName());
$response->setFooterVisible(false); // wth is that anyway? $response->setFooterVisible(false); // wth is that anyway?
$response->setContentSecurityPolicy($policy); $response->setContentSecurityPolicy(PageController::getCSP());
return $response; return $response;
} }