Skip to content

Commit 2643052

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 c6c6017 commit 2643052

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

3rdparty/libzipplugin/libzipplugin.cpp

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

789789
strFileName = m_common->trans2uft8(statBuffer.name, m_mapFileCode[index]); // 解压文件名(压缩包中)
790-
//fix 232873
791-
if(strFileName.indexOf("../") != -1) {
790+
//fix 232873 - Remove all "../" components to prevent path traversal attacks
791+
while(strFileName.contains("../")) {
792792
qInfo() << "skipped ../ path component(s) in " << strFileName;
793793
strFileName = strFileName.replace("../", "");
794794
}
@@ -886,6 +886,15 @@ ErrorType LibzipPlugin::extractEntry(zip_t *archive, zip_int64_t index, const Ex
886886
// 解压完整文件名(含路径)
887887
QString strDestFileName = options.strTargetPath + QDir::separator() + strFileName;
888888

889+
// Additional security check: ensure the final path is within the target directory
890+
QString cleanTargetPath = QDir::cleanPath(QDir(options.strTargetPath).absolutePath());
891+
QString cleanDestPath = QDir::cleanPath(QDir(strDestFileName).absolutePath());
892+
if (!cleanDestPath.startsWith(cleanTargetPath + QDir::separator()) &&
893+
cleanDestPath != cleanTargetPath) {
894+
qInfo() << "Path traversal detected! Rejected path: " << strFileName;
895+
return ET_FileWriteError;
896+
}
897+
889898
QFile file(strDestFileName);
890899

891900
// Store parent mtime.

0 commit comments

Comments
 (0)