Allow plugins to override CSP rules

This commit is contained in:
Frederic Guillot
2015-10-10 18:59:06 -04:00
parent e3521db6a8
commit 0e233673e3
4 changed files with 34 additions and 2 deletions

View File

@@ -80,7 +80,7 @@ abstract class Base extends \Core\Base
private function sendHeaders($action) private function sendHeaders($action)
{ {
// HTTP secure headers // HTTP secure headers
$this->response->csp(array('style-src' => "'self' 'unsafe-inline'", 'img-src' => '* data:')); $this->response->csp($this->container['cspRules']);
$this->response->nosniff(); $this->response->nosniff();
$this->response->xss(); $this->response->xss();

View File

@@ -18,6 +18,17 @@ abstract class Base extends \Core\Base
*/ */
abstract public function initialize(); abstract public function initialize();
/**
* Override default CSP rules
*
* @access public
* @param array $rules
*/
public function setContentSecurityPolicy(array $rules)
{
$this->container['cspRules'] = $rules;
}
/** /**
* Returns all classes that needs to be stored in the DI container * Returns all classes that needs to be stored in the DI container
* *

View File

@@ -126,5 +126,7 @@ class ClassProvider implements ServiceProviderInterface
}; };
$container['pluginLoader'] = new Loader($container); $container['pluginLoader'] = new Loader($container);
$container['cspRules'] = array('style-src' => "'self' 'unsafe-inline'", 'img-src' => '* data:');
} }
} }

View File

@@ -329,6 +329,25 @@ $this->on('session.bootstrap', function($container) {
The translations must be stored in `plugins/Myplugin/Locale/xx_XX/translations.php`. The translations must be stored in `plugins/Myplugin/Locale/xx_XX/translations.php`.
Override HTTP Content Security Policy
-------------------------------------
If you would like to replace the default HTTP Content Security Policy header, you can use the method `setContentSecurityPolicy()`:
```php
<?php
namespace Plugin\Csp;
class Plugin extends \Core\Plugin\Base
{
public function initialize()
{
$this->setContentSecurityPolicy(array('script-src' => 'something'));
}
}
```
Dependency Injection Container Dependency Injection Container
------------------------------ ------------------------------