Skip to content

Commit aa84b11

Browse files
authored
better and secure file handling
1 parent 6b6c044 commit aa84b11

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/zip_install.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,20 @@ public function handleFileUpload(): string
5959
/** @var array{name: string, type: string, tmp_name: string, error: int, size: int} $uploadedFile */
6060
$uploadedFile = $_FILES['zip_file'];
6161

62+
// Check mime type
63+
$allowedMimeTypes = ['application/zip', 'application/octet-stream'];
64+
if (!in_array($uploadedFile['type'], $allowedMimeTypes)) {
65+
return rex_view::error(rex_i18n::msg('zip_install_mime_error'));
66+
}
67+
6268

6369
// Check filesize
6470
$maxSize = $this->addon->getConfig('upload_max_size', 20) * 1024 * 1024; // Convert MB to bytes
6571
if ($uploadedFile['size'] > $maxSize) {
6672
return rex_view::error(rex_i18n::msg('zip_install_size_error', $this->addon->getConfig('upload_max_size', 20)));
6773
}
6874

69-
$tmpFile = $this->tmpFolder . '/temp.zip';
75+
$tmpFile = $this->tmpFolder . '/' . uniqid('upload_') . '.zip'; // Generate unique filename
7076
try {
7177
if (!move_uploaded_file($uploadedFile['tmp_name'], $tmpFile)) {
7278
throw new Exception(rex_i18n::msg('zip_install_upload_failed'));
@@ -114,7 +120,7 @@ public function handleUrlInput(string $url): string
114120
}
115121

116122
// Download file
117-
$tmpFile = $this->tmpFolder . '/download.zip';
123+
$tmpFile = $this->tmpFolder . '/' . uniqid('download_') . '.zip'; // Generate unique filename
118124
if (!$this->downloadFile($url, $tmpFile)) {
119125
return rex_view::error(rex_i18n::msg('zip_install_url_file_not_loaded'));
120126
}

0 commit comments

Comments
 (0)