-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
bugSomething isn't workingSomething isn't working
Description
描述 (Description)
当尝试将文件上传到包含非 ASCII 字符(如中文)的目录时,服务器未对 URL 参数中的路径进行解码。导致系统没有使用现有的中文目录,而是在文件系统中创建了一个名为 URL 编码字符串(如 %E6%9D%82%E9%A1%B9)的新目录。
复现步骤 (Steps to Reproduce)
- 在
files目录下存在一个中文文件夹,例如files/杂项。 - 在浏览器中进入该文件夹,URL 显示为
.../files/%E6%9D%82%E9%A1%B9/。 - 拖拽一个文件上传到该目录。
- 检查服务器的
files目录,发现多了一个名为%E6%9D%82%E9%A1%B9的文件夹,文件被上传到了这里,而不是原有的files/杂项目录。
原因分析 (Root Cause)
在 index.js 处理 /upload 请求时,直接从 URL 查询参数 dir 中获取路径,但没有进行 decodeURIComponent 解码。
前端浏览器通常会将 URL 中的中文路径自动编码传输,Node.js 接收到的是编码后的字符串,随后直接用作文件系统路径创建目录。
相关代码 (Code Snippet)
index.js 约第 36 行:
// 当前代码
let uploadDir = urlObj.searchParams.get('dir') || '/';
// uploadDir 此时可能为 "/%E6%9D%82%E9%A1%B9/"
uploadDir = path.join(FILES_DIR, uploadDir); 建议方案 (Proposed Solution)
在获取 uploadDir 后,立即对其进行解码。
// 建议修改
let uploadDir = urlObj.searchParams.get('dir') || '/';
uploadDir = decodeURIComponent(uploadDir); // 添加解码步骤
uploadDir = path.join(FILES_DIR, uploadDir);Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working