Skip to content

Commit 1072bb9

Browse files
Adding tarfile member sanitization to extractall()
1 parent ebf0253 commit 1072bb9

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

sagemaker_pipelines/paddleocr/evaluate.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,29 @@
2121
logger.debug("Starting evaluation.")
2222
model_path = "/opt/ml/processing/model/model.tar.gz"
2323
with tarfile.open(model_path) as tar:
24-
tar.extractall(path=".")
24+
25+
import os
26+
27+
def is_within_directory(directory, target):
28+
29+
abs_directory = os.path.abspath(directory)
30+
abs_target = os.path.abspath(target)
31+
32+
prefix = os.path.commonprefix([abs_directory, abs_target])
33+
34+
return prefix == abs_directory
35+
36+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
37+
38+
for member in tar.getmembers():
39+
member_path = os.path.join(path, member.name)
40+
if not is_within_directory(path, member_path):
41+
raise Exception("Attempted Path Traversal in Tar File")
42+
43+
tar.extractall(path, members, numeric_owner=numeric_owner)
44+
45+
46+
safe_extract(tar, path=".")
2547

2648
logger.debug("Loading xgboost model.")
2749
model = pickle.load(open("xgboost-model", "rb"))

0 commit comments

Comments
 (0)