Skip to content

Commit 5ed6211

Browse files
authored
Merge pull request #5501 from kenjis/fix-kint-RichRenderer
fix: KINT visual error when activating CSP
2 parents ac84d20 + 9c37908 commit 5ed6211

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
$parameters->set(Option::SKIP, [
7272
__DIR__ . '/app/Views',
7373
__DIR__ . '/system/Debug/Toolbar/Views/toolbar.tpl.php',
74+
__DIR__ . '/system/Debug/Kint/RichRenderer.php',
7475
__DIR__ . '/system/ThirdParty',
7576
__DIR__ . '/tests/system/Config/fixtures',
7677
__DIR__ . '/tests/_support',

system/CodeIgniter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter;
1313

1414
use Closure;
15+
use CodeIgniter\Debug\Kint\RichRenderer;
1516
use CodeIgniter\Debug\Timer;
1617
use CodeIgniter\Events\Events;
1718
use CodeIgniter\Exceptions\FrameworkException;
@@ -33,7 +34,6 @@
3334
use Exception;
3435
use Kint;
3536
use Kint\Renderer\CliRenderer;
36-
use Kint\Renderer\RichRenderer;
3737

3838
/**
3939
* This class is the core of the framework, and will analyse the
@@ -257,6 +257,8 @@ protected function initializeKint()
257257
Kint::$plugins = $config->plugins;
258258
}
259259

260+
Kint::$renderers[Kint::MODE_RICH] = RichRenderer::class;
261+
260262
RichRenderer::$theme = $config->richTheme;
261263
RichRenderer::$folder = $config->richFolder;
262264
RichRenderer::$sort = $config->richSort;

system/Debug/Kint/RichRenderer.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Debug\Kint;
13+
14+
use Kint\Renderer\RichRenderer as KintRichRenderer;
15+
16+
/**
17+
* Overrides RichRenderer::preRender() for CSP
18+
*/
19+
class RichRenderer extends KintRichRenderer
20+
{
21+
public function preRender()
22+
{
23+
$output = '';
24+
25+
if ($this->pre_render) {
26+
foreach (self::$pre_render_sources as $type => $values) {
27+
$contents = '';
28+
29+
foreach ($values as $v) {
30+
$contents .= $v($this);
31+
}
32+
33+
if (! \strlen($contents)) {
34+
continue;
35+
}
36+
37+
switch ($type) {
38+
case 'script':
39+
$output .= '<script {csp-script-nonce} class="kint-rich-script">' . $contents . '</script>';
40+
break;
41+
42+
case 'style':
43+
$output .= '<style {csp-style-nonce} class="kint-rich-style">' . $contents . '</style>';
44+
break;
45+
46+
default:
47+
$output .= $contents;
48+
}
49+
}
50+
51+
// Don't pre-render on every dump
52+
if (! $this->force_pre_render) {
53+
self::$needs_pre_render = false;
54+
}
55+
}
56+
57+
$output .= '<div class="kint-rich';
58+
59+
if ($this->use_folder) {
60+
$output .= ' kint-file';
61+
62+
if (self::$needs_folder_render || $this->force_pre_render) {
63+
$output = $this->renderFolder() . $output;
64+
65+
if (! $this->force_pre_render) {
66+
self::$needs_folder_render = false;
67+
}
68+
}
69+
}
70+
71+
$output .= '">';
72+
73+
return $output;
74+
}
75+
}

tests/system/CommonFunctionsTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Config\Logger;
3030
use Config\Modules;
3131
use InvalidArgumentException;
32+
use Kint;
3233
use stdClass;
3334
use Tests\Support\Models\JobModel;
3435

@@ -482,4 +483,37 @@ public function testIsCli()
482483
$this->assertIsBool(is_cli());
483484
$this->assertTrue(is_cli());
484485
}
486+
487+
public function testDWithCSP()
488+
{
489+
/** @var App $config */
490+
$config = config(App::class);
491+
$CSPEnabled = $config->CSPEnabled;
492+
$cliDetection = Kint::$cli_detection;
493+
494+
$config->CSPEnabled = true;
495+
Kint::$cli_detection = false;
496+
497+
$this->expectOutputRegex('/<script {csp-script-nonce} class="kint-rich-script">/u');
498+
d('string');
499+
500+
// Restore settings
501+
$config->CSPEnabled = $CSPEnabled;
502+
Kint::$cli_detection = $cliDetection;
503+
}
504+
505+
/**
506+
* @runInSeparateProcess
507+
* @preserveGlobalState disabled
508+
*/
509+
public function testTraceWithCSP()
510+
{
511+
/** @var App $config */
512+
$config = config(App::class);
513+
$config->CSPEnabled = true;
514+
Kint::$cli_detection = false;
515+
516+
$this->expectOutputRegex('/<style {csp-style-nonce} class="kint-rich-style">/u');
517+
trace();
518+
}
485519
}

0 commit comments

Comments
 (0)