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 Spam-Email-Classifier/Lambda/sagemaker/local/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,26 @@ def _prepare_serving_volumes(self, model_location):
for filename in model_data_source.get_file_list():
if tarfile.is_tarfile(filename):
with tarfile.open(filename) as tar:
tar.extractall(path=model_data_source.get_root_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=model_data_source.get_root_dir())

volumes.append(_Volume(model_data_source.get_root_dir(), "/opt/ml/model"))

Expand Down
42 changes: 40 additions & 2 deletions Spam-Email-Classifier/Lambda/sagemaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,26 @@ def _create_or_update_code_dir(
download_file_from_url(source_directory, local_code_path, sagemaker_session)

with tarfile.open(name=local_code_path, mode="r:gz") as t:
t.extractall(path=code_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(t, path=code_dir)

elif source_directory:
if os.path.exists(code_dir):
Expand Down Expand Up @@ -579,7 +598,26 @@ def _extract_model(model_uri, sagemaker_session, tmp):
else:
local_model_path = model_uri.replace("file://", "")
with tarfile.open(name=local_model_path, mode="r:gz") as t:
t.extractall(path=tmp_model_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(t, path=tmp_model_dir)
return tmp_model_dir


Expand Down