Skip to content

Commit 4cdbee1

Browse files
authored
fix: ensure CSP nonces are Base64 encoded (#9907)
1 parent 30ae112 commit 4cdbee1

File tree

8 files changed

+17
-14
lines changed

8 files changed

+17
-14
lines changed

system/HTTP/ContentSecurityPolicy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public function enabled(): bool
303303
public function getStyleNonce(): string
304304
{
305305
if ($this->styleNonce === null) {
306-
$this->styleNonce = bin2hex(random_bytes(12));
306+
$this->styleNonce = base64_encode(random_bytes(12));
307307
$this->styleSrc[] = 'nonce-' . $this->styleNonce;
308308
}
309309

@@ -316,7 +316,7 @@ public function getStyleNonce(): string
316316
public function getScriptNonce(): string
317317
{
318318
if ($this->scriptNonce === null) {
319-
$this->scriptNonce = bin2hex(random_bytes(12));
319+
$this->scriptNonce = base64_encode(random_bytes(12));
320320
$this->scriptSrc[] = 'nonce-' . $this->scriptNonce;
321321
}
322322

tests/system/CommonFunctionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ public function testDWithCSP(): void
731731
$cliDetection = Kint::$cli_detection;
732732
Kint::$cli_detection = false;
733733

734-
$this->expectOutputRegex('/<script class="kint-rich-script" nonce="[0-9a-z]{24}">/u');
734+
$this->expectOutputRegex('/<script class="kint-rich-script" nonce="[a-zA-Z0-9+\/-_]+[=]{0,2}">/u');
735735
d('string');
736736

737737
// Restore settings
@@ -754,7 +754,7 @@ public function testTraceWithCSP(): void
754754

755755
Kint::$cli_detection = false;
756756

757-
$this->expectOutputRegex('/<style class="kint-rich-style" nonce="[0-9a-z]{24}">/u');
757+
$this->expectOutputRegex('/<style class="kint-rich-style" nonce="[a-zA-Z0-9+\/-_]+[=]{0,2}">/u');
758758
trace();
759759
}
760760

tests/system/HTTP/ContentSecurityPolicyTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,18 +578,20 @@ public function testGetScriptNonce(): void
578578
{
579579
$this->prepare();
580580

581-
$nonce = $this->csp->getScriptNonce();
582-
583-
$this->assertMatchesRegularExpression('/\A[0-9a-z]{24}\z/', $nonce);
581+
$this->assertMatchesRegularExpression(
582+
'/\A[a-zA-Z0-9+\/-_]+[=]{0,2}\z/',
583+
$this->csp->getScriptNonce(),
584+
);
584585
}
585586

586587
public function testGetStyleNonce(): void
587588
{
588589
$this->prepare();
589590

590-
$nonce = $this->csp->getStyleNonce();
591-
592-
$this->assertMatchesRegularExpression('/\A[0-9a-z]{24}\z/', $nonce);
591+
$this->assertMatchesRegularExpression(
592+
'/\A[a-zA-Z0-9+\/-_]+[=]{0,2}\z/',
593+
$this->csp->getStyleNonce(),
594+
);
593595
}
594596

595597
#[PreserveGlobalState(false)]

tests/system/Helpers/HTMLHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function testScriptTagWithCsp(): void
337337
$html = script_tag($target);
338338

339339
$this->assertMatchesRegularExpression(
340-
'!<script nonce="\w+?" src="http://site.com/js/mystyles.js".*?>!u',
340+
'!<script nonce="[a-zA-Z0-9+\/-_]+[=]{0,2}" src="http://site.com/js/mystyles.js".*?>!u',
341341
$html,
342342
);
343343

tests/system/Helpers/URLHelper/MiscUrlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public function testSafeMailtoWithCsp(): void
493493

494494
$html = safe_mailto('foo@example.jp', 'Foo');
495495

496-
$this->assertMatchesRegularExpression('/<script .*?nonce="\w+?".*?>/u', $html);
496+
$this->assertMatchesRegularExpression('/<script .*?nonce="[a-zA-Z0-9+\/]+[=]{0,2}".*?>/u', $html);
497497
}
498498

499499
/**

tests/system/Honeypot/HoneypotTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function testAttachHoneypotAndContainerWithCSP(): void
107107
$this->response->setBody('<head></head><body><form></form></body>');
108108
$this->honeypot->attachHoneypot($this->response);
109109

110-
$regex = '!<head><style nonce="[0-9a-f]+">#hpc { display:none }</style></head><body><form><div style="display:none" id="hpc"><label>Fill This Field</label><input type="text" name="honeypot" value=""></div></form></body>!u';
110+
$regex = '!<head><style nonce="[a-zA-Z0-9+\/-_]+[=]{0,2}">#hpc { display:none }</style></head><body><form><div style="display:none" id="hpc"><label>Fill This Field</label><input type="text" name="honeypot" value=""></div></form></body>!u';
111111
$this->assertMatchesRegularExpression($regex, $this->response->getBody());
112112
}
113113

tests/system/View/ParserPluginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function testCspScriptNonceWithCspEnabled(): void
151151
$template = 'aaa {+ csp_script_nonce +} bbb';
152152

153153
$this->assertMatchesRegularExpression(
154-
'/aaa nonce="[0-9a-z]{24}" bbb/',
154+
'/aaa nonce="[a-zA-Z0-9+\/-_]+[=]{0,2}" bbb/',
155155
$this->parser->renderString($template),
156156
);
157157
}

user_guide_src/source/changelogs/v4.6.5.rst

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

3333
- **Database:** Fixed a bug where ``Seeder::call()`` did not pass the database connection to child seeders, causing them to use the default connection instead of the one specified via ``Database::seeder('group')``.
34+
- **HTTP:** Updated the Content Security Policy nonce generation to use base64 encoding instead of hexadecimal, ensuring compatibility with CSP specifications.
3435

3536
See the repo's
3637
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)