Skip to content

Commit b7b045c

Browse files
committed
Enhance file handling and MIME type validation 🎉
- Added MIME type constants for better image type handling in `lhmailconvparser.php` 🖼️ - Improved inline download logic in `inlinedownload.php` to check for supported image types before disposition 🖼️ - Updated download logic in `downloadfile.php` to handle inline and attachment cases more robustly 📥 - Enhanced REST API file handling in `file.php` to validate MIME types and provide appropriate responses for unsupported types 🚫
1 parent e7ccd97 commit b7b045c

4 files changed

Lines changed: 22 additions & 5 deletions

File tree

lhc_web/lib/core/lhmailconv/lhmailconvparser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class erLhcoreClassMailconvParser {
44

55
const IMAGE_EXTENSIONS = ['png','bmp','gif','jfif','jpg','jpeg','webp','heic'];
6+
const IMAGE_MIME_TYPES = ['image/png','image/bmp','image/gif','image/jpeg','image/webp','image/heic'];
67

78
public static function getRawConnection($mailbox)
89
{

lhc_web/modules/lhfile/downloadfile.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157

158158
if (!(isset($_GET['modal']) && $_GET['modal'] === 'true')) {
159159
header('Content-type: '.$file->type);
160-
if (!isset($Params['user_parameters_unordered']['inline']) || $Params['user_parameters_unordered']['inline'] != 'true') {
160+
if (!isset($Params['user_parameters_unordered']['inline']) || $Params['user_parameters_unordered']['inline'] != 'true' || (isset($Params['user_parameters_unordered']['inline']) && $Params['user_parameters_unordered']['inline'] === 'true' && !in_array($file->type,['image/png','image/bmp','image/gif','image/jpeg','image/webp','image/heic']))) {
161161
// Download with file name
162162
header('Content-Disposition: attachment; filename="'.$file->id.'-'.pathinfo($file->upload_name, PATHINFO_FILENAME).'.'.$file->extension.'"');
163163
header('Referrer-Policy: no-referrer');
@@ -169,8 +169,7 @@
169169
}
170170
}
171171

172-
$response = erLhcoreClassChatEventDispatcher::getInstance()->dispatch('file.download', array('chat_file' => $file));
173-
172+
$response = erLhcoreClassChatEventDispatcher::getInstance()->dispatch('file.download', array('chat_file' => $file));
174173

175174
if (isset($_GET['modal']) && $_GET['modal'] === 'true') {
176175

lhc_web/modules/lhmailconv/inlinedownload.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
}
1414
}
1515

16-
if ($file->disposition != 'INLINE') {
16+
$inlineSupported = in_array($file->extension, erLhcoreClassMailconvParser::IMAGE_EXTENSIONS) && in_array(explode(';',$file->type)[0], erLhcoreClassMailconvParser::IMAGE_MIME_TYPES);
17+
18+
if ($inlineSupported === true && $file->disposition != 'INLINE') {
1719
$mcOptions = erLhcoreClassModelChatConfig::fetch('mailconv_options');
1820
$mcOptionsData = (array)$mcOptions->data;
1921
if ($file->extension === 'jpg' && !str_ends_with($file->name, '.jpg')) {
@@ -177,6 +179,11 @@
177179
header('X-Content-Type-Options: nosniff');
178180
header('Referrer-Policy: no-referrer');
179181
header("Cache-Control: private, max-age=3600");
182+
183+
if ($inlineSupported === false) {
184+
header('Content-Disposition: attachment; filename="'.$file->name.'"');
185+
}
186+
180187
echo file_get_contents($file->file_path_server);
181188
} else {
182189
echo file_get_contents('design/defaulttheme/images/general/denied.png');

lhc_web/modules/lhrestapi/file.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,17 @@
9696
$response = erLhcoreClassChatEventDispatcher::getInstance()->dispatch('file.download', array('chat_file' => $file));
9797

9898
// There was no callbacks or file not found etc, we try to download from standard location
99-
if ($response === false) {
99+
if ($response === false && str_starts_with($file->file_path_server, 'var/')) {
100+
if (\erLhcoreClassChatWebhookIncoming::getExtensionByMime($file->extension, true) != $file->type) {
101+
if (in_array($file->extension,['jpg','jpeg','png'])) {
102+
$denyImage = 'design/defaulttheme/images/general/denied.png';
103+
header('Content-type: image/png; charset=binary');
104+
echo file_get_contents($denyImage);
105+
exit;
106+
} else {
107+
exit('Mime type does not match!');
108+
}
109+
}
100110
echo file_get_contents($file->file_path_server);
101111
} else {
102112
echo $response['filedata'];

0 commit comments

Comments
 (0)