← PHP / Laravel · 目录

PHP-014 / 入门

仅信任上传文件扩展名或 MIME 类型

A04:2021 Insecure Design · CWE-434 Unrestricted Upload of File with Dangerous Type

为什么存在问题?

客户端提供的 Content-Type 与扩展名可伪造,也存在 .php.jpg 等绕过方式。

如何修复

php
$request->validate(['img' => 'required|image|mimes:jpg,png|max:2048']);
$path = $request->file('img')->store('avatars', 'private'); // 随机文件名,私有磁盘

延伸阅读