diff --git a/storage/operator/filter.go b/storage/operator/filter.go index 8c22e93..c2f4cfd 100644 --- a/storage/operator/filter.go +++ b/storage/operator/filter.go @@ -2,6 +2,7 @@ package operator import ( "fmt" + "regexp" "strings" "go.mongodb.org/mongo-driver/bson" @@ -25,6 +26,10 @@ func (cis CaseInsensitives) MarshalBSON() ([]byte, error) { values := cis.Values if len(cis.Values) == 0 { values = []string{".*"} + } else { + for i := range values { + values[i] = escapeRegex(values[i]) + } } ci := CaseInsensitive{ Key: cis.Key, @@ -42,6 +47,8 @@ func (ci CaseInsensitive) MarshalBSON() ([]byte, error) { value := ci.Value if value == "" { value = "^$" + } else { + value = escapeRegex(value) } return bson.Marshal(Regex{Key: ci.Key, Value: "(?i)" + ci.Value}) } @@ -100,3 +107,9 @@ func filterMarshal(op, key string, value interface{}) ([]byte, error) { } return bson.Marshal(m) } + +func escapeRegex(s string) string { + re := regexp.MustCompile(`[^a-zA-Z0-9]`) + s = re.ReplaceAllString(s, `.`) + return s +}