-
Notifications
You must be signed in to change notification settings - Fork 17
/
tar.go
278 lines (239 loc) · 7.91 KB
/
tar.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tar
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/sirupsen/logrus"
)
// Compress the provided `tarContentsPath` into the `tarFilePath` while
// excluding the `exclude` regular expression patterns. This function will
// preserve path between `tarFilePath` and `tarContentsPath` directories inside
// the archive (see `CompressWithoutPreservingPath` as an alternative).
func Compress(tarFilePath, tarContentsPath string, excludes ...*regexp.Regexp) error {
return compress(true, tarFilePath, tarContentsPath, excludes...)
}
// Compress the provided `tarContentsPath` into the `tarFilePath` while
// excluding the `exclude` regular expression patterns. This function will
// not preserve path leading to the `tarContentsPath` directory in the archive.
func CompressWithoutPreservingPath(tarFilePath, tarContentsPath string, excludes ...*regexp.Regexp) error {
return compress(false, tarFilePath, tarContentsPath, excludes...)
}
func compress(preserveRootDirStructure bool, tarFilePath, tarContentsPath string, excludes ...*regexp.Regexp) error {
tarFile, err := os.Create(tarFilePath)
if err != nil {
return fmt.Errorf("create tar file %q: %w", tarFilePath, err)
}
defer tarFile.Close()
gzipWriter := gzip.NewWriter(tarFile)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
if err := filepath.Walk(tarContentsPath, func(filePath string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
var link string
isLink := fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink
if isLink {
link, err = os.Readlink(filePath)
if err != nil {
return fmt.Errorf("read file link of %s: %w", filePath, err)
}
}
header, err := tar.FileInfoHeader(fileInfo, link)
if err != nil {
return fmt.Errorf("create file info header for %q: %w", filePath, err)
}
if fileInfo.IsDir() || filePath == tarFilePath {
logrus.Tracef("Skipping: %s", filePath)
return nil
}
for _, re := range excludes {
if re != nil && re.MatchString(filePath) {
logrus.Tracef("Excluding: %s", filePath)
return nil
}
}
// Make the path inside the tar relative to the archive path if
// necessary.
//
// The default way this works is that we preserve the path between
// `tarFilePath` and `tarContentsPath` directories inside the archive.
// This might not work well if `tarFilePath` and `tarContentsPath`
// are on different levels in the file system (e.g. they don't have
// common parent directory).
// In such case we can disable `preserveRootDirStructure` flag which
// will make paths inside the archive relative to `tarContentsPath`.
dropPath := filepath.Dir(tarFilePath)
if !preserveRootDirStructure {
dropPath = tarContentsPath
}
header.Name = strings.TrimLeft(
strings.TrimPrefix(filePath, dropPath),
string(filepath.Separator),
)
header.Linkname = filepath.ToSlash(header.Linkname)
if err := tarWriter.WriteHeader(header); err != nil {
return fmt.Errorf("writing tar header: %w", err)
}
if !isLink {
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("open file %q: %w", filePath, err)
}
if _, err := io.Copy(tarWriter, file); err != nil {
return fmt.Errorf("writing file to tar writer: %w", err)
}
file.Close()
}
return nil
}); err != nil {
return fmt.Errorf("walking tree in %q: %w", tarContentsPath, err)
}
return nil
}
// Extract can be used to extract the provided `tarFilePath` into the
// `destinationPath`.
func Extract(tarFilePath, destinationPath string) error {
return iterateTarball(
tarFilePath,
func(reader *tar.Reader, header *tar.Header) (stop bool, err error) {
switch header.Typeflag {
case tar.TypeDir:
targetDir, err := SanitizeArchivePath(destinationPath, header.Name)
if err != nil {
return false, fmt.Errorf("SanitizeArchivePath: %w", err)
}
logrus.Tracef("Creating directory %s", targetDir)
if err := os.MkdirAll(targetDir, os.FileMode(0o755)); err != nil {
return false, fmt.Errorf("create target directory: %w", err)
}
case tar.TypeSymlink:
targetFile, err := SanitizeArchivePath(destinationPath, header.Name)
if err != nil {
return false, fmt.Errorf("SanitizeArchivePath: %w", err)
}
logrus.Tracef(
"Creating symlink %s -> %s", header.Linkname, targetFile,
)
if err := os.MkdirAll(
filepath.Dir(targetFile), os.FileMode(0o755),
); err != nil {
return false, fmt.Errorf("create target directory: %w", err)
}
if err := os.Symlink(header.Linkname, targetFile); err != nil {
return false, fmt.Errorf("create symlink: %w", err)
}
// tar.TypeRegA has been deprecated since Go 1.11
// should we just remove?
case tar.TypeReg:
targetFile, err := SanitizeArchivePath(destinationPath, header.Name)
if err != nil {
return false, fmt.Errorf("SanitizeArchivePath: %w", err)
}
logrus.Tracef("Creating file %s", targetFile)
if err := os.MkdirAll(
filepath.Dir(targetFile), os.FileMode(0o755),
); err != nil {
return false, fmt.Errorf("create target directory: %w", err)
}
outFile, err := os.Create(targetFile)
if err != nil {
return false, fmt.Errorf("create target file: %w", err)
}
//nolint:gosec // integer overflow highly unlikely
if err := outFile.Chmod(os.FileMode(header.Mode)); err != nil {
return false, fmt.Errorf("chmod target file: %w", err)
}
if _, err := io.Copy(outFile, reader); err != nil {
return false, fmt.Errorf("copy file contents %s: %w", targetFile, err)
}
outFile.Close()
default:
logrus.Warnf(
"File %s has unknown type %s",
header.Name, string(header.Typeflag),
)
}
return false, nil
},
)
}
// Sanitize archive file pathing from "G305: Zip Slip vulnerability"
// https://security.snyk.io/research/zip-slip-vulnerability
func SanitizeArchivePath(d, t string) (v string, err error) {
v = filepath.Join(d, t)
if strings.HasPrefix(v, filepath.Clean(d)) {
return v, nil
}
return "", fmt.Errorf("%s: %s", "content filepath is tainted", t)
}
// ReadFileFromGzippedTar opens a tarball and reads contents of a file inside.
func ReadFileFromGzippedTar(
tarPath, filePath string,
) (res io.Reader, err error) {
if err := iterateTarball(
tarPath,
func(reader *tar.Reader, header *tar.Header) (stop bool, err error) {
if header.Name == filePath {
res = reader
return true, nil
}
return false, nil
},
); err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("unable to find file %q in tarball %q: %w", tarPath, filePath, err)
}
return res, nil
}
// iterateTarball can be used to iterate over the contents of a tarball by
// calling the callback for each entry.
func iterateTarball(
tarPath string,
callback func(*tar.Reader, *tar.Header) (stop bool, err error),
) error {
file, err := os.Open(tarPath)
if err != nil {
return fmt.Errorf("opening tar file %q: %w", tarPath, err)
}
gzipReader, err := gzip.NewReader(file)
if err != nil {
return fmt.Errorf("creating gzip reader for file %q: %w", tarPath, err)
}
tarReader := tar.NewReader(gzipReader)
for {
tarHeader, err := tarReader.Next()
if err == io.EOF {
break // End of archive
}
stop, err := callback(tarReader, tarHeader)
if err != nil {
return err
}
if stop {
// User wants to stop
break
}
}
return nil
}