Skip to content

Commit e5719b5

Browse files
authored
fix: omit empty CSP headers (#10412)
1 parent b635434 commit e5719b5

3 files changed

Lines changed: 75 additions & 9 deletions

File tree

‎system/HTTP/ContentSecurityPolicy.php‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,6 @@ protected function buildHeaders(ResponseInterface $response)
941941
return;
942942
}
943943

944-
$response->setHeader('Content-Security-Policy', []);
945-
$response->setHeader('Content-Security-Policy-Report-Only', []);
946-
$response->setHeader('Reporting-Endpoints', []);
947-
948944
if (in_array($this->baseURI, ['', null, []], true)) {
949945
$this->baseURI = 'self';
950946
}
@@ -967,9 +963,7 @@ protected function buildHeaders(ResponseInterface $response)
967963
}
968964
}
969965

970-
// Compile our own header strings here since if we just
971-
// append it to the response, it will be joined with
972-
// commas, not semi-colons as we need.
966+
// Compile each generated header value before appending it to the response.
973967
if ($this->reportingEndpoints !== []) {
974968
$endpoints = [];
975969

‎tests/system/HTTP/ContentSecurityPolicyTest.php‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\HTTP;
1515

16+
use CodeIgniter\Config\Services;
1617
use CodeIgniter\Exceptions\InvalidArgumentException;
1718
use CodeIgniter\Test\CIUnitTestCase;
1819
use CodeIgniter\Test\TestResponse;
@@ -90,15 +91,68 @@ public function testExistence(): void
9091
{
9192
$this->assertTrue($this->work());
9293
$this->assertHeaderEmitted('Content-Security-Policy:');
94+
$this->assertHeaderNotEmitted('Content-Security-Policy-Report-Only:');
95+
$this->assertHeaderNotEmitted('Reporting-Endpoints:');
9396
}
9497

9598
#[PreserveGlobalState(false)]
9699
#[RunInSeparateProcess]
97100
public function testReportOnly(): void
98101
{
99-
$this->csp->reportOnly(false);
102+
$config = new CSPConfig();
103+
$config->reportOnly = true;
104+
105+
Services::injectMock('csp', new ContentSecurityPolicy($config));
106+
107+
$this->response = new Response(config(App::class));
108+
$this->response->pretend(false);
109+
$this->csp = $this->response->getCSP();
110+
100111
$this->assertTrue($this->work());
101-
$this->assertHeaderEmitted('Content-Security-Policy:');
112+
$this->assertHeaderEmitted('Content-Security-Policy-Report-Only:');
113+
$this->assertHeaderNotEmitted('Content-Security-Policy:');
114+
$this->assertHeaderNotEmitted('Reporting-Endpoints:');
115+
}
116+
117+
#[PreserveGlobalState(false)]
118+
#[RunInSeparateProcess]
119+
public function testExistingCSPHeaderStringIsPreserved(): void
120+
{
121+
$this->response->setHeader('Content-Security-Policy', "frame-src 'none'");
122+
123+
$this->assertTrue($this->work());
124+
125+
$header = $this->getHeaderEmitted('Content-Security-Policy:');
126+
$this->assertIsString($header);
127+
$this->assertStringContainsString("frame-src 'none', base-uri 'self'", $header);
128+
}
129+
130+
#[PreserveGlobalState(false)]
131+
#[RunInSeparateProcess]
132+
public function testExistingCSPHeaderArrayIsPreserved(): void
133+
{
134+
$this->response->setHeader('Content-Security-Policy', [
135+
"frame-src 'none'",
136+
"media-src 'none'",
137+
]);
138+
139+
$this->assertTrue($this->work());
140+
141+
$header = $this->getHeaderEmitted('Content-Security-Policy:');
142+
$this->assertIsString($header);
143+
$this->assertStringContainsString("frame-src 'none', media-src 'none', base-uri 'self'", $header);
144+
}
145+
146+
#[PreserveGlobalState(false)]
147+
#[RunInSeparateProcess]
148+
public function testExistingReportOnlyHeaderIsPreserved(): void
149+
{
150+
$this->response->setHeader('Content-Security-Policy-Report-Only', "frame-src 'none'");
151+
152+
$this->assertTrue($this->work());
153+
154+
$header = $this->getHeaderEmitted('Content-Security-Policy-Report-Only:');
155+
$this->assertSame("Content-Security-Policy-Report-Only: frame-src 'none'", $header);
102156
}
103157

104158
#[PreserveGlobalState(false)]
@@ -609,6 +663,23 @@ public function testReportTo(): void
609663
$this->assertSame('Reporting-Endpoints: default="http://example.com/csp-reports"', $header);
610664
}
611665

666+
#[PreserveGlobalState(false)]
667+
#[RunInSeparateProcess]
668+
public function testExistingReportingEndpointsHeaderIsPreserved(): void
669+
{
670+
$this->response->setHeader('Reporting-Endpoints', 'existing="http://example.com/existing-reports"');
671+
$this->csp->addReportingEndpoints(['default' => 'http://example.com/csp-reports']);
672+
$this->csp->setReportToEndpoint('default');
673+
674+
$this->assertTrue($this->work());
675+
676+
$header = $this->getHeaderEmitted('Reporting-Endpoints:');
677+
$this->assertSame(
678+
'Reporting-Endpoints: existing="http://example.com/existing-reports", default="http://example.com/csp-reports"',
679+
$header,
680+
);
681+
}
682+
612683
#[PreserveGlobalState(false)]
613684
#[RunInSeparateProcess]
614685
public function testReportToMultipleEndpoints(): void

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **Content Security Policy:** Fixed a bug where empty ``Content-Security-Policy``, ``Content-Security-Policy-Report-Only``, and ``Reporting-Endpoints`` response headers were generated when no corresponding values existed.
3334
- **Logger:** Fixed a bug where interpolating a log message with array or non-stringable context values could raise PHP warnings or errors.
3435

3536
See the repo's

0 commit comments

Comments
 (0)