Skip to content

Commit

Permalink
chore: improve GetField logic (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
jptosso authored Nov 2, 2023
1 parent 4b1b82d commit a0a65dd
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,29 +561,25 @@ func (tx *Transaction) GetField(rv ruleVariableParams) []types.MatchData {
matches = col.FindAll()
}

var rmi []int
for i, c := range matches {
// in the most common scenario filteredMatches length will be
// the same as matches length, so we avoid allocating per result
filteredMatches := make([]types.MatchData, 0, len(matches))

for _, c := range matches {
isException := false
lkey := strings.ToLower(c.Key())
for _, ex := range rv.Exceptions {
lkey := strings.ToLower(c.Key())
// in case it matches the regex or the keyStr
// Since keys are case sensitive we need to check with lower case
if (ex.KeyRx != nil && ex.KeyRx.MatchString(lkey)) || strings.ToLower(ex.KeyStr) == lkey {
// we remove the exception from the list of values
// we tried with standard append, but it fails... let's do some hacking
// m2 := append(matches[:i], matches[i+1:]...)
rmi = append(rmi, i)
isException = true
break
}
}
}
// we read the list of indexes backwards
// then we remove each one of them because of the exceptions
for i := len(rmi) - 1; i >= 0; i-- {
if len(matches) < rmi[i]+1 {
matches = matches[:rmi[i]-1]
} else {
matches = append(matches[:rmi[i]], matches[rmi[i]+1:]...)
if !isException {
filteredMatches = append(filteredMatches, c)
}
}
matches = filteredMatches

if rv.Count {
count := len(matches)
matches = []types.MatchData{
Expand Down

0 comments on commit a0a65dd

Please sign in to comment.