Skip to content

Commit 5dffe86

Browse files
committed
1 parent 2aefef2 commit 5dffe86

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

shared_utils/handle_upload.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ def zip_extract_member_new(self, member, targetpath, pwd):
8888
return targetpath
8989

9090

91+
92+
def safe_extract_rar(file_path, dest_dir):
93+
import rarfile
94+
import posixpath
95+
with rarfile.RarFile(file_path) as rf:
96+
os.makedirs(dest_dir, exist_ok=True)
97+
base_path = os.path.abspath(dest_dir)
98+
for file_info in rf.infolist():
99+
orig_filename = file_info.filename
100+
filename = posixpath.normpath(orig_filename).lstrip('/')
101+
# 路径遍历防护
102+
if '..' in filename or filename.startswith('../'):
103+
raise Exception(f"Attempted Path Traversal in {orig_filename}")
104+
# 符号链接防护
105+
if hasattr(file_info, 'is_symlink') and file_info.is_symlink():
106+
raise Exception(f"Attempted Symlink in {orig_filename}")
107+
# 构造完整目标路径
108+
target_path = os.path.join(base_path, filename)
109+
final_path = os.path.normpath(target_path)
110+
# 最终路径校验
111+
if not final_path.startswith(base_path):
112+
raise Exception(f"Attempted Path Traversal in {orig_filename}")
113+
rf.extractall(dest_dir)
114+
115+
116+
91117
def extract_archive(file_path, dest_dir):
92118
import zipfile
93119
import tarfile
@@ -132,14 +158,11 @@ def extract_archive(file_path, dest_dir):
132158
# 此外,Windows上还需要安装winrar软件,配置其Path环境变量,如"C:\Program Files\WinRAR"才可以
133159
elif file_extension == ".rar":
134160
try:
135-
import rarfile
136-
137-
with rarfile.RarFile(file_path) as rf:
138-
rf.extractall(path=dest_dir)
139-
logger.info("Successfully extracted rar archive to {}".format(dest_dir))
161+
import rarfile # 用来检查rarfile是否安装,不要删除
162+
safe_extract_rar(file_path, dest_dir)
140163
except:
141164
logger.info("Rar format requires additional dependencies to install")
142-
return "\n\n解压失败! 需要安装pip install rarfile来解压rar文件。建议:使用zip压缩格式。"
165+
return "<br/><br/>解压失败! 需要安装pip install rarfile来解压rar文件。建议:使用zip压缩格式。"
143166

144167
# 第三方库,需要预先pip install py7zr
145168
elif file_extension == ".7z":
@@ -151,7 +174,7 @@ def extract_archive(file_path, dest_dir):
151174
logger.info("Successfully extracted 7z archive to {}".format(dest_dir))
152175
except:
153176
logger.info("7z format requires additional dependencies to install")
154-
return "\n\n解压失败! 需要安装pip install py7zr来解压7z文件"
177+
return "<br/><br/>解压失败! 需要安装pip install py7zr来解压7z文件"
155178
else:
156179
return ""
157180
return ""

0 commit comments

Comments
 (0)