安全实现示意(Python)
安全实现示意(Python)
import os
BASE = os.path.realpath('/srv/files')
name = os.path.basename(user_input) # 先剥离路径成分
target = os.path.realpath(os.path.join(BASE, name))
if not target.startswith(BASE + os.sep):
raise PermissionError('path out of base')
# 更稳的做法:文件名与数据库 ID 映射,磁盘文件使用 UUID 命名多轮编码(%2e%2e%2f 等)会在框架解码后变成 ../;校验必须发生在解码之后、文件系统调用之前。