Implemented: editor uploads are constrained to safe image files inside

the upload tree, and public form submissions are
size-bounded and rate-limited
This commit is contained in:
2026-11-30 16:51:15 +01:00
parent e1767dcd2c
commit 25030638a4
11 changed files with 417 additions and 23 deletions
@@ -135,15 +135,35 @@ class PageBuilder implements PageBuilderContract
{
$publicId = sha1(uniqid(rand(), true));
$uploader = phpb_instance(Uploader::class, ['files']);
$allowedImages = [
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/gif' => 'gif',
'image/webp' => 'webp',
'image/bmp' => 'bmp',
];
if (!is_string($uploader->file_src_name) || !isset($allowedImages[$uploader->file_src_mime])) {
http_response_code(415);
die('Upload error: Unsupported image type');
}
$sourceName = basename(str_replace('\\', '/', $uploader->file_src_name));
$originalFile = preg_replace('/[^A-Za-z0-9._-]/', '-', $sourceName);
$originalFile = substr($originalFile, 0, 150);
$baseName = preg_replace('/[^A-Za-z0-9_-]+/', '-', pathinfo($sourceName, PATHINFO_FILENAME));
$baseName = trim(substr($baseName, 0, 100), '-_');
$fileName = ($baseName !== '' ? $baseName : 'image') . '.' . $allowedImages[$uploader->file_src_mime];
$uploader->MIME_allowed = array_keys($allowedImages);
$uploader
->file_name($publicId . '/' . str_replace(' ', '-', $uploader->file_src_name))
->file_name($publicId . '/' . $fileName)
->upload_to(phpb_config('storage.uploads_folder') . '/')
->mime_check()
->run();
if (! $uploader->was_uploaded) {
die("Upload error: {$uploader->error}");
} else {
$originalFile = str_replace(' ', '-', $uploader->file_src_name);
$originalMime = $uploader->file_src_mime;
$serverFile = $uploader->final_file_name;
@@ -27,7 +27,6 @@ class Uploader {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
if ($finfo) {
$mime = finfo_file($finfo, $filePath);
finfo_close($finfo);
if (!empty($mime)) {
return $mime;
}
@@ -256,6 +255,11 @@ class Uploader {
* @throws Exception
*/
public function run() {
$uploadRoot = rtrim($this->upload_to, '/\\');
if ($uploadRoot === '') {
$uploadRoot = DIRECTORY_SEPARATOR;
}
// check whether the final name is in random mode
if ($this->file_name !== true) {
// preserve file extension if provided
@@ -264,13 +268,24 @@ class Uploader {
} else {
$file = $this->file_name;
}
$path = $this->upload_to . $file;
$this->upload_to = dirname($path);
} else {
$hash = bin2hex(random_bytes(16));
$file = $hash . '.' . $this->file_src_name_ext;
$path = $this->upload_to . $file;
}
if (!$this->isSafeRelativePath($file)) {
$this->was_uploaded = false;
$this->error = 'Invalid destination file name.';
return false;
}
$relativeFile = str_replace('\\', '/', $file);
$destinationDirectory = $this->getSafeDestinationDirectory($uploadRoot, $relativeFile);
if ($destinationDirectory === null) {
$this->was_uploaded = false;
$this->error = 'Destination directory must remain inside the upload folder.';
return false;
}
$path = $destinationDirectory . DIRECTORY_SEPARATOR . basename($relativeFile);
$this->upload_to = $destinationDirectory;
$get_mime = isset($this->mime_check) ? true : false;
@@ -282,6 +297,9 @@ class Uploader {
$this->was_uploaded = false;
$this->error = "Incorrect type of file";
}
if (!$this->was_uploaded) {
return false;
}
// check file maximum size
if ($this->file_src_size > $this->get_file_max_size) {
@@ -290,18 +308,6 @@ class Uploader {
return false;
}
// check whether destination directory exists or attempt to create it
if ($this->get_auto_create_path) {
if (! $this->r_mkdir($this->upload_to)) {
$this->was_uploaded = false;
$this->error = "Destination directory can't be created. Can't carry on a process";
return false;
}
} elseif (! is_dir($this->upload_to)) {
$this->error = "Destination directory doesn't exist. Can't carry on a process";
return false;
}
// check whether file already exists or if are replacing it
if (file_exists($path)) {
if (! $this->get_auto_replace) {
@@ -364,6 +370,60 @@ class Uploader {
return false;
}
protected function isSafeRelativePath(string $file): bool
{
$file = str_replace('\\', '/', $file);
if ($file === '' || str_starts_with($file, '/') || preg_match('/^[A-Za-z]:/', $file) || str_contains($file, "\0")) {
return false;
}
foreach (explode('/', $file) as $segment) {
if ($segment === '' || $segment === '.' || $segment === '..') {
return false;
}
}
return true;
}
protected function getSafeDestinationDirectory(string $uploadRoot, string $relativeFile): ?string
{
if (!is_dir($uploadRoot) && (!$this->get_auto_create_path || !$this->r_mkdir($uploadRoot))) {
$this->error = "Destination directory can't be created. Can't carry on a process";
return null;
}
$realRoot = realpath($uploadRoot);
if (!$realRoot) {
return null;
}
$relativeDirectory = dirname($relativeFile);
if ($relativeDirectory === '.') {
return $realRoot;
}
$directory = $realRoot;
foreach (explode('/', $relativeDirectory) as $segment) {
$nextDirectory = $directory . DIRECTORY_SEPARATOR . $segment;
if (is_link($nextDirectory)) {
return null;
}
if (!is_dir($nextDirectory) && (!$this->get_auto_create_path || !$this->_mkdir($nextDirectory))) {
$this->error = "Destination directory can't be created. Can't carry on a process";
return null;
}
$realDirectory = realpath($nextDirectory);
if (!$realDirectory || !str_starts_with($realDirectory, $realRoot . DIRECTORY_SEPARATOR)) {
return null;
}
$directory = $realDirectory;
}
return $directory;
}
/**
* Create directories recursively.
*
+67
View File
@@ -0,0 +1,67 @@
<?php
use Plugi\Modules\GrapesJS\Upload\Uploader;
require_once __DIR__ . '/../src/Modules/GrapesJS/Upload/Uploader.php';
function assertUploadSecurity(bool $condition, string $message): void
{
if (!$condition) {
throw new RuntimeException($message);
}
}
$uploadRoot = sys_get_temp_dir() . '/plugi-upload-security-' . bin2hex(random_bytes(8));
assertUploadSecurity(mkdir($uploadRoot), 'The upload security test folder could not be created.');
$outsideFile = dirname($uploadRoot) . '/plugi-upload-escape-' . bin2hex(random_bytes(8)) . '.php';
$imagePath = __DIR__ . '/../dist/pagebuilder/images/image-placeholder.png';
$traversalUpload = new Uploader([
'name' => 'image.png',
'tmp_name' => $imagePath,
'size' => filesize($imagePath),
'error' => UPLOAD_ERR_OK,
], false);
$traversalUpload
->file_name('../' . basename($outsideFile))
->upload_to($uploadRoot . '/')
->run();
assertUploadSecurity(!$traversalUpload->was_uploaded, 'Path traversal destinations must be rejected.');
assertUploadSecurity(!file_exists($outsideFile), 'Path traversal must not create files outside the upload folder.');
$linkedDirectory = $uploadRoot . '/linked';
assertUploadSecurity(symlink(dirname($uploadRoot), $linkedDirectory), 'The upload symlink test could not be created.');
$symlinkFile = dirname($uploadRoot) . '/plugi-upload-symlink-' . bin2hex(random_bytes(8)) . '.png';
$symlinkUpload = new Uploader([
'name' => 'image.png',
'tmp_name' => $imagePath,
'size' => filesize($imagePath),
'error' => UPLOAD_ERR_OK,
], false);
$symlinkUpload
->file_name('linked/' . basename($symlinkFile))
->upload_to($uploadRoot . '/')
->run();
assertUploadSecurity(!$symlinkUpload->was_uploaded, 'Symlinked destination folders must be rejected.');
assertUploadSecurity(!file_exists($symlinkFile), 'Symlinked destinations must not create files outside the upload folder.');
assertUploadSecurity(unlink($linkedDirectory), 'The upload symlink test could not be removed.');
$scriptUpload = new Uploader([
'name' => 'payload.php',
'tmp_name' => __FILE__,
'size' => filesize(__FILE__),
'error' => UPLOAD_ERR_OK,
], false);
$scriptUpload->MIME_allowed = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/bmp'];
$scriptUpload
->file_name('payload.jpg')
->upload_to($uploadRoot . '/')
->mime_check()
->run();
assertUploadSecurity(!$scriptUpload->was_uploaded, 'Non-image uploads must be rejected when image MIME validation is enabled.');
$uploadRules = file_get_contents(dirname(__DIR__, 2) . '/uploads/.htaccess');
assertUploadSecurity(strpos($uploadRules, 'Require all denied') !== false, 'Uploaded PHP scripts must be denied by the Apache upload rules.');
assertUploadSecurity(rmdir($uploadRoot), 'The upload security test folder could not be removed.');
echo "Upload security checks passed.\n";