fix: 修复两个安全漏洞
1. 修复分享下载接口越权访问漏洞(高危) - 添加isPathWithinShare函数验证请求路径是否在分享范围内 - 单文件分享只允许下载该文件 - 目录分享只允许下载该目录及子目录的文件 - 防止攻击者通过构造path参数访问分享者的任意文件 - 相关文件:backend/server.js 2. 修复本地存储路径处理问题(中高危) - 优化getFullPath方法处理绝对路径的逻辑 - 修复Linux环境下path.join处理'/'导致的路径错误 - 将绝对路径转换为相对路径,确保正确拼接用户目录 - 相关文件:backend/storage.js 🔐 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -195,12 +195,23 @@ class LocalStorageClient {
|
||||
*/
|
||||
getFullPath(relativePath) {
|
||||
// 1. 规范化路径,移除 ../ 等危险路径
|
||||
const normalized = path.normalize(relativePath).replace(/^(\.\.[\/\\])+/, '');
|
||||
let normalized = path.normalize(relativePath || '').replace(/^(\.\.[\/\])+/, '');
|
||||
|
||||
// 2. 拼接完整路径
|
||||
// 2. ✅ 修复:将绝对路径转换为相对路径(解决Linux环境下的问题)
|
||||
if (path.isAbsolute(normalized)) {
|
||||
// 移除开头的 / 或 Windows 盘符,转为相对路径
|
||||
normalized = normalized.replace(/^[\/\]+/, '').replace(/^[a-zA-Z]:/, '');
|
||||
}
|
||||
|
||||
// 3. 空字符串或 . 表示根目录
|
||||
if (normalized === '' || normalized === '.') {
|
||||
return this.basePath;
|
||||
}
|
||||
|
||||
// 4. 拼接完整路径
|
||||
const fullPath = path.join(this.basePath, normalized);
|
||||
|
||||
// 3. 安全检查:确保路径在用户目录内(防止目录遍历攻击)
|
||||
// 5. 安全检查:确保路径在用户目录内(防止目录遍历攻击)
|
||||
if (!fullPath.startsWith(this.basePath)) {
|
||||
throw new Error('非法路径访问');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user