mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 06:43:42 +00:00
fix: sanitize archive entry paths before extraction to prevent path traversal (#1526)
This commit is contained in:
3 files changed
+20
-14
No files matched your search
@@ -282,19 +282,15 @@ func countArchiveFiles(archivePath string, password string) (int, error) {
|
||||
|
||||
// extractFile handles extracting a single file from an archive
|
||||
func extractFile(ctx context.Context, fileInfo archives.FileInfo, destDir string) error {
|
||||
// Skip directories, they will be created when extracting files
|
||||
if fileInfo.IsDir() {
|
||||
destPath := filepath.Join(destDir, fileInfo.NameInArchive)
|
||||
return os.MkdirAll(destPath, fileInfo.Mode())
|
||||
}
|
||||
|
||||
// Sanitize the path to prevent path traversal attacks
|
||||
cleanPath := filepath.Clean(fileInfo.NameInArchive)
|
||||
if strings.HasPrefix(cleanPath, "..") || filepath.IsAbs(cleanPath) {
|
||||
// Skip files with suspicious paths
|
||||
return nil
|
||||
}
|
||||
|
||||
if fileInfo.IsDir() {
|
||||
return os.MkdirAll(filepath.Join(destDir, cleanPath), fileInfo.Mode())
|
||||
}
|
||||
|
||||
destPath := filepath.Join(destDir, cleanPath)
|
||||
|
||||
// Create parent directories
|
||||
|
||||
Reference in new issue
Block a user