Skip to content

Commit

Permalink
Merge pull request #39 from hashicorp/fix-empty-archive-entries
Browse files Browse the repository at this point in the history
Fixed a bug when archives contain entries without a name
  • Loading branch information
SwiftEngineer committed Apr 17, 2023
2 parents 9277946 + e7b9f60 commit 66525e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,14 @@ 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.
if path == "" {
continue
}

// Get rid of absolute paths.
if path[0] == '/' {
path = path[1:]
}
Expand Down
31 changes: 31 additions & 0 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,37 @@ 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
err = Unpack(&buf, dir)
if err != nil {
t.Fatalf("err:%v", err)
}
}

// 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)
Expand Down

0 comments on commit 66525e4

Please sign in to comment.