mirror of
https://github.com/clearml/clearml
synced 2025-02-12 15:45:25 +00:00
Adding tarfile member sanitization to extractall() (#803)
This commit is contained in:
parent
b598ee5866
commit
d17903d4e9
@ -165,10 +165,48 @@ class StorageManager(object):
|
|||||||
ZipFile(cached_file.as_posix()).extractall(path=temp_target_folder.as_posix())
|
ZipFile(cached_file.as_posix()).extractall(path=temp_target_folder.as_posix())
|
||||||
elif suffix == ".tar.gz":
|
elif suffix == ".tar.gz":
|
||||||
with tarfile.open(cached_file.as_posix()) as file:
|
with tarfile.open(cached_file.as_posix()) as file:
|
||||||
file.extractall(temp_target_folder.as_posix())
|
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=numeric_owner)
|
||||||
|
|
||||||
|
|
||||||
|
safe_extract(file, temp_target_folder.as_posix())
|
||||||
elif suffix == ".tgz":
|
elif suffix == ".tgz":
|
||||||
with tarfile.open(cached_file.as_posix(), mode='r:gz') as file:
|
with tarfile.open(cached_file.as_posix(), mode='r:gz') as file:
|
||||||
file.extractall(temp_target_folder.as_posix())
|
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=numeric_owner)
|
||||||
|
|
||||||
|
|
||||||
|
safe_extract(file, temp_target_folder.as_posix())
|
||||||
|
|
||||||
if temp_target_folder != target_folder:
|
if temp_target_folder != target_folder:
|
||||||
# we assume we will have such folder if we already extract the file
|
# we assume we will have such folder if we already extract the file
|
||||||
|
Loading…
Reference in New Issue
Block a user