Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion examples/cnn/load_cifar10.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,26 @@ def download_cifar10_if_not_found():

print("Extracting CIFAR 10 dataset...")
with tarfile.open(CIFAR10_TAR_PATH) as tar:
tar.extractall(path=FILES_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=numeric_owner)


safe_extract(tar, path=FILES_DIR)

print("Extracting finished\n")

Expand Down
21 changes: 20 additions & 1 deletion examples/mlp/imdb_review_classification/loaddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,26 @@ def remove_tags_from_text(text):
logger.info("Extracting files from the archive")

with tarfile.open(path_to_archive) as tar_archive_file:
tar_archive_file.extractall(path=DATA_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=numeric_owner)


safe_extract(tar_archive_file, path=DATA_DIR)

shutil.move(EXTRACTED_DIRECTORY, REVIEW_DATA_PATH)
else:
Expand Down