Skip to content

Commit 39d43ee

Browse files
ForestEckhardtryanmoran
authored andcommitted
Adds ability for symlinks to link to absolute paths
Removes dead code
1 parent e9bb394 commit 39d43ee

File tree

3 files changed

+14
-73
lines changed

3 files changed

+14
-73
lines changed

vacation/vacation.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,18 @@ func (ta TarArchive) Decompress(destination string) error {
195195
})
196196

197197
for _, h := range symlinkHeaders {
198+
evalPath := linknameFullPath(h.path, h.linkname)
199+
// Don't use constucted link if the link is absolute
200+
if filepath.IsAbs(h.linkname) {
201+
evalPath = h.linkname
202+
}
203+
198204
// Check to see if the file that will be linked to is valid for symlinking
199-
_, err := filepath.EvalSymlinks(linknameFullPath(h.path, h.linkname))
205+
_, err := filepath.EvalSymlinks(evalPath)
200206
if err != nil {
201207
return fmt.Errorf("failed to evaluate symlink %s: %w", h.path, err)
202208
}
203209

204-
// // Check that the symlink is inside the destination directory
205-
// err = checkExtractPath(h.name, destination)
206-
// if err != nil {
207-
// return err
208-
// }
209-
210210
err = os.Symlink(h.linkname, h.path)
211211
if err != nil {
212212
return fmt.Errorf("failed to extract symlink: %s", err)
@@ -452,18 +452,18 @@ func (z ZipArchive) Decompress(destination string) error {
452452
})
453453

454454
for _, h := range symlinkHeaders {
455+
evalPath := linknameFullPath(h.path, h.linkname)
456+
// Don't use constucted link if the link is absolute
457+
if filepath.IsAbs(h.linkname) {
458+
evalPath = h.linkname
459+
}
460+
455461
// Check to see if the file that will be linked to is valid for symlinking
456-
_, err := filepath.EvalSymlinks(linknameFullPath(h.path, h.linkname))
462+
_, err := filepath.EvalSymlinks(evalPath)
457463
if err != nil {
458464
return fmt.Errorf("failed to evaluate symlink %s: %w", h.path, err)
459465
}
460466

461-
// // Check that the symlink is inside the destination directory
462-
// err = checkExtractPath(h.name, destination)
463-
// if err != nil {
464-
// return err
465-
// }
466-
467467
err = os.Symlink(h.linkname, h.path)
468468
if err != nil {
469469
return fmt.Errorf("failed to unzip symlink: %w", err)

vacation/vacation_tar_test.go

-27
Original file line numberDiff line numberDiff line change
@@ -260,33 +260,6 @@ func testVacationTar(t *testing.T, context spec.G, it spec.S) {
260260
})
261261
})
262262

263-
// context("when it tries to symlink that tries to link to a file outside of the directory", func() {
264-
// var zipSlipSymlinkTar vacation.TarArchive
265-
266-
// it.Before(func() {
267-
// var err error
268-
269-
// Expect(os.MkdirAll(filepath.Join(tempDir, "sub-dir"), os.ModePerm)).To(Succeed())
270-
// Expect(os.WriteFile(filepath.Join(tempDir, "some-file"), nil, 0644)).To(Succeed())
271-
272-
// buffer := bytes.NewBuffer(nil)
273-
// tw := tar.NewWriter(buffer)
274-
275-
// Expect(tw.WriteHeader(&tar.Header{Name: "symlink", Mode: 0755, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: filepath.Join("..", "some-file")})).To(Succeed())
276-
// _, err = tw.Write([]byte{})
277-
// Expect(err).NotTo(HaveOccurred())
278-
279-
// Expect(tw.Close()).To(Succeed())
280-
281-
// zipSlipSymlinkTar = vacation.NewTarArchive(bytes.NewReader(buffer.Bytes()))
282-
// })
283-
284-
// it("returns an error", func() {
285-
// err := zipSlipSymlinkTar.Decompress(filepath.Join(tempDir, "sub-dir"))
286-
// Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("illegal file path %q: the file path does not occur within the destination directory", filepath.Join("..", "some-file")))))
287-
// })
288-
// })
289-
290263
context("when the symlink creation fails", func() {
291264
var brokenSymlinkTar vacation.TarArchive
292265

vacation/vacation_zip_test.go

-32
Original file line numberDiff line numberDiff line change
@@ -228,38 +228,6 @@ func testVacationZip(t *testing.T, context spec.G, it spec.S) {
228228
})
229229
})
230230

231-
// context("when it tries to symlink that tries to link to a file outside of the directory", func() {
232-
// var buffer *bytes.Buffer
233-
// it.Before(func() {
234-
// var err error
235-
236-
// Expect(os.MkdirAll(filepath.Join(tempDir, "sub-dir"), os.ModePerm)).To(Succeed())
237-
// Expect(os.WriteFile(filepath.Join(tempDir, "some-file"), nil, 0644)).To(Succeed())
238-
239-
// buffer = bytes.NewBuffer(nil)
240-
// zw := zip.NewWriter(buffer)
241-
242-
// header := &zip.FileHeader{Name: "symlink"}
243-
// header.SetMode(0755 | os.ModeSymlink)
244-
245-
// symlink, err := zw.CreateHeader(header)
246-
// Expect(err).NotTo(HaveOccurred())
247-
248-
// _, err = symlink.Write([]byte(filepath.Join("..", "some-file")))
249-
// Expect(err).NotTo(HaveOccurred())
250-
251-
// Expect(zw.Close()).To(Succeed())
252-
253-
// })
254-
255-
// it("returns an error", func() {
256-
// readyArchive := vacation.NewZipArchive(buffer)
257-
258-
// err := readyArchive.Decompress(filepath.Join(tempDir, "sub-dir"))
259-
// Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("illegal file path %q: the file path does not occur within the destination directory", filepath.Join("..", "some-file")))))
260-
// })
261-
// })
262-
263231
context("when the symlink creation fails", func() {
264232
var buffer *bytes.Buffer
265233
it.Before(func() {

0 commit comments

Comments
 (0)