From 702571d1ef55af11c268933018aa63089244df0a Mon Sep 17 00:00:00 2001 From: meraklbz Date: Tue, 10 Feb 2026 21:04:14 +0800 Subject: [PATCH] fix bug --- backend/app/api/v1/endpoints/agent_tasks.py | 7 ++----- backend/app/services/agent/tools/file_tool.py | 5 +++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/backend/app/api/v1/endpoints/agent_tasks.py b/backend/app/api/v1/endpoints/agent_tasks.py index 90181db..d976d9f 100644 --- a/backend/app/api/v1/endpoints/agent_tasks.py +++ b/backend/app/api/v1/endpoints/agent_tasks.py @@ -2444,11 +2444,8 @@ async def _get_project_root( check_cancelled() # 🔥 解压前再次检查 with zipfile.ZipFile(zip_path, 'r') as zip_ref: # 🔥 逐个文件解压,支持取消检查 - file_list = zip_ref.namelist() - for i, file_name in enumerate(file_list): - if i % 50 == 0: # 每50个文件检查一次 - check_cancelled() - zip_ref.extract(file_name, base_path) + # 🔥 Security Fix: 使用 safe_extract_zip 替代 extract,防止 Zip Slip 和软链接攻击 + safe_extract_zip(zip_ref, base_path, task_id) logger.info(f"✅ Extracted ZIP project {project.id} to {base_path}") await emit(f"✅ ZIP 文件解压完成") except Exception as e: diff --git a/backend/app/services/agent/tools/file_tool.py b/backend/app/services/agent/tools/file_tool.py index 009cf44..572b218 100644 --- a/backend/app/services/agent/tools/file_tool.py +++ b/backend/app/services/agent/tools/file_tool.py @@ -135,8 +135,9 @@ class FileReadTool(AgentTool): ) # 安全检查:防止路径遍历 - full_path = os.path.normpath(os.path.join(self.project_root, file_path)) - if not full_path.startswith(os.path.normpath(self.project_root)): + # Security Fix: 使用 realpath 解析软链接,防止绕过项目根目录检查 + full_path = os.path.realpath(os.path.join(self.project_root, file_path)) + if not full_path.startswith(os.path.realpath(self.project_root)): return ToolResult( success=False, error="安全错误:不允许访问项目目录外的文件",