diff --git a/internal/config/waf.go b/internal/config/waf.go index 0691438..9009433 100644 --- a/internal/config/waf.go +++ b/internal/config/waf.go @@ -27,6 +27,19 @@ import ( "sneak.berlin/go/smallwebwaf/internal/remotelog" ) +// The names of the settings that waf writes into log lines and ban notes, +// as the setting that refused a request or lowered a client's limits. +const ( + ExclusivelyAllowedCountriesSetting = "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES" + TagLimitPercentSetting = "SWW_WAF_TAG_LIMIT_PERCENT" + ASNBytesLimitPercentSetting = "SWW_WAF_ASN_BYTES_LIMIT_PERCENT" + CountryBytesLimitPercentSetting = "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT" + UnknownCountryLimitPercentSetting = "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT" + ASNLimitPercentURLSetting = "SWW_WAF_ASN_LIMIT_PERCENT_URL" + BlocklistActionSetting = "SWW_WAF_BLOCKLIST_ACTION" + DNSBLAbuseIPDBActionSetting = "SWW_WAF_DNSBL_ABUSEIPDB_ACTION" +) + // WAF is the settings of `sww waf`. A time or size limit of zero is off. type WAF struct { // ListenAddr is where waf listens (SWW_WAF_LISTEN_ADDR). @@ -569,24 +582,24 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) { "800", ), ExclusivelyAllowedCountries: env.countries( - "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", ""), + ExclusivelyAllowedCountriesSetting, ""), TagLists: env.tagLists("SWW_WAF_TAG_LISTS"), TagDeny: env.tags("SWW_WAF_TAG_DENY"), - TagLimitPercent: env.tagPercents("SWW_WAF_TAG_LIMIT_PERCENT"), + TagLimitPercent: env.tagPercents(TagLimitPercentSetting), RouteWatchURL: env.routeWatchURL("SWW_WAF_ROUTEWATCH_URL"), ASNBytesLimitPercent: env.percents( - "SWW_WAF_ASN_BYTES_LIMIT_PERCENT", + ASNBytesLimitPercentSetting, ParseASN, ), CountryBytesLimitPercent: env.percents( - "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT", + CountryBytesLimitPercentSetting, parseCountry, ), UnknownCountryLimitPercent: env.percent( - "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT", + UnknownCountryLimitPercentSetting, "100", ), - ASNLimitPercentURL: env.listURL("SWW_WAF_ASN_LIMIT_PERCENT_URL"), + ASNLimitPercentURL: env.listURL(ASNLimitPercentURLSetting), BlocklistURLs: env.listURLs("SWW_WAF_BLOCKLIST_URLS"), BlocklistRefresh: env.refresh("SWW_WAF_BLOCKLIST_REFRESH", "24h"), DNSBLZones: env.zones("SWW_WAF_DNSBL_ZONES"), @@ -760,9 +773,9 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) { cfg.AddLookupHeaders = env.boolean("SWW_WAF_ADD_LOOKUP_HEADERS", strconv.FormatBool(cfg.LookupSource != off)) cfg.BlocklistAction, cfg.BlocklistLimitPercent = env.action( - "SWW_WAF_BLOCKLIST_ACTION", "deny") + BlocklistActionSetting, "deny") cfg.DNSBLAbuseIPDBAction, cfg.DNSBLAbuseIPDBLimitPercent = env.action( - "SWW_WAF_DNSBL_ABUSEIPDB_ACTION", "limit:25") + DNSBLAbuseIPDBActionSetting, "limit:25") env.checkInstanceNameForNtfy(cfg.InstanceName, cfg.AlertNtfyURL != nil) env.checkLookupDBPath(cfg) @@ -1159,7 +1172,7 @@ func (e *environment) checkLookupDBPath(cfg *WAF) { func (e *environment) checkCountriesAndLookups(cfg *WAF) { for _, country := range cfg.ExclusivelyAllowedCountries { if slices.Contains(cfg.TagDeny, CountryTagPrefix+country) { - e.check("SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", + e.check(ExclusivelyAllowedCountriesSetting, fmt.Errorf("%q %w", country, errOnBothLists)) } } @@ -1178,15 +1191,15 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) { name string set bool }{ - {"SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", len(cfg.ExclusivelyAllowedCountries) > 0}, + {ExclusivelyAllowedCountriesSetting, len(cfg.ExclusivelyAllowedCountries) > 0}, {"SWW_WAF_ADD_LOOKUP_HEADERS", cfg.AddLookupHeaders}, {"SWW_WAF_TAG_DENY", slices.ContainsFunc(cfg.TagDeny, onlyLookupGives)}, - {"SWW_WAF_TAG_LIMIT_PERCENT", slices.ContainsFunc( + {TagLimitPercentSetting, slices.ContainsFunc( slices.Collect(maps.Keys(cfg.TagLimitPercent)), onlyLookupGives)}, - {"SWW_WAF_ASN_BYTES_LIMIT_PERCENT", len(cfg.ASNBytesLimitPercent) > 0}, - {"SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT", len(cfg.CountryBytesLimitPercent) > 0}, - {"SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT", cfg.UnknownCountryLimitPercent < 100}, - {"SWW_WAF_ASN_LIMIT_PERCENT_URL", cfg.ASNLimitPercentURL != ""}, + {ASNBytesLimitPercentSetting, len(cfg.ASNBytesLimitPercent) > 0}, + {CountryBytesLimitPercentSetting, len(cfg.CountryBytesLimitPercent) > 0}, + {UnknownCountryLimitPercentSetting, cfg.UnknownCountryLimitPercent < 100}, + {ASNLimitPercentURLSetting, cfg.ASNLimitPercentURL != ""}, {"SWW_WAF_ANOMALY_ASN_REQUESTS_PER_MINUTE", cfg.AnomalyASN.RequestsPerMinute > 0}, {"SWW_WAF_ANOMALY_ASN_REQUESTS_PER_HOUR", cfg.AnomalyASN.RequestsPerHour > 0}, {"SWW_WAF_ANOMALY_ASN_BYTES_PER_MINUTE", cfg.AnomalyASN.BytesPerMinute > 0}, @@ -1206,7 +1219,7 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) { // blocklist too: the file at a URL is fetched as one list or the other. func (e *environment) checkASNLimitPercentURL(cfg *WAF) { if slices.Contains(cfg.BlocklistURLs, cfg.ASNLimitPercentURL) { - e.check("SWW_WAF_ASN_LIMIT_PERCENT_URL", + e.check(ASNLimitPercentURLSetting, fmt.Errorf("%q %w", cfg.ASNLimitPercentURL, errInBlocklistURLs)) } } @@ -1233,7 +1246,7 @@ func (e *environment) checkTags(cfg *WAF) { } for tag := range cfg.TagLimitPercent { - e.check("SWW_WAF_TAG_LIMIT_PERCENT", checkTag(cfg, tag)) + e.check(TagLimitPercentSetting, checkTag(cfg, tag)) } } diff --git a/internal/waf/biased.go b/internal/waf/biased.go index 3e729df..82a9b8f 100644 --- a/internal/waf/biased.go +++ b/internal/waf/biased.go @@ -53,20 +53,20 @@ func (rq *request) limitPercentages() (percentage, percentage) { if country == "" && !rq.skippedLookupWait { unknown = percentage{ cfg.UnknownCountryLimitPercent, - "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT", + config.UnknownCountryLimitPercentSetting, } } fetched := percentage{percent: whole} if percent, listed := rq.h.lists.ASNLimitPercent(asn); listed { - fetched = percentage{percent, "SWW_WAF_ASN_LIMIT_PERCENT_URL"} + fetched = percentage{percent, config.ASNLimitPercentURLSetting} } blocklisted := percentage{percent: whole} if rq.blocklisted && cfg.BlocklistAction == "limit" { blocklisted = percentage{ cfg.BlocklistLimitPercent, - "SWW_WAF_BLOCKLIST_ACTION", + config.BlocklistActionSetting, } } @@ -75,11 +75,11 @@ func (rq *request) limitPercentages() (percentage, percentage) { cfg.DNSBLAbuseIPDBAction == "limit" { dnsblAbuseIPDBListed = percentage{ cfg.DNSBLAbuseIPDBLimitPercent, - "SWW_WAF_DNSBL_ABUSEIPDB_ACTION", + config.DNSBLAbuseIPDBActionSetting, } } - const tagSetting = "SWW_WAF_TAG_LIMIT_PERCENT" + const tagSetting = config.TagLimitPercentSetting asnRequests := lowest( given(cfg.TagLimitPercent, asnTag(asn), tagSetting), @@ -107,7 +107,7 @@ func (rq *request) limitPercentages() (percentage, percentage) { asnBytes = given( cfg.ASNBytesLimitPercent, asn, - "SWW_WAF_ASN_BYTES_LIMIT_PERCENT", + config.ASNBytesLimitPercentSetting, ) } @@ -115,7 +115,7 @@ func (rq *request) limitPercentages() (percentage, percentage) { countryBytes = given( cfg.CountryBytesLimitPercent, country, - "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT", + config.CountryBytesLimitPercentSetting, ) } diff --git a/internal/waf/countries.go b/internal/waf/countries.go index 7bae8d5..a32172e 100644 --- a/internal/waf/countries.go +++ b/internal/waf/countries.go @@ -7,6 +7,7 @@ import ( "slices" "time" + "sneak.berlin/go/smallwebwaf/internal/config" "sneak.berlin/go/smallwebwaf/internal/requestlog" ) @@ -33,7 +34,8 @@ func (countries) check( return &verdict{ action: requestlog.ActionCountryDenied, figures: map[string]any{ - "country": country, "setting": "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", + "country": country, + "setting": config.ExclusivelyAllowedCountriesSetting, }, } }