diff --git a/benchmark_test.go b/benchmark_test.go index 672a7aa..f652df7 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -3,7 +3,7 @@ package main import ( "github.com/kaanaktas/go-slm/config" "github.com/kaanaktas/go-slm/executor" - "io/ioutil" + "io" "log" "os" "testing" @@ -17,7 +17,7 @@ func init() { log.Println("Starting with number of worker", config.NumberOfWorker) log.SetFlags(0) - log.SetOutput(ioutil.Discard) + log.SetOutput(io.Discard) } func Benchmark(b *testing.B) { @@ -27,6 +27,7 @@ func Benchmark(b *testing.B) { data := string(content) serviceName := "test3" + b.ResetTimer() for i := 0; i < b.N; i++ { executor.Apply(data, serviceName, config.Request) } diff --git a/datafilter/base.go b/datafilter/base.go index 1314611..3a9e0bf 100644 --- a/datafilter/base.go +++ b/datafilter/base.go @@ -14,17 +14,17 @@ type Validator interface { // PatternValidator represents a validation rule based on a regular expression patternValidator. type patternValidator struct { - Name string `json:"name"` - Rule string `json:"rule"` - Sample string `json:"sample"` - Message string `json:"message"` - Disable bool `json:"disable"` + Name string `yaml:"name"` + Regex *regexp.Regexp `yaml:"-"` + Rule string `yaml:"rule"` + Sample string `yaml:"sample"` + Message string `yaml:"message"` + Disable bool `yaml:"disable"` } // Validate checks whether the given data string satisfies the regular expression pattern. func (pv *patternValidator) Validate(data *string) bool { - matched, _ := regexp.MatchString(pv.Rule, *data) - return matched + return pv.Regex.MatchString(*data) } // ToString returns a string representation of the validation rule. @@ -36,3 +36,7 @@ func (pv *patternValidator) ToString() string { func (pv *patternValidator) IsDisabled() bool { return pv.Disable } + +func (pv *patternValidator) compileRule() { + pv.Regex = regexp.MustCompile(pv.Rule) +} diff --git a/datafilter/load.go b/datafilter/load.go index 090a8ec..d3dc8f4 100644 --- a/datafilter/load.go +++ b/datafilter/load.go @@ -10,7 +10,7 @@ import ( "path/filepath" ) -//filter types +// filter types const ( PAN = "pan" OWASP = "owasp" @@ -50,10 +50,12 @@ func LoadDataFilterRules(dataFilterRuleSetPath string) { switch set.Type { case PAN: for i, v := range patterns { + v.compileRule() validateRule[i] = &pan{patternValidator: v} } case OWASP: for i, v := range patterns { + v.compileRule() validateRule[i] = &owasp{patternValidator: v} } } diff --git a/datafilter/pan.go b/datafilter/pan.go index 778c036..775a2fa 100644 --- a/datafilter/pan.go +++ b/datafilter/pan.go @@ -2,7 +2,6 @@ package datafilter import ( "math" - "regexp" "strconv" "strings" ) @@ -13,8 +12,7 @@ type pan struct { func (p *pan) Validate(data *string) bool { dataWithoutSpace := strings.ReplaceAll(*data, " ", "") - r := regexp.MustCompile(p.Rule) - matchList := r.FindAllString(dataWithoutSpace, -1) + matchList := p.Regex.FindAllString(dataWithoutSpace, -1) for _, v := range matchList { if isValidPan(v) { return true