@@ -59,21 +59,48 @@ public function handleFileUpload(): string
59
59
/** @var array{name: string, type: string, tmp_name: string, error: int, size: int} $uploadedFile */
60
60
$ uploadedFile = $ _FILES ['zip_file ' ];
61
61
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 ' ));
66
66
}
67
67
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
+ }
68
85
69
86
// Check filesize
70
87
$ maxSize = $ this ->addon ->getConfig ('upload_max_size ' , 20 ) * 1024 * 1024 ; // Convert MB to bytes
71
88
if ($ uploadedFile ['size ' ] > $ maxSize ) {
72
89
return rex_view::error (rex_i18n::msg ('zip_install_size_error ' , $ this ->addon ->getConfig ('upload_max_size ' , 20 )));
73
90
}
74
91
75
- $ tmpFile = $ this ->tmpFolder . '/ ' . uniqid ('upload_ ' ) . '.zip ' ; // Generate unique filename
92
+ $ tmpFile = $ this ->tmpFolder . '/ ' . uniqid ('upload_ ' ) . '.zip ' ; // Generate unique filename
93
+
76
94
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
+
77
104
if (!move_uploaded_file ($ uploadedFile ['tmp_name ' ], $ tmpFile )) {
78
105
throw new Exception (rex_i18n::msg ('zip_install_upload_failed ' ));
79
106
}
@@ -120,7 +147,7 @@ public function handleUrlInput(string $url): string
120
147
}
121
148
122
149
// Download file
123
- $ tmpFile = $ this ->tmpFolder . '/ ' . uniqid ('download_ ' ) . '.zip ' ; // Generate unique filename
150
+ $ tmpFile = $ this ->tmpFolder . '/ ' . uniqid ('download_ ' ) . '.zip ' ; // Generate unique filename
124
151
if (!$ this ->downloadFile ($ url , $ tmpFile )) {
125
152
return rex_view::error (rex_i18n::msg ('zip_install_url_file_not_loaded ' ));
126
153
}
0 commit comments