Skip to content

Commit cb20c2f

Browse files
authored
Refactor _unpack_src to use tarfile data filter
This commit refactors the _unpack_src function to use the filter='data' argument in tarfile.extractall(). This change addresses the path traversal vulnerability more robustly, as suggested during code review. It also improves code clarity and removes the need for manual path validation checks.
1 parent d264c0d commit cb20c2f

File tree

1 file changed

+5
-30
lines changed

1 file changed

+5
-30
lines changed

Tools/ssl/multissltests.py

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -293,45 +293,20 @@ def _unpack_src(self):
293293
if os.path.isdir(self.build_dir):
294294
shutil.rmtree(self.build_dir)
295295
os.makedirs(self.build_dir)
296+
296297
tf = tarfile.open(self.src_file)
297298
name = self.build_template.format(self.version)
298299
base = name + '/'
299300
# force extraction into build dir
300301
members = tf.getmembers()
301-
302-
log.info("Unpacking files to {}".format(self.build_dir))
303-
build_dir_real = os.path.realpath(self.build_dir)
304-
305-
for member in members:
302+
for member in list(members):
306303
if member.name == name:
307-
continue
304+
members.remove(member)
308305
elif not member.name.startswith(base):
309306
raise ValueError(member.name, base)
310-
311-
# Strip the base directory and normalize the path
312307
member.name = member.name[len(base):].lstrip('/')
313-
314-
# Security checks for safe extraction
315-
if member.islnk() or member.issym():
316-
log.warning("Skipping symbolic/hard link: {}".format(member.name))
317-
continue
318-
319-
# Normalize the member path
320-
normalized_path = os.path.normpath(member.name)
321-
322-
# Check for directory traversal attempts
323-
if normalized_path.startswith('/') or '..' in normalized_path.split(os.sep):
324-
log.warning("Skipping potentially malicious member: {}".format(member.name))
325-
continue
326-
327-
# Construct final path and validate it's within build directory
328-
final_path = os.path.realpath(os.path.join(self.build_dir, normalized_path))
329-
if not final_path.startswith(build_dir_real + os.sep) and final_path != build_dir_real:
330-
log.warning("Skipping member attempting directory traversal: {}".format(member.name))
331-
continue
332-
333-
# Safe extraction
334-
tf.extract(member, self.build_dir)
308+
log.info("Unpacking files to {}".format(self.build_dir))
309+
tf.extractall(self.build_dir, members, filter='data')
335310

336311
def _build_src(self, config_args=()):
337312
"""Now build openssl"""

0 commit comments

Comments
 (0)