Skip to content

Commit 8bc77b6

Browse files
Patch Zip Slip leading to RCE in Backup
1 parent 379ebb6 commit 8bc77b6

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

modules/Backup/Controllers/Backup.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,35 @@ public function restore()
9595
$file->move($uploadPath, $newName);
9696
$filePath = WRITEPATH . 'uploads/' . $newName;
9797
$sqlPath = $filePath;
98-
if ($ext === 'zip') {
98+
if ($ext === 'zip') {
9999
$zip = new \ZipArchive();
100100
if ($zip->open($filePath) === true) {
101-
$zip->extractTo($uploadPath);
102-
$sqlPath = $uploadPath . $zip->getNameIndex(0);
101+
$extractedSql = '';
102+
for ($i = 0; $i < $zip->numFiles; $i++) {
103+
$entryName = $zip->getNameIndex($i);
104+
$safeEntryName = basename($entryName);
105+
106+
if (preg_match('/\.sql$/i', $safeEntryName)) {
107+
$fileContent = $zip->getFromIndex($i);
108+
$extractedSql = $uploadPath . $safeEntryName;
109+
file_put_contents($extractedSql, $fileContent);
110+
break;
111+
}
112+
}
103113
$zip->close();
104114
@unlink($filePath);
115+
116+
if (empty($extractedSql)) {
117+
return redirect()->route('backup')->with('error', lang('Backup.dbNotRestore'));
118+
}
119+
$sqlPath = $extractedSql;
120+
} else {
121+
@unlink($filePath);
122+
return redirect()->route('backup')->with('error', lang('Backup.dbNotRestore'));
105123
}
106124
}
125+
}
126+
}
107127

108128
$dbBackup = new \Modules\Backup\Libraries\DbBackup();
109129
if ($dbBackup->restore($sqlPath)) {

0 commit comments

Comments
 (0)