mirror of
https://gitee.com/technical-laohu/mpay.git
synced 2025-11-11 13:13:44 +08:00
更新图片上传可能的漏洞
This commit is contained in:
@@ -7,6 +7,7 @@ namespace app\controller\api;
|
|||||||
use app\BaseController;
|
use app\BaseController;
|
||||||
use app\model\PayAccount;
|
use app\model\PayAccount;
|
||||||
use app\model\PayChannel;
|
use app\model\PayChannel;
|
||||||
|
use \think\facade\Log;
|
||||||
|
|
||||||
class PayManageController extends BaseController
|
class PayManageController extends BaseController
|
||||||
{
|
{
|
||||||
@@ -125,30 +126,59 @@ class PayManageController extends BaseController
|
|||||||
// 上传二维码图片
|
// 上传二维码图片
|
||||||
public function uploadQrcode()
|
public function uploadQrcode()
|
||||||
{
|
{
|
||||||
$img = $this->request->file('codeimg');
|
try {
|
||||||
if (!$img) {
|
// 获取上传的文件
|
||||||
return json(backMsg(1, '请选择要上传的文件'));
|
$img = $this->request->file('codeimg');
|
||||||
}
|
if (!$img) {
|
||||||
// 验证文件类型
|
return json(backMsg(1, '请选择要上传的文件'));
|
||||||
$allowedTypes = ['image/png', 'image/jpeg', 'image/gif'];
|
}
|
||||||
$fileMimeType = $img->getMime();
|
|
||||||
if (!in_array($fileMimeType, $allowedTypes)) {
|
// 验证文件大小,防止大文件攻击
|
||||||
return json(backMsg(1, '只允许上传PNG、JPEG或GIF格式的图片'));
|
$maxSize = 2 * 1024 * 1024; // 2MB
|
||||||
}
|
if ($img->getSize() > $maxSize) {
|
||||||
// 生成唯一文件名
|
return json(backMsg(1, '文件大小不能超过 2MB'));
|
||||||
$filename = 'img_' . time() . '_' . uniqid() . '.' . $img->getOriginalExtension();
|
}
|
||||||
// 设置文件保存路径
|
|
||||||
$path = public_path() . '/files/qrcode/';
|
// 验证文件类型,防止恶意文件上传
|
||||||
if (!is_dir($path)) {
|
$allowedTypes = ['image/png', 'image/jpeg', 'image/gif'];
|
||||||
mkdir($path, 0755, true);
|
$fileMimeType = $img->getMime();
|
||||||
}
|
if (!in_array($fileMimeType, $allowedTypes)) {
|
||||||
// 移动文件到指定目录
|
return json(backMsg(1, '只允许上传 PNG、JPEG 或 GIF 格式的图片'));
|
||||||
$info = $img->move($path, $filename);
|
}
|
||||||
if ($info) {
|
|
||||||
$imgpath = '/files/qrcode/' . $filename;
|
// 二次验证文件类型,通过文件内容判断
|
||||||
return json(backMsg(0, '上传成功', ['imgpath' => $imgpath]));
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||||
} else {
|
$realMimeType = finfo_file($finfo, $img->getRealPath());
|
||||||
return json(backMsg(1, '上传失败'));
|
finfo_close($finfo);
|
||||||
|
if (!in_array($realMimeType, $allowedTypes)) {
|
||||||
|
return json(backMsg(1, '文件类型验证失败,请上传有效的图片文件'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成唯一文件名,避免文件名冲突
|
||||||
|
$filename = 'img_' . time() . '_' . uniqid() . '.' . $img->getOriginalExtension();
|
||||||
|
|
||||||
|
// 过滤文件名,防止路径遍历攻击
|
||||||
|
$filename = preg_replace('/[^a-zA-Z0-9_\-\.]/', '', $filename);
|
||||||
|
|
||||||
|
// 设置文件保存路径
|
||||||
|
$path = public_path() . '/files/qrcode/';
|
||||||
|
if (!is_dir($path)) {
|
||||||
|
if (!mkdir($path, 0755, true)) {
|
||||||
|
return json(backMsg(1, '创建目录失败'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移动文件到指定目录
|
||||||
|
$info = $img->move($path, $filename);
|
||||||
|
if ($info) {
|
||||||
|
$imgpath = '/files/qrcode/' . $filename;
|
||||||
|
return json(backMsg(0, '上传成功', ['imgpath' => $imgpath]));
|
||||||
|
} else {
|
||||||
|
return json(backMsg(1, '上传失败'));
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error('上传过程中出现异常: ' . $e->getMessage());
|
||||||
|
return json(backMsg(1, '上传过程中出现异常,请稍后重试'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 获取账号交易流水
|
// 获取账号交易流水
|
||||||
|
|||||||
Reference in New Issue
Block a user