服务端校验清单
服务端校验清单
1. 扩展名白名单(按业务需求列出,如 jpg/png/pdf)
2. 内容特征校验(图片解码验证、文件头魔数)
3. 重命名存储(UUID + 固定扩展名),不使用用户文件名
4. 存储目录禁止解析执行(服务器配置层)
5. 大小与数量限制,防止资源耗尽图片内容校验示意(Python Pillow)
图片内容校验示意(Python Pillow)
from PIL import Image
img = Image.open(path)
img.verify() # 校验是否为有效图片
if img.format not in ('JPEG', 'PNG', 'WEBP'):
raise ValueError('unsupported image format')MIME 类型(Content-Type)由客户端提供、可伪造,只能作为辅助信号,不能作为唯一依据。