From 9af2e03008e3fac42d0276e70cdcddcd7bdf5881 Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 19 Nov 2026 16:37:00 +0000 Subject: [PATCH] sww: build the Core Rule Set from the settings (closes #434) The body of waf.NewCoreRuleSet moves into newCoreRuleSet in internal/sww, beside the providers of the other parts; waf no longer exports it. The waf test helper builds the Core Rule Set with coreruleset.New while the mode is not off. Model: opus-5-5 --- internal/sww/waf.go | 31 ++++++++++++++++++++++--- internal/waf/proxy.go | 39 ++----------------------------- internal/waf/proxy_test.go | 47 +++++++++++++++++++++++++++----------- 3 files changed, 64 insertions(+), 53 deletions(-) diff --git a/internal/sww/waf.go b/internal/sww/waf.go index 72f8685..415e848 100644 --- a/internal/sww/waf.go +++ b/internal/sww/waf.go @@ -2,6 +2,7 @@ package sww import ( "context" + "fmt" "io" "log/slog" "net" @@ -175,11 +176,35 @@ func newRuleFiles( return ruleFiles, nil } -// newCoreRuleSet returns the Core Rule Set, as waf.NewCoreRuleSet does. +// newCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL, +// without the rules SWW_WAF_CORE_RULE_SET_DISABLED_RULES switches off, reading bodies +// up to SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE, with the exclusions in +// SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE and the fixes of the apps in SWW_WAF_APPS, +// or nil while SWW_WAF_CORE_RULE_SET_MODE is off. An exclusions file that +// coreruleset.New refuses is an error in every mode. While the mode is off, a +// set file is loaded with the Core Rule Set only to check it. func newCoreRuleSet(cfg *config.WAF) (*coreruleset.CoreRuleSet, error) { - crs, err := waf.NewCoreRuleSet(cfg) + if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff && + cfg.CoreRuleSetExclusionsFile == "" { + return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error + } + + crs, err := coreruleset.New(coreruleset.Params{ + ParanoiaLevel: cfg.CoreRuleSetParanoiaLevel, + DisabledRules: cfg.CoreRuleSetDisabledRules, + BodyInspectionSize: cfg.CoreRuleSetBodyInspectionSize, + ExclusionsFile: cfg.CoreRuleSetExclusionsFile, + Apps: cfg.Apps, + }) if err != nil { - return nil, startError{"cannot use the Core Rule Set", err} + return nil, startError{ + "cannot use the Core Rule Set", + fmt.Errorf("SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE: %w", err), + } + } + + if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff { + return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error } return crs, nil diff --git a/internal/waf/proxy.go b/internal/waf/proxy.go index 966fe8b..a65430a 100644 --- a/internal/waf/proxy.go +++ b/internal/waf/proxy.go @@ -6,7 +6,6 @@ package waf import ( "cmp" - "fmt" "io" "log/slog" "net/http" @@ -83,8 +82,8 @@ type Params struct { // LookupFile is the lookup database they are looked up in while // SWW_WAF_LOOKUP_SOURCE is file, and nil otherwise. LookupFile *lookup.File - // CoreRuleSet is what NewCoreRuleSet returns: what inspects requests - // while SWW_WAF_CORE_RULE_SET_MODE is not off, and nil while it is. + // CoreRuleSet is what inspects requests while + // SWW_WAF_CORE_RULE_SET_MODE is not off, and nil while it is. CoreRuleSet CoreRuleSetInspector // Now tells the time by which requests are counted for the rate // limits, bans are made and run out, and GeoJS's answers are kept, @@ -174,40 +173,6 @@ func newHandler(params Params) *handler { } } -// NewCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL, -// without the rules SWW_WAF_CORE_RULE_SET_DISABLED_RULES switches off, reading bodies -// up to SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE, with the exclusions in -// SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE and the fixes of the apps in SWW_WAF_APPS, -// or nil while SWW_WAF_CORE_RULE_SET_MODE is off. An exclusions file that -// coreruleset.New refuses is an error in every mode. While the mode is off, a -// set file is loaded with the Core Rule Set only to check it. -func NewCoreRuleSet(cfg *config.WAF) (*coreruleset.CoreRuleSet, error) { - if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff && - cfg.CoreRuleSetExclusionsFile == "" { - return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error - } - - coreRuleSet, err := coreruleset.New(coreruleset.Params{ - ParanoiaLevel: cfg.CoreRuleSetParanoiaLevel, - DisabledRules: cfg.CoreRuleSetDisabledRules, - BodyInspectionSize: cfg.CoreRuleSetBodyInspectionSize, - ExclusionsFile: cfg.CoreRuleSetExclusionsFile, - Apps: cfg.Apps, - }) - if err != nil { - return nil, fmt.Errorf( - "SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE: %w", - err, - ) - } - - if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff { - return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error - } - - return coreRuleSet, nil -} - // handler is waf's handler. It holds what every request shares; what // belongs to one request is in a request. type handler struct { diff --git a/internal/waf/proxy_test.go b/internal/waf/proxy_test.go index 9d0dfe3..f5eeb27 100644 --- a/internal/waf/proxy_test.go +++ b/internal/waf/proxy_test.go @@ -20,6 +20,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/bans" "sneak.berlin/go/smallwebwaf/internal/config" "sneak.berlin/go/smallwebwaf/internal/config/configtest" + "sneak.berlin/go/smallwebwaf/internal/coreruleset" "sneak.berlin/go/smallwebwaf/internal/geojs" "sneak.berlin/go/smallwebwaf/internal/lookup" "sneak.berlin/go/smallwebwaf/internal/metrics" @@ -504,24 +505,20 @@ func newWAFWith( } } - coreRuleSet, err := waf.NewCoreRuleSet(cfg) - if err != nil { - t.Fatalf("Core Rule Set: %v", err) - } - params := waf.Params{ - Config: cfg, - RequestLog: out, - ProcessLog: processLog, - LookupFile: lookupFile, - CoreRuleSet: coreRuleSet, - Now: now, - Rules: ruleFiles, - Alerts: alertQueue, + Config: cfg, + RequestLog: out, + ProcessLog: processLog, + LookupFile: lookupFile, + Now: now, + Rules: ruleFiles, + Alerts: alertQueue, // The tests of the limit set it back, and hold the Core Rule Set // with a stand-in. BesideLookupTimeLimit: time.Hour, } + + addCoreRuleSet(t, ¶ms) addCounters(¶ms) addOutsideSources(¶ms, geojsURL, abuseIPDBCheckURL) @@ -530,6 +527,30 @@ func newWAFWith( return server, out, alertQueue } +// addCoreRuleSet adds to params the Core Rule Set, with the settings for it, +// while SWW_WAF_CORE_RULE_SET_MODE is not off. +func addCoreRuleSet(t *testing.T, params *waf.Params) { + t.Helper() + + cfg := params.Config + if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff { + return + } + + coreRuleSet, err := coreruleset.New(coreruleset.Params{ + ParanoiaLevel: cfg.CoreRuleSetParanoiaLevel, + DisabledRules: cfg.CoreRuleSetDisabledRules, + BodyInspectionSize: cfg.CoreRuleSetBodyInspectionSize, + ExclusionsFile: cfg.CoreRuleSetExclusionsFile, + Apps: cfg.Apps, + }) + if err != nil { + t.Fatalf("Core Rule Set: %v", err) + } + + params.CoreRuleSet = coreRuleSet +} + // addCounters adds to params the metrics, the table of clients, the ledger // and the anomaly counters, as sww builds them from params' settings. The // metrics read the ledger, the table of clients and the rules. -- 2.54.0