Skip to content
Merged
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
57 changes: 51 additions & 6 deletions tests/TestCase/Controller/Admin/QueuedJobsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,36 @@ public function testStats() {
}

/**
* CakePHP's CspMiddleware stores the nonce as `cspScriptNonce`.
* CakePHP's CspMiddleware stores the nonces as `cspScriptNonce` and `cspStyleNonce`.
*
* @return void
*/
public function testStatsUsesCspScriptNonce(): void {
Configure::write('Queue.isStatisticEnabled', true);
$this->withRequestAttribute('cspScriptNonce', 'core-nonce');
$this->withRequestAttribute('cspStyleNonce', 'style-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=)/');
$this->assertAllTagsHaveNonce('core-nonce', 'style-nonce');
}

/**
* @return void
*/
public function testStatsPrefersCspScriptNonceOverCspNonce(): void {
Configure::write('Queue.isStatisticEnabled', true);
$this->withRequestAttribute('cspScriptNonce', 'core-nonce');
$this->withRequestAttribute('cspNonce', 'custom-nonce');

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

$this->assertResponseCode(200);
$this->assertResponseNotContains('custom-nonce');
$this->assertTagsHaveNonce('script', 'core-nonce');
}

/**
Expand All @@ -203,7 +219,7 @@ public function testStatsFallsBackToCspNonce(): void {

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

/**
Expand All @@ -213,14 +229,14 @@ public function testStatsFallsBackToCspNonce(): void {
*/
public function testHeatmapUsesCspStyleNonce(): void {
Configure::write('Queue.isStatisticEnabled', true);
$this->withRequestAttribute('cspScriptNonce', 'core-nonce');
$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/');
$this->assertMatchesRegularExpression('/<style nonce="style-nonce">\s*\.heatmap-container\s*\{/', (string)$this->_response->getBody());
$this->assertAllTagsHaveNonce('core-nonce', 'style-nonce');
}

/**
Expand Down Expand Up @@ -543,6 +559,35 @@ protected function createJob(array $data = []) {
return $queuedJob;
}

/**
* @param string $scriptNonce
* @param string $styleNonce
*
* @return void
*/
protected function assertAllTagsHaveNonce(string $scriptNonce, string $styleNonce): void {
$this->assertTagsHaveNonce('script', $scriptNonce);
$this->assertTagsHaveNonce('style', $styleNonce);
$this->assertTagsHaveNonce('link', $styleNonce, '/\brel=["\'][^"\']*\bstylesheet\b/i');
}

/**
* @param string $tag
* @param string $nonce
* @param string|null $filter Only check tags matching this pattern.
*
* @return void
*/
protected function assertTagsHaveNonce(string $tag, string $nonce, ?string $filter = null): void {
preg_match_all('/<' . $tag . '\b[^>]*>/i', (string)$this->_response->getBody(), $matches);
$tags = $filter ? preg_grep($filter, $matches[0]) : $matches[0];

$this->assertNotEmpty($tags, 'No <' . $tag . '> tags found.');
foreach ($tags as $html) {
$this->assertMatchesRegularExpression('/\snonce="' . preg_quote($nonce, '/') . '"/', $html, 'Wrong or missing nonce: ' . $html);
}
}

/**
* Adds a middleware that sets a request attribute, like a CSP middleware would.
*
Expand Down
Loading