From 6ddd3b0b470d16dfe62caf5fff21011cfff44a76 Mon Sep 17 00:00:00 2001 From: WGH Date: Mon, 9 Sep 2019 08:48:21 +0300 Subject: Implement webhook branch filter (#7791) * Fix validate() function to handle errors in embedded anon structs * Implement webhook branch filter See #2025, #3998. --- modules/validation/binding.go | 25 +++++++++++++ modules/validation/binding_test.go | 5 +-- modules/validation/glob_pattern_test.go | 62 +++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 modules/validation/glob_pattern_test.go (limited to 'modules/validation') diff --git a/modules/validation/binding.go b/modules/validation/binding.go index fc420e62fb..6ed75b50fb 100644 --- a/modules/validation/binding.go +++ b/modules/validation/binding.go @@ -10,11 +10,15 @@ import ( "strings" "gitea.com/macaron/binding" + "github.com/gobwas/glob" ) const ( // ErrGitRefName is git reference name error ErrGitRefName = "GitRefNameError" + + // ErrGlobPattern is returned when glob pattern is invalid + ErrGlobPattern = "GlobPattern" ) var ( @@ -28,6 +32,7 @@ var ( func AddBindingRules() { addGitRefNameBindingRule() addValidURLBindingRule() + addGlobPatternRule() } func addGitRefNameBindingRule() { @@ -82,6 +87,26 @@ func addValidURLBindingRule() { }) } +func addGlobPatternRule() { + binding.AddRule(&binding.Rule{ + IsMatch: func(rule string) bool { + return rule == "GlobPattern" + }, + IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) { + str := fmt.Sprintf("%v", val) + + if len(str) != 0 { + if _, err := glob.Compile(str); err != nil { + errs.Add([]string{name}, ErrGlobPattern, err.Error()) + return false, errs + } + } + + return true, errs + }, + }) +} + func portOnly(hostport string) string { colon := strings.IndexByte(hostport, ':') if colon == -1 { diff --git a/modules/validation/binding_test.go b/modules/validation/binding_test.go index 5ac88c9317..9fc9a6db08 100644 --- a/modules/validation/binding_test.go +++ b/modules/validation/binding_test.go @@ -26,8 +26,9 @@ type ( } TestForm struct { - BranchName string `form:"BranchName" binding:"GitRefName"` - URL string `form:"ValidUrl" binding:"ValidUrl"` + BranchName string `form:"BranchName" binding:"GitRefName"` + URL string `form:"ValidUrl" binding:"ValidUrl"` + GlobPattern string `form:"GlobPattern" binding:"GlobPattern"` } ) diff --git a/modules/validation/glob_pattern_test.go b/modules/validation/glob_pattern_test.go new file mode 100644 index 0000000000..26775167b4 --- /dev/null +++ b/modules/validation/glob_pattern_test.go @@ -0,0 +1,62 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package validation + +import ( + "testing" + + "gitea.com/macaron/binding" + "github.com/gobwas/glob" +) + +func getGlobPatternErrorString(pattern string) string { + // It would be unwise to rely on that glob + // compilation errors don't ever change. + if _, err := glob.Compile(pattern); err != nil { + return err.Error() + } + return "" +} + +var globValidationTestCases = []validationTestCase{ + { + description: "Empty glob pattern", + data: TestForm{ + GlobPattern: "", + }, + expectedErrors: binding.Errors{}, + }, + { + description: "Valid glob", + data: TestForm{ + GlobPattern: "{master,release*}", + }, + expectedErrors: binding.Errors{}, + }, + + { + description: "Invalid glob", + data: TestForm{ + GlobPattern: "[a-", + }, + expectedErrors: binding.Errors{ + binding.Error{ + FieldNames: []string{"GlobPattern"}, + Classification: ErrGlobPattern, + Message: getGlobPatternErrorString("[a-"), + }, + }, + }, +} + +func Test_GlobPatternValidation(t *testing.T) { + AddBindingRules() + + for _, testCase := range globValidationTestCases { + t.Run(testCase.description, func(t *testing.T) { + performValidationTest(t, testCase) + }) + } +} -- cgit v1.2.3