From 70625366ae5fa901bc901c435903b008c417353f Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 25 Dec 2021 11:57:59 +0900 Subject: [PATCH 1/2] fix: KINT visual error when activating CSP --- rector.php | 1 + system/CodeIgniter.php | 4 +- system/Debug/Kint/RichRenderer.php | 75 ++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 system/Debug/Kint/RichRenderer.php diff --git a/rector.php b/rector.php index 8be24cd7024d..68d16bbb66e4 100644 --- a/rector.php +++ b/rector.php @@ -71,6 +71,7 @@ $parameters->set(Option::SKIP, [ __DIR__ . '/app/Views', __DIR__ . '/system/Debug/Toolbar/Views/toolbar.tpl.php', + __DIR__ . '/system/Debug/Kint/RichRenderer.php', __DIR__ . '/system/ThirdParty', __DIR__ . '/tests/system/Config/fixtures', __DIR__ . '/tests/_support', diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index d309ca81c27a..56cb0df4b7bd 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -12,6 +12,7 @@ namespace CodeIgniter; use Closure; +use CodeIgniter\Debug\Kint\RichRenderer; use CodeIgniter\Debug\Timer; use CodeIgniter\Events\Events; use CodeIgniter\Exceptions\FrameworkException; @@ -33,7 +34,6 @@ use Exception; use Kint; use Kint\Renderer\CliRenderer; -use Kint\Renderer\RichRenderer; /** * This class is the core of the framework, and will analyse the @@ -257,6 +257,8 @@ protected function initializeKint() Kint::$plugins = $config->plugins; } + Kint::$renderers[Kint::MODE_RICH] = RichRenderer::class; + RichRenderer::$theme = $config->richTheme; RichRenderer::$folder = $config->richFolder; RichRenderer::$sort = $config->richSort; diff --git a/system/Debug/Kint/RichRenderer.php b/system/Debug/Kint/RichRenderer.php new file mode 100644 index 000000000000..756cac75e144 --- /dev/null +++ b/system/Debug/Kint/RichRenderer.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Debug\Kint; + +use Kint\Renderer\RichRenderer as KintRichRenderer; + +/** + * Overrides RichRenderer::preRender() for CSP + */ +class RichRenderer extends KintRichRenderer +{ + public function preRender() + { + $output = ''; + + if ($this->pre_render) { + foreach (self::$pre_render_sources as $type => $values) { + $contents = ''; + + foreach ($values as $v) { + $contents .= $v($this); + } + + if (! \strlen($contents)) { + continue; + } + + switch ($type) { + case 'script': + $output .= ''; + break; + + case 'style': + $output .= ''; + break; + + default: + $output .= $contents; + } + } + + // Don't pre-render on every dump + if (! $this->force_pre_render) { + self::$needs_pre_render = false; + } + } + + $output .= '
'; + + return $output; + } +} From 9c379088e8afa6cbfa470cbda79359f800ab5b24 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 27 Dec 2021 11:37:44 +0900 Subject: [PATCH 2/2] test: add tests for Kint with CSP --- tests/system/CommonFunctionsTest.php | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index c6f1d6b9b2e1..b7f4f381c233 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -29,6 +29,7 @@ use Config\Logger; use Config\Modules; use InvalidArgumentException; +use Kint; use stdClass; use Tests\Support\Models\JobModel; @@ -482,4 +483,37 @@ public function testIsCli() $this->assertIsBool(is_cli()); $this->assertTrue(is_cli()); } + + public function testDWithCSP() + { + /** @var App $config */ + $config = config(App::class); + $CSPEnabled = $config->CSPEnabled; + $cliDetection = Kint::$cli_detection; + + $config->CSPEnabled = true; + Kint::$cli_detection = false; + + $this->expectOutputRegex('/