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:
2026-11-24 09:26:15 +00:00
parent d6c06b82d5
commit 2b73242d04
34 changed files with 311 additions and 61 deletions
@@ -15,6 +15,31 @@ use Exception;
#[\AllowDynamicProperties]
class Uploader {
/**
* Detect the MIME type of a file using server-side inspection.
*
* @param string $filePath Path to the file
* @return string The detected MIME type
*/
protected function detectMimeType(string $filePath): string
{
if (function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if ($finfo) {
$mime = finfo_file($finfo, $filePath);
finfo_close($finfo);
if (!empty($mime)) {
return $mime;
}
}
}
// Fallback to mime_content_type if available
if (function_exists('mime_content_type')) {
return mime_content_type($filePath);
}
return '';
}
/**
* Indicates if the file has been uploaded properly.
*
@@ -216,7 +241,8 @@ class Uploader {
$this->file_src_temp = $file["tmp_name"];
$this->file_src_size = $file["size"];
$this->file_src_errors = $file['error'];
$this->file_src_mime = $file['type'];
// Use server-side MIME detection instead of trusting client-supplied $_FILES['type']
$this->file_src_mime = $this->detectMimeType($file['tmp_name']);
$this->file_src_name_ext = pathinfo($file["name"], PATHINFO_EXTENSION);
$this->was_uploaded = true;
@@ -241,7 +267,7 @@ class Uploader {
$path = $this->upload_to . $file;
$this->upload_to = dirname($path);
} else {
$hash = md5(uniqid(rand(), true));
$hash = bin2hex(random_bytes(16));
$file = $hash . '.' . $this->file_src_name_ext;
$path = $this->upload_to . $file;
}
@@ -317,7 +343,7 @@ class Uploader {
$this->final_file_name = $file;
// extract image dimensions
list($w, $h) = getimagesize($path);
[$w, $h] = getimagesize($path);
$this->file_width = $w;
$this->file_height = $h;
@@ -345,7 +371,7 @@ class Uploader {
* @param integer $mode Optional permissions
* @return boolean Success
*/
protected function r_mkdir($path, $mode = 0777) {
protected function r_mkdir($path, $mode = 0755) {
return is_dir($path) || ( $this->r_mkdir(dirname($path), $mode) && $this->_mkdir($path, $mode) );
}
@@ -356,7 +382,7 @@ class Uploader {
* @param integer $mode Optional permissions
* @return boolean Success
*/
protected function _mkdir($path, $mode = 0777) {
protected function _mkdir($path, $mode = 0755) {
$old = umask(0);
$res = @mkdir($path, $mode);
umask($old);