Skip to content

Commit

Permalink
auto dedup rules
Browse files Browse the repository at this point in the history
  • Loading branch information
buixor committed Nov 14, 2024
1 parent 351a41c commit f699308
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions pkg/acquisition/modules/appsec/appsec_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"slices"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -31,23 +32,38 @@ type AppsecRunner struct {
logger *log.Entry
}

func (r *AppsecRunner) MergeDedupRules(collections []appsec.AppsecCollection, logger *log.Entry) string {
var rulesArr []string
dedupRules := make(map[string]struct{})

for _, collection := range collections {
for _, rule := range collection.Rules {
if _, ok := dedupRules[rule]; !ok {
rulesArr = append(rulesArr, rule)
dedupRules[rule] = struct{}{}
} else {
logger.Debugf("Discarding duplicate rule : %s", rule)
}

Check warning on line 46 in pkg/acquisition/modules/appsec/appsec_runner.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec_runner.go#L45-L46

Added lines #L45 - L46 were not covered by tests
}
}
if len(rulesArr) != len(dedupRules) {
logger.Warningf("%d rules were discarded as they were duplicates", len(rulesArr)-len(dedupRules))
}

Check warning on line 51 in pkg/acquisition/modules/appsec/appsec_runner.go

View check run for this annotation

Codecov / codecov/patch

pkg/acquisition/modules/appsec/appsec_runner.go#L50-L51

Added lines #L50 - L51 were not covered by tests

return strings.Join(rulesArr, "\n")
}

func (r *AppsecRunner) Init(datadir string) error {
var err error
fs := os.DirFS(datadir)

inBandRules := ""
outOfBandRules := ""

for _, collection := range r.AppsecRuntime.InBandRules {
inBandRules += collection.String()
}

for _, collection := range r.AppsecRuntime.OutOfBandRules {
outOfBandRules += collection.String()
}
inBandLogger := r.logger.Dup().WithField("band", "inband")
outBandLogger := r.logger.Dup().WithField("band", "outband")

//While loading rules, we dedup rules based on their content, while keeping the order
inBandRules := r.MergeDedupRules(r.AppsecRuntime.InBandRules, inBandLogger)
outOfBandRules := r.MergeDedupRules(r.AppsecRuntime.OutOfBandRules, outBandLogger)

//setting up inband engine
inbandCfg := coraza.NewWAFConfig().WithDirectives(inBandRules).WithRootFS(fs).WithDebugLogger(appsec.NewCrzLogger(inBandLogger))
if !r.AppsecRuntime.Config.InbandOptions.DisableBodyInspection {
Expand Down

0 comments on commit f699308

Please sign in to comment.