forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar_archive_test.go
383 lines (289 loc) · 12.9 KB
/
tar_archive_test.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package vacation_test
import (
"archive/tar"
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2/vacation"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testTarArchive(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
)
context("Decompress", func() {
var (
tempDir string
tarArchive vacation.TarArchive
)
it.Before(func() {
var err error
tempDir, err = os.MkdirTemp("", "vacation")
Expect(err).NotTo(HaveOccurred())
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
// Some archive files will make a relative top level path directory these
// should still successfully decompress.
Expect(tw.WriteHeader(&tar.Header{Name: "./", Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: "./some-dir", Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: filepath.Join("some-dir", "some-other-dir"), Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: "symlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: "first"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: "hardlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeLink, Linkname: "first"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
nestedFile := filepath.Join("some-dir", "some-other-dir", "some-file")
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
for _, file := range []string{"first", "second", "third"} {
Expect(tw.WriteHeader(&tar.Header{Name: file, Mode: 0755, Size: int64(len(file))})).To(Succeed())
_, err = tw.Write([]byte(file))
Expect(err).NotTo(HaveOccurred())
}
Expect(tw.Close()).To(Succeed())
tarArchive = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it.After(func() {
Expect(os.RemoveAll(tempDir)).To(Succeed())
})
it("unpackages the archive into the path", func() {
var err error
err = tarArchive.Decompress(tempDir)
Expect(err).ToNot(HaveOccurred())
files, err := filepath.Glob(fmt.Sprintf("%s/*", tempDir))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(tempDir, "first"),
filepath.Join(tempDir, "hardlink"),
filepath.Join(tempDir, "second"),
filepath.Join(tempDir, "some-dir"),
filepath.Join(tempDir, "symlink"),
filepath.Join(tempDir, "third"),
}))
info, err := os.Stat(filepath.Join(tempDir, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir")).To(BeADirectory())
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir", "some-file")).To(BeARegularFile())
data, err := os.ReadFile(filepath.Join(tempDir, "symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(data).To(Equal([]byte(`first`)))
data, err = os.ReadFile(filepath.Join(tempDir, "hardlink"))
Expect(err).NotTo(HaveOccurred())
Expect(data).To(Equal([]byte(`first`)))
})
it("unpackages the archive into the path but also strips the first component", func() {
var err error
err = tarArchive.StripComponents(1).Decompress(tempDir)
Expect(err).ToNot(HaveOccurred())
files, err := filepath.Glob(fmt.Sprintf("%s/*", tempDir))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(tempDir, "some-other-dir"),
}))
Expect(filepath.Join(tempDir, "some-other-dir")).To(BeADirectory())
Expect(filepath.Join(tempDir, "some-other-dir", "some-file")).To(BeARegularFile())
})
context("there is no directory metadata", func() {
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
nestedFile := filepath.Join("some-dir", "some-other-dir", "some-file")
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: filepath.Join("sym-dir", "symlink"), Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: filepath.Join("..", nestedFile)})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
tarArchive = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("unpackages the archive into the path", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).ToNot(HaveOccurred())
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir")).To(BeADirectory())
Expect(filepath.Join(tempDir, "some-dir", "some-other-dir", "some-file")).To(BeARegularFile())
data, err := os.ReadFile(filepath.Join(tempDir, "sym-dir", "symlink"))
Expect(err).NotTo(HaveOccurred())
Expect(data).To(Equal([]byte(filepath.Join("some-dir", "some-other-dir", "some-file"))))
})
})
context("failure cases", func() {
context("when a file is not inside of the destination director (Zip Slip)", func() {
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
nestedFile := filepath.Join("..", "some-dir", "some-other-dir", "some-file")
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
tarArchive = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("illegal file path %q: the file path does not occur within the destination directory", filepath.Join("..", "some-dir", "some-other-dir", "some-file")))))
})
})
context("when it fails to read the tar response", func() {
it("returns an error", func() {
readyArchive := vacation.NewTarArchive(bytes.NewBuffer([]byte(`something`)))
err := readyArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to read tar response")))
})
})
context("when it is unable to create an archived directory", func() {
it.Before(func() {
Expect(os.Chmod(tempDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(tempDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to create archived directory")))
})
context("when there are no directory headers", func() {
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
nestedFile := filepath.Join("some-dir", "some-other-dir", "some-file")
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
tarArchive = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to create archived directory from file path")))
})
})
})
context("when it is unable to create an archived file", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(tempDir, "some-dir", "some-other-dir"), os.ModePerm)).To(Succeed())
Expect(os.Chmod(filepath.Join(tempDir, "some-dir", "some-other-dir"), 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(filepath.Join(tempDir, "some-dir", "some-other-dir"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := tarArchive.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to create archived file")))
})
})
context("when there is a symlink cycle", func() {
var cyclicalSymlinkTar vacation.TarArchive
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
Expect(tw.WriteHeader(&tar.Header{Name: "a-symlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: "b-symlink"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: "b-symlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: "a-symlink"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
cyclicalSymlinkTar = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := cyclicalSymlinkTar.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed: max iterations reached: this link graph contains a cycle")))
})
})
context("when it tries to symlink to a file that does not exist", func() {
var symlinkNotExistTar vacation.TarArchive
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
Expect(tw.WriteHeader(&tar.Header{Name: "symlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: "some-file"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
symlinkNotExistTar = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := symlinkNotExistTar.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to evaluate symlink")))
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
context("when the symlink creation fails", func() {
var brokenSymlinkTar vacation.TarArchive
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
Expect(tw.WriteHeader(&tar.Header{Name: "symlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: "some-file"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
// Create a symlink in the target to force the new symlink create to
// fail
Expect(os.WriteFile(filepath.Join(tempDir, "some-file"), nil, 0644)).To(Succeed())
Expect(os.Symlink("some-file", filepath.Join(tempDir, "symlink"))).To(Succeed())
brokenSymlinkTar = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := brokenSymlinkTar.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to extract symlink")))
})
})
})
context("when there is a link cycle", func() {
var cyclicalLinkTar vacation.TarArchive
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
Expect(tw.WriteHeader(&tar.Header{Name: "a-link", Mode: 0755, Size: int64(0), Typeflag: tar.TypeLink, Linkname: "b-link"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.WriteHeader(&tar.Header{Name: "b-link", Mode: 0755, Size: int64(0), Typeflag: tar.TypeLink, Linkname: "a-link"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
cyclicalLinkTar = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := cyclicalLinkTar.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed: max iterations reached: this link graph contains a cycle")))
})
})
context("when it tries to symlink to a file that does not exist", func() {
var linkNotExistTar vacation.TarArchive
it.Before(func() {
var err error
buffer := bytes.NewBuffer(nil)
tw := tar.NewWriter(buffer)
Expect(tw.WriteHeader(&tar.Header{Name: "link", Mode: 0755, Size: int64(0), Typeflag: tar.TypeLink, Linkname: "some-file"})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
linkNotExistTar = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
})
it("returns an error", func() {
err := linkNotExistTar.Decompress(tempDir)
Expect(err).To(MatchError(ContainSubstring("failed to extract link")))
Expect(err).To(MatchError(ContainSubstring("no such file or directory")))
})
})
})
}