Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: HTTP Response - Fix crash on CSP methods CSP is disabled #2506

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,10 @@ public function __construct($config)
// Also ensures that a Cache-control header exists.
$this->noCache();

// Are we enforcing a Content Security Policy?
if ($config->CSPEnabled === true)
{
$this->CSP = new ContentSecurityPolicy(new \Config\ContentSecurityPolicy());
$this->CSPEnabled = true;
}
// We need CSP object even if not enabled to avoid calls to non existing methods
$this->CSP = new ContentSecurityPolicy(new \Config\ContentSecurityPolicy());

$this->CSPEnabled = $config->CSPEnabled;
$this->cookiePrefix = $config->cookiePrefix;
$this->cookieDomain = $config->cookieDomain;
$this->cookiePath = $config->cookiePath;
Expand Down
17 changes: 15 additions & 2 deletions tests/system/HTTP/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class ContentSecurityPolicyTest extends \CIUnitTestCase
{

// Having this method as setUp() doesn't work - can't find Config\App !?
protected function prepare()
protected function prepare(bool $CSPEnabled = true)
{
$config = new App();
$config->CSPEnabled = true;
$config->CSPEnabled = $CSPEnabled;
$this->response = new Response($config);
$this->response->pretend(false);
$this->csp = $this->response->CSP;
Expand Down Expand Up @@ -490,4 +490,17 @@ public function testHeaderIgnoreCase()
$this->assertContains("base-uri 'self';", $result);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testCSPDisabled()
{
$this->prepare(false);
$result = $this->work();
$this->response->CSP->addStyleSrc('https://example.com');

$this->assertHeaderNotEmitted('content-security-policy', true);
}

}