Unverified Commit 5c6af804 authored by TrellixVulnTeam's avatar TrellixVulnTeam Committed by GitHub
Browse files

CVE-2007-4559 Patch (#5122)



* Adding tarfile member sanitization to extractall()

* Update utils.py
Co-authored-by: default avatarHongzhi (Steve), Chen <chenhongzhi.nkcs@gmail.com>
parent f1fb859f
...@@ -257,7 +257,20 @@ def extract_archive(file, target_dir, overwrite=False): ...@@ -257,7 +257,20 @@ def extract_archive(file, target_dir, overwrite=False):
import tarfile import tarfile
with tarfile.open(file, "r") as archive: with tarfile.open(file, "r") as archive:
archive.extractall(path=target_dir) def is_within_directory(directory, target):
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)
prefix = os.path.commonprefix([abs_directory, abs_target])
return prefix == abs_directory
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")
tar.extractall(path, members, numeric_owner)
safe_extract(archive, path=target_dir)
elif file.endswith(".gz"): elif file.endswith(".gz"):
import gzip import gzip
import shutil import shutil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment