From e029a04123dc77499d5e953ca81d6b849aa39252 Mon Sep 17 00:00:00 2001 From: Emiliano Ciavatta Date: Wed, 28 Jul 2021 13:33:46 +0200 Subject: Fix error in rule pattern management after hyperscan update --- rules_manager.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/rules_manager.go b/rules_manager.go index 5d6cded..736d561 100644 --- a/rules_manager.go +++ b/rules_manager.go @@ -21,12 +21,14 @@ import ( "context" "errors" "fmt" - "github.com/flier/gohs/hyperscan" - "github.com/go-playground/validator/v10" - log "github.com/sirupsen/logrus" "sort" + "strings" "sync" "time" + + "github.com/flier/gohs/hyperscan" + "github.com/go-playground/validator/v10" + log "github.com/sirupsen/logrus" ) const DirectionBoth = 0 @@ -336,7 +338,8 @@ func (rm *rulesManagerImpl) validateAndAddRuleLocal(rule *Rule) error { startID := len(rm.patterns) for id, pattern := range newPatterns { rm.patterns = append(rm.patterns, pattern) - rm.patternsIds[pattern.String()] = uint(startID + id) + regex := pattern.String() + rm.patternsIds[regex[strings.IndexByte(regex, ':')+1:]] = uint(startID + id) } rm.rules[rule.ID] = *rule @@ -363,7 +366,11 @@ func (rm *rulesManagerImpl) generateDatabase(version RowID) error { } func (p *Pattern) BuildPattern() (*hyperscan.Pattern, error) { - hp, err := hyperscan.ParsePattern(fmt.Sprintf("/%s/", p.Regex)) + if !strings.HasPrefix(p.Regex, "/") || !strings.HasSuffix(p.Regex, "/") { + p.Regex = fmt.Sprintf("/%s/", p.Regex) + } + + hp, err := hyperscan.ParsePattern(p.Regex) if err != nil { return nil, err } -- cgit v1.2.3-70-g09d2