WAF增加包含任一字符串、包含所有字符串操作符

This commit is contained in:
刘祥超
2023-01-06 20:07:15 +08:00
parent 8a8881ac47
commit a17878f5b2
5 changed files with 74 additions and 10 deletions

View File

@@ -3,5 +3,5 @@
package values
func ParseIPList(v string) *StringList {
return ParseStringList(v)
return ParseStringList(v, false)
}

View File

@@ -8,17 +8,19 @@ import (
)
type StringList struct {
ValueMap map[string]zero.Zero
ValueMap map[string]zero.Zero
CaseInsensitive bool
}
func NewStringList() *StringList {
func NewStringList(caseInsensitive bool) *StringList {
return &StringList{
ValueMap: map[string]zero.Zero{},
ValueMap: map[string]zero.Zero{},
CaseInsensitive: caseInsensitive,
}
}
func ParseStringList(v string) *StringList {
var list = NewStringList()
func ParseStringList(v string, caseInsensitive bool) *StringList {
var list = NewStringList(caseInsensitive)
if len(v) == 0 {
return list
}
@@ -34,6 +36,9 @@ func ParseStringList(v string) *StringList {
for _, value := range values {
value = strings.TrimSpace(value)
if len(value) > 0 {
if caseInsensitive {
value = strings.ToLower(value)
}
list.ValueMap[value] = zero.Zero{}
}
}
@@ -42,6 +47,9 @@ func ParseStringList(v string) *StringList {
}
func (this *StringList) Contains(f string) bool {
if this.CaseInsensitive {
f = strings.ToLower(f)
}
_, ok := this.ValueMap[f]
return ok
}

View File

@@ -12,7 +12,7 @@ func TestParseStringList(t *testing.T) {
var a = assert.NewAssertion(t)
{
var list = values.ParseStringList("")
var list = values.ParseStringList("", false)
a.IsFalse(list.Contains("hello"))
}
@@ -22,9 +22,22 @@ func TestParseStringList(t *testing.T) {
world
hi
people`)
people`, false)
a.IsTrue(list.Contains("hello"))
a.IsFalse(list.Contains("hello1"))
a.IsFalse(list.Contains("Hello"))
a.IsTrue(list.Contains("hi"))
}
{
var list = values.ParseStringList(`Hello
world
hi
people`, true)
a.IsTrue(list.Contains("hello"))
a.IsTrue(list.Contains("Hello"))
a.IsTrue(list.Contains("HELLO"))
a.IsFalse(list.Contains("How"))
}
}