Skip to content

Commit 83fc0f9

Browse files
authored
Merge pull request #6570 from kenjis/fix-csp-autoNonce
fix: CSP autoNonce = false
2 parents 70b2341 + 60d5e76 commit 83fc0f9

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

system/HTTP/ContentSecurityPolicy.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,10 @@ public function getScriptNonce(): string
300300
*/
301301
public function finalize(ResponseInterface $response)
302302
{
303-
if ($this->autoNonce === false) {
304-
return;
303+
if ($this->autoNonce) {
304+
$this->generateNonces($response);
305305
}
306306

307-
$this->generateNonces($response);
308307
$this->buildHeaders($response);
309308
}
310309

tests/system/HTTP/ContentSecurityPolicyTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\HTTP;
1313

1414
use CodeIgniter\Test\CIUnitTestCase;
15+
use CodeIgniter\Test\TestResponse;
1516
use Config\App;
1617
use Config\ContentSecurityPolicy as CSPConfig;
1718

@@ -533,6 +534,9 @@ public function testBodyScriptNonceDisableAutoNonce()
533534
$csp->finalize($response);
534535

535536
$this->assertStringContainsString('{csp-script-nonce}', $response->getBody());
537+
538+
$result = new TestResponse($response);
539+
$result->assertHeader('Content-Security-Policy');
536540
}
537541

538542
public function testBodyStyleNonceDisableAutoNonce()
@@ -549,6 +553,9 @@ public function testBodyStyleNonceDisableAutoNonce()
549553
$csp->finalize($response);
550554

551555
$this->assertStringContainsString('{csp-style-nonce}', $response->getBody());
556+
557+
$result = new TestResponse($response);
558+
$result->assertHeader('Content-Security-Policy');
552559
}
553560

554561
/**

user_guide_src/source/changelogs/v4.2.7.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ none.
3333
Bugs Fixed
3434
**********
3535

36-
none.
36+
- Fixed a bug that prevents CSP headers from being sent when ``Config\ContentSecurityPolicy::$autoNonce`` is false.
3737

3838
See the repo's `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_ for a complete list of bugs fixed.

0 commit comments

Comments
 (0)