Skip to content

Commit 63d943b

Browse files
LiHua000deepin-bot[bot]
authored andcommitted
fix: Fix path traversal vulnerability in zip extraction (bug #232873)
- Replace single-pass "../" removal with loop to remove all occurrences - Add final path validation to ensure extracted files stay within target directory Log: fix CITIVD Bug: https://pms.uniontech.com/bug-view-342883.html
1 parent e8724af commit 63d943b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

3rdparty/libzipplugin/libzipplugin.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,8 @@ ErrorType LibzipPlugin::extractEntry(zip_t *archive, zip_int64_t index, const Ex
761761
}
762762

763763
strFileName = m_common->trans2uft8(statBuffer.name, m_mapFileCode[index]); // 解压文件名(压缩包中)
764-
//fix 232873
765-
if(strFileName.indexOf("../") != -1) {
764+
//fix 232873 - Remove all "../" components to prevent path traversal attacks
765+
while(strFileName.contains("../")) {
766766
qInfo() << "skipped ../ path component(s) in " << strFileName;
767767
strFileName = strFileName.replace("../", "");
768768
}
@@ -860,6 +860,15 @@ ErrorType LibzipPlugin::extractEntry(zip_t *archive, zip_int64_t index, const Ex
860860
// 解压完整文件名(含路径)
861861
QString strDestFileName = options.strTargetPath + QDir::separator() + strFileName;
862862

863+
// Additional security check: ensure the final path is within the target directory
864+
QString cleanTargetPath = QDir::cleanPath(QDir(options.strTargetPath).absolutePath());
865+
QString cleanDestPath = QDir::cleanPath(QDir(strDestFileName).absolutePath());
866+
if (!cleanDestPath.startsWith(cleanTargetPath + QDir::separator()) &&
867+
cleanDestPath != cleanTargetPath) {
868+
qInfo() << "Path traversal detected! Rejected path: " << strFileName;
869+
return ET_FileWriteError;
870+
}
871+
863872
QFile file(strDestFileName);
864873

865874
// Store parent mtime.

0 commit comments

Comments
 (0)