Skip to content

Commit 8a110e5

Browse files
authored
Merge branch 'develop' into test/gdhandler-save-coverage
2 parents 00cf519 + 7505c73 commit 8a110e5

8 files changed

Lines changed: 136 additions & 18 deletions

File tree

‎.github/scripts/random-tests-config.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Cookie
2525
# Entity
2626
Events
2727
Files
28-
# Filters
28+
Filters
2929
Format
3030
HTTP
3131
# Helpers

‎.github/workflows/test-scss.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
3737

3838
- name: Setup Node
39-
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
39+
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
4040
with:
4141
node-version: '24'
4242

‎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/Commands/Cache/ClearCacheTest.php‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use CodeIgniter\Cache\CacheFactory;
1717
use CodeIgniter\Cache\Handlers\FileHandler;
1818
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\Config\Factories;
1920
use CodeIgniter\Test\CIUnitTestCase;
2021
use CodeIgniter\Test\StreamFilterTrait;
22+
use Config\Cache;
2123
use Config\Services;
2224
use PHPUnit\Framework\Attributes\Group;
2325

@@ -29,23 +31,45 @@ final class ClearCacheTest extends CIUnitTestCase
2931
{
3032
use StreamFilterTrait;
3133

34+
private Cache $config;
35+
3236
protected function setUp(): void
3337
{
3438
parent::setUp();
3539

3640
CLI::reset();
3741
$this->resetServices();
3842

43+
$this->config = new Cache();
44+
$this->config->file['storePath'] = rtrim($this->config->file['storePath'], DIRECTORY_SEPARATOR)
45+
. DIRECTORY_SEPARATOR . 'FileHandlerCommands';
46+
47+
if (! is_dir($this->config->file['storePath'])) {
48+
mkdir($this->config->file['storePath'], 0777, true);
49+
}
50+
51+
Factories::injectMock('config', Cache::class, $this->config);
52+
3953
// Make sure we are testing with the correct handler (override injections)
40-
Services::injectMock('cache', CacheFactory::getHandler(config('Cache')));
54+
$handler = CacheFactory::getHandler($this->config);
55+
$handler->clean();
56+
Services::injectMock('cache', $handler);
4157
}
4258

4359
protected function tearDown(): void
4460
{
45-
parent::tearDown();
61+
$this->config->handler = 'file';
62+
63+
if (is_dir($this->config->file['storePath'])) {
64+
CacheFactory::getHandler($this->config)->clean();
65+
rmdir($this->config->file['storePath']);
66+
}
4667

4768
CLI::reset();
69+
$this->resetFactories();
4870
$this->resetServices();
71+
72+
parent::tearDown();
4973
}
5074

5175
public function testClearCacheInvalidHandler(): void
@@ -72,7 +96,7 @@ public function testClearCacheWorks(): void
7296
public function testClearCacheFails(): void
7397
{
7498
$cache = $this->getMockBuilder(FileHandler::class)
75-
->setConstructorArgs([config('Cache')])
99+
->setConstructorArgs([$this->config])
76100
->onlyMethods(['clean'])
77101
->getMock();
78102
$cache->expects($this->once())->method('clean')->willReturn(false);

‎tests/system/Commands/Cache/InfoCacheTest.php‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
namespace CodeIgniter\Commands\Cache;
1515

1616
use CodeIgniter\Cache\CacheFactory;
17+
use CodeIgniter\Config\Factories;
1718
use CodeIgniter\Test\CIUnitTestCase;
1819
use CodeIgniter\Test\StreamFilterTrait;
20+
use Config\Cache;
1921
use Config\Services;
2022
use PHPUnit\Framework\Attributes\Group;
2123

@@ -27,18 +29,41 @@ final class InfoCacheTest extends CIUnitTestCase
2729
{
2830
use StreamFilterTrait;
2931

32+
private Cache $config;
33+
3034
protected function setUp(): void
3135
{
3236
parent::setUp();
3337

38+
$this->config = new Cache();
39+
$this->config->file['storePath'] = rtrim($this->config->file['storePath'], DIRECTORY_SEPARATOR)
40+
. DIRECTORY_SEPARATOR . 'FileHandlerCommands';
41+
42+
if (! is_dir($this->config->file['storePath'])) {
43+
mkdir($this->config->file['storePath'], 0777, true);
44+
}
45+
46+
Factories::injectMock('config', Cache::class, $this->config);
47+
3448
// Make sure we are testing with the correct handler (override injections)
35-
Services::injectMock('cache', CacheFactory::getHandler(config('Cache')));
49+
$handler = CacheFactory::getHandler($this->config);
50+
$handler->clean();
51+
Services::injectMock('cache', $handler);
3652
}
3753

3854
protected function tearDown(): void
3955
{
40-
// restore default cache handler
41-
config('Cache')->handler = 'file';
56+
$this->config->handler = 'file';
57+
58+
if (is_dir($this->config->file['storePath'])) {
59+
CacheFactory::getHandler($this->config)->clean();
60+
rmdir($this->config->file['storePath']);
61+
}
62+
63+
$this->resetFactories();
64+
$this->resetServices();
65+
66+
parent::tearDown();
4267
}
4368

4469
protected function getBuffer(): string
@@ -48,7 +73,7 @@ protected function getBuffer(): string
4873

4974
public function testInfoCacheErrorsOnInvalidHandler(): void
5075
{
51-
config('Cache')->handler = 'redis';
76+
$this->config->handler = 'redis';
5277
cache()->save('foo', 'bar');
5378
command('cache:info');
5479

‎tests/system/Filters/CSRFTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ final class CSRFTest extends CIUnitTestCase
4040
protected function setUp(): void
4141
{
4242
parent::setUp();
43+
44+
$this->resetServices();
45+
4346
$this->config = new \Config\Filters();
4447
}
4548

‎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)