Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

Media upload file type check by mime type to avoid uploading svg files with different extension, vulnerability reported by @KhanMarshaI @0xHamy

givanz6f3a4ab53f8a
diff --git a/admin/controller/media/media.php b/admin/controller/media/media.php
index cae502e..6b04fe6 100755
--- a/admin/controller/media/media.php
+++ b/admin/controller/media/media.php
@@ -33,10 +33,6 @@ class Media extends Base {
protected $dirMedia = DIR_MEDIA;
- protected $uploadDenyExtensions = ['php', 'svg', 'js'];
-
- //protected $uploadAllowExtensions = ['ico','jpg','jpeg','png','gif','webp', 'mp4', 'mkv', 'mov'];
-
function index() {
$adminPath = \Vvveb\adminPath();
$controllerPath = $adminPath . 'index.php?module=media/media';
diff --git a/system/traits/media.php b/system/traits/media.php
index 7a90a05..dc7cb7c 100644
--- a/system/traits/media.php
+++ b/system/traits/media.php
@@ -28,6 +28,14 @@ use function Vvveb\parseQuantity;
use function Vvveb\sanitizeFileName;
trait Media {
+ protected $uploadDenyExtensions = ['php', 'svg', 'js', 'exe'];
+
+ protected $uploadDenyMime = ['image/svg', 'image/svg+xml', 'application/javascript', 'application/x-msdownload'];
+
+ protected $stripMetadataMime = ['image/jpg', 'image/jpeg', 'image/png', 'image/webp', 'image/avif'];
+
+ //protected $uploadAllowExtensions = ['ico','jpg','jpeg','png','gif','webp', 'mp4', 'mkv', 'mov'];
+
function dirForType($type) {
switch ($type) {
case 'public':
@@ -78,11 +86,13 @@ trait Media {
$fileName = sanitizeFileName($files['name'][$count]);
if (V_SUBDIR_INSTALL && strpos($path, V_SUBDIR_INSTALL) === 0) {
+ //$path = str_replace(V_SUBDIR_INSTALL, '', $path);
$path = substr_replace($path, '', 0, strlen(V_SUBDIR_INSTALL));
}
$path = preg_replace('@.*[\\\/]public[\\\/]media|.*[\\\/]media|.*[\\\/]public@', '', $path);
$extension = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
+ $mimeType = mime_content_type($files['tmp_name'][$count]);
if ($files['error'][$count] == UPLOAD_ERR_OK) {
$success = true;
@@ -95,6 +105,15 @@ trait Media {
$success = false;
}
+ if (isset($this->uploadDenyMime) && in_array($mimeType, $this->uploadDenyMime)) {
+ $message .= __('File type not allowed!');
+ $success = false;
+ }
+
+ if (isset($this->stripMetadataMime) && in_array($mimeType, $this->stripMetadataMime)) {
+ $files['tmp_name'][$count];
+ }
+
$origFilename = $fileName;
$i = 1;
@@ -160,6 +179,13 @@ trait Media {
$currentFile = $dirMedia . DS . $file;
$targetFile = $dirMedia . DS . $newfile;
+ $extension = strtolower(substr($newfile, strrpos($newfile, '.') + 1));
+
+ if (isset($this->uploadDenyExtensions) && in_array($extension, $this->uploadDenyExtensions)) {
+ $message .= __('File type not allowed!');
+ $success = false;
+ }
+
if ($duplicate) {
if (copy($currentFile, $targetFile)) {
$message = ['success' => true, 'message' => __('File copied!')];