Skip to content
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
3 changes: 2 additions & 1 deletion templates/Admin/QueuedJobs/heatmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@
</div>
</div>

<style>
<?php $cspStyleNonce = (string)$this->getRequest()->getAttribute('cspStyleNonce', ''); ?>
<style<?= $cspStyleNonce !== '' ? ' nonce="' . h($cspStyleNonce) . '"' : '' ?>>
.heatmap-container {
overflow-x: auto;
}
Expand Down
2 changes: 1 addition & 1 deletion templates/Admin/QueuedJobs/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
?>

<?php $this->append('script'); ?>
<?php $cspNonce = (string)$this->getRequest()->getAttribute('cspNonce', ''); ?>
<?php $cspNonce = (string)($this->getRequest()->getAttribute('cspScriptNonce') ?? $this->getRequest()->getAttribute('cspNonce', '')); ?>
<script<?= $cspNonce !== '' ? ' nonce="' . h($cspNonce) . '"' : '' ?>>
document.addEventListener('DOMContentLoaded', function() {
var chartCanvas = document.getElementById('job-chart');
Expand Down
21 changes: 12 additions & 9 deletions templates/layout/queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
if ($request && $request->getParam('controller') === 'Queue' && $request->getParam('action') === 'index') {
$autoRefresh = (int)Configure::read('Queue.dashboardAutoRefresh') ?: 0;
}
$cspNonce = (string)($this->getRequest()->getAttribute('cspScriptNonce') ?? $this->getRequest()->getAttribute('cspNonce', ''));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
$nonceAttr = $cspNonce !== '' ? ' nonce="' . h($cspNonce) . '"' : '';
$cspStyleNonce = (string)$this->getRequest()->getAttribute('cspStyleNonce', '');
$styleNonceAttr = $cspStyleNonce !== '' ? ' nonce="' . h($cspStyleNonce) . '"' : '';
?>
<!DOCTYPE html>
<html lang="en">
Expand All @@ -24,18 +28,18 @@
<title><?= $this->fetch('title') ? strip_tags($this->fetch('title')) . ' - ' : '' ?>Queue Admin</title>

<!-- Bootstrap 5.3.3 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link<?= $styleNonceAttr ?> href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

<!-- Font Awesome 6.7.2 -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet" integrity="sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg==" crossorigin="anonymous">
<link<?= $styleNonceAttr ?> href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet" integrity="sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg==" crossorigin="anonymous">
<!-- Chart.js for stats page -->
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.7/dist/chart.umd.min.js"></script>
<script<?= $nonceAttr ?> src="https://cdn.jsdelivr.net/npm/chart.js@4.4.7/dist/chart.umd.min.js"></script>

<!-- Flatpickr for datetime inputs -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/dist/flatpickr.min.css">
<script src="https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/dist/flatpickr.min.js"></script>
<link<?= $styleNonceAttr ?> rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/dist/flatpickr.min.css">
<script<?= $nonceAttr ?> src="https://cdn.jsdelivr.net/npm/flatpickr@4.6.13/dist/flatpickr.min.js"></script>

<style>
<style<?= $styleNonceAttr ?>>
:root {
--queue-primary: #0d6efd;
--queue-success: #198754;
Expand Down Expand Up @@ -505,10 +509,9 @@
<?= $this->fetch('postLink') ?>

<!-- Bootstrap 5.3.3 JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script<?= $nonceAttr ?> src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>

<?php $cspNonce = (string)$this->getRequest()->getAttribute('cspNonce', ''); ?>
<script<?= $cspNonce !== '' ? ' nonce="' . h($cspNonce) . '"' : '' ?>>
<script<?= $nonceAttr ?>>
document.addEventListener('DOMContentLoaded', function() {
// Initialize tooltips
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
Expand Down
72 changes: 72 additions & 0 deletions tests/TestCase/Controller/Admin/QueuedJobsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Event\EventInterface;
use Cake\Event\EventManager;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\MiddlewareQueue;
use Cake\I18n\DateTime;
use Cake\TestSuite\IntegrationTestTrait;
use Laminas\Diactoros\UploadedFile;
use PHPUnit\Framework\Attributes\UsesClass;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Queue\Controller\Admin\QueuedJobsController;
use Shim\TestSuite\TestCase;

Expand Down Expand Up @@ -167,6 +173,56 @@ public function testStats() {
$this->assertResponseCode(200);
}

/**
* CakePHP's CspMiddleware stores the nonce as `cspScriptNonce`.
*
* @return void
*/
public function testStatsUsesCspScriptNonce(): void {
Configure::write('Queue.isStatisticEnabled', true);
$this->withRequestAttribute('cspScriptNonce', 'core-nonce');

$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'stats']);

$this->assertResponseCode(200);
$this->assertResponseContains('<script nonce="core-nonce" src="https://cdn.jsdelivr.net/npm/chart.js');
$this->assertResponseContains('<script nonce="core-nonce" src="https://cdn.jsdelivr.net/npm/bootstrap');
$this->assertResponseNotRegExp('/<script(?![^>]*nonce=)/');
}

/**
* A custom middleware may still provide the nonce as `cspNonce`.
*
* @return void
*/
public function testStatsFallsBackToCspNonce(): void {
Configure::write('Queue.isStatisticEnabled', true);
$this->withRequestAttribute('cspNonce', 'custom-nonce');

$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'stats']);

$this->assertResponseCode(200);
$this->assertResponseContains('<script nonce="custom-nonce">');
$this->assertResponseNotRegExp('/<script(?![^>]*nonce=)/');
}

/**
* Inline styles and CDN stylesheets need the style nonce under a nonce-only `style-src`.
*
* @return void
*/
public function testHeatmapUsesCspStyleNonce(): void {
Configure::write('Queue.isStatisticEnabled', true);
$this->withRequestAttribute('cspStyleNonce', 'style-nonce');

$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'heatmap']);

$this->assertResponseCode(200);
$this->assertResponseContains('<style nonce="style-nonce">');
$this->assertResponseNotRegExp('/<style(?![^>]*nonce=)/');
$this->assertResponseNotRegExp('/<link(?![^>]*nonce=)[^>]*stylesheet/');
}

/**
* @return void
*/
Expand Down Expand Up @@ -487,4 +543,20 @@ protected function createJob(array $data = []) {
return $queuedJob;
}

/**
* Adds a middleware that sets a request attribute, like a CSP middleware would.
*
* @param string $name
* @param string $value
*
* @return void
*/
protected function withRequestAttribute(string $name, string $value): void {
EventManager::instance()->on('Server.buildMiddleware', function (EventInterface $event, MiddlewareQueue $queue) use ($name, $value): void {
$queue->prepend(function (ServerRequestInterface $request, RequestHandlerInterface $handler) use ($name, $value): ResponseInterface {
return $handler->handle($request->withAttribute($name, $value));
});
});
}

}
Loading