Skip to content

Commit

Permalink
Be more strict to validate uri + support idna
Browse files Browse the repository at this point in the history
  • Loading branch information
sapk committed Dec 2, 2018
1 parent db2dc6f commit 5b562ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
26 changes: 16 additions & 10 deletions gluster/driver/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,35 @@ import (

"github.com/docker/go-plugins-helpers/volume"
"github.com/sapk/docker-volume-helpers/basic"
"golang.org/x/net/idna"
)

const (
validVolUriRegex = `([^:]+?):\/?([^\/]+)(/.+)?`
validHostnameRegex = `(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9\-])\.)*([A-Za-z0-9\-]|[A-Za-z0-9\-][A-Za-z0-9\-]*[A-Za-z0-9\-])`
validVolURIRegex = `((` + validHostnameRegex + `)(,` + validHostnameRegex + `)*):\/?([^\/]+)(/.+)?`
)

func isValidURI(volURI string) bool {
re := regexp.MustCompile(validVolUriRegex)
volURI, err := idna.ToASCII(volURI)
if err != nil {
return false
}
re := regexp.MustCompile(validVolURIRegex)
return re.MatchString(volURI)
}

func parseVolURI(volURI string) string {
re := regexp.MustCompile(validVolUriRegex)
volURI, _ = idna.ToASCII(volURI)
re := regexp.MustCompile(validVolURIRegex)
res := re.FindAllStringSubmatch(volURI, -1)
volServers := strings.Split(res[0][1], ",")
volumeId := res[0][2]
subDir := res[0][3]

if (subDir == "") {
return fmt.Sprintf("--volfile-id='%s' -s '%s'", volumeId, strings.Join(volServers, "' -s '"))
} else {
return fmt.Sprintf("--volfile-id='%s' --subdir-mount='%s' -s '%s'", volumeId, subDir, strings.Join(volServers, "' -s '"))
volumeID := res[0][10]
subDir := res[0][11]

if subDir == "" {
return fmt.Sprintf("--volfile-id='%s' -s '%s'", volumeID, strings.Join(volServers, "' -s '"))
}
return fmt.Sprintf("--volfile-id='%s' --subdir-mount='%s' -s '%s'", volumeID, subDir, strings.Join(volServers, "' -s '"))
}

//GetMountName get moint point base on request and driver config (mountUniqName)
Expand Down
17 changes: 13 additions & 4 deletions gluster/driver/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@ func TestIsValidURI(t *testing.T) {
{"test,volume", false},
{"test,test2:volume", true},
{"192.168.1.1:volume", true},
{"192.168.1.:volume", true},
{"192.168.1.:volume", false},
{"192.168.1.1,10.8.0.1:volume", true},
{"192.168.1.1,test2:volume", true},
{"192.168.1.1,test2:volume/subdir", true},
{"192.168.1.1,test2:/volume/subdir", true},
{"192.168.1.1,test2://volume/subdir", false},
{"192.168.1.1,test2:/volume", true},
{"مثال.إختبار:volume", true},
{"例子.测试:volume", true},
{"例子.測試:volume", true},
{"παράδειγμα.δοκιμή:volume", true},
{"उदाहरण.परीक्षा:volume", true},
{"例え.テスト:volume", true},
{"실례.테스트:volume", true},
{"مثال.آزمایشی:volume", true},
{"пример.испытание:volume", true},
}

for _, test := range tt {
r := isValidURI(test.value)
if test.result != r {
t.Errorf("Expected to be '%v' , got '%v'", test.result, r)
t.Errorf("Expected URI '%s' to be '%v' , got '%v'", test.value, test.result, r)
}
}
}
Expand All @@ -49,10 +58,10 @@ func TestParseVolURI(t *testing.T) {
{"192.168.1.1,test2:volume", "--volfile-id='volume' -s '192.168.1.1' -s 'test2'"},
}

for _, test := range tt {
for i, test := range tt {
r := parseVolURI(test.value)
if test.result != r {
t.Errorf("Expected to be '%v' , got '%v'", test.result, r)
t.Errorf("Expected test %d to be '%v' , got '%v'", i, test.result, r)
}
}
}
Expand Down

0 comments on commit 5b562ac

Please sign in to comment.