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
13 changes: 9 additions & 4 deletions templates/layout/captcha-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@
*
* @var \App\View\AppView $this
*/

$cspScriptNonce = (string)($this->getRequest()->getAttribute('cspScriptNonce') ?? $this->getRequest()->getAttribute('cspNonce', ''));
$scriptNonceAttr = $cspScriptNonce !== '' ? ' nonce="' . h($cspScriptNonce) . '"' : '';
$cspStyleNonce = (string)$this->getRequest()->getAttribute('cspStyleNonce', '');
$styleNonceAttr = $cspStyleNonce !== '' ? ' nonce="' . h($cspStyleNonce) . '"' : '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title><?= $this->fetch('title') ?: __d('captcha', 'Captcha Admin') ?></title>
<link rel="stylesheet"
<link<?= $styleNonceAttr ?> rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous"
referrerpolicy="no-referrer">
<link rel="stylesheet"
<link<?= $styleNonceAttr ?> rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.2/css/all.min.css"
integrity="sha384-PPIZEGYM1v8zp5Py7UjFb79S58UeqCL9pYVnVPURKEqvioPROaVAJKKLzvH2rDnI"
crossorigin="anonymous"
referrerpolicy="no-referrer">
<style>
<style<?= $styleNonceAttr ?>>
body { background: #f5f7fa; }
.captcha-admin-nav { background: #1f2937; }
.captcha-admin-nav .navbar-brand,
Expand Down Expand Up @@ -66,7 +71,7 @@
<?= $this->fetch('content') ?>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
<script<?= $scriptNonceAttr ?> src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"
referrerpolicy="no-referrer"></script>
Expand Down
57 changes: 57 additions & 0 deletions tests/TestCase/Controller/Admin/CaptchaControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Cake\Http\Exception\ForbiddenException;
use Cake\Http\Exception\MethodNotAllowedException;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\MiddlewareQueue;
use Cake\Http\ServerRequest;
use Cake\I18n\DateTime;
use Cake\TestSuite\IntegrationTestTrait;
Expand All @@ -19,6 +20,9 @@
use Captcha\Controller\Admin\CaptchaController;
use LogicException;
use PHPUnit\Framework\Attributes\UsesClass;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;

#[UsesClass(CaptchaController::class)]
Expand Down Expand Up @@ -53,6 +57,40 @@ public function testIndexDeniedByDefault() {
$this->get(['plugin' => 'Captcha', 'prefix' => 'Admin', 'controller' => 'Captcha', 'action' => 'index']);
}

/**
* CakePHP's CspMiddleware stores the nonces as `cspScriptNonce` and `cspStyleNonce`.
*
* @return void
*/
public function testIndexUsesCoreCspNonces(): void {
Configure::write('Captcha.adminAccess', fn (ServerRequest $request): bool => true);
$this->withRequestAttributes(['cspScriptNonce' => 'script-nonce', 'cspStyleNonce' => 'style-nonce']);

$this->get(['plugin' => 'Captcha', 'prefix' => 'Admin', 'controller' => 'Captcha', 'action' => 'index']);

$this->assertResponseOk();
$this->assertResponseContains('<script nonce="script-nonce" src="https://cdn.jsdelivr.net/npm/bootstrap');
$this->assertResponseContains('<style nonce="style-nonce">');
$this->assertResponseNotRegExp('/<script(?![^>]*nonce="script-nonce")/');
$this->assertResponseNotRegExp('/<style(?![^>]*nonce="style-nonce")/');
$this->assertResponseNotRegExp('/<link(?![^>]*nonce="style-nonce")[^>]*rel="stylesheet"/');
}

/**
* A custom middleware may still provide the script nonce as `cspNonce`.
*
* @return void
*/
public function testIndexFallsBackToCspNonce(): void {
Configure::write('Captcha.adminAccess', fn (ServerRequest $request): bool => true);
$this->withRequestAttributes(['cspNonce' => 'custom-nonce']);

$this->get(['plugin' => 'Captcha', 'prefix' => 'Admin', 'controller' => 'Captcha', 'action' => 'index']);

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

/**
* @return void
*/
Expand Down Expand Up @@ -316,4 +354,23 @@ public function skipAuthorization(): void {
$this->assertRedirect('/login');
}

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

return $handler->handle($request);
});
});
}

}
Loading