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:
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user