Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 3cf520c

Browse files
authored
Merge pull request #1929 from thaJeztah/19.03_backport_fix_empty_context_import
[19.03 backport] Fix detection of invalid context files when importing Upstream-commit: 49236a4391a4622e35b218acb1024f93ab0f6309 Component: cli
2 parents 935ce4c + 7d1a3b5 commit 3cf520c

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

components/cli/cli/context/store/store.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func importTar(name string, s Writer, reader io.Reader) error {
300300
tlsData := ContextTLSData{
301301
Endpoints: map[string]EndpointTLSData{},
302302
}
303-
303+
var importedMetaFile bool
304304
for {
305305
hdr, err := tr.Next()
306306
if err == io.EOF {
@@ -325,6 +325,7 @@ func importTar(name string, s Writer, reader io.Reader) error {
325325
if err := s.CreateOrUpdate(meta); err != nil {
326326
return err
327327
}
328+
importedMetaFile = true
328329
} else if strings.HasPrefix(hdr.Name, "tls/") {
329330
data, err := ioutil.ReadAll(tr)
330331
if err != nil {
@@ -335,7 +336,9 @@ func importTar(name string, s Writer, reader io.Reader) error {
335336
}
336337
}
337338
}
338-
339+
if !importedMetaFile {
340+
return errdefs.InvalidParameter(errors.New("invalid context: no metadata found"))
341+
}
339342
return s.ResetTLSMaterial(name, &tlsData)
340343
}
341344

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

358+
var importedMetaFile bool
355359
for _, zf := range zr.File {
356360
fi := zf.FileInfo()
357361
if fi.IsDir() {
@@ -376,6 +380,7 @@ func importZip(name string, s Writer, reader io.Reader) error {
376380
if err := s.CreateOrUpdate(meta); err != nil {
377381
return err
378382
}
383+
importedMetaFile = true
379384
} else if strings.HasPrefix(zf.Name, "tls/") {
380385
f, err := zf.Open()
381386
if err != nil {
@@ -392,7 +397,9 @@ func importZip(name string, s Writer, reader io.Reader) error {
392397
}
393398
}
394399
}
395-
400+
if !importedMetaFile {
401+
return errdefs.InvalidParameter(errors.New("invalid context: no metadata found"))
402+
}
396403
return s.ResetTLSMaterial(name, &tlsData)
397404
}
398405

components/cli/cli/context/store/store_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package store
22

33
import (
4+
"archive/tar"
45
"archive/zip"
56
"bufio"
67
"bytes"
@@ -144,6 +145,39 @@ func TestDetectImportContentType(t *testing.T) {
144145
assert.Assert(t, zipType != ct)
145146
}
146147

148+
func TestImportTarInvalid(t *testing.T) {
149+
testDir, err := ioutil.TempDir("", t.Name())
150+
assert.NilError(t, err)
151+
defer os.RemoveAll(testDir)
152+
153+
tf := path.Join(testDir, "test.context")
154+
155+
f, err := os.Create(tf)
156+
defer f.Close()
157+
assert.NilError(t, err)
158+
159+
tw := tar.NewWriter(f)
160+
hdr := &tar.Header{
161+
Name: "dummy-file",
162+
Mode: 0600,
163+
Size: int64(len("hello world")),
164+
}
165+
err = tw.WriteHeader(hdr)
166+
assert.NilError(t, err)
167+
_, err = tw.Write([]byte("hello world"))
168+
assert.NilError(t, err)
169+
err = tw.Close()
170+
assert.NilError(t, err)
171+
172+
source, err := os.Open(tf)
173+
assert.NilError(t, err)
174+
defer source.Close()
175+
var r io.Reader = source
176+
s := New(testDir, testCfg)
177+
err = Import("tarInvalid", s, r)
178+
assert.ErrorContains(t, err, "invalid context: no metadata found")
179+
}
180+
147181
func TestImportZip(t *testing.T) {
148182
testDir, err := ioutil.TempDir("", t.Name())
149183
assert.NilError(t, err)
@@ -194,3 +228,31 @@ func TestImportZip(t *testing.T) {
194228
err = Import("zipTest", s, r)
195229
assert.NilError(t, err)
196230
}
231+
232+
func TestImportZipInvalid(t *testing.T) {
233+
testDir, err := ioutil.TempDir("", t.Name())
234+
assert.NilError(t, err)
235+
defer os.RemoveAll(testDir)
236+
237+
zf := path.Join(testDir, "test.zip")
238+
239+
f, err := os.Create(zf)
240+
defer f.Close()
241+
assert.NilError(t, err)
242+
w := zip.NewWriter(f)
243+
244+
df, err := w.Create("dummy-file")
245+
assert.NilError(t, err)
246+
_, err = df.Write([]byte("hello world"))
247+
assert.NilError(t, err)
248+
err = w.Close()
249+
assert.NilError(t, err)
250+
251+
source, err := os.Open(zf)
252+
assert.NilError(t, err)
253+
defer source.Close()
254+
var r io.Reader = source
255+
s := New(testDir, testCfg)
256+
err = Import("zipInvalid", s, r)
257+
assert.ErrorContains(t, err, "invalid context: no metadata found")
258+
}

0 commit comments

Comments
 (0)