generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
archive.go
138 lines (121 loc) · 3.07 KB
/
archive.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
package u
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"go.uber.org/multierr"
)
// UnzipBytes is similar to Unzip but takes a zip archive as bytes instead of looking for a real file.
func UnzipBytes(src []byte, dest string) ([]string, error) {
buf := bytes.NewReader(src)
r, err := zip.NewReader(buf, int64(len(src)))
if err != nil {
return nil, err
}
return unzip(r, dest)
}
// Unzip decompresses a zip archive, moving all files and folders within the zip file to an output directory.
// Based on https://golangcode.com/unzip-files-in-go/ (MIT).
func Unzip(src string, dest string) ([]string, error) {
r, err := zip.OpenReader(src)
if err != nil {
return nil, err
}
defer r.Close()
return unzip(&r.Reader, dest)
}
func findFirstExistingDir(path string) string {
for path != "" && path != "/" {
_, err := os.Stat(path)
if err == nil {
return path
}
path = filepath.Dir(path)
}
return ""
}
func unzip(r *zip.Reader, dest string) ([]string, error) {
var (
filenames = make([]string, 0)
errs error
)
// eval symlink on dest dir to compare for zip slip
var destLink string
{
if firstExisting := findFirstExistingDir(dest); firstExisting != "" && firstExisting != "/" {
link, err := filepath.EvalSymlinks(firstExisting)
if err != nil {
return nil, err
}
destLink = link
}
}
for _, f := range r.File {
fpath := filepath.Join(dest, f.Name)
// check for ZipSlip. more Info: https://snyk.io/research/zip-slip-vulnerability#go
{
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
errs = multierr.Append(errs, fmt.Errorf("%s: illegal file path", fpath))
continue
}
if firstExisting := findFirstExistingDir(fpath); firstExisting != "" && firstExisting != "/" {
link, err := filepath.EvalSymlinks(firstExisting)
if err != nil {
errs = multierr.Append(errs, err)
continue
}
if !strings.HasPrefix(link, filepath.Clean(destLink)) {
errs = multierr.Append(errs, fmt.Errorf("%s: illegal file path", fpath))
continue
}
}
}
if f.FileInfo().IsDir() {
// dir
if err := os.MkdirAll(fpath, os.ModePerm); err != nil {
errs = multierr.Append(errs, err)
continue
}
} else {
// file
if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
errs = multierr.Append(errs, err)
continue
}
rc, err := f.Open()
if err != nil {
errs = multierr.Append(errs, err)
continue
}
if f.Mode()&os.ModeSymlink != 0 {
buff, _ := ioutil.ReadAll(rc)
rc.Close()
err = os.Symlink(string(buff), fpath)
if err != nil {
errs = multierr.Append(errs, err)
continue
}
} else {
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
errs = multierr.Append(errs, err)
continue
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
errs = multierr.Append(errs, err)
continue
}
}
}
filenames = append(filenames, fpath)
}
return filenames, errs
}