Skip to content

Commit

Permalink
Merge pull request docker#1924 from thaJeztah/fix_empty_context_import
Browse files Browse the repository at this point in the history
Fix detection of invalid context files when importing
  • Loading branch information
thaJeztah authored Jun 6, 2019
2 parents 2ec7306 + 5f93509 commit 32d7596
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cli/context/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func importTar(name string, s Writer, reader io.Reader) error {
tlsData := ContextTLSData{
Endpoints: map[string]EndpointTLSData{},
}

var importedMetaFile bool
for {
hdr, err := tr.Next()
if err == io.EOF {
Expand All @@ -325,6 +325,7 @@ func importTar(name string, s Writer, reader io.Reader) error {
if err := s.CreateOrUpdate(meta); err != nil {
return err
}
importedMetaFile = true
} else if strings.HasPrefix(hdr.Name, "tls/") {
data, err := ioutil.ReadAll(tr)
if err != nil {
Expand All @@ -335,7 +336,9 @@ func importTar(name string, s Writer, reader io.Reader) error {
}
}
}

if !importedMetaFile {
return errdefs.InvalidParameter(errors.New("invalid context: no metadata found"))
}
return s.ResetTLSMaterial(name, &tlsData)
}

Expand All @@ -352,6 +355,7 @@ func importZip(name string, s Writer, reader io.Reader) error {
Endpoints: map[string]EndpointTLSData{},
}

var importedMetaFile bool
for _, zf := range zr.File {
fi := zf.FileInfo()
if fi.IsDir() {
Expand All @@ -376,6 +380,7 @@ func importZip(name string, s Writer, reader io.Reader) error {
if err := s.CreateOrUpdate(meta); err != nil {
return err
}
importedMetaFile = true
} else if strings.HasPrefix(zf.Name, "tls/") {
f, err := zf.Open()
if err != nil {
Expand All @@ -392,7 +397,9 @@ func importZip(name string, s Writer, reader io.Reader) error {
}
}
}

if !importedMetaFile {
return errdefs.InvalidParameter(errors.New("invalid context: no metadata found"))
}
return s.ResetTLSMaterial(name, &tlsData)
}

Expand Down
62 changes: 62 additions & 0 deletions cli/context/store/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store

import (
"archive/tar"
"archive/zip"
"bufio"
"bytes"
Expand Down Expand Up @@ -144,6 +145,39 @@ func TestDetectImportContentType(t *testing.T) {
assert.Assert(t, zipType != ct)
}

func TestImportTarInvalid(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name())
assert.NilError(t, err)
defer os.RemoveAll(testDir)

tf := path.Join(testDir, "test.context")

f, err := os.Create(tf)
defer f.Close()
assert.NilError(t, err)

tw := tar.NewWriter(f)
hdr := &tar.Header{
Name: "dummy-file",
Mode: 0600,
Size: int64(len("hello world")),
}
err = tw.WriteHeader(hdr)
assert.NilError(t, err)
_, err = tw.Write([]byte("hello world"))
assert.NilError(t, err)
err = tw.Close()
assert.NilError(t, err)

source, err := os.Open(tf)
assert.NilError(t, err)
defer source.Close()
var r io.Reader = source
s := New(testDir, testCfg)
err = Import("tarInvalid", s, r)
assert.ErrorContains(t, err, "invalid context: no metadata found")
}

func TestImportZip(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name())
assert.NilError(t, err)
Expand Down Expand Up @@ -194,3 +228,31 @@ func TestImportZip(t *testing.T) {
err = Import("zipTest", s, r)
assert.NilError(t, err)
}

func TestImportZipInvalid(t *testing.T) {
testDir, err := ioutil.TempDir("", t.Name())
assert.NilError(t, err)
defer os.RemoveAll(testDir)

zf := path.Join(testDir, "test.zip")

f, err := os.Create(zf)
defer f.Close()
assert.NilError(t, err)
w := zip.NewWriter(f)

df, err := w.Create("dummy-file")
assert.NilError(t, err)
_, err = df.Write([]byte("hello world"))
assert.NilError(t, err)
err = w.Close()
assert.NilError(t, err)

source, err := os.Open(zf)
assert.NilError(t, err)
defer source.Close()
var r io.Reader = source
s := New(testDir, testCfg)
err = Import("zipInvalid", s, r)
assert.ErrorContains(t, err, "invalid context: no metadata found")
}

0 comments on commit 32d7596

Please sign in to comment.