mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-01-22 04:08:13 -05:00
For an extension, I needed to call a script from an external domain. Unfortunately, the CSP headers didn't allow this domain and I had to patch manually the FreshRSS FrontController for my extension. It's obviously not a long-term solution since it has nothing to do in the core of FRSS, and I don't want to apply this patch manually at each update. With this patch, I allow changing the CSP header from inside the controller actions. It allows extensions to modify headers. It's also an opportunity to remove a bit of code from the FrontController. I wasn't happy with the previous implementation anyhow. Reference: https://github.com/flusio/xExtension-Flus/commit/ed12d56#diff-ff12e33ed31b23bda327499fa6e84eccR143
75 lines
1.8 KiB
PHP
75 lines
1.8 KiB
PHP
<?php
|
|
/**
|
|
* MINZ - Copyright 2011 Marien Fressinaud
|
|
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
/**
|
|
* La classe ActionController représente le contrôleur de l'application
|
|
*/
|
|
class Minz_ActionController {
|
|
protected $view;
|
|
private $csp_policies = array(
|
|
'default-src' => "'self'",
|
|
);
|
|
|
|
/**
|
|
* Constructeur
|
|
*/
|
|
public function __construct () {
|
|
$this->view = new Minz_View();
|
|
$view_path = Minz_Request::controllerName() . '/' . Minz_Request::actionName() . '.phtml';
|
|
$this->view->_path($view_path);
|
|
$this->view->attributeParams ();
|
|
}
|
|
|
|
/**
|
|
* Getteur
|
|
*/
|
|
public function view () {
|
|
return $this->view;
|
|
}
|
|
|
|
/**
|
|
* Set CSP policies.
|
|
*
|
|
* A default-src directive should always be given.
|
|
*
|
|
* References:
|
|
* - https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
|
|
* - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src
|
|
*
|
|
* @param array $policies An array where keys are directives and values are sources.
|
|
*/
|
|
protected function _csp($policies) {
|
|
if (!isset($policies['default-src'])) {
|
|
$action = Minz_Request::controllerName() . '#' . Minz_Request::actionName();
|
|
Minz_Log::warning(
|
|
"Default CSP policy is not declared for action {$action}.",
|
|
ADMIN_LOG
|
|
);
|
|
}
|
|
$this->csp_policies = $policies;
|
|
}
|
|
|
|
/**
|
|
* Send HTTP Content-Security-Policy header based on declared policies.
|
|
*/
|
|
public function declareCspHeader() {
|
|
$policies = [];
|
|
foreach ($this->csp_policies as $directive => $sources) {
|
|
$policies[] = $directive . ' ' . $sources;
|
|
}
|
|
header('Content-Security-Policy: ' . implode('; ', $policies));
|
|
}
|
|
|
|
/**
|
|
* Méthodes à redéfinir (ou non) par héritage
|
|
* firstAction est la première méthode exécutée par le Dispatcher
|
|
* lastAction est la dernière
|
|
*/
|
|
public function init () { }
|
|
public function firstAction () { }
|
|
public function lastAction () { }
|
|
}
|