Skip to content

Commit 0da29fd

Browse files
authored
Update zip_install.php
1 parent ea10cf6 commit 0da29fd

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

lib/zip_install.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,48 @@ 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'));
62+
// Validate file extension
63+
$fileExtension = strtolower(pathinfo($uploadedFile['name'], PATHINFO_EXTENSION));
64+
if ($fileExtension !== 'zip') {
65+
return rex_view::error(rex_i18n::msg('zip_install_extension_error'));
6666
}
6767

68+
// Check mime type (as before)
69+
$allowedMimeTypes = ['application/zip', 'application/octet-stream'];
70+
if (!in_array($uploadedFile['type'], $allowedMimeTypes)) {
71+
72+
// Check actual mime type with fileinfo extension
73+
if (function_exists('finfo_open')) {
74+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
75+
$actualMimeType = finfo_file($finfo, $uploadedFile['tmp_name']);
76+
finfo_close($finfo);
77+
if (!in_array($actualMimeType, $allowedMimeTypes)) {
78+
return rex_view::error(rex_i18n::msg('zip_install_mime_error'));
79+
}
80+
}
81+
else {
82+
return rex_view::error(rex_i18n::msg('zip_install_mime_error'));
83+
}
84+
}
6885

6986
// Check filesize
7087
$maxSize = $this->addon->getConfig('upload_max_size', 20) * 1024 * 1024; // Convert MB to bytes
7188
if ($uploadedFile['size'] > $maxSize) {
7289
return rex_view::error(rex_i18n::msg('zip_install_size_error', $this->addon->getConfig('upload_max_size', 20)));
7390
}
7491

75-
$tmpFile = $this->tmpFolder . '/' . uniqid('upload_') . '.zip'; // Generate unique filename
92+
$tmpFile = $this->tmpFolder . '/' . uniqid('upload_') . '.zip'; // Generate unique filename
93+
7694
try {
95+
96+
// Verify file content before moving
97+
$zip = new ZipArchive();
98+
if ($zip->open($uploadedFile['tmp_name']) !== true) {
99+
throw new Exception(rex_i18n::msg('zip_install_invalid_zip'));
100+
}
101+
$zip->close();
102+
103+
77104
if (!move_uploaded_file($uploadedFile['tmp_name'], $tmpFile)) {
78105
throw new Exception(rex_i18n::msg('zip_install_upload_failed'));
79106
}
@@ -120,7 +147,7 @@ public function handleUrlInput(string $url): string
120147
}
121148

122149
// Download file
123-
$tmpFile = $this->tmpFolder . '/' . uniqid('download_') . '.zip'; // Generate unique filename
150+
$tmpFile = $this->tmpFolder . '/' . uniqid('download_') . '.zip'; // Generate unique filename
124151
if (!$this->downloadFile($url, $tmpFile)) {
125152
return rex_view::error(rex_i18n::msg('zip_install_url_file_not_loaded'));
126153
}

0 commit comments

Comments
 (0)