- New SecurityHeadersMiddleware (outermost): CSP with a per-request script nonce, framing (SAMEORIGIN, embeds via frame-ancestors), nosniff, Referrer- and Permissions-Policy. JSON, files and error pages are covered now, not only rendered HTML; headers set by controllers still win. - The inline theme script carries the nonce; flash messages are dismissed via data-dismiss instead of inline onclick handlers, and the flash elements share one template. - reCAPTCHA sources are only allowed when it is enabled. - The embed CSRF exception and framing use the same isEmbed() check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
5.0 KiB
PHP
138 lines
5.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
|
*
|
|
* Licensed under The MIT License
|
|
* For full copyright and license information, please see the LICENSE.txt
|
|
* Redistributions of files must retain the above copyright notice.
|
|
*
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
|
* @since 3.3.0
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
|
*/
|
|
namespace App;
|
|
|
|
use App\Middleware\HostHeaderMiddleware;
|
|
use App\Middleware\SecurityHeadersMiddleware;
|
|
use Cake\Core\Configure;
|
|
use Cake\Core\ContainerInterface;
|
|
use Cake\Datasource\FactoryLocator;
|
|
use Cake\Error\Middleware\ErrorHandlerMiddleware;
|
|
use Cake\Event\EventManagerInterface;
|
|
use Cake\Http\BaseApplication;
|
|
use Cake\Http\Middleware\BodyParserMiddleware;
|
|
use Cake\Http\Middleware\CsrfProtectionMiddleware;
|
|
use Cake\Http\MiddlewareQueue;
|
|
use Cake\ORM\Locator\TableLocator;
|
|
use Cake\Routing\Middleware\AssetMiddleware;
|
|
use Cake\Routing\Middleware\RoutingMiddleware;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
/**
|
|
* Application setup class.
|
|
*
|
|
* This defines the bootstrapping logic and middleware layers you
|
|
* want to use in your application.
|
|
*
|
|
* @extends \Cake\Http\BaseApplication<\App\Application>
|
|
*/
|
|
class Application extends BaseApplication
|
|
{
|
|
/**
|
|
* Load all the application configuration and bootstrap logic.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function bootstrap(): void
|
|
{
|
|
// Call parent to load bootstrap from files.
|
|
parent::bootstrap();
|
|
|
|
// By default, does not allow fallback classes.
|
|
FactoryLocator::add(
|
|
'Table',
|
|
(new TableLocator())->allowFallbackClass(false),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Setup the middleware queue your application will use.
|
|
*
|
|
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
|
|
* @return \Cake\Http\MiddlewareQueue The updated middleware queue.
|
|
*/
|
|
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
|
|
{
|
|
$middlewareQueue
|
|
// Outermost, so error pages get the security headers (CSP, framing, nosniff) too.
|
|
->add(new SecurityHeadersMiddleware())
|
|
|
|
// Catch any exceptions in the lower layers,
|
|
// and make an error page/response
|
|
->add(new ErrorHandlerMiddleware(Configure::read('Error'), $this))
|
|
|
|
// Validate Host header to prevent Host Header Injection attacks.
|
|
// In production, ensures App.fullBaseUrl is configured and validates
|
|
// the incoming Host header against it.
|
|
->add(new HostHeaderMiddleware())
|
|
|
|
// Handle plugin/theme assets like CakePHP normally does.
|
|
->add(new AssetMiddleware([
|
|
'cacheTime' => Configure::read('Asset.cacheTime'),
|
|
]))
|
|
|
|
// Add routing middleware.
|
|
// If you have a large number of routes connected, turning on routes
|
|
// caching in production could improve performance.
|
|
// See https://github.com/CakeDC/cakephp-cached-routing
|
|
->add(new RoutingMiddleware($this))
|
|
|
|
// Parse various types of encoded request bodies so that they are
|
|
// available as array through $request->getData()
|
|
// https://book.cakephp.org/5/en/controllers/middleware.html#body-parser-middleware
|
|
->add(new BodyParserMiddleware())
|
|
|
|
// Cross Site Request Forgery (CSRF) Protection Middleware
|
|
// https://book.cakephp.org/5/en/security/csrf.html#cross-site-request-forgery-csrf-middleware
|
|
->add((new CsrfProtectionMiddleware([
|
|
'httponly' => true,
|
|
]))->skipCheckCallback(
|
|
// Embedded slides run in third-party iframes (no cookies); the secret key is the authorisation.
|
|
fn(ServerRequestInterface $request): bool => SecurityHeadersMiddleware::isEmbed($request),
|
|
));
|
|
|
|
return $middlewareQueue;
|
|
}
|
|
|
|
/**
|
|
* Register application container services.
|
|
*
|
|
* @param \Cake\Core\ContainerInterface $container The Container to update.
|
|
* @return void
|
|
* @link https://book.cakephp.org/5/en/development/dependency-injection.html#dependency-injection
|
|
*/
|
|
public function services(ContainerInterface $container): void
|
|
{
|
|
// Allow your Tables to be dependency injected
|
|
//$container->delegate(new \Cake\ORM\Locator\TableContainer());
|
|
}
|
|
|
|
/**
|
|
* Register custom event listeners here
|
|
*
|
|
* @param \Cake\Event\EventManagerInterface $eventManager
|
|
* @return \Cake\Event\EventManagerInterface
|
|
* @link https://book.cakephp.org/5/en/core-libraries/events.html#registering-listeners
|
|
*/
|
|
public function events(EventManagerInterface $eventManager): EventManagerInterface
|
|
{
|
|
// $eventManager->on(new SomeCustomListenerClass());
|
|
|
|
return $eventManager;
|
|
}
|
|
}
|