Skip to content

Commit

Permalink
ignore zero value variables for context (#3436)
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus authored Jan 31, 2025
1 parent 6827f06 commit 763959f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/alertcontext/alertcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"slices"
"strconv"

Expand Down Expand Up @@ -202,6 +203,10 @@ func EvalAlertContextRules(evt types.Event, match *types.MatchedRule, request *h
}
}
default:
r := reflect.ValueOf(output)
if r.IsZero() || r.IsNil() {
continue
}
val := fmt.Sprintf("%v", output)
if val != "" && !slices.Contains(tmpContext[key], val) {
tmpContext[key] = append(tmpContext[key], val)
Expand Down
46 changes: 46 additions & 0 deletions pkg/alertcontext/alertcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,49 @@ func TestAppsecEventToContext(t *testing.T) {
assert.ElementsMatch(t, test.expectedResult, metas)
}
}

func TestEvalAlertContextRules(t *testing.T) {
tests := []struct {
name string
contextToSend map[string][]string
event types.Event
match types.MatchedRule
req *http.Request
expectedResult map[string][]string
expectedErrLen int
}{
{
name: "no appsec match",
contextToSend: map[string][]string{
"source_ip": {"evt.Parsed.source_ip"},
"id": {"match.id"},
},
event: types.Event{
Parsed: map[string]string{
"source_ip": "1.2.3.4",
"source_machine": "mymachine",
"uri": "/test/test/test/../../../../../../../../",
},
},
expectedResult: map[string][]string{
"source_ip": {"1.2.3.4"},
"id": {},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
contextDict := make(map[string][]string)

alertContext = Context{}
if err := NewAlertContext(test.contextToSend, 100); err != nil {
t.Fatalf("failed to compile %s: %s", test.name, err)
}

errs := EvalAlertContextRules(test.event, &test.match, test.req, contextDict)
assert.Len(t, errs, test.expectedErrLen)
assert.Equal(t, test.expectedResult, contextDict)
})
}
}

0 comments on commit 763959f

Please sign in to comment.