/
decompress.go
159 lines (135 loc) 路 3.04 KB
/
decompress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Package decompress implements routines for unzipping and "untarring".
package decompress
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type ErrZipInvalidFilePath struct {
FilePath string
}
func (e ErrZipInvalidFilePath) Error() string {
return fmt.Sprintf("invalid file path: %q", e.FilePath)
}
// Untar a file to a target directory.
func UntarFile(filePath, targetDir string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
return Untar(file, targetDir)
}
// Untar a reader to a target directory.
func Untar(reader io.Reader, targetDir string) error {
gzr, err := gzip.NewReader(reader)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
filePath := header.Name
if targetDir != "" {
sanitized, pathErr := sanitizeExtractPath(targetDir, filePath)
if pathErr != nil {
return pathErr
}
filePath = sanitized
}
switch header.Typeflag {
case tar.TypeDir:
if _, err := os.Stat(filePath); err != nil {
if err := os.MkdirAll(filePath, 0755); err != nil {
return err
}
}
case tar.TypeReg:
dstFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
for {
_, fileErr := io.CopyN(dstFile, tr, 1024)
if fileErr != nil {
if errors.Is(fileErr, io.EOF) {
break
}
return fileErr
}
}
dstFile.Close()
}
}
}
// Unzip a file to a target directory.
func Unzip(filePath, targetDir string) (err error) {
archive, err := zip.OpenReader(filePath)
if err != nil {
return
}
defer archive.Close()
for _, f := range archive.File {
filePath := f.Name
if targetDir != "" {
sanitized, pathErr := sanitizeExtractPath(targetDir, filePath)
if pathErr != nil {
return pathErr
}
filePath = sanitized
}
if f.FileInfo().IsDir() {
err = os.MkdirAll(filePath, os.ModePerm)
if err != nil {
return
}
continue
}
err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
if err != nil {
return
}
dstFile, fileErr := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if fileErr != nil {
return fileErr
}
fileInArchive, fileErr := f.Open()
if fileErr != nil {
return fileErr
}
for {
_, fileErr := io.CopyN(dstFile, fileInArchive, 1024)
if fileErr != nil {
if errors.Is(fileErr, io.EOF) {
break
}
return fileErr
}
}
dstFile.Close()
fileInArchive.Close()
}
return
}
// Sanitize archive file path to avoid "G305: Zip Slip vulnerability"
// See: https://snyk.io/research/zip-slip-vulnerability
func sanitizeExtractPath(targetDir, filePath string) (sanitized string, err error) {
destPath := filepath.Join(targetDir, filePath)
if !strings.HasPrefix(destPath, filepath.Clean(targetDir)+string(os.PathSeparator)) {
return "", &ErrZipInvalidFilePath{FilePath: filePath}
}
return destPath, nil
}