From ba7906aef38a0a9b1c586b60bf1a4109367c4343 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 4 Feb 2021 18:38:51 -0800 Subject: [PATCH] archive: avoid creating parent dirs for XGlobalHeader Signed-off-by: Tonis Tiigi --- pkg/archive/archive.go | 6 ++++++ pkg/archive/archive_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/archive/archive.go b/pkg/archive/archive.go index 887ece9ce9..134749d684 100644 --- a/pkg/archive/archive.go +++ b/pkg/archive/archive.go @@ -931,6 +931,12 @@ loop: return err } + // ignore XGlobalHeader early to avoid creating parent directories for them + if hdr.Typeflag == tar.TypeXGlobalHeader { + logrus.Debugf("PAX Global Extended Headers found for %s and ignored", hdr.Name) + continue + } + // Normalize name, for safety and for a simple is-root check // This keeps "../" as-is, but normalizes "/../" to "/". Or Windows: // This keeps "..\" as-is, but normalizes "\..\" to "\". diff --git a/pkg/archive/archive_test.go b/pkg/archive/archive_test.go index e89c78264a..d7632e1f06 100644 --- a/pkg/archive/archive_test.go +++ b/pkg/archive/archive_test.go @@ -4,6 +4,7 @@ import ( "archive/tar" "bytes" "compress/gzip" + "errors" "fmt" "io" "io/ioutil" @@ -1174,6 +1175,26 @@ func TestTempArchiveCloseMultipleTimes(t *testing.T) { } } +// TestXGlobalNoParent is a regression test to check parent directories are not crated for PAX headers +func TestXGlobalNoParent(t *testing.T) { + buf := &bytes.Buffer{} + w := tar.NewWriter(buf) + err := w.WriteHeader(&tar.Header{ + Name: "foo/bar", + Typeflag: tar.TypeXGlobalHeader, + }) + assert.NilError(t, err) + tmpDir, err := ioutil.TempDir("", "pax-test") + assert.NilError(t, err) + defer os.RemoveAll(tmpDir) + err = Untar(buf, tmpDir, nil) + assert.NilError(t, err) + + _, err = os.Lstat(filepath.Join(tmpDir, "foo")) + assert.Check(t, err != nil) + assert.Check(t, errors.Is(err, os.ErrNotExist)) +} + func TestReplaceFileTarWrapper(t *testing.T) { filesInArchive := 20 testcases := []struct {