Browse code

Forbid certain paths within docker build ADD

Michael Crosby authored on 2013/08/07 01:58:52
Showing 2 changed files
... ...
@@ -278,6 +278,9 @@ func (b *buildFile) addContext(container *Container, orig, dest string) error {
278 278
 	if strings.HasSuffix(dest, "/") {
279 279
 		destPath = destPath + "/"
280 280
 	}
281
+	if !strings.HasPrefix(origPath, b.context) {
282
+		return fmt.Errorf("Forbidden path: %s", origPath)
283
+	}
281 284
 	fi, err := os.Stat(origPath)
282 285
 	if err != nil {
283 286
 		return err
... ...
@@ -423,3 +423,52 @@ func TestBuildImageWithoutCache(t *testing.T) {
423 423
 		t.Fail()
424 424
 	}
425 425
 }
426
+
427
+func TestForbiddenContextPath(t *testing.T) {
428
+	runtime, err := newTestRuntime()
429
+	if err != nil {
430
+		t.Fatal(err)
431
+	}
432
+	defer nuke(runtime)
433
+
434
+	srv := &Server{
435
+		runtime:     runtime,
436
+		pullingPool: make(map[string]struct{}),
437
+		pushingPool: make(map[string]struct{}),
438
+	}
439
+
440
+	context := testContextTemplate{`
441
+        from {IMAGE}
442
+        maintainer dockerio
443
+        add ../../ test/
444
+        `,
445
+		[][2]string{{"test.txt", "test1"}, {"other.txt", "other"}}, nil}
446
+
447
+	httpServer, err := mkTestingFileServer(context.remoteFiles)
448
+	if err != nil {
449
+		t.Fatal(err)
450
+	}
451
+	defer httpServer.Close()
452
+
453
+	idx := strings.LastIndex(httpServer.URL, ":")
454
+	if idx < 0 {
455
+		t.Fatalf("could not get port from test http server address %s", httpServer.URL)
456
+	}
457
+	port := httpServer.URL[idx+1:]
458
+
459
+	ip := srv.runtime.networkManager.bridgeNetwork.IP
460
+	dockerfile := constructDockerfile(context.dockerfile, ip, port)
461
+
462
+	buildfile := NewBuildFile(srv, ioutil.Discard, false, true)
463
+	_, err = buildfile.Build(mkTestContext(dockerfile, context.files, t))
464
+
465
+	if err == nil {
466
+		t.Log("Error should not be nil")
467
+		t.Fail()
468
+	}
469
+
470
+	if err.Error() != "Forbidden path: /" {
471
+		t.Logf("Error message is not expected: %s", err.Error())
472
+		t.Fail()
473
+	}
474
+}