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:
@@ -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