一家网站安全审计公司为我的一个 Drupal 项目提出了同样的问题。我的场景,如果我将 .exe 文件更改为 .jpg 然后尝试通过表单上传,它是为我上传的。
我在 web 和 drupal 社区上进行了搜索,但没有得到任何合适的答案。最终
我在核心“include/file.inc”文件中进行了编辑,并将支持的 mime 类型定义为“'image/jpg'、'image/jpeg'、'image/png'、'image/gif'”。
但不建议更改 Drupal 核心文件。这是完整的代码:
“include/file.inc”文件中函数file_validate_mime_type的变化
$errors = array();
$allowedmimetype = explode('::', $mime_types);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$tmpmime =$finfo->file($file->uri);
if (!in_array($tmpmime, $allowedmimetype) )
{
$errors[] = t('Not allowed this mime type '.$tmpmime.'! Only files with the following mime type are allowed: %files-allowed.', array('%files-allowed' => $mime_types));
}
return $errors;
}
在“modules/image/image_field.inc”文件中添加了以下两行
$elements[$delta]['#upload_validators']['file_validate_mime_type'][0] = implode('::', $supported_mime);
在下一行之前
$elements[$delta]['#process'][] = 'image_field_widget_process';
网络安全审计团队通过了此代码。可能在未来我们会得到一些更好的答案。