Skip to content

Commit ae73eb0

Browse files
committed
Fix vulnerability CVE-2007-4559
Fixes the vulnerability identified by a tool of the Trellix security research center.
1 parent 5eb6865 commit ae73eb0

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

netpyne_ui/api.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from notebook.base.handlers import IPythonHandler
1111
from netpyne_ui.constants import ALLOWED_EXTENSIONS, UPLOAD_FOLDER_PATH
1212

13+
1314
def allowed_file(filename, allowed_extensions=ALLOWED_EXTENSIONS):
1415
return '.' in filename and \
1516
filename.rsplit('.', 1)[1].lower() in allowed_extensions
@@ -43,6 +44,23 @@ def get_file_paths(handler):
4344
return file_paths
4445

4546

47+
def is_within_directory(directory, target):
48+
abs_directory = os.path.abspath(directory)
49+
abs_target = os.path.abspath(target)
50+
51+
prefix = os.path.commonprefix([abs_directory, abs_target])
52+
53+
return prefix == abs_directory
54+
55+
56+
def safe_extract_tar(tar, path=".", members=None, *, numeric_owner=False):
57+
for member in tar.getmembers():
58+
member_path = os.path.join(path, member.name)
59+
if not is_within_directory(path, member_path):
60+
raise Exception("Attempted Path Traversal in Tar File")
61+
tar.extractall(path, members, numeric_owner=numeric_owner)
62+
63+
4664
class NetPyNEController: # pytest: no cover
4765

4866
@post('/uploads')
@@ -74,7 +92,7 @@ def uploads(handler: IPythonHandler):
7492

7593
elif filename.endswith('.tar.gz'):
7694
with tarfile.open(file_path, mode='r:gz') as tar:
77-
tar.extractall(UPLOAD_FOLDER_PATH)
95+
safe_extract_tar(tar, UPLOAD_FOLDER_PATH)
7896

7997
elif filename.endswith('.gz'):
8098
with gzip.open(file_path, "rb") as gz, open(file_path.replace('.gz', ''), 'wb') as ff:

0 commit comments

Comments
 (0)