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

fix: CSP reportOnly behavior #6201

Merged
merged 1 commit into from
Jul 1, 2022
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
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