[feature] add binary rules capability

add binary characters to the rules engine capability.
In fact, the issue is that I want to bypass the utf-8 issue with
Golang and have a dedicated capability to create binary rules.
This commit is contained in:
toufik-airane
2020-04-21 20:50:35 +02:00
parent 147464b129
commit 53ac2db540
2 changed files with 49 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package matchers
import (
"encoding/hex"
"net/http"
"strings"
)
@@ -36,6 +37,18 @@ func (m *Matcher) Match(resp *http.Response, body, headers string) bool {
}
return m.matchRegex(body)
}
case BinaryMatcher:
// Match the parts as required for binary characters check
if m.part == BodyPart {
return m.matchBinary(body)
} else if m.part == HeaderPart {
return m.matchBinary(headers)
} else {
if !m.matchBinary(headers) {
return false
}
return m.matchBinary(body)
}
}
return false
}
@@ -127,3 +140,34 @@ func (m *Matcher) matchRegex(corpus string) bool {
}
return false
}
// matchWords matches a word check against an HTTP Response/Headers.
func (m *Matcher) matchBinary(corpus string) bool {
// Iterate over all the words accepted as valid
for i, binary := range m.Binary {
// Continue if the word doesn't match
hexa, _ := hex.DecodeString(binary)
if !strings.Contains(corpus, string(hexa)) {
// If we are in an AND request and a match failed,
// return false as the AND condition fails on any single mismatch.
if m.condition == ANDCondition {
return false
}
// Continue with the flow since its an OR Condition.
continue
}
// If the condition was an OR, return on the first match.
if m.condition == ORCondition {
return true
}
// If we are at the end of the words, return with true
if len(m.Binary)-1 == i {
return true
}
}
return false
}

View File

@@ -19,6 +19,8 @@ type Matcher struct {
Words []string `yaml:"words,omitempty"`
// Regex are the regex pattern required to be present in the response
Regex []string `yaml:"regex,omitempty"`
// Binary are the binary characters required to be present in the response
Binary []string `yaml:"binary,omitempty"`
// regexCompiled is the compiled variant
regexCompiled []*regexp.Regexp
@@ -45,6 +47,8 @@ const (
WordsMatcher MatcherType = iota + 1
// RegexMatcher matches responses with regexes
RegexMatcher
// BinaryMatcher matches responses with words
BinaryMatcher MatcherType = iota + 2
// StatusMatcher matches responses with status codes
StatusMatcher
// SizeMatcher matches responses with response size
@@ -57,6 +61,7 @@ var MatcherTypes = map[string]MatcherType{
"size": SizeMatcher,
"word": WordsMatcher,
"regex": RegexMatcher,
"binary": BinaryMatcher,
}
// ConditionType is the type of condition for matcher