Skip to content

Commit 516e7dd

Browse files
authored
Merge pull request #6201 from kenjis/fix-csp-reportOnly
fix: CSP reportOnly behavior
2 parents a1e8197 + 6b61e3c commit 516e7dd

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

system/HTTP/ContentSecurityPolicy.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ protected function buildHeaders(ResponseInterface $response)
776776
protected function addToHeader(string $name, $values = null)
777777
{
778778
if (is_string($values)) {
779-
$values = [$values => 0];
779+
$values = [$values => $this->reportOnly];
780780
}
781781

782782
$sources = [];
@@ -785,13 +785,15 @@ protected function addToHeader(string $name, $values = null)
785785
foreach ($values as $value => $reportOnly) {
786786
if (is_numeric($value) && is_string($reportOnly) && ! empty($reportOnly)) {
787787
$value = $reportOnly;
788-
$reportOnly = 0;
788+
$reportOnly = $this->reportOnly;
789+
}
790+
791+
if (strpos($value, 'nonce-') === 0) {
792+
$value = "'{$value}'";
789793
}
790794

791795
if ($reportOnly === true) {
792796
$reportSources[] = in_array($value, $this->validSources, true) ? "'{$value}'" : $value;
793-
} elseif (strpos($value, 'nonce-') === 0) {
794-
$sources[] = "'{$value}'";
795797
} else {
796798
$sources[] = in_array($value, $this->validSources, true) ? "'{$value}'" : $value;
797799
}

tests/system/HTTP/ContentSecurityPolicyTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function testConnectSrc()
132132
$result = $this->work();
133133

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

138138
/**
@@ -165,9 +165,10 @@ public function testFormAction()
165165
$result = $this->work();
166166

167167
$result = $this->getHeaderEmitted('Content-Security-Policy-Report-Only');
168-
$this->assertStringContainsString('form-action surveysrus.com;', $result);
168+
$this->assertStringContainsString("form-action 'self' surveysrus.com;", $result);
169+
169170
$result = $this->getHeaderEmitted('Content-Security-Policy');
170-
$this->assertStringContainsString("form-action 'self';", $result);
171+
$this->assertStringNotContainsString("form-action 'self';", $result);
171172
}
172173

173174
/**

0 commit comments

Comments
 (0)