Skip to content

Commit

Permalink
Merge pull request moby#41984 from tonistiigi/pax-parent
Browse files Browse the repository at this point in the history
archive: avoid creating parent dirs for XGlobalHeader
  • Loading branch information
thaJeztah authored Feb 12, 2021
2 parents 806a090 + ba7906a commit 01ae718
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "\".
Expand Down
21 changes: 21 additions & 0 deletions pkg/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 01ae718

Please sign in to comment.