Default the netblock and AS number anomaly thresholds (closes #410)
check / check (push) Successful in 6m7s

The alert-only thresholds per netblock and per AS number now have
defaults that alert on obvious abuse alone: per netblock 1200 and
15000 requests a minute and an hour, 5G and 50G a minute and an hour;
per AS number five times those. The AS number ones default to off
while SWW_WAF_LOOKUP_SOURCE is off. The other scopes stay off. The
README says to tune them to the site's traffic.

Model: opus-5-5
This commit was merged in pull request #484.
This commit is contained in:
clawbot committed 2026-11-20 06:42:25 +01:00
1 parent 8219664ad1
commit 23eaa869dd
8 files changed
+152 -33

No files matched your search

+51 -13
View File
@@ -313,7 +313,9 @@ type WAF struct {
// per netblock around a client (SWW_WAF_ANOMALY_NET_*), per AS number
// (SWW_WAF_ANOMALY_ASN_*), for the whole service (SWW_WAF_ANOMALY_TOTAL_*)
// and per watched netblock (SWW_WAF_ANOMALY_WATCHED_NET_*), each 0 while
// it is off. AnomalyNetV4Prefix and AnomalyNetV6Prefix are the lengths
// it is off. Only the thresholds per netblock and per AS number are on by
// default, those per AS number only while clients are looked up.
// AnomalyNetV4Prefix and AnomalyNetV6Prefix are the lengths
// of the netblock around a client (SWW_WAF_ANOMALY_NET_V4_PREFIX and
// SWW_WAF_ANOMALY_NET_V6_PREFIX), and AnomalyWatchedNets the watched
// netblocks (SWW_WAF_ANOMALY_WATCHED_NETS).
@@ -555,11 +557,6 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
"SWW_WAF_ALERT_MAX_PER_HOUR",
"60",
),
AnomalyClient: env.thresholds("SWW_WAF_ANOMALY_CLIENT_"),
AnomalyNet: env.thresholds("SWW_WAF_ANOMALY_NET_"),
AnomalyASN: env.thresholds("SWW_WAF_ANOMALY_ASN_"),
AnomalyTotal: env.thresholds("SWW_WAF_ANOMALY_TOTAL_"),
AnomalyWatchedNet: env.thresholds("SWW_WAF_ANOMALY_WATCHED_NET_"),
AnomalyNetV4Prefix: env.v4Prefix(
"SWW_WAF_ANOMALY_NET_V4_PREFIX",
"24",
@@ -578,6 +575,7 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
// The app gets the headers by default, unless no client is looked up.
cfg.AddLookupHeaders = env.boolean("SWW_WAF_ADD_LOOKUP_HEADERS",
strconv.FormatBool(cfg.LookupSource != LookupSourceOff))
env.anomalyThresholds(cfg)
cfg.BlocklistAction, cfg.BlocklistLimitPercent = env.action(
BlocklistActionSetting, string(ReputationActionDeny))
cfg.DNSBLAbuseIPDBAction, cfg.DNSBLAbuseIPDBLimitPercent = env.action(
@@ -2693,15 +2691,55 @@ func (e *environment) duration(name, defaultValue string) time.Duration {
return duration
}
// thresholdDefaults are the defaults of a scope's four anomaly thresholds,
// each a count, a size or off.
type thresholdDefaults struct {
requestsPerMinute, requestsPerHour, bytesPerMinute, bytesPerHour string
}
// anomalyThresholds reads the anomaly thresholds of every scope into cfg.
// Only those per netblock and per AS number are on by default, high
// enough to alert on obvious abuse alone. Those per netblock count the
// requests of ten clients at the default per-client rate limits, and the
// bytes of one client at its default byte limit per hour, a tenth of
// that in a minute. Those per AS number are five times those per
// netblock. They are off by default while no client is looked up, since
// no request then has an AS number.
func (e *environment) anomalyThresholds(cfg *WAF) {
allOff := thresholdDefaults{off, off, off, off}
asnDefaults := thresholdDefaults{
requestsPerMinute: "6000", requestsPerHour: "75000",
bytesPerMinute: "25G", bytesPerHour: "250G",
}
if cfg.LookupSource == LookupSourceOff {
asnDefaults = allOff
}
cfg.AnomalyClient = e.thresholds("SWW_WAF_ANOMALY_CLIENT_", allOff)
cfg.AnomalyNet = e.thresholds("SWW_WAF_ANOMALY_NET_", thresholdDefaults{
requestsPerMinute: "1200", requestsPerHour: "15000",
bytesPerMinute: "5G", bytesPerHour: "50G",
})
cfg.AnomalyASN = e.thresholds("SWW_WAF_ANOMALY_ASN_", asnDefaults)
cfg.AnomalyTotal = e.thresholds("SWW_WAF_ANOMALY_TOTAL_", allOff)
cfg.AnomalyWatchedNet = e.thresholds("SWW_WAF_ANOMALY_WATCHED_NET_",
allOff)
}
// thresholds reads the four anomaly thresholds whose settings' names
// start with prefix: requests and bytes per minute and per hour. Each is
// off by default.
func (e *environment) thresholds(prefix string) anomaly.Thresholds {
// start with prefix: requests and bytes per minute and per hour.
func (e *environment) thresholds(
prefix string, defaults thresholdDefaults,
) anomaly.Thresholds {
return anomaly.Thresholds{
RequestsPerMinute: e.count(prefix+"REQUESTS_PER_MINUTE", off),
RequestsPerHour: e.count(prefix+"REQUESTS_PER_HOUR", off),
BytesPerMinute: e.size(prefix+"BYTES_PER_MINUTE", off),
BytesPerHour: e.size(prefix+"BYTES_PER_HOUR", off),
RequestsPerMinute: e.count(prefix+"REQUESTS_PER_MINUTE",
defaults.requestsPerMinute),
RequestsPerHour: e.count(prefix+"REQUESTS_PER_HOUR",
defaults.requestsPerHour),
BytesPerMinute: e.size(prefix+"BYTES_PER_MINUTE",
defaults.bytesPerMinute),
BytesPerHour: e.size(prefix+"BYTES_PER_HOUR", defaults.bytesPerHour),
}
}
+40 -1
View File
@@ -178,6 +178,17 @@ func loggedAnomalyDefaults() map[string]string {
logged[name] = off
}
maps.Copy(logged, map[string]string{
anomalyNet + requestsPerMinute: "1200",
anomalyNet + requestsPerHour: "15000",
anomalyNet + bytesPerMinute: "5G",
anomalyNet + bytesPerHour: "50G",
anomalyASN + requestsPerMinute: "6000",
anomalyASN + requestsPerHour: "75000",
anomalyASN + bytesPerMinute: "25G",
anomalyASN + bytesPerHour: "250G",
})
return logged
}
@@ -1565,7 +1576,35 @@ func TestAnomalySettingsDefaults(t *testing.T) {
t.Parallel()
cfg := wafFromEnvironment(t, environment{})
wantAllOff(t, cfg)
const gibibyte = int64(1) << 30
want := map[string]anomaly.Thresholds{
anomalyClient: {},
anomalyNet: {
RequestsPerMinute: 1200, RequestsPerHour: 15000,
BytesPerMinute: 5 * gibibyte, BytesPerHour: 50 * gibibyte,
},
anomalyASN: {
RequestsPerMinute: 6000, RequestsPerHour: 75000,
BytesPerMinute: 25 * gibibyte, BytesPerHour: 250 * gibibyte,
},
anomalyTotal: {}, anomalyWatchedNet: {},
}
if got := thresholdsByScope(cfg); !maps.Equal(got, want) {
t.Errorf("thresholds by scope\n%+v\nwant\n%+v", got, want)
}
// With no client looked up, the thresholds per AS number are off.
want[anomalyASN] = anomaly.Thresholds{}
got := thresholdsByScope(
wafFromEnvironment(t, environment{lookupSource: off}))
if !maps.Equal(got, want) {
t.Errorf("with %s off, thresholds by scope\n%+v\nwant\n%+v",
lookupSource, got, want)
}
if cfg.AnomalyNetV4Prefix != 24 || cfg.AnomalyNetV6Prefix != 48 ||
len(cfg.AnomalyWatchedNets) != 0 {
+16
View File
@@ -32,6 +32,22 @@ const (
bytesPerHour = "BYTES_PER_HOUR"
)
// netAndASNThresholdsOff returns the anomaly thresholds per netblock and
// per AS number, the ones on by default, each set to off.
func netAndASNThresholdsOff() map[string]string {
settings := map[string]string{}
for _, scope := range []string{anomalyNet, anomalyASN} {
for _, end := range []string{
requestsPerMinute, requestsPerHour, bytesPerMinute, bytesPerHour,
} {
settings[scope+end] = off
}
}
return settings
}
// The other anomaly settings.
const (
anomalyNetV4Prefix = "SWW_WAF_ANOMALY_NET_V4_PREFIX"
+4 -1
View File
@@ -456,7 +456,9 @@ func (w *wafAndParts) banAbuseIPDBBlacklist(
// at abuseIPDBURL while env sets SWW_WAF_ABUSEIPDB_KEY, and its blacklist is
// fetched from abuseIPDBBlacklistURL.
// SWW_WAF_CORE_RULE_SET_MODE is off unless env sets it, so that only the tests of the
// Core Rule Set have their requests inspected by it.
// Core Rule Set have their requests inspected by it. Likewise the anomaly thresholds
// per netblock and per AS number are off unless env sets them, so that only the tests
// of the anomaly thresholds count requests.
func newWAF(
t *testing.T,
geojsURL string,
@@ -486,6 +488,7 @@ func newWAFWith(
settings[lookupSource] = off
}
maps.Copy(settings, netAndASNThresholdsOff())
maps.Copy(settings, env)
cfg, err := config.WAFFromEnvironment(configtest.Viper(settings))
+1 -1
View File
@@ -405,7 +405,7 @@ func (rq *request) addToHistory() {
// SWW_WAF_RATE_LIMIT_EXEMPT_NETS, and one for a path in
// SWW_WAF_RATE_LIMIT_EXEMPT_PATHS are counted too. It is counted for its
// client's AS number when answerAtTheEnd gives one. With every anomaly
// threshold off, the default, it does nothing.
// threshold off, it does nothing.
func (rq *request) countAnomalies() {
if !anomalyThresholdsSet(rq.h.config) {
return