security: add CSRF protection and harden core handling
Add CSRF token validation and include tokens in authentication, website manager, and GrapesJS forms and requests. Harden login with password hash verification and session ID regeneration, validate database identifiers and charset, and secure upload MIME detection and filename generation. Update nullable type declarations and PHP syntax across core contracts and modules, switch the Docker image to PHP 8.4, and move the theme script asset to the end of the page. Refresh the PageBuilderBody distribution assets.
This commit is contained in:
@@ -55,6 +55,12 @@ class Plugi
|
||||
}
|
||||
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
// Harden session cookies against hijacking and fixation
|
||||
$isHttps = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
|
||||
ini_set('session.cookie_httponly', '1');
|
||||
ini_set('session.cookie_samesite', 'Strict');
|
||||
ini_set('session.cookie_secure', $isHttps ? '1' : '0');
|
||||
ini_set('session.use_strict_mode', '1');
|
||||
session_start();
|
||||
}
|
||||
|
||||
@@ -360,8 +366,14 @@ class Plugi
|
||||
* @param string|null $action
|
||||
* @return bool
|
||||
*/
|
||||
public function handleRequest($action = null)
|
||||
public function handleRequest(?string $action = null)
|
||||
{
|
||||
// Set security headers on every request
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
header('X-Frame-Options: SAMEORIGIN');
|
||||
header('X-XSS-Protection: 1; mode=block');
|
||||
header('Referrer-Policy: strict-origin-when-cross-origin');
|
||||
|
||||
$route = $route ?? $_GET['route'] ?? null;
|
||||
$action = $action ?? $_GET['action'] ?? $_POST['action'] ?? null;
|
||||
if (! phpb_config('auth.use_login') || ! phpb_config('website_manager.use_website_manager')) {
|
||||
@@ -369,6 +381,16 @@ class Plugi
|
||||
. 'Implement a piece of code that checks whether the user is logged in. If logged in, call handleAuthenticatedRequest() or else call handlePublicRequest().');
|
||||
}
|
||||
|
||||
// Validate CSRF token on POST requests for authenticated admin routes only
|
||||
// (login has its own protection, public extension routes don't need CSRF)
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $action !== 'login') {
|
||||
$isAdminRoute = phpb_in_module('website_manager') || phpb_in_module('pagebuilder');
|
||||
if ($isAdminRoute && !phpb_validate_csrf()) {
|
||||
header('HTTP/1.1 403 Forbidden');
|
||||
die('Invalid CSRF token. Please refresh the page and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
// handle login and logout requests
|
||||
$this->auth->handleRequest($action);
|
||||
|
||||
@@ -525,7 +547,7 @@ class Plugi
|
||||
* @param string|null $route
|
||||
* @param string|null $action
|
||||
*/
|
||||
public function handleAuthenticatedRequest($route = null, $action = null)
|
||||
public function handleAuthenticatedRequest(?string $route = null, ?string $action = null)
|
||||
{
|
||||
$route = $route ?? $_GET['route'] ?? null;
|
||||
$action = $action ?? $_GET['action'] ?? null;
|
||||
@@ -568,14 +590,21 @@ class Plugi
|
||||
}
|
||||
|
||||
$uploadedFile = $uploadedFile[0];
|
||||
$serverFile = realpath(phpb_config('storage.uploads_folder') . '/' . $uploadedFile->server_file);
|
||||
$uploadsFolder = realpath(phpb_config('storage.uploads_folder'));
|
||||
$serverFile = realpath($uploadsFolder . '/' . $uploadedFile->server_file);
|
||||
// add backwards compatibility for files uploaded with Plugi <= v0.12.0, stored as /uploads/{id}.{extension}
|
||||
if (! $serverFile) $serverFile = realpath(phpb_config('storage.uploads_folder') . '/' . basename($uploadedFile->server_file));
|
||||
if (! $serverFile) $serverFile = realpath($uploadsFolder . '/' . basename($uploadedFile->server_file));
|
||||
if (! $serverFile) {
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
exit();
|
||||
}
|
||||
|
||||
// prevent path traversal by ensuring the resolved path stays within the uploads folder
|
||||
if (strpos($serverFile, $uploadsFolder) !== 0) {
|
||||
header("HTTP/1.1 403 Forbidden");
|
||||
exit();
|
||||
}
|
||||
|
||||
header('Content-Type: ' . $uploadedFile->mime_type);
|
||||
header('Content-Disposition: inline; filename="' . basename($uploadedFile->original_file) . '"');
|
||||
header('Expires: 0');
|
||||
|
||||
Reference in New Issue
Block a user