Skip to content

Commit 0fa6a71

Browse files
authored
Merge pull request #176 from TrellixVulnTeam/master
CVE-2007-4559 Patch
2 parents 7aabc4b + 8bf75ec commit 0fa6a71

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

kb_python/ref.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,26 @@ def download_reference(
341341

342342
logger.info('Extracting files from {}'.format(local_path))
343343
with tarfile.open(local_path, 'r:gz') as f:
344-
f.extractall(temp_dir)
344+
def is_within_directory(directory, target):
345+
346+
abs_directory = os.path.abspath(directory)
347+
abs_target = os.path.abspath(target)
348+
349+
prefix = os.path.commonprefix([abs_directory, abs_target])
350+
351+
return prefix == abs_directory
352+
353+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
354+
355+
for member in tar.getmembers():
356+
member_path = os.path.join(path, member.name)
357+
if not is_within_directory(path, member_path):
358+
raise Exception("Attempted Path Traversal in Tar File")
359+
360+
tar.extractall(path, members, numeric_owner=numeric_owner)
361+
362+
363+
safe_extract(f, temp_dir)
345364

346365
for option in reference.files:
347366
os.rename(

0 commit comments

Comments
 (0)