Default the netblock and AS number anomaly thresholds (closes #410)
check / check (push) Failing after 1m37s

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 is contained in:
sneak committed 2026-11-20 05:05:29 +00:00
1 parent 8219664ad1
commit b8abb87738
7 files changed
+149 -32

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),
}
}