Skip to content

Commit c263d8e

Browse files
committed
CVE-2007-4559の対応(Thank you TrellixVulnTeam.)
1 parent 630ea11 commit c263d8e

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

PyExtendTools.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,38 +2488,51 @@ def OutSideArchiveCreate(self):
24882488
for TargetFolder, __, TargetFile in os.walk(self.filePath(ArchiveDFile)):
24892489
for TFile in TargetFile:
24902490
if not TFile.startswith('.'):
2491-
FilePaths = os.path.join(TargetFolder, TFile).replace(os.getcwd(), os.curdir)
2491+
FilePaths = os.path.join(TargetFolder, TFile).replace(os.getcwd().replace(os.sep, '/'), os.curdir)
24922492
ZF.write(FilePaths)
24932493
os.chdir(BackupNowPath[0])
24942494
if mode == 'TarArchive':
24952495
os.chdir(self.rootPath())
24962496
with tarfile.open(FileName, 'w:gz') as Tgz:
24972497
for TarAddFiles in self.selectedIndexes():
2498-
Tgz.add(self.filePath(TarAddFiles).replace(os.getcwd(), os.curdir))
2498+
Tgz.add(self.filePath(TarAddFiles).replace(os.getcwd().replace(os.sep, '/'), os.curdir))
24992499
os.chdir(BackupNowPath[0])
25002500
if mode == '7ZipArchive':
25012501
os.chdir(self.rootPath())
25022502
with py7zr.SevenZipFile(FileName, 'w') as SevenZipper:
25032503
for SevenFilesIndex in self.selectedIndexes():
2504-
SevenZipper.writeall(self.filePath(SevenFilesIndex).replace(os.getcwd(), os.curdir))
2504+
SevenZipper.writeall(self.filePath(SevenFilesIndex).replace(os.getcwd().replace(os.sep, '/'), os.curdir))
25052505
os.chdir(BackupNowPath[0])
25062506

2507+
def is_within_directory(self, directory, target):
2508+
abs_directory = os.path.abspath(directory)
2509+
abs_target = os.path.abspath(target)
2510+
prefix = os.path.commonprefix([abs_directory, abs_target])
2511+
return prefix == abs_directory
2512+
2513+
def safe_extract(self, tars, path='.', members=None, numeric_owner=False):
2514+
for member in tars.getmembers():
2515+
member_path = os.path.join(path, member.name)
2516+
if not self.is_within_directory(path, member_path):
2517+
raise Exception("Attempted Path Traversal in Tar File")
2518+
tars.extractall(path=path, members=members, numeric_owner=numeric_owner)
2519+
25072520
def OutSideUnArchive(self):
25082521
BackupNowPath[0] = os.getcwd()
25092522
os.chdir(self.rootPath())
25102523
for DetectFile in self.selectedIndexes():
25112524
if self.filePath(DetectFile).endswith('.zip'):
25122525
os.makedirs(self.filePath(DetectFile).replace(os.getcwd(), os.curdir).split('.zip')[0], exist_ok=True)
25132526
with zipfile.ZipFile(self.filePath(DetectFile), 'r') as ExtractZip:
2514-
ExtractZip.extractall(path='{}{}{}'.format(os.getcwd(), '/', self.filePath(DetectFile).split(os.getcwd())[-1].split('.zip')[0]))
2527+
ExtractZip.extractall(path='{}{}{}'.format(os.getcwd(), '/', self.filePath(DetectFile).split(os.getcwd().replace(os.sep, '/'))[-1].split('.zip')[0]))
25152528
if self.filePath(DetectFile).endswith('.tar.gz'):
25162529
os.makedirs(self.filePath(DetectFile).replace(os.getcwd(), os.curdir).split('.tar.gz')[0], exist_ok=True)
25172530
with tarfile.open(self.filePath(DetectFile), 'r') as ExtractTgz:
2518-
ExtractTgz.extractall(path='{}{}{}'.format(os.getcwd(), '/', self.filePath(DetectFile).split(os.getcwd())[-1].split('.tar.gz')[0]))
2531+
self.safe_extract(ExtractTgz, path='{}{}{}'.format(os.getcwd(), '/', self.filePath(DetectFile).split(os.getcwd().replace(os.sep, '/'))[-1].split('.tar.gz')[0]))
25192532
if self.filePath(DetectFile).endswith('.7z'):
25202533
os.makedirs(self.filePath(DetectFile).replace(os.getcwd(), os.curdir).split('.7z')[0], exist_ok=True)
25212534
with py7zr.SevenZipFile(self.filePath(DetectFile), 'r') as ExtractSevenZip:
2522-
ExtractSevenZip.extractall(path='{}{}{}'.format(os.getcwd(), '/', self.filePath(DetectFile).split(os.getcwd())[-1].split('.7z')[0]))
2535+
ExtractSevenZip.extractall(path='{}{}{}'.format(os.getcwd(), '/', self.filePath(DetectFile).split(os.getcwd().replace(os.sep, '/'))[-1].split('.7z')[0]))
25232536
os.chdir(BackupNowPath[0])
25242537

25252538
def dragEnterEvent(self, event):

0 commit comments

Comments
 (0)