Skip to content

Commit aa0d83e

Browse files
authored
fix: reset Kint CSP state in worker mode (#10139)
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 4291460 commit aa0d83e

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

system/CodeIgniter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ public function resetForWorkerMode(): void
208208
// Reset timing
209209
$this->startTime = null;
210210
$this->totalTime = 0;
211+
212+
$this->resetKintForWorkerMode();
213+
}
214+
215+
/**
216+
* Resets Kint request-specific state for worker mode.
217+
*/
218+
private function resetKintForWorkerMode(): void
219+
{
220+
if (! CI_DEBUG || ! class_exists(Kint::class, false)) {
221+
return;
222+
}
223+
224+
$csp = service('csp');
225+
if ($csp->enabled()) {
226+
RichRenderer::$js_nonce = $csp->getScriptNonce();
227+
RichRenderer::$css_nonce = $csp->getStyleNonce();
228+
} else {
229+
RichRenderer::$js_nonce = null;
230+
RichRenderer::$css_nonce = null;
231+
}
232+
233+
RichRenderer::$needs_pre_render = true;
211234
}
212235

213236
/**

tests/system/CodeIgniterTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Config\Filters as FiltersConfig;
3131
use Config\Modules;
3232
use Config\Routing;
33+
use Kint\Renderer\RichRenderer;
3334
use PHPUnit\Framework\Attributes\BackupGlobals;
3435
use PHPUnit\Framework\Attributes\DataProvider;
3536
use PHPUnit\Framework\Attributes\Group;
@@ -1273,6 +1274,15 @@ public function testRouteAttributesDisabledInConfig(): void
12731274

12741275
public function testResetForWorkerMode(): void
12751276
{
1277+
$this->resetServices();
1278+
1279+
$appConfig = config(App::class);
1280+
$appConfig->CSPEnabled = true;
1281+
1282+
RichRenderer::$js_nonce = 'stale-script-nonce';
1283+
RichRenderer::$css_nonce = 'stale-style-nonce';
1284+
RichRenderer::$needs_pre_render = false;
1285+
12761286
$config = new App();
12771287
$codeigniter = new MockCodeIgniter($config);
12781288

@@ -1292,5 +1302,11 @@ public function testResetForWorkerMode(): void
12921302
$this->assertNull($this->getPrivateProperty($codeigniter, 'controller'));
12931303
$this->assertNull($this->getPrivateProperty($codeigniter, 'method'));
12941304
$this->assertNull($this->getPrivateProperty($codeigniter, 'output'));
1305+
1306+
$csp = service('csp');
1307+
1308+
$this->assertSame($csp->getScriptNonce(), RichRenderer::$js_nonce);
1309+
$this->assertSame($csp->getStyleNonce(), RichRenderer::$css_nonce);
1310+
$this->assertTrue(RichRenderer::$needs_pre_render);
12951311
}
12961312
}

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Bugs Fixed
4040
- **CLI:** Fixed a bug where ``CLI::generateDimensions()`` leaked ``stty`` error output (e.g., ``stty: 'standard input': Inappropriate ioctl for device``) to stderr when stdin was not a TTY.
4141
- **Commands:** Fixed a bug in the ``env`` command where passing options only would cause the command to throw a ``TypeError`` instead of showing the current environment.
4242
- **Common:** Fixed a bug where the ``command()`` helper function did not properly clean up output buffers, which could lead to risky tests when exceptions were thrown.
43+
- **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets.
4344
- **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``.
4445

4546
See the repo's

0 commit comments

Comments
 (0)