Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: KINT visual error when activating CSP #5501

Merged
merged 2 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
*
* 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();
}
}