Skip to content

Commit

Permalink
Merge pull request #6201 from kenjis/fix-csp-reportOnly
Browse files Browse the repository at this point in the history
fix: CSP reportOnly behavior
  • Loading branch information
kenjis authored Jul 1, 2022
2 parents a1e8197 + 6b61e3c commit 516e7dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 6 additions & 4 deletions system/HTTP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ protected function buildHeaders(ResponseInterface $response)
protected function addToHeader(string $name, $values = null)
{
if (is_string($values)) {
$values = [$values => 0];
$values = [$values => $this->reportOnly];
}

$sources = [];
Expand All @@ -785,13 +785,15 @@ protected function addToHeader(string $name, $values = null)
foreach ($values as $value => $reportOnly) {
if (is_numeric($value) && is_string($reportOnly) && ! empty($reportOnly)) {
$value = $reportOnly;
$reportOnly = 0;
$reportOnly = $this->reportOnly;
}

if (strpos($value, 'nonce-') === 0) {
$value = "'{$value}'";
}

if ($reportOnly === true) {
$reportSources[] = in_array($value, $this->validSources, true) ? "'{$value}'" : $value;
} elseif (strpos($value, 'nonce-') === 0) {
$sources[] = "'{$value}'";
} else {
$sources[] = in_array($value, $this->validSources, true) ? "'{$value}'" : $value;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/system/HTTP/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function testConnectSrc()
$result = $this->work();

$result = $this->getHeaderEmitted('Content-Security-Policy-Report-Only');
$this->assertStringContainsString('connect-src iffy.com maybe.com;', $result);
$this->assertStringContainsString("connect-src 'self' iffy.com maybe.com;", $result);
}

/**
Expand Down Expand Up @@ -165,9 +165,10 @@ public function testFormAction()
$result = $this->work();

$result = $this->getHeaderEmitted('Content-Security-Policy-Report-Only');
$this->assertStringContainsString('form-action surveysrus.com;', $result);
$this->assertStringContainsString("form-action 'self' surveysrus.com;", $result);

$result = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertStringContainsString("form-action 'self';", $result);
$this->assertStringNotContainsString("form-action 'self';", $result);
}

/**
Expand Down

0 comments on commit 516e7dd

Please sign in to comment.