Skip to content

Commit

Permalink
Merge pull request from GHSA-rh7j-jfvq-857j
Browse files Browse the repository at this point in the history
Prevent path traversal for improved security
  • Loading branch information
Qhaoduoyu committed Apr 14, 2024
1 parent ba0a8b7 commit f77ab27
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion shared_utils/handle_upload.py
Expand Up @@ -104,7 +104,15 @@ def extract_archive(file_path, dest_dir):

elif file_extension in [".tar", ".gz", ".bz2"]:
with tarfile.open(file_path, "r:*") as tarobj:
tarobj.extractall(path=dest_dir)
for member in tarobj.getmembers():
# 清理提取路径,移除任何不安全的元素
member_path = os.path.normpath(member.name)
full_path = os.path.join(dest_dir, member_path)
full_path = os.path.abspath(full_path)
if not full_path.startswith(os.path.abspath(dest_dir) + os.sep):
raise Exception(f"Attempted Path Traversal in {member.name}")

tarobj.extract(member, path=dest_dir)
print("Successfully extracted tar archive to {}".format(dest_dir))

# 第三方库,需要预先pip install rarfile
Expand Down

0 comments on commit f77ab27

Please sign in to comment.