Browse code

master and node config files should not contain backsteps

deads2k authored on 2015/03/25 21:39:00
Showing 3 changed files
... ...
@@ -14,7 +14,7 @@ import (
14 14
 )
15 15
 
16 16
 func RelativizeMasterConfigPaths(config *MasterConfig, base string) error {
17
-	return cmdutil.RelativizePaths(GetMasterFileReferences(config), base)
17
+	return cmdutil.RelativizePathWithNoBacksteps(GetMasterFileReferences(config), base)
18 18
 }
19 19
 
20 20
 func ResolveMasterConfigPaths(config *MasterConfig, base string) error {
... ...
@@ -57,7 +57,7 @@ func GetMasterFileReferences(config *MasterConfig) []*string {
57 57
 }
58 58
 
59 59
 func RelativizeNodeConfigPaths(config *NodeConfig, base string) error {
60
-	return cmdutil.RelativizePaths(GetNodeFileReferences(config), base)
60
+	return cmdutil.RelativizePathWithNoBacksteps(GetNodeFileReferences(config), base)
61 61
 }
62 62
 
63 63
 func ResolveNodeConfigPaths(config *NodeConfig, base string) error {
... ...
@@ -1,8 +1,10 @@
1 1
 package util
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"os"
5 6
 	"path/filepath"
7
+	"strings"
6 8
 )
7 9
 
8 10
 func MakeAbs(path, base string) (string, error) {
... ...
@@ -47,3 +49,29 @@ func RelativizePaths(refs []*string, base string) error {
47 47
 	}
48 48
 	return nil
49 49
 }
50
+
51
+// RelativizePathWithNoBacksteps updates the given refs to be relative paths, relative to the given base directory as long as they do not require backsteps.
52
+// Any path requiring a backstep is left as-is as long it is absolute.  Any non-absolute path that can't be relativized produces an error
53
+func RelativizePathWithNoBacksteps(refs []*string, base string) error {
54
+	for _, ref := range refs {
55
+		// Don't relativize empty paths
56
+		if len(*ref) > 0 {
57
+			rel, err := filepath.Rel(base, *ref)
58
+			if err != nil {
59
+				return err
60
+			}
61
+
62
+			// if we have a backstep, don't mess with the path
63
+			if strings.HasPrefix(rel, "../") {
64
+				if filepath.IsAbs(*ref) {
65
+					continue
66
+				}
67
+
68
+				return fmt.Errorf("%v requires backsteps and is not absolute", *ref)
69
+			}
70
+
71
+			*ref = rel
72
+		}
73
+	}
74
+	return nil
75
+}
50 76
new file mode 100644
... ...
@@ -0,0 +1,27 @@
0
+package util
1
+
2
+import (
3
+	"testing"
4
+)
5
+
6
+func TestRejectNonAbsolutePathsThatRequireBacksteps(t *testing.T) {
7
+	path := "../foo"
8
+	paths := []*string{}
9
+	paths = append(paths, &path)
10
+
11
+	expectedError := "../foo requires backsteps and is not absolute"
12
+
13
+	if err := RelativizePathWithNoBacksteps(paths, "."); err == nil || expectedError != err.Error() {
14
+		t.Errorf("expected %v, got %v", expectedError, err)
15
+	}
16
+}
17
+
18
+func TestAcceptAbsolutePath(t *testing.T) {
19
+	path := "/foo"
20
+	paths := []*string{}
21
+	paths = append(paths, &path)
22
+
23
+	if err := RelativizePathWithNoBacksteps(paths, "/home/deads"); err != nil {
24
+		t.Errorf("unexpected error: %v", err)
25
+	}
26
+}