Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding tarfile member sanitization to extractall()
  • Loading branch information
TrellixVulnTeam committed Oct 6, 2022
1 parent fa9e813 commit 81435a1
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion examples/rnn_classifer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,24 @@ def download():
if not os.path.exists(DATA_FN):
urlretrieve(DATA_URL, DATA_FN)
with tarfile.open(DATA_FN, 'r:gz') as f:
f.extractall()
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):
# See:
# https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extractall
# on why plain tar.extractall can be dangerous
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise OSError("Attempted Path Traversal in Tar File")

tar.extractall(path, members, numeric_owner=numeric_owner)

safe_extract(f)


class DatasetLoader(IDatasetLoader):
Expand Down

0 comments on commit 81435a1

Please sign in to comment.