From 33732e4f8c1061a78731b141a50209819329c7e6 Mon Sep 17 00:00:00 2001 From: SwiftEngineer Date: Mon, 17 Apr 2023 11:06:52 -0700 Subject: [PATCH 1/3] Fixed a bug when archives contain files without a name --- slug.go | 6 ++++++ slug_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/slug.go b/slug.go index 3707417..48e35c2 100644 --- a/slug.go +++ b/slug.go @@ -376,6 +376,12 @@ func (p *Packer) Unpack(r io.Reader, dst string) error { // Get rid of absolute paths. path := header.Name + + // If the entry has no name, ignore it. + if path == "" { + continue + } + if path[0] == '/' { path = path[1:] } diff --git a/slug_test.go b/slug_test.go index a9e1ce9..e8b0b3f 100644 --- a/slug_test.go +++ b/slug_test.go @@ -1017,6 +1017,34 @@ func TestNewPacker(t *testing.T) { } } +func TestUnpackEmptyName(t *testing.T) { + var buf bytes.Buffer + + gw := gzip.NewWriter(&buf) + + tw := tar.NewWriter(gw) + + tw.WriteHeader(&tar.Header{ + Typeflag: tar.TypeDir, + }) + + tw.Close() + gw.Close() + + if buf.Len() == 0 { + t.Fatal("unable to create tar properly") + } + + dir, err := ioutil.TempDir("", "slug") + if err != nil { + t.Fatalf("err:%v", err) + } + defer os.RemoveAll(dir) + + // This crashes unless the bug is fixed + Unpack(&buf, dir) +} + // This is a reusable assertion for when packing testdata/archive-dir func assertArchiveFixture(t *testing.T, slug *bytes.Buffer, got *Meta) { gzipR, err := gzip.NewReader(slug) From 8428636ec21322e12c9ec70381d8426700514456 Mon Sep 17 00:00:00 2001 From: SwiftEngineer Date: Mon, 17 Apr 2023 13:20:08 -0700 Subject: [PATCH 2/3] move comment for clarity --- slug.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slug.go b/slug.go index 48e35c2..fd28d27 100644 --- a/slug.go +++ b/slug.go @@ -374,7 +374,6 @@ func (p *Packer) Unpack(r io.Reader, dst string) error { return fmt.Errorf("failed to untar slug: %w", err) } - // Get rid of absolute paths. path := header.Name // If the entry has no name, ignore it. @@ -382,6 +381,7 @@ func (p *Packer) Unpack(r io.Reader, dst string) error { continue } + // Get rid of absolute paths. if path[0] == '/' { path = path[1:] } From e7b9f60a01f782761671f791e0002849ee89abc4 Mon Sep 17 00:00:00 2001 From: SwiftEngineer Date: Mon, 17 Apr 2023 13:21:10 -0700 Subject: [PATCH 3/3] assert error is not nil for empty archive test --- slug_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/slug_test.go b/slug_test.go index e8b0b3f..9ff5758 100644 --- a/slug_test.go +++ b/slug_test.go @@ -1042,7 +1042,10 @@ func TestUnpackEmptyName(t *testing.T) { defer os.RemoveAll(dir) // This crashes unless the bug is fixed - Unpack(&buf, dir) + err = Unpack(&buf, dir) + if err != nil { + t.Fatalf("err:%v", err) + } } // This is a reusable assertion for when packing testdata/archive-dir