Browse code

Fix breakouts from git root during build

Signed-off-by: Tõnis Tiigi <tonistiigi@gmail.com>

Tonis Tiigi authored on 2015/06/02 03:41:45
Showing 2 changed files
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"path/filepath"
11 11
 	"strings"
12 12
 
13
+	"github.com/docker/docker/pkg/symlink"
13 14
 	"github.com/docker/docker/pkg/urlutil"
14 15
 )
15 16
 
... ...
@@ -69,7 +70,11 @@ func checkoutGit(fragment, root string) (string, error) {
69 69
 	}
70 70
 
71 71
 	if len(refAndDir) > 1 && len(refAndDir[1]) != 0 {
72
-		newCtx := filepath.Join(root, refAndDir[1])
72
+		newCtx, err := symlink.FollowSymlinkInScope(filepath.Join(root, refAndDir[1]), root)
73
+		if err != nil {
74
+			return "", fmt.Errorf("Error setting git context, %q not within git root: %s", refAndDir[1], err)
75
+		}
76
+
73 77
 		fi, err := os.Stat(newCtx)
74 78
 		if err != nil {
75 79
 			return "", err
... ...
@@ -103,6 +103,14 @@ func TestCheckoutGit(t *testing.T) {
103 103
 		t.Fatal(err)
104 104
 	}
105 105
 
106
+	if err = os.Symlink("../subdir", filepath.Join(gitDir, "parentlink")); err != nil {
107
+		t.Fatal(err)
108
+	}
109
+
110
+	if err = os.Symlink("/subdir", filepath.Join(gitDir, "absolutelink")); err != nil {
111
+		t.Fatal(err)
112
+	}
113
+
106 114
 	if _, err = gitWithinDir(gitDir, "add", "-A"); err != nil {
107 115
 		t.Fatal(err)
108 116
 	}
... ...
@@ -147,6 +155,9 @@ func TestCheckoutGit(t *testing.T) {
147 147
 		{":Dockerfile", "", true}, // not a directory error
148 148
 		{"master:nosubdir", "", true},
149 149
 		{"master:subdir", "FROM scratch\nEXPOSE 5000", false},
150
+		{"master:parentlink", "FROM scratch\nEXPOSE 5000", false},
151
+		{"master:absolutelink", "FROM scratch\nEXPOSE 5000", false},
152
+		{"master:../subdir", "", true},
150 153
 		{"test", "FROM scratch\nEXPOSE 3000", false},
151 154
 		{"test:", "FROM scratch\nEXPOSE 3000", false},
152 155
 		{"test:subdir", "FROM busybox\nEXPOSE 5000", false},