archive: avoid creating parent dirs for XGlobalHeader

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi 2021-02-04 18:38:51 -08:00
parent 0af8ed47bb
commit ba7906aef3
2 changed files with 27 additions and 0 deletions

View File

@ -931,6 +931,12 @@ loop:
return err 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 // 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 "/". Or Windows:
// This keeps "..\" as-is, but normalizes "\..\" to "\". // This keeps "..\" as-is, but normalizes "\..\" to "\".

View File

@ -4,6 +4,7 @@ import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "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) { func TestReplaceFileTarWrapper(t *testing.T) {
filesInArchive := 20 filesInArchive := 20
testcases := []struct { testcases := []struct {