1
1
package tool
2
2
3
3
import (
4
+ "fmt"
4
5
"io"
5
6
"io/fs"
6
7
"os"
7
- stdpath "path"
8
+ "path/filepath "
8
9
"strings"
9
10
10
11
"github.com/OpenListTeam/OpenList/v4/internal/model"
@@ -40,13 +41,13 @@ func GenerateMetaTreeFromFolderTraversal(r ArchiveReader) (bool, []model.ObjTree
40
41
isNewFolder := false
41
42
if ! file .FileInfo ().IsDir () {
42
43
// 先将 文件 添加到 所在的文件夹
43
- dir = stdpath .Dir (name )
44
+ dir = filepath .Dir (name )
44
45
dirObj = dirMap [dir ]
45
46
if dirObj == nil {
46
47
isNewFolder = dir != "."
47
48
dirObj = & model.ObjectTree {}
48
49
dirObj .IsFolder = true
49
- dirObj .Name = stdpath .Base (dir )
50
+ dirObj .Name = filepath .Base (dir )
50
51
dirObj .Modified = file .FileInfo ().ModTime ()
51
52
dirMap [dir ] = dirObj
52
53
}
@@ -64,28 +65,28 @@ func GenerateMetaTreeFromFolderTraversal(r ArchiveReader) (bool, []model.ObjTree
64
65
dirMap [dir ] = dirObj
65
66
}
66
67
dirObj .IsFolder = true
67
- dirObj .Name = stdpath .Base (dir )
68
+ dirObj .Name = filepath .Base (dir )
68
69
dirObj .Modified = file .FileInfo ().ModTime ()
69
70
}
70
71
if isNewFolder {
71
72
// 将 文件夹 添加到 父文件夹
72
73
// 考虑压缩包仅记录文件的路径,不记录文件夹
73
74
// 循环创建所有父文件夹
74
- parentDir := stdpath .Dir (dir )
75
+ parentDir := filepath .Dir (dir )
75
76
for {
76
77
parentDirObj := dirMap [parentDir ]
77
78
if parentDirObj == nil {
78
79
parentDirObj = & model.ObjectTree {}
79
80
if parentDir != "." {
80
81
parentDirObj .IsFolder = true
81
- parentDirObj .Name = stdpath .Base (parentDir )
82
+ parentDirObj .Name = filepath .Base (parentDir )
82
83
parentDirObj .Modified = file .FileInfo ().ModTime ()
83
84
}
84
85
dirMap [parentDir ] = parentDirObj
85
86
}
86
87
parentDirObj .Children = append (parentDirObj .Children , dirObj )
87
88
88
- parentDir = stdpath .Dir (parentDir )
89
+ parentDir = filepath .Dir (parentDir )
89
90
if dirMap [parentDir ] != nil {
90
91
break
91
92
}
@@ -127,7 +128,7 @@ func DecompressFromFolderTraversal(r ArchiveReader, outputPath string, args mode
127
128
}
128
129
} else {
129
130
innerPath := strings .TrimPrefix (args .InnerPath , "/" )
130
- innerBase := stdpath .Base (innerPath )
131
+ innerBase := filepath .Base (innerPath )
131
132
createdBaseDir := false
132
133
for _ , file := range files {
133
134
name := file .Name ()
@@ -138,7 +139,7 @@ func DecompressFromFolderTraversal(r ArchiveReader, outputPath string, args mode
138
139
}
139
140
break
140
141
} else if strings .HasPrefix (name , innerPath + "/" ) {
141
- targetPath := stdpath .Join (outputPath , innerBase )
142
+ targetPath := filepath .Join (outputPath , innerBase )
142
143
if ! createdBaseDir {
143
144
err = os .Mkdir (targetPath , 0700 )
144
145
if err != nil {
@@ -159,12 +160,16 @@ func DecompressFromFolderTraversal(r ArchiveReader, outputPath string, args mode
159
160
160
161
func decompress (file SubFile , filePath , outputPath , password string ) error {
161
162
targetPath := outputPath
162
- dir , base := stdpath .Split (filePath )
163
+ dir , base := filepath .Split (filePath )
163
164
if dir != "" {
164
- targetPath = stdpath .Join (targetPath , dir )
165
- err := os .MkdirAll (targetPath , 0700 )
166
- if err != nil {
167
- return err
165
+ targetPath = filepath .Join (targetPath , dir )
166
+ if strings .HasPrefix (targetPath , outputPath + string (os .PathSeparator )) {
167
+ err := os .MkdirAll (targetPath , 0700 )
168
+ if err != nil {
169
+ return err
170
+ }
171
+ } else {
172
+ targetPath = outputPath
168
173
}
169
174
}
170
175
if base != "" {
@@ -185,7 +190,11 @@ func _decompress(file SubFile, targetPath, password string, up model.UpdateProgr
185
190
return err
186
191
}
187
192
defer func () { _ = rc .Close () }()
188
- f , err := os .OpenFile (stdpath .Join (targetPath , file .FileInfo ().Name ()), os .O_WRONLY | os .O_CREATE | os .O_EXCL , 0600 )
193
+ destPath := filepath .Join (targetPath , file .FileInfo ().Name ())
194
+ if ! strings .HasPrefix (destPath , targetPath + string (os .PathSeparator )) {
195
+ return fmt .Errorf ("illegal file path: %s" , file .FileInfo ().Name ())
196
+ }
197
+ f , err := os .OpenFile (destPath , os .O_WRONLY | os .O_CREATE | os .O_EXCL , 0600 )
189
198
if err != nil {
190
199
return err
191
200
}
0 commit comments