Skip to content

Commit

Permalink
Merge pull request #5501 from kenjis/fix-kint-RichRenderer
Browse files Browse the repository at this point in the history
fix: KINT visual error when activating CSP
  • Loading branch information
kenjis authored Dec 30, 2021
2 parents ac84d20 + 9c37908 commit 5ed6211
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
75 changes: 75 additions & 0 deletions system/Debug/Kint/RichRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* 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 .= '<script {csp-script-nonce} class="kint-rich-script">' . $contents . '</script>';
break;

case 'style':
$output .= '<style {csp-style-nonce} class="kint-rich-style">' . $contents . '</style>';
break;

default:
$output .= $contents;
}
}

// Don't pre-render on every dump
if (! $this->force_pre_render) {
self::$needs_pre_render = false;
}
}

$output .= '<div class="kint-rich';

if ($this->use_folder) {
$output .= ' kint-file';

if (self::$needs_folder_render || $this->force_pre_render) {
$output = $this->renderFolder() . $output;

if (! $this->force_pre_render) {
self::$needs_folder_render = false;
}
}
}

$output .= '">';

return $output;
}
}
34 changes: 34 additions & 0 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Config\Logger;
use Config\Modules;
use InvalidArgumentException;
use Kint;
use stdClass;
use Tests\Support\Models\JobModel;

Expand Down Expand Up @@ -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('/<script {csp-script-nonce} class="kint-rich-script">/u');
d('string');

// Restore settings
$config->CSPEnabled = $CSPEnabled;
Kint::$cli_detection = $cliDetection;
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testTraceWithCSP()
{
/** @var App $config */
$config = config(App::class);
$config->CSPEnabled = true;
Kint::$cli_detection = false;

$this->expectOutputRegex('/<style {csp-style-nonce} class="kint-rich-style">/u');
trace();
}
}

0 comments on commit 5ed6211

Please sign in to comment.