From 9afbd1b683217618731dc7eb942d24449862a26a Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Fri, 20 Nov 2026 01:44:08 +0100 Subject: [PATCH] config: each waf setting's code in one place (closes #449) internal/config/waf.go is reordered by setting, in the order of the settings sections of SPEC and docs/settings.md. The struct WAF and WAFFromEnvironment stay first. Each setting's reader, parser, errors and check sit together. A helper several settings share sits before the first of them. Moves only. The one error block and the one block of constants are split into a block beside each user, so gofmt re-aligns the = of some of those lines. Model: opus-5-5 --- internal/config/waf.go | 3065 +++++++++++++++++++++------------------- 1 file changed, 1621 insertions(+), 1444 deletions(-) diff --git a/internal/config/waf.go b/internal/config/waf.go index bd721e8..fd77328 100644 --- a/internal/config/waf.go +++ b/internal/config/waf.go @@ -331,246 +331,6 @@ type WAF struct { settings []slog.Attr } -// BytesCounted is which body bytes count toward the byte limits and the -// byte anomaly thresholds, a value of SWW_WAF_BYTES_COUNTED. -type BytesCounted string - -// The values of SWW_WAF_BYTES_COUNTED. -const ( - // BytesCountedResponse counts the response's body bytes. - BytesCountedResponse BytesCounted = "response" - // BytesCountedRequest counts the request's body bytes. - BytesCountedRequest BytesCounted = "request" - // BytesCountedBoth counts the body bytes of both. - BytesCountedBoth BytesCounted = "both" -) - -// LookupSource is where each client's AS number and country are looked up, -// a value of SWW_WAF_LOOKUP_SOURCE. -type LookupSource string - -// The values of SWW_WAF_LOOKUP_SOURCE. -const ( - // LookupSourceGeoJS asks GeoJS. - LookupSourceGeoJS LookupSource = "geojs" - // LookupSourceFile looks clients up in the lookup database, the file - // SWW_WAF_LOOKUP_DB_PATH names. - LookupSourceFile LookupSource = "file" - // LookupSourceOff looks no client up. - LookupSourceOff LookupSource = "off" -) - -// ReputationAction is what is done with a client a reputation source -// lists, a value of SWW_WAF_BLOCKLIST_ACTION and of -// SWW_WAF_DNSBL_ABUSEIPDB_ACTION. -type ReputationAction string - -// The values of SWW_WAF_BLOCKLIST_ACTION and SWW_WAF_DNSBL_ABUSEIPDB_ACTION. -const ( - // ReputationActionDeny refuses the client's requests. - ReputationActionDeny ReputationAction = "deny" - // ReputationActionLimit gives the client a percentage of every limit, - // written limit: in the setting. - ReputationActionLimit ReputationAction = "limit" - // ReputationActionLog only notes the hit in the log line and raises an - // alert. - ReputationActionLog ReputationAction = "log" -) - -// CoreRuleSetMode is what the Core Rule Set does, a value of -// SWW_WAF_CORE_RULE_SET_MODE. -type CoreRuleSetMode string - -// The values of SWW_WAF_CORE_RULE_SET_MODE. -const ( - // CoreRuleSetModeOff runs no request through the Core Rule Set. - CoreRuleSetModeOff CoreRuleSetMode = "off" - // CoreRuleSetModeDetect logs and alerts a match, and refuses nothing. - CoreRuleSetModeDetect CoreRuleSetMode = "detect" - // CoreRuleSetModeBlock refuses a match with 403. - CoreRuleSetModeBlock CoreRuleSetMode = "block" -) - -const ( - day = 24 * time.Hour - ipv4Bits = 32 - ipv6Bits = 128 - // minIPv6GroupPrefix is the shortest SWW_WAF_IPV6_GROUP_PREFIX, the - // netblock a provider is usually given: a shorter one would make one - // client of the customers of several providers. - minIPv6GroupPrefix = 32 - // minAbuseIPDBThreshold is the lowest score AbuseIPDB's blacklist can - // be cut at. - minAbuseIPDBThreshold = 25 - // minTokenLength is the fewest characters a token may have. - minTokenLength = 32 - // maxParanoiaLevel is the Core Rule Set's highest paranoia level. - maxParanoiaLevel = 4 - // firstSetupRuleID to lastSetupRuleID are the ids the Core Rule Set - // keeps for the rules that set it up, which sww's own rules - // have too (see internal/coreruleset). Switching one off would undo a change - // that no setting undoes. - firstSetupRuleID = 900000 - lastSetupRuleID = 900999 - // masked is what the log shows for a token that is set, and in place of - // a secret in another setting. - masked = "********" -) - -var ( - errNotDuration = errors.New( - "is not a duration such as 90s, 15m or 7d, or off") - errNotCount = errors.New( - "is not a whole number of requests such as 1000, or off") - errNotCountry = errors.New( - "is not a two-letter country code such as de or kp") - errNotHeaderName = errors.New( - "is not a header name such as accept-language") - errHeaderTakenOut = errors.New( - "is taken out of every request by Go's HTTP server, so it can never " + - "be logged", - ) - errOnBothLists = errors.New("is in SWW_WAF_TAG_DENY too") - errNotLookupSource = errors.New("is not geojs, file or off") - errNeedsLookups = errors.New("it needs each client looked up") - errNeedsDBPath = errors.New( - "it names the file to look clients up in", - ) - errDBPathUnused = errors.New("only file reads it") - errOver8K = errors.New( - "is more than 8K, the most the Core Rule Set can read in time") - errNotDurationAboveZero = errors.New( - "is not a duration above zero, such as 1h or 7d") - errNotNumberAboveZero = errors.New( - "is not a whole number above zero, such as 5000") - errNotBanResponse = errors.New("is not 403, 429 or close") - errNotV4Prefix = errors.New( - "is not the length of an IPv4 netblock, from 0 to 32, such as 24") - errNotV6Prefix = errors.New( - "is not the length of an IPv6 netblock, from 0 to 128, such as 48") - errNotIPv6GroupPrefix = errors.New( - "is not the length of an IPv6 netblock, from 32 to 128, such as 64") - errNotLogLevel = errors.New("is not debug, info, warn or error") - errNotNamedNetblock = errors.New( - "is not a name, = and a netblock, such as office=203.0.113.0/24") - errNotAbsolutePath = errors.New( - "is not an absolute path, such as /var/lib/sww") - errShortToken = errors.New("is shorter than 32 characters") - errNotMode = errors.New("is not enforce or observe") - errNotBytesCounted = errors.New("is not response, request or both") - errNotPathPrefix = errors.New( - "is not a path prefix starting with /, such as /assets/") - errNotTrapPath = errors.New( - "is not a path starting with / and without a ?, such as /wp-login.php", - ) - errNotAppsEntry = errors.New( - "is not a hostname, : and a list of apps, such as git.example.com:gitea", - ) - errNotHostname = errors.New( - "is not a DNS name such as git.example.com", - ) - errNotApp = errors.New( - "is not an app sww has fixes for; the apps are", - ) - errNotCoreRuleSetMode = errors.New("is not off, detect or block") - errNotParanoiaLevel = errors.New( - "is not a paranoia level, from 1 to 4", - ) - errNotRuleID = errors.New( - "is not the id of a Core Rule Set rule, a whole number such as 942100", - ) - errSetupRuleID = errors.New( - "is from 900000 to 900999, the ids of the rules that set the Core Rule Set " + - "up and of sww's own, which cannot be switched off", - ) - errNotBoolean = errors.New("is not true or false") - errNotLogRemoteURL = errors.New( - "is not syslog+udp, syslog+tcp or syslog+tls with a host and a port, " + - "and nothing more, such as syslog+tls://logs.example:6514", - ) - errNoCertificate = errors.New("holds no PEM certificate") - errNotFacility = errors.New( - "is not a syslog facility such as local0 or daemon", - ) - errNotAppName = errors.New( - "is not 1 to 48 printable ASCII characters without a space, such as blog", - ) - errNotWebhookURL = errors.New( - "is not an http or https URL without a user or a fragment, " + - "such as https://alerts.example/sww") - errNotWebhookHeader = errors.New( - "is not a header name followed by : and the header's value, " + - "such as Authorization:Bearer ") - errControlCharacter = errors.New( - "holds a control character, such as the carriage return of a Windows line end", - ) - errNotAlertEvent = errors.New( - "is not ban, permanent_ban, core_rule_set_match, anomaly, reputation_hit, " + - "source_failure or file_error", - ) - errNotNumberOrOff = errors.New( - "is not a whole number above zero, such as 60, or off", - ) - errNotUTF8 = errors.New("is not valid UTF-8") - errNotPercentItem = errors.New( - "is not a code, : and a percentage, such as AS64496:50 or cn:25") - errNotAbuseIPDBThreshold = errors.New( - "is not a whole number from 25 to 100, the scores AbuseIPDB's " + - "blacklist can be cut at", - ) - errNotWholeUnits = errors.New( - "is not a whole number above zero, such as 30, that fits a duration", - ) - errListedTwice = errors.New("is listed twice") - errNotListURL = errors.New( - "is not an http or https URL without a user or a fragment, " + - "such as https://www.spamhaus.org/drop/drop.txt") - errInBlocklistURLs = errors.New("is in SWW_WAF_BLOCKLIST_URLS too") - errNotAnHourOrMore = errors.New( - "is not a duration of 1h or more, such as 24h", - ) - errNotAction = errors.New( - "is not deny, limit: such as limit:25, or log") - errNotZone = errors.New( - "is not a DNS zone such as dnsbl.dronebl.org", - ) - errZoneTooLong = errors.New( - "is longer than 189 characters, too long for the " + - "names IPv6 clients are asked about by", - ) - errNotResolver = errors.New( - "is not an IP address with an optional port, " + - "such as 192.0.2.53 or [2001:db8::53]:5353", - ) - errNotRouteWatchURL = errors.New( - "is not an http or https URL without a user or a fragment, " + - "such as https://routewatch.example") - errFetchedOtherwise = errors.New( - "is in SWW_WAF_BLOCKLIST_URLS or is SWW_WAF_ASN_LIMIT_PERCENT_URL too", - ) - errNotTagList = errors.New( - "is not a name, = and an http or https URL, such as " + - "torexit=https://example.com/tor.txt") - errNotTagPercentItem = errors.New( - "is not a tag, : and a percentage, such as vpn:50 or asn=14061:25") - errNotTagPercent = errors.New("is not a percentage from 1 to 99") - errNotASNTag = errors.New( - "is not asn= and a number, such as asn=14061", - ) - errNotPrefixTag = errors.New( - "is not prefix= and a netblock, such as prefix=192.0.2.0/24") - errExploitTag = errors.New( - "cannot lower limits: the rate limits count a request before its " + - "rules are matched") - errNoRequestGetsTag = errors.New( - "is a tag no request can get: it is not a name in SWW_WAF_TAG_LISTS, an " + - "asn= or country= tag, a dnsbl= tag of a zone in SWW_WAF_DNSBL_ZONES, " + - "a prefix= tag with SWW_WAF_ROUTEWATCH_URL set, abuseipdb with " + - "SWW_WAF_ABUSEIPDB_KEY set, or exploitsignature with rule files or the " + - "Core Rule Set on", - ) -) - // WAFFromEnvironment reads the settings through v, normally the viper // NewViper returns, bound to the process's environment. A setting may // instead be given as a file: the variable named by the setting's name @@ -839,6 +599,12 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) { return cfg, nil } +// LogValue makes a WAF log as each setting's name with the value it was +// given, or its default. +func (c *WAF) LogValue() slog.Value { + return slog.GroupValue(c.settings...) +} + // WAFListenAddr reads only SWW_WAF_LISTEN_ADDR, which may be given as a // file, as WAFFromEnvironment does. The health check in waf's container needs // no other setting, so it reads no other, nor a file that another names. @@ -849,6 +615,26 @@ func WAFListenAddr(v *viper.Viper) (string, error) { return listenAddr, env.err } +var ( + errNotUTF8 = errors.New("is not valid UTF-8") +) + +// instanceName reads SWW_WAF_INSTANCE_NAME, by default the host's name. It +// must be valid UTF-8: the metrics library panics on a label that is not. +func (e *environment) instanceName() string { + hostname, _ := os.Hostname() // "" when the host has no name to give + + value := e.value("SWW_WAF_INSTANCE_NAME", hostname) + if !utf8.ValidString(value) { + e.check( + "SWW_WAF_INSTANCE_NAME", + fmt.Errorf("%q %w", value, errNotUTF8), + ) + } + + return value +} + // InstanceName reads only SWW_WAF_INSTANCE_NAME, which may be given as a // file, as WAFFromEnvironment does, so that the line saying a setting is // invalid carries it too. A file that cannot be read gives the default @@ -859,12 +645,32 @@ func InstanceName(v *viper.Viper) string { return env.instanceName() } -// LogValue makes a WAF log as each setting's name with the value it was -// given, or its default. -func (c *WAF) LogValue() slog.Value { - return slog.GroupValue(c.settings...) +var ( + errControlCharacter = errors.New( + "holds a control character, such as the carriage return of a Windows line end", + ) +) + +// checkInstanceNameForNtfy refuses an instance name that holds a control +// character while ntfySet, SWW_WAF_ALERT_NTFY_URL being set: ntfy is sent +// the instance name in a header, which cannot hold one. +func (e *environment) checkInstanceNameForNtfy( + instanceName string, + ntfySet bool, +) { + if ntfySet && strings.ContainsFunc(instanceName, unicode.IsControl) { + e.check("SWW_WAF_INSTANCE_NAME", fmt.Errorf( + "%q %w, and is sent to ntfy in a header while SWW_WAF_ALERT_NTFY_URL is set", + instanceName, + errControlCharacter, + )) + } } +var ( + errNotMode = errors.New("is not enforce or observe") +) + // observe reads the setting that is the mode, enforce or observe, and // reports whether it is observe. func (e *environment) observe(name, defaultValue string) bool { @@ -876,37 +682,420 @@ func (e *environment) observe(name, defaultValue string) bool { return mode == "observe" } -// boolean reads a setting that is true or false. -func (e *environment) boolean(name, defaultValue string) bool { +const ( + ipv6Bits = 128 + // minIPv6GroupPrefix is the shortest SWW_WAF_IPV6_GROUP_PREFIX, the + // netblock a provider is usually given: a shorter one would make one + // client of the customers of several providers. + minIPv6GroupPrefix = 32 +) + +var ( + errNotIPv6GroupPrefix = errors.New( + "is not the length of an IPv6 netblock, from 32 to 128, such as 64") +) + +// ipv6GroupPrefix reads the setting that is the length of the IPv6 +// netblock that is one client, from minIPv6GroupPrefix to 128. +func (e *environment) ipv6GroupPrefix(name, defaultValue string) int { value := e.value(name, defaultValue) - if value != "true" && value != "false" { - e.check(name, fmt.Errorf("%q %w", value, errNotBoolean)) + + length, err := strconv.Atoi(value) + if err != nil || length < minIPv6GroupPrefix || length > ipv6Bits { + e.check(name, fmt.Errorf("%q %w", value, errNotIPv6GroupPrefix)) } - return value == "true" + return length } -// duration reads a setting that is a duration. -func (e *environment) duration(name, defaultValue string) time.Duration { - duration, err := parseDuration(e.value(name, defaultValue)) +var ( + errNotNumberAboveZero = errors.New( + "is not a whole number above zero, such as 5000") +) + +// numberNotOff reads a setting that is a whole number above zero, which +// cannot be off. +func (e *environment) numberNotOff(name, defaultValue string) int { + number, err := parseNumberNotOff(e.value(name, defaultValue)) + e.check(name, err) + + return number +} + +// parseNumberNotOff reads a whole number above zero. +func parseNumberNotOff(value string) (int, error) { + n, err := strconv.Atoi(value) + if err != nil || n <= 0 { + return 0, fmt.Errorf("%q %w", value, errNotNumberAboveZero) + } + + return n, nil +} + +const ( + // minTokenLength is the fewest characters a token may have. + minTokenLength = 32 + // masked is what the log shows for a token that is set, and in place of + // a secret in another setting. + masked = "********" +) + +var ( + errShortToken = errors.New("is shorter than 32 characters") +) + +// token reads a setting that is a bearer token. Unset, it is "", which +// switches off what it guards; set, it must be at least minTokenLength +// characters. Neither the log nor an error shows its value. +func (e *environment) token(name string) string { + value, set := e.lookup(name) + if !set { + e.settings = append(e.settings, slog.String(name, "")) + + return "" + } + + e.settings = append(e.settings, slog.String(name, masked)) + + if utf8.RuneCountInString(value) < minTokenLength { + e.check(name, errShortToken) + } + + return value +} + +var ( + errNotAbsolutePath = errors.New( + "is not an absolute path, such as /var/lib/sww") +) + +// absolutePath reads a setting that is an absolute path. +func (e *environment) absolutePath(name, defaultValue string) string { + path := e.value(name, defaultValue) + if !filepath.IsAbs(path) { + e.check(name, fmt.Errorf("%q %w", path, errNotAbsolutePath)) + } + + return path +} + +const ( + day = 24 * time.Hour +) + +var ( + errNotDuration = errors.New( + "is not a duration such as 90s, 15m or 7d, or off") + errNotDurationAboveZero = errors.New( + "is not a duration above zero, such as 1h or 7d") +) + +// parseDuration reads a duration in Go's syntax, such as 90s or 15m, a +// whole number of days such as 7d, or off. +func parseDuration(value string) (time.Duration, error) { + if value == off { + return 0, nil + } + + duration, err := durationOrDays(value) + if err != nil { + return 0, fmt.Errorf("%q %w", value, errNotDuration) + } + + if duration <= 0 { + return 0, fmt.Errorf("%q %w", value, errNotPositive) + } + + return duration, nil +} + +// durationOrDays reads Go's duration syntax, or a whole number of days. +func durationOrDays(value string) (time.Duration, error) { + days, isDays := strings.CutSuffix(value, "d") + if !isDays { + return time.ParseDuration(value) + } + + n, err := strconv.ParseInt(days, 10, 64) + if err != nil || n < 0 || n > math.MaxInt64/int64(day) { + return 0, errNotDuration + } + + return time.Duration(n) * day, nil +} + +// durationNotOff reads a setting that is a duration and, unlike a +// timeout, cannot be off. +func (e *environment) durationNotOff( + name, defaultValue string, +) time.Duration { + duration, err := ParseDurationNotOff(e.value(name, defaultValue)) e.check(name, err) return duration } -// coreRuleSetBodyInspectionSize reads the setting that is the most of a -// request body the Core Rule Set reads. -func (e *environment) coreRuleSetBodyInspectionSize( - name, defaultValue string, -) int64 { - limit, err := parseCoreRuleSetBodyInspectionSize( - e.value(name, defaultValue), +// ParseDurationNotOff reads a duration above zero, as parseDuration does, +// but not off. The ban endpoint reads the duration of a ban with it too. +func ParseDurationNotOff(value string) (time.Duration, error) { + duration, err := parseDuration(value) + if err != nil || duration == 0 { + return 0, fmt.Errorf("%q %w", value, errNotDurationAboveZero) + } + + return duration, nil +} + +var ( + errNotLogLevel = errors.New("is not debug, info, warn or error") +) + +// logLevel reads the setting that is the least severe of the process's +// own messages that are written: debug, info, warn or error. +func (e *environment) logLevel(name, defaultValue string) slog.Level { + value := e.value(name, defaultValue) + + level, known := map[string]slog.Level{ + "debug": slog.LevelDebug, + "info": slog.LevelInfo, + "warn": slog.LevelWarn, + "error": slog.LevelError, + }[value] + if !known { + e.check(name, fmt.Errorf("%q %w", value, errNotLogLevel)) + } + + return level +} + +var ( + errNotHeaderName = errors.New( + "is not a header name such as accept-language") + errHeaderTakenOut = errors.New( + "is taken out of every request by Go's HTTP server, so it can never " + + "be logged", ) +) + +// headerNames reads a setting that is a list of header names, and +// returns them in lower case. +func (e *environment) headerNames(name, defaultValue string) []string { + headers, err := parseHeaderNames(e.value(name, defaultValue)) e.check(name, err) - return limit + return headers } +// parseHeaderNames reads a comma-separated list of header names in either +// case, and returns them in lower case. Host and Transfer-Encoding are +// refused: Go's HTTP server takes them out of the request's headers. +func parseHeaderNames(value string) ([]string, error) { + items, err := parseList(value) + if err != nil { + return nil, err + } + + headers := make([]string, 0, len(items)) + + for _, item := range items { + if !httpguts.ValidHeaderFieldName(item) { + return nil, fmt.Errorf("%q %w", item, errNotHeaderName) + } + + header := strings.ToLower(item) + switch header { + case "host": + return nil, fmt.Errorf( + "%q %w; the request's host is the field host", + item, + errHeaderTakenOut, + ) + case "transfer-encoding": + return nil, fmt.Errorf("%q %w", item, errHeaderTakenOut) + } + + headers = append(headers, header) + } + + return headers, nil +} + +var ( + errNotLogRemoteURL = errors.New( + "is not syslog+udp, syslog+tcp or syslog+tls with a host and a port, " + + "and nothing more, such as syslog+tls://logs.example:6514", + ) +) + +// logRemoteURL reads the setting that is where every log line is also +// sent. Unset or empty, it is nil, and nothing is sent. +func (e *environment) logRemoteURL(name string) *url.URL { + value := e.value(name, "") + if value == "" { + return nil + } + + remote, err := parseLogRemoteURL(value) + e.check(name, err) + + return remote +} + +// parseLogRemoteURL reads where every log line is also sent: +// syslog+udp, syslog+tcp or syslog+tls, a host and a port from 1 to +// 65535, and nothing else. +func parseLogRemoteURL(value string) (*url.URL, error) { + remote, err := url.Parse(value) + if err != nil { + return nil, fmt.Errorf("%q %w", value, errNotLogRemoteURL) + } + + schemes := []string{ + remotelog.SchemeUDP, + remotelog.SchemeTCP, + remotelog.SchemeTLS, + } + port, err := strconv.ParseUint(remote.Port(), 10, 16) + + onlySchemeHostAndPort := slices.Contains(schemes, remote.Scheme) && + remote.Hostname() != "" && err == nil && port != 0 && + remote.User == nil && remote.Opaque == "" && + (remote.Path == "" || remote.Path == "/") && + remote.RawQuery == "" && remote.Fragment == "" + if !onlySchemeHostAndPort { + return nil, fmt.Errorf("%q %w", value, errNotLogRemoteURL) + } + + return remote, nil +} + +var ( + errNoCertificate = errors.New("holds no PEM certificate") +) + +// certificates reads a setting that is the path of a file of PEM +// certificates. Unset or empty, it is nil. Its value names a file +// already, so, unlike the other settings, it has no _FILE form. +func (e *environment) certificates(name string) *x509.CertPool { + path, _ := readSetting(e.v, name) + e.settings = append(e.settings, slog.String(name, path)) + + if path == "" { + return nil + } + + pem, err := os.ReadFile(path) //nolint:gosec // a file the admin names + if err != nil { + e.check(name, fmt.Errorf("cannot be read: %w", err)) + + return nil + } + + pool := x509.NewCertPool() + if !pool.AppendCertsFromPEM(pem) { + e.check(name, fmt.Errorf("%q %w", path, errNoCertificate)) + + return nil + } + + return pool +} + +var ( + errNotFacility = errors.New( + "is not a syslog facility such as local0 or daemon", + ) +) + +// facility reads a setting that is a syslog facility, and returns its +// number. +func (e *environment) facility(name, defaultValue string) int { + number, err := parseFacility(e.value(name, defaultValue)) + e.check(name, err) + + return number +} + +// parseFacility reads the name of a syslog facility, and returns its +// number, as RFC 5424 numbers them. +func parseFacility(value string) (int, error) { + //nolint:mnd // the facilities' numbers in RFC 5424 + number, known := map[string]int{ + "kern": 0, "user": 1, "mail": 2, "daemon": 3, "auth": 4, "syslog": 5, + "lpr": 6, "news": 7, "uucp": 8, "cron": 9, "authpriv": 10, "ftp": 11, + "local0": 16, "local1": 17, "local2": 18, "local3": 19, + "local4": 20, "local5": 21, "local6": 22, "local7": 23, + }[value] + if !known { + return 0, fmt.Errorf("%q %w", value, errNotFacility) + } + + return number, nil +} + +var ( + errNotAppName = errors.New( + "is not 1 to 48 printable ASCII characters without a space, such as blog", + ) +) + +// appName reads the setting that is the APP-NAME of the records the log +// lines are sent in, by default the instance name. Its value is checked +// when it is set, and, while lines are sent, when it is the instance name. +func (e *environment) appName( + name, instanceName string, + sending bool, +) string { + value, set := e.lookup(name) + if !set { + value = instanceName + } + + e.settings = append(e.settings, slog.String(name, value)) + + switch { + case isAppName(value): + case set: + e.check(name, fmt.Errorf("%q %w", value, errNotAppName)) + case sending: + e.check( + name, + fmt.Errorf( + "is unset, and SWW_WAF_INSTANCE_NAME %q, its default, %w", + value, + errNotAppName, + ), + ) + } + + return value +} + +// appNameMaxLength is the most characters RFC 5424 allows in an +// APP-NAME. +const appNameMaxLength = 48 + +// isAppName reports whether value can be an APP-NAME: 1 to +// appNameMaxLength printable ASCII characters, none of them a space. +func isAppName(value string) bool { + if value == "" || len(value) > appNameMaxLength { + return false + } + + for _, char := range []byte(value) { + if char < '!' || char > '~' { + return false + } + } + + return true +} + +var ( + errNotCount = errors.New( + "is not a whole number of requests such as 1000, or off") +) + // count reads a setting that is a number of requests. func (e *environment) count(name, defaultValue string) int64 { count, err := parseCount(e.value(name, defaultValue)) @@ -915,6 +1104,72 @@ func (e *environment) count(name, defaultValue string) int64 { return count } +// parseCount reads a whole number of requests, or off. +func parseCount(value string) (int64, error) { + if value == off { + return 0, nil + } + + n, err := strconv.ParseInt(value, 10, 64) + if err != nil { + return 0, fmt.Errorf("%q %w", value, errNotCount) + } + + if n <= 0 { + return 0, fmt.Errorf("%q %w", value, errNotPositive) + } + + return n, nil +} + +var ( + errNotPathPrefix = errors.New( + "is not a path prefix starting with /, such as /assets/") +) + +// pathPrefixes reads a setting that is a list of path prefixes. +func (e *environment) pathPrefixes(name, defaultValue string) []string { + prefixes, err := parsePathPrefixes(e.value(name, defaultValue)) + e.check(name, err) + + return prefixes +} + +// parsePathPrefixes reads a comma-separated list of path prefixes, each +// starting with /. +func parsePathPrefixes(value string) ([]string, error) { + prefixes, err := parseList(value) + if err != nil { + return nil, err + } + + for _, prefix := range prefixes { + if !strings.HasPrefix(prefix, "/") { + return nil, fmt.Errorf("%q %w", prefix, errNotPathPrefix) + } + } + + return prefixes, nil +} + +// BytesCounted is which body bytes count toward the byte limits and the +// byte anomaly thresholds, a value of SWW_WAF_BYTES_COUNTED. +type BytesCounted string + +// The values of SWW_WAF_BYTES_COUNTED. +const ( + // BytesCountedResponse counts the response's body bytes. + BytesCountedResponse BytesCounted = "response" + // BytesCountedRequest counts the request's body bytes. + BytesCountedRequest BytesCounted = "request" + // BytesCountedBoth counts the body bytes of both. + BytesCountedBoth BytesCounted = "both" +) + +var ( + errNotBytesCounted = errors.New("is not response, request or both") +) + // bytesCounted reads the setting that is which body bytes count toward the // byte limits and the byte anomaly thresholds: response, request or both. func (e *environment) bytesCounted( @@ -929,262 +1184,24 @@ func (e *environment) bytesCounted( return value } -// pathPrefixes reads a setting that is a list of path prefixes. -func (e *environment) pathPrefixes(name, defaultValue string) []string { - prefixes, err := parsePathPrefixes(e.value(name, defaultValue)) - e.check(name, err) +// LookupSource is where each client's AS number and country are looked up, +// a value of SWW_WAF_LOOKUP_SOURCE. +type LookupSource string - return prefixes -} +// The values of SWW_WAF_LOOKUP_SOURCE. +const ( + // LookupSourceGeoJS asks GeoJS. + LookupSourceGeoJS LookupSource = "geojs" + // LookupSourceFile looks clients up in the lookup database, the file + // SWW_WAF_LOOKUP_DB_PATH names. + LookupSourceFile LookupSource = "file" + // LookupSourceOff looks no client up. + LookupSourceOff LookupSource = "off" +) -// apps reads the setting that is the apps each hostname serves. It is nil -// while the setting is unset, and logged as null, unlike a setting set -// empty. -func (e *environment) apps(name string) map[string][]string { - value, set := e.lookup(name) - if !set { - e.settings = append(e.settings, slog.Any(name, nil)) - - return nil - } - - e.settings = append(e.settings, slog.String(name, value)) - - apps, err := parseApps(value) - e.check(name, err) - - return apps -} - -// trapPaths reads the setting that is the list of trap paths. It is empty -// by default. -func (e *environment) trapPaths(name string) []string { - paths, err := parseTrapPaths(e.value(name, "")) - e.check(name, err) - - return paths -} - -// coreRuleSetMode reads the setting that is what the Core Rule Set does: off, -// detect or block. -func (e *environment) coreRuleSetMode( - name string, defaultValue CoreRuleSetMode, -) CoreRuleSetMode { - mode := CoreRuleSetMode(e.value(name, string(defaultValue))) - if mode != CoreRuleSetModeOff && mode != CoreRuleSetModeDetect && - mode != CoreRuleSetModeBlock { - e.check(name, fmt.Errorf("%q %w", mode, errNotCoreRuleSetMode)) - } - - return mode -} - -// paranoiaLevel reads the setting that is the Core Rule Set's paranoia -// level, from 1 to 4. -func (e *environment) paranoiaLevel(name, defaultValue string) int { - value := e.value(name, defaultValue) - - level, err := strconv.Atoi(value) - if err != nil || level < 1 || level > maxParanoiaLevel { - e.check(name, fmt.Errorf("%q %w", value, errNotParanoiaLevel)) - } - - return level -} - -// ruleIDs reads the setting that is a list of the ids of Core Rule Set -// rules. -func (e *environment) ruleIDs(name, defaultValue string) []int { - ids, err := parseRuleIDs(e.value(name, defaultValue)) - e.check(name, err) - - return ids -} - -// countries reads a setting that is a list of countries. -func (e *environment) countries(name, defaultValue string) []string { - countries, err := parseCountries(e.value(name, defaultValue)) - e.check(name, err) - - return countries -} - -// percents reads a setting that is a list of AS numbers or countries, -// which parseCode reads, each with a percentage. It is empty by default. -func (e *environment) percents( - name string, parseCode func(string) (string, error), -) map[string]int64 { - percents, err := parsePercents(e.value(name, ""), parseCode) - e.check(name, err) - - return percents -} - -// tagLists reads SWW_WAF_TAG_LISTS, which is empty by default. -func (e *environment) tagLists(name string) []reputation.TagList { - lists, err := parseTagLists(e.value(name, "")) - e.check(name, err) - - return lists -} - -// tags reads a setting that is a list of tags. It is empty by default. -func (e *environment) tags(name string) []string { - tags, err := parseTags(e.value(name, "")) - e.check(name, err) - - return tags -} - -// tagPercents reads a setting that is a list of tags, each with a -// percentage. It is empty by default. -func (e *environment) tagPercents(name string) map[string]int64 { - percents, err := parseTagPercents(e.value(name, "")) - e.check(name, err) - - return percents -} - -// percent reads a setting that is a percentage, from 0 to 100. -func (e *environment) percent(name, defaultValue string) int64 { - percent, err := reputation.ParsePercent(e.value(name, defaultValue)) - e.check(name, err) - - return percent -} - -// abuseIPDBThreshold reads the setting that is the least score of the -// clients on AbuseIPDB's blacklist: a whole number from 25 to 100, the -// range AbuseIPDB takes. -func (e *environment) abuseIPDBThreshold(name, defaultValue string) int64 { - value := e.value(name, defaultValue) - - threshold, err := strconv.ParseInt(value, 10, 64) - if err != nil || threshold < minAbuseIPDBThreshold || threshold > 100 { - e.check(name, fmt.Errorf("%q %w", value, errNotAbuseIPDBThreshold)) - } - - return threshold -} - -// wholeUnits reads a setting that is a whole number above zero of unit, -// such as minutes or days, and returns it as a duration. -func (e *environment) wholeUnits( - name, defaultValue string, unit time.Duration, -) time.Duration { - value := e.value(name, defaultValue) - - n, err := strconv.ParseInt(value, 10, 64) - if err != nil || n <= 0 || n > math.MaxInt64/int64(unit) { - e.check(name, fmt.Errorf("%q %w", value, errNotWholeUnits)) - } - - return time.Duration(n) * unit -} - -// listURL reads a setting that is the URL a list is fetched from, "" while -// it is unset or empty. -func (e *environment) listURL(name string) string { - value := e.value(name, "") - if value != "" && !isListURL(value) { - e.check(name, fmt.Errorf("%q %w", value, errNotListURL)) - } - - return value -} - -// listURLs reads a setting that is a list of the URLs lists are fetched -// from. It is empty by default. -func (e *environment) listURLs(name string) []string { - urls, err := parseListURLs(e.value(name, "")) - e.check(name, err) - - return urls -} - -// refresh reads the setting that is how long after a list was last -// fetched or tried it is fetched again: a duration of an hour or more, -// since the Spamhaus lists may be fetched no more often, which cannot be -// off. -func (e *environment) refresh(name, defaultValue string) time.Duration { - value := e.value(name, defaultValue) - - duration, err := parseDuration(value) - if err != nil || duration < time.Hour { - e.check(name, fmt.Errorf("%q %w", value, errNotAnHourOrMore)) - } - - return duration -} - -// action reads a setting that is what is done with a client a blocklist -// or a DNSBL zone lists: deny, log, or limit:, which it returns -// as limit and the percentage. -func (e *environment) action( - name, defaultValue string, -) (ReputationAction, int64) { - value := ReputationAction(e.value(name, defaultValue)) - if value == ReputationActionDeny || value == ReputationActionLog { - return value, 0 - } - - percentText, isLimit := strings.CutPrefix( - string(value), string(ReputationActionLimit)+":", - ) - - percent, err := reputation.ParsePercent(percentText) - if !isLimit || err != nil { - e.check(name, fmt.Errorf("%q %w", value, errNotAction)) - } - - return ReputationActionLimit, percent -} - -// zones reads the setting that is the list of DNSBL zones. It is empty by -// default. The log shows each zone with its key masked, as -// reputation.MaskZoneKey masks it. -func (e *environment) zones(name string) []string { - value, _ := e.lookup(name) - zones, err := parseZones(value) - e.check(name, err) - - logged := make([]string, len(zones)) - for i, zone := range zones { - logged[i] = reputation.MaskZoneKey(zone) - } - - e.settings = append( - e.settings, - slog.String(name, strings.Join(logged, ",")), - ) - - return zones -} - -// resolver reads the setting that is the resolver the DNSBL zones are -// asked through, the zero AddrPort while it is unset or empty. -func (e *environment) resolver(name string) netip.AddrPort { - resolver, err := parseResolver(e.value(name, "")) - e.check(name, err) - - return resolver -} - -// routeWatchURL reads the setting that is the URL of a routewatch instance, -// "" while it is unset or empty. -func (e *environment) routeWatchURL(name string) string { - value := e.value(name, "") - if value == "" { - return "" - } - - instance, err := url.Parse(value) - if err != nil || !isHTTPURL(instance) { - e.check(name, fmt.Errorf("%q %w", value, errNotRouteWatchURL)) - } - - return value -} +var ( + errNotLookupSource = errors.New("is not geojs, file or off") +) // lookupSource reads the setting that is where clients are looked up: // geojs, file, or off. @@ -1200,6 +1217,13 @@ func (e *environment) lookupSource( return source } +var ( + errNeedsDBPath = errors.New( + "it names the file to look clients up in", + ) + errDBPathUnused = errors.New("only file reads it") +) + // checkLookupDBPath refuses SWW_WAF_LOOKUP_SOURCE=file without // SWW_WAF_LOOKUP_DB_PATH, and SWW_WAF_LOOKUP_DB_PATH with any other source: // one source at a time. @@ -1219,6 +1243,25 @@ func (e *environment) checkLookupDBPath(cfg *WAF) { } } +var ( + errNotBoolean = errors.New("is not true or false") +) + +// boolean reads a setting that is true or false. +func (e *environment) boolean(name, defaultValue string) bool { + value := e.value(name, defaultValue) + if value != "true" && value != "false" { + e.check(name, fmt.Errorf("%q %w", value, errNotBoolean)) + } + + return value == "true" +} + +var ( + errOnBothLists = errors.New("is in SWW_WAF_TAG_DENY too") + errNeedsLookups = errors.New("it needs each client looked up") +) + // checkCountriesAndLookups refuses a country in // SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES whose tag is in SWW_WAF_TAG_DENY, and, // while SWW_WAF_LOOKUP_SOURCE is off, each setting that needs clients looked @@ -1273,15 +1316,179 @@ func (e *environment) checkCountriesAndLookups(cfg *WAF) { } } -// checkASNLimitPercentURL refuses SWW_WAF_ASN_LIMIT_PERCENT_URL naming a -// 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(ASNLimitPercentURLSetting, - fmt.Errorf("%q %w", cfg.ASNLimitPercentURL, errInBlocklistURLs)) - } +var ( + errNotCountry = errors.New( + "is not a two-letter country code such as de or kp") +) + +// countries reads a setting that is a list of countries. +func (e *environment) countries(name, defaultValue string) []string { + countries, err := parseCountries(e.value(name, defaultValue)) + e.check(name, err) + + return countries } +// countryCodes are the two-letter codes ISO 3166-1 assigns today, and XK, +// the code in common use for Kosovo. golang.org/x/text/language cannot +// check them: it also takes withdrawn codes such as su, and reserved ones +// such as ac, as countries. +const countryCodes = ` +AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ +BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ +CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ +DE DJ DK DM DO DZ +EC EE EG EH ER ES ET +FI FJ FK FM FO FR +GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY +HK HM HN HR HT HU +ID IE IL IM IN IO IQ IR IS IT +JE JM JO JP +KE KG KH KI KM KN KP KR KW KY KZ +LA LB LC LI LK LR LS LT LU LV LY +MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ +NA NC NE NF NG NI NL NO NP NR NU NZ +OM +PA PE PF PG PH PK PL PM PN PR PS PT PW PY +QA +RE RO RS RU RW +SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ +TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ +UA UG UM US UY UZ +VA VC VE VG VI VN VU +WF WS +XK +YE YT +ZA ZM ZW +` + +// parseCountries reads a comma-separated list of country codes in either +// case, and returns them in capitals. +func parseCountries(value string) ([]string, error) { + items, err := parseList(value) + if err != nil { + return nil, err + } + + countries := make([]string, 0, len(items)) + + for _, item := range items { + country, err := parseCountry(item) + if err != nil { + return nil, err + } + + countries = append(countries, country) + } + + return countries, nil +} + +// parseCountry reads a country code in either case, and returns it in +// capitals. +func parseCountry(value string) (string, error) { + country := strings.ToUpper(value) + if !slices.Contains(strings.Fields(countryCodes), country) { + return "", fmt.Errorf("%q %w", value, errNotCountry) + } + + return country, nil +} + +var ( + errListedTwice = errors.New("is listed twice") +) + +// isHTTPURL reports whether u is http or https, with a host, and an +// optional port from 1 to 65535, path and query, without a user or a +// fragment. +func isHTTPURL(u *url.URL) bool { + port, err := strconv.ParseUint(u.Port(), 10, 16) + + return (u.Scheme == "http" || u.Scheme == "https") && + u.Hostname() != "" && + (u.Port() == "" || (err == nil && port != 0)) && + u.User == nil && + u.Opaque == "" && + u.Fragment == "" +} + +// isListURL reports whether value is a URL a list can be fetched from, as +// isHTTPURL says. +func isListURL(value string) bool { + u, err := url.Parse(value) + + return err == nil && isHTTPURL(u) +} + +// The tags a request can get that the settings can name, as waf +// writes them. The prefixes are followed by a value. +const ( + ASNTagPrefix = "asn=" + CountryTagPrefix = "country=" + DNSBLTagPrefix = "dnsbl=" + PrefixTagPrefix = "prefix=" + AbuseIPDBTag = "abuseipdb" + // ExploitSignatureTag is the tag of a request a rule file or the Core + // Rule Set matched. + ExploitSignatureTag = "exploitsignature" +) + +var ( + errNotTagList = errors.New( + "is not a name, = and an http or https URL, such as " + + "torexit=https://example.com/tor.txt") +) + +// tagLists reads SWW_WAF_TAG_LISTS, which is empty by default. +func (e *environment) tagLists(name string) []reputation.TagList { + lists, err := parseTagLists(e.value(name, "")) + e.check(name, err) + + return lists +} + +// parseTagLists reads a comma-separated list of name=URL pairs, such as +// torexit=https://example.com/tor.txt, and returns each list with its name +// in lower case. A name can hold a =, as vpn=mullvad does, and ends at the +// = before http. A name or a URL listed twice is an error. +func parseTagLists(value string) ([]reputation.TagList, error) { + items, err := parseList(value) + if err != nil { + return nil, err + } + + lists := make([]reputation.TagList, 0, len(items)) + + for _, item := range items { + end := strings.Index(item, "=http") + if end <= 0 || !isListURL(item[end+1:]) { + return nil, fmt.Errorf("%q %w", item, errNotTagList) + } + + list := reputation.TagList{ + Name: strings.ToLower(item[:end]), + URL: item[end+1:], + } + + if slices.ContainsFunc(lists, func(earlier reputation.TagList) bool { + return earlier.Name == list.Name || earlier.URL == list.URL + }) { + return nil, fmt.Errorf("%q %w", item, errListedTwice) + } + + lists = append(lists, list) + } + + return lists, nil +} + +var ( + errFetchedOtherwise = errors.New( + "is in SWW_WAF_BLOCKLIST_URLS or is SWW_WAF_ASN_LIMIT_PERCENT_URL too", + ) +) + // checkTagLists refuses a list of SWW_WAF_TAG_LISTS whose URL is fetched as // another list too: the file at a URL is read as one kind of list. func (e *environment) checkTagLists(cfg *WAF) { @@ -1296,6 +1503,180 @@ func (e *environment) checkTagLists(cfg *WAF) { } } +var ( + errNotASNTag = errors.New( + "is not asn= and a number, such as asn=14061", + ) + errNotPrefixTag = errors.New( + "is not prefix= and a netblock, such as prefix=192.0.2.0/24") +) + +// tags reads a setting that is a list of tags. It is empty by default. +func (e *environment) tags(name string) []string { + tags, err := parseTags(e.value(name, "")) + e.check(name, err) + + return tags +} + +// parseTags reads a comma-separated list of tags, which parseTag reads. A +// tag listed twice is an error, which shows the tag as +// reputation.MaskZoneKey does. +func parseTags(value string) ([]string, error) { + items, err := parseList(value) + if err != nil { + // parseList's error, for an empty item, shows the whole value, keys + // included. + return nil, errEmptyItem + } + + tags := make([]string, 0, len(items)) + + for _, item := range items { + tag, err := parseTag(item) + if err != nil { + return nil, err + } + + if slices.Contains(tags, tag) { + return nil, fmt.Errorf( + "%q %w", + reputation.MaskZoneKey(item), + errListedTwice, + ) + } + + tags = append(tags, tag) + } + + return tags, nil +} + +// parseTag reads a tag in either case, with spaces around it, and returns it +// as a request carries it: in lower case, but for a country code, which is in +// capitals, an asn= tag's number without leading zeros, and a prefix= tag's +// netblock as routewatch writes it. An asn=, country= or prefix= tag must name +// a number, a country or a netblock, so that a mistyped one stops the start +// instead of never matching. +func parseTag(value string) (string, error) { + tag := strings.ToLower(strings.TrimSpace(value)) + + if digits, isASN := strings.CutPrefix(tag, ASNTagPrefix); isASN { + number, err := strconv.ParseUint(digits, 10, 32) + if err != nil { + return "", fmt.Errorf("%q %w", value, errNotASNTag) + } + + return ASNTagPrefix + strconv.FormatUint(number, 10), nil + } + + if code, isCountry := strings.CutPrefix(tag, CountryTagPrefix); isCountry { + country, err := parseCountry(code) + if err != nil { + return "", err + } + + return CountryTagPrefix + country, nil + } + + if text, isPrefix := strings.CutPrefix(tag, PrefixTagPrefix); isPrefix { + netblock, err := netip.ParsePrefix(text) + if err != nil || netblock != netblock.Masked() { + return "", fmt.Errorf("%q %w", value, errNotPrefixTag) + } + + return PrefixTagPrefix + netblock.String(), nil + } + + return tag, nil +} + +var ( + errNotTagPercentItem = errors.New( + "is not a tag, : and a percentage, such as vpn:50 or asn=14061:25") + errNotTagPercent = errors.New("is not a percentage from 1 to 99") + errExploitTag = errors.New( + "cannot lower limits: the rate limits count a request before its " + + "rules are matched") +) + +// tagPercents reads a setting that is a list of tags, each with a +// percentage. It is empty by default. +func (e *environment) tagPercents(name string) map[string]int64 { + percents, err := parseTagPercents(e.value(name, "")) + e.check(name, err) + + return percents +} + +// parseTagPercents reads a comma-separated list of tags, each with : and a +// percentage from 1 to 99, such as vpn:50 or asn=14061:25. A tag listed +// twice is an error, and so is ExploitSignatureTag, which a request gets +// only after the rate limits have counted it. +func parseTagPercents(value string) (map[string]int64, error) { + items, err := parseList(value) + if err != nil { + // parseList's error, for an empty item, shows the whole value, keys + // included. + return nil, errEmptyItem + } + + percents := make(map[string]int64, len(items)) + + for _, item := range items { + colon := strings.LastIndex(item, ":") + if colon <= 0 { + return nil, fmt.Errorf( + "%q %w", + reputation.MaskZoneKey(item), + errNotTagPercentItem, + ) + } + + tag, err := parseTag(item[:colon]) + if err != nil { + return nil, err + } + + percent, err := strconv.ParseInt(item[colon+1:], 10, 64) + if err != nil || percent < 1 || percent > 99 { + return nil, fmt.Errorf( + "%q %w", + reputation.MaskZoneKey(item[colon+1:]), + errNotTagPercent, + ) + } + + if tag == ExploitSignatureTag { + return nil, fmt.Errorf("%q %w", tag, errExploitTag) + } + + if _, listed := percents[tag]; listed { + // tag, since item[:colon] may end in a space, which MaskZoneKey + // would not mask. + return nil, fmt.Errorf( + "%q %w", + reputation.MaskZoneKey(tag), + errListedTwice, + ) + } + + percents[tag] = percent + } + + return percents, nil +} + +var ( + errNoRequestGetsTag = errors.New( + "is a tag no request can get: it is not a name in SWW_WAF_TAG_LISTS, an " + + "asn= or country= tag, a dnsbl= tag of a zone in SWW_WAF_DNSBL_ZONES, " + + "a prefix= tag with SWW_WAF_ROUTEWATCH_URL set, abuseipdb with " + + "SWW_WAF_ABUSEIPDB_KEY set, or exploitsignature with rule files or the " + + "Core Rule Set on", + ) +) + // checkTags refuses a tag of SWW_WAF_TAG_DENY or SWW_WAF_TAG_LIMIT_PERCENT that would // never apply: any tag these settings cannot give a request. func (e *environment) checkTags(cfg *WAF) { @@ -1376,518 +1757,135 @@ func isLookupTag(tag string) bool { strings.HasPrefix(tag, CountryTagPrefix) } -// headerNames reads a setting that is a list of header names, and -// returns them in lower case. -func (e *environment) headerNames(name, defaultValue string) []string { - headers, err := parseHeaderNames(e.value(name, defaultValue)) - e.check(name, err) - - return headers -} - -// durationNotOff reads a setting that is a duration and, unlike a -// timeout, cannot be off. -func (e *environment) durationNotOff( - name, defaultValue string, -) time.Duration { - duration, err := ParseDurationNotOff(e.value(name, defaultValue)) - e.check(name, err) - - return duration -} - -// numberNotOff reads a setting that is a whole number above zero, which -// cannot be off. -func (e *environment) numberNotOff(name, defaultValue string) int { - number, err := parseNumberNotOff(e.value(name, defaultValue)) - e.check(name, err) - - return number -} - -// banResponse reads a setting that is how a refused client is answered. -func (e *environment) banResponse(name, defaultValue string) int { - status, err := parseBanResponse(e.value(name, defaultValue)) - e.check(name, err) - - return status -} - -// v4Prefix reads a setting that is the length of an IPv4 netblock. -func (e *environment) v4Prefix(name, defaultValue string) int { - length, err := parseV4Prefix(e.value(name, defaultValue)) - e.check(name, err) - - return length -} - -// v6Prefix reads a setting that is the length of an IPv6 netblock. -func (e *environment) v6Prefix(name, defaultValue string) int { - length, err := parseV6Prefix(e.value(name, defaultValue)) - e.check(name, err) - - return length -} - -// ipv6GroupPrefix reads the setting that is the length of the IPv6 -// netblock that is one client, from minIPv6GroupPrefix to 128. -func (e *environment) ipv6GroupPrefix(name, defaultValue string) int { - value := e.value(name, defaultValue) - - length, err := strconv.Atoi(value) - if err != nil || length < minIPv6GroupPrefix || length > ipv6Bits { - e.check(name, fmt.Errorf("%q %w", value, errNotIPv6GroupPrefix)) - } - - return length -} - -// logLevel reads the setting that is the least severe of the process's -// own messages that are written: debug, info, warn or error. -func (e *environment) logLevel(name, defaultValue string) slog.Level { - value := e.value(name, defaultValue) - - level, known := map[string]slog.Level{ - "debug": slog.LevelDebug, - "info": slog.LevelInfo, - "warn": slog.LevelWarn, - "error": slog.LevelError, - }[value] - if !known { - e.check(name, fmt.Errorf("%q %w", value, errNotLogLevel)) - } - - return level -} - -// 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 { - 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), - } -} - -// namedNetblocks reads a setting that is a list of named netblocks. It is -// empty by default. -func (e *environment) namedNetblocks(name string) []anomaly.NamedNetblock { - named, err := parseNamedNetblocks(e.value(name, "")) - e.check(name, err) - - return named -} - -// absolutePath reads a setting that is an absolute path. -func (e *environment) absolutePath(name, defaultValue string) string { - path := e.value(name, defaultValue) - if !filepath.IsAbs(path) { - e.check(name, fmt.Errorf("%q %w", path, errNotAbsolutePath)) - } - - return path -} - -// token reads a setting that is a bearer token. Unset, it is "", which -// switches off what it guards; set, it must be at least minTokenLength -// characters. Neither the log nor an error shows its value. -func (e *environment) token(name string) string { - value, set := e.lookup(name) - if !set { - e.settings = append(e.settings, slog.String(name, "")) +var ( + errNotRouteWatchURL = errors.New( + "is not an http or https URL without a user or a fragment, " + + "such as https://routewatch.example") +) +// routeWatchURL reads the setting that is the URL of a routewatch instance, +// "" while it is unset or empty. +func (e *environment) routeWatchURL(name string) string { + value := e.value(name, "") + if value == "" { return "" } - e.settings = append(e.settings, slog.String(name, masked)) - - if utf8.RuneCountInString(value) < minTokenLength { - e.check(name, errShortToken) + instance, err := url.Parse(value) + if err != nil || !isHTTPURL(instance) { + e.check(name, fmt.Errorf("%q %w", value, errNotRouteWatchURL)) } return value } -// logRemoteURL reads the setting that is where every log line is also -// sent. Unset or empty, it is nil, and nothing is sent. -func (e *environment) logRemoteURL(name string) *url.URL { - value := e.value(name, "") - if value == "" { - return nil - } +var ( + errNotPercentItem = errors.New( + "is not a code, : and a percentage, such as AS64496:50 or cn:25") +) - remote, err := parseLogRemoteURL(value) +// percents reads a setting that is a list of AS numbers or countries, +// which parseCode reads, each with a percentage. It is empty by default. +func (e *environment) percents( + name string, parseCode func(string) (string, error), +) map[string]int64 { + percents, err := parsePercents(e.value(name, ""), parseCode) e.check(name, err) - return remote + return percents } -// filePath reads a setting that is the path of a file, "" while it is -// unset. Its value names a file already, so, unlike the other settings, -// it has no _FILE form. Whoever uses the file reads it. -func (e *environment) filePath(name string) string { - path, _ := readSetting(e.v, name) - e.settings = append(e.settings, slog.String(name, path)) - - return path -} - -// certificates reads a setting that is the path of a file of PEM -// certificates. Unset or empty, it is nil. Its value names a file -// already, so, unlike the other settings, it has no _FILE form. -func (e *environment) certificates(name string) *x509.CertPool { - path, _ := readSetting(e.v, name) - e.settings = append(e.settings, slog.String(name, path)) - - if path == "" { - return nil - } - - pem, err := os.ReadFile(path) //nolint:gosec // a file the admin names - if err != nil { - e.check(name, fmt.Errorf("cannot be read: %w", err)) - - return nil - } - - pool := x509.NewCertPool() - if !pool.AppendCertsFromPEM(pem) { - e.check(name, fmt.Errorf("%q %w", path, errNoCertificate)) - - return nil - } - - return pool -} - -// facility reads a setting that is a syslog facility, and returns its -// number. -func (e *environment) facility(name, defaultValue string) int { - number, err := parseFacility(e.value(name, defaultValue)) - e.check(name, err) - - return number -} - -// instanceName reads SWW_WAF_INSTANCE_NAME, by default the host's name. It -// must be valid UTF-8: the metrics library panics on a label that is not. -func (e *environment) instanceName() string { - hostname, _ := os.Hostname() // "" when the host has no name to give - - value := e.value("SWW_WAF_INSTANCE_NAME", hostname) - if !utf8.ValidString(value) { - e.check( - "SWW_WAF_INSTANCE_NAME", - fmt.Errorf("%q %w", value, errNotUTF8), - ) - } - - return value -} - -// appName reads the setting that is the APP-NAME of the records the log -// lines are sent in, by default the instance name. Its value is checked -// when it is set, and, while lines are sent, when it is the instance name. -func (e *environment) appName( - name, instanceName string, - sending bool, -) string { - value, set := e.lookup(name) - if !set { - value = instanceName - } - - e.settings = append(e.settings, slog.String(name, value)) - - switch { - case isAppName(value): - case set: - e.check(name, fmt.Errorf("%q %w", value, errNotAppName)) - case sending: - e.check( - name, - fmt.Errorf( - "is unset, and SWW_WAF_INSTANCE_NAME %q, its default, %w", - value, - errNotAppName, - ), - ) - } - - return value -} - -// checkInstanceNameForNtfy refuses an instance name that holds a control -// character while ntfySet, SWW_WAF_ALERT_NTFY_URL being set: ntfy is sent -// the instance name in a header, which cannot hold one. -func (e *environment) checkInstanceNameForNtfy( - instanceName string, - ntfySet bool, -) { - if ntfySet && strings.ContainsFunc(instanceName, unicode.IsControl) { - e.check("SWW_WAF_INSTANCE_NAME", fmt.Errorf( - "%q %w, and is sent to ntfy in a header while SWW_WAF_ALERT_NTFY_URL is set", - instanceName, - errControlCharacter, - )) - } -} - -// webhookURL reads a setting that is a URL each alert is posted to: -// SWW_WAF_ALERT_WEBHOOK_URL, SWW_WAF_ALERT_SLACK_WEBHOOK_URL or -// SWW_WAF_ALERT_NTFY_URL. Unset or empty, it is nil, and no alert is posted -// there. The log shows ******** in place of its path and query, and an -// error shows none of it, since a webhook or an ntfy topic can carry its -// secret there. -func (e *environment) webhookURL(name string) *url.URL { - value, _ := e.lookup(name) - webhook, logged, err := parseWebhookURL(value) - e.settings = append(e.settings, slog.String(name, logged)) - e.check(name, err) - - return webhook -} - -// webhookHeaders reads the setting that is the headers sent with each -// alert. The log shows each header's value as ********, since a header -// such as Authorization carries a secret. -func (e *environment) webhookHeaders(name string) http.Header { - value, _ := e.lookup(name) - headers, logged, err := parseWebhookHeaders(value) - e.settings = append(e.settings, slog.String(name, logged)) - e.check(name, err) - - return headers -} - -// secret reads a setting that is a secret another service gave, such as -// an ntfy token or an AbuseIPDB key, "" while it is unset. It is sent in -// a header, which cannot hold a control character, so one in it is an -// error. The log shows ******** in place of a value that is not empty, and -// an error shows none of it. -func (e *environment) secret(name string) string { - value, _ := e.lookup(name) - - logged := "" - if value != "" { - logged = masked - } - - e.settings = append(e.settings, slog.String(name, logged)) - - if strings.ContainsFunc(value, unicode.IsControl) { - e.check(name, errControlCharacter) - } - - return value -} - -// alertEvents reads the setting that is the events alerts are sent for, -// every event by default. -func (e *environment) alertEvents(name string) []alerts.Event { - every := make([]string, 0, len(alerts.Events())) - for _, event := range alerts.Events() { - every = append(every, string(event)) - } - - events, err := parseAlertEvents(e.value(name, strings.Join(every, ","))) - e.check(name, err) - - return events -} - -// numberOrOff reads a setting that is a whole number above zero, or off, -// which is 0. -func (e *environment) numberOrOff(name, defaultValue string) int { - number, err := parseNumberOrOff(e.value(name, defaultValue)) - e.check(name, err) - - return number -} - -// parseDuration reads a duration in Go's syntax, such as 90s or 15m, a -// whole number of days such as 7d, or off. -func parseDuration(value string) (time.Duration, error) { - if value == off { - return 0, nil - } - - duration, err := durationOrDays(value) - if err != nil { - return 0, fmt.Errorf("%q %w", value, errNotDuration) - } - - if duration <= 0 { - return 0, fmt.Errorf("%q %w", value, errNotPositive) - } - - return duration, nil -} - -// durationOrDays reads Go's duration syntax, or a whole number of days. -func durationOrDays(value string) (time.Duration, error) { - days, isDays := strings.CutSuffix(value, "d") - if !isDays { - return time.ParseDuration(value) - } - - n, err := strconv.ParseInt(days, 10, 64) - if err != nil || n < 0 || n > math.MaxInt64/int64(day) { - return 0, errNotDuration - } - - return time.Duration(n) * day, nil -} - -// parseCoreRuleSetBodyInspectionSize reads the most of a request body the Core Rule Set -// reads: a size as parseSize reads it, or off, but at most 8K, so that one -// request cannot keep the Core Rule Set busy past rproxy's timeout. -func parseCoreRuleSetBodyInspectionSize(value string) (int64, error) { - limit, err := parseSize(value) - if err != nil { - return 0, err - } - - if limit > 8*kibibyte { - return 0, fmt.Errorf("%q %w", value, errOver8K) - } - - return limit, nil -} - -// parseCount reads a whole number of requests, or off. -func parseCount(value string) (int64, error) { - if value == off { - return 0, nil - } - - n, err := strconv.ParseInt(value, 10, 64) - if err != nil { - return 0, fmt.Errorf("%q %w", value, errNotCount) - } - - if n <= 0 { - return 0, fmt.Errorf("%q %w", value, errNotPositive) - } - - return n, nil -} - -// ParseDurationNotOff reads a duration above zero, as parseDuration does, -// but not off. The ban endpoint reads the duration of a ban with it too. -func ParseDurationNotOff(value string) (time.Duration, error) { - duration, err := parseDuration(value) - if err != nil || duration == 0 { - return 0, fmt.Errorf("%q %w", value, errNotDurationAboveZero) - } - - return duration, nil -} - -// parseNumberNotOff reads a whole number above zero. -func parseNumberNotOff(value string) (int, error) { - n, err := strconv.Atoi(value) - if err != nil || n <= 0 { - return 0, fmt.Errorf("%q %w", value, errNotNumberAboveZero) - } - - return n, nil -} - -// parseBanResponse reads how a refused client is answered: 403, 429, or -// close, which is 0. -func parseBanResponse(value string) (int, error) { - switch value { - case "403": - return http.StatusForbidden, nil - case "429": - return http.StatusTooManyRequests, nil - case "close": - return 0, nil - default: - return 0, fmt.Errorf("%q %w", value, errNotBanResponse) - } -} - -// parseV4Prefix reads the length of an IPv4 netblock, from 0 to 32. -func parseV4Prefix(value string) (int, error) { - n, err := strconv.Atoi(value) - if err != nil || n < 0 || n > ipv4Bits { - return 0, fmt.Errorf("%q %w", value, errNotV4Prefix) - } - - return n, nil -} - -// parseV6Prefix reads the length of an IPv6 netblock, from 0 to 128. -func parseV6Prefix(value string) (int, error) { - n, err := strconv.Atoi(value) - if err != nil || n < 0 || n > ipv6Bits { - return 0, fmt.Errorf("%q %w", value, errNotV6Prefix) - } - - return n, nil -} - -// parseNamedNetblocks reads a comma-separated list of named netblocks, -// each a name, = and a netblock, such as office=203.0.113.0/24. An empty -// value is an empty list. A name listed twice is an error. -func parseNamedNetblocks(value string) ([]anomaly.NamedNetblock, error) { +// parsePercents reads a comma-separated list of items, each an AS number +// or a country, which parseCode reads, then : and a percentage, such as +// AS64496:50 or cn:25, and returns each one's percentage. An empty value +// is an empty list. An AS number or country listed twice is an error. +func parsePercents( + value string, parseCode func(string) (string, error), +) (map[string]int64, error) { items, err := parseList(value) if err != nil { return nil, err } - named := make([]anomaly.NamedNetblock, 0, len(items)) + percents := make(map[string]int64, len(items)) for _, item := range items { - name, netblockText, found := strings.Cut(item, "=") - name = strings.TrimSpace(name) - - if !found || name == "" { - return nil, fmt.Errorf("%q %w", item, errNotNamedNetblock) + codeText, percentText, found := strings.Cut(item, ":") + if !found { + return nil, fmt.Errorf("%q %w", item, errNotPercentItem) } - netblock, err := reputation.ParseNetblock( - strings.TrimSpace(netblockText), - ) + code, err := parseCode(codeText) if err != nil { return nil, err } - if slices.ContainsFunc(named, func(n anomaly.NamedNetblock) bool { - return n.Name == name - }) { - return nil, fmt.Errorf("%q %w", name, errListedTwice) + percent, err := reputation.ParsePercent(percentText) + if err != nil { + return nil, err } - named = append( - named, - anomaly.NamedNetblock{Name: name, Netblock: netblock}, - ) + if _, listed := percents[code]; listed { + return nil, fmt.Errorf("%q %w", codeText, errListedTwice) + } + + percents[code] = percent } - return named, nil + return percents, nil } -// parsePathPrefixes reads a comma-separated list of path prefixes, each -// starting with /. -func parsePathPrefixes(value string) ([]string, error) { - prefixes, err := parseList(value) - if err != nil { - return nil, err +// percent reads a setting that is a percentage, from 0 to 100. +func (e *environment) percent(name, defaultValue string) int64 { + percent, err := reputation.ParsePercent(e.value(name, defaultValue)) + e.check(name, err) + + return percent +} + +var ( + errNotListURL = errors.New( + "is not an http or https URL without a user or a fragment, " + + "such as https://www.spamhaus.org/drop/drop.txt") +) + +// listURL reads a setting that is the URL a list is fetched from, "" while +// it is unset or empty. +func (e *environment) listURL(name string) string { + value := e.value(name, "") + if value != "" && !isListURL(value) { + e.check(name, fmt.Errorf("%q %w", value, errNotListURL)) } - for _, prefix := range prefixes { - if !strings.HasPrefix(prefix, "/") { - return nil, fmt.Errorf("%q %w", prefix, errNotPathPrefix) - } - } + return value +} - return prefixes, nil +var ( + errInBlocklistURLs = errors.New("is in SWW_WAF_BLOCKLIST_URLS too") +) + +// checkASNLimitPercentURL refuses SWW_WAF_ASN_LIMIT_PERCENT_URL naming a +// 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(ASNLimitPercentURLSetting, + fmt.Errorf("%q %w", cfg.ASNLimitPercentURL, errInBlocklistURLs)) + } +} + +var ( + errNotTrapPath = errors.New( + "is not a path starting with / and without a ?, such as /wp-login.php", + ) +) + +// trapPaths reads the setting that is the list of trap paths. It is empty +// by default. +func (e *environment) trapPaths(name string) []string { + paths, err := parseTrapPaths(e.value(name, "")) + e.check(name, err) + + return paths } // parseTrapPaths reads a comma-separated list of trap paths. Each is @@ -1909,6 +1907,219 @@ func parseTrapPaths(value string) ([]string, error) { return paths, nil } +// CoreRuleSetMode is what the Core Rule Set does, a value of +// SWW_WAF_CORE_RULE_SET_MODE. +type CoreRuleSetMode string + +// The values of SWW_WAF_CORE_RULE_SET_MODE. +const ( + // CoreRuleSetModeOff runs no request through the Core Rule Set. + CoreRuleSetModeOff CoreRuleSetMode = "off" + // CoreRuleSetModeDetect logs and alerts a match, and refuses nothing. + CoreRuleSetModeDetect CoreRuleSetMode = "detect" + // CoreRuleSetModeBlock refuses a match with 403. + CoreRuleSetModeBlock CoreRuleSetMode = "block" +) + +var ( + errNotCoreRuleSetMode = errors.New("is not off, detect or block") +) + +// coreRuleSetMode reads the setting that is what the Core Rule Set does: off, +// detect or block. +func (e *environment) coreRuleSetMode( + name string, defaultValue CoreRuleSetMode, +) CoreRuleSetMode { + mode := CoreRuleSetMode(e.value(name, string(defaultValue))) + if mode != CoreRuleSetModeOff && mode != CoreRuleSetModeDetect && + mode != CoreRuleSetModeBlock { + e.check(name, fmt.Errorf("%q %w", mode, errNotCoreRuleSetMode)) + } + + return mode +} + +const ( + // maxParanoiaLevel is the Core Rule Set's highest paranoia level. + maxParanoiaLevel = 4 +) + +var ( + errNotParanoiaLevel = errors.New( + "is not a paranoia level, from 1 to 4", + ) +) + +// paranoiaLevel reads the setting that is the Core Rule Set's paranoia +// level, from 1 to 4. +func (e *environment) paranoiaLevel(name, defaultValue string) int { + value := e.value(name, defaultValue) + + level, err := strconv.Atoi(value) + if err != nil || level < 1 || level > maxParanoiaLevel { + e.check(name, fmt.Errorf("%q %w", value, errNotParanoiaLevel)) + } + + return level +} + +var ( + errNotNumberOrOff = errors.New( + "is not a whole number above zero, such as 60, or off", + ) +) + +// numberOrOff reads a setting that is a whole number above zero, or off, +// which is 0. +func (e *environment) numberOrOff(name, defaultValue string) int { + number, err := parseNumberOrOff(e.value(name, defaultValue)) + e.check(name, err) + + return number +} + +// parseNumberOrOff reads a whole number above zero, or off, which is 0. +func parseNumberOrOff(value string) (int, error) { + if value == off { + return 0, nil + } + + n, err := strconv.Atoi(value) + if err != nil || n <= 0 { + return 0, fmt.Errorf("%q %w", value, errNotNumberOrOff) + } + + return n, nil +} + +const ( + // firstSetupRuleID to lastSetupRuleID are the ids the Core Rule Set + // keeps for the rules that set it up, which sww's own rules + // have too (see internal/coreruleset). Switching one off would undo a change + // that no setting undoes. + firstSetupRuleID = 900000 + lastSetupRuleID = 900999 +) + +var ( + errNotRuleID = errors.New( + "is not the id of a Core Rule Set rule, a whole number such as 942100", + ) + errSetupRuleID = errors.New( + "is from 900000 to 900999, the ids of the rules that set the Core Rule Set " + + "up and of sww's own, which cannot be switched off", + ) +) + +// ruleIDs reads the setting that is a list of the ids of Core Rule Set +// rules. +func (e *environment) ruleIDs(name, defaultValue string) []int { + ids, err := parseRuleIDs(e.value(name, defaultValue)) + e.check(name, err) + + return ids +} + +// parseRuleIDs reads a comma-separated list of the ids of Core Rule Set +// rules, each a whole number above zero and outside firstSetupRuleID to +// lastSetupRuleID. +func parseRuleIDs(value string) ([]int, error) { + items, err := parseList(value) + if err != nil { + return nil, err + } + + ids := make([]int, len(items)) + + for i, item := range items { + ids[i], err = strconv.Atoi(item) + if err != nil || ids[i] <= 0 { + return nil, fmt.Errorf("%q %w", item, errNotRuleID) + } + + if ids[i] >= firstSetupRuleID && ids[i] <= lastSetupRuleID { + return nil, fmt.Errorf("%q %w", item, errSetupRuleID) + } + } + + return ids, nil +} + +var ( + errOver8K = errors.New( + "is more than 8K, the most the Core Rule Set can read in time") +) + +// coreRuleSetBodyInspectionSize reads the setting that is the most of a +// request body the Core Rule Set reads. +func (e *environment) coreRuleSetBodyInspectionSize( + name, defaultValue string, +) int64 { + limit, err := parseCoreRuleSetBodyInspectionSize( + e.value(name, defaultValue), + ) + e.check(name, err) + + return limit +} + +// parseCoreRuleSetBodyInspectionSize reads the most of a request body the Core Rule Set +// reads: a size as parseSize reads it, or off, but at most 8K, so that one +// request cannot keep the Core Rule Set busy past rproxy's timeout. +func parseCoreRuleSetBodyInspectionSize(value string) (int64, error) { + limit, err := parseSize(value) + if err != nil { + return 0, err + } + + if limit > 8*kibibyte { + return 0, fmt.Errorf("%q %w", value, errOver8K) + } + + return limit, nil +} + +// filePath reads a setting that is the path of a file, "" while it is +// unset. Its value names a file already, so, unlike the other settings, +// it has no _FILE form. Whoever uses the file reads it. +func (e *environment) filePath(name string) string { + path, _ := readSetting(e.v, name) + e.settings = append(e.settings, slog.String(name, path)) + + return path +} + +var ( + errNotAppsEntry = errors.New( + "is not a hostname, : and a list of apps, such as git.example.com:gitea", + ) + errNotHostname = errors.New( + "is not a DNS name such as git.example.com", + ) + errNotApp = errors.New( + "is not an app sww has fixes for; the apps are", + ) +) + +// apps reads the setting that is the apps each hostname serves. It is nil +// while the setting is unset, and logged as null, unlike a setting set +// empty. +func (e *environment) apps(name string) map[string][]string { + value, set := e.lookup(name) + if !set { + e.settings = append(e.settings, slog.Any(name, nil)) + + return nil + } + + e.settings = append(e.settings, slog.String(name, value)) + + apps, err := parseApps(value) + e.check(name, err) + + return apps +} + // parseApps reads the apps each hostname serves: entries separated by // semicolons, each a hostname, a colon, and a comma-separated list of the // names coreruleset.AppNames returns, such as @@ -1968,437 +2179,91 @@ func parseApps(value string) (map[string][]string, error) { return apps, nil } -// parseRuleIDs reads a comma-separated list of the ids of Core Rule Set -// rules, each a whole number above zero and outside firstSetupRuleID to -// lastSetupRuleID. -func parseRuleIDs(value string) ([]int, error) { - items, err := parseList(value) - if err != nil { - return nil, err - } - - ids := make([]int, len(items)) - - for i, item := range items { - ids[i], err = strconv.Atoi(item) - if err != nil || ids[i] <= 0 { - return nil, fmt.Errorf("%q %w", item, errNotRuleID) - } - - if ids[i] >= firstSetupRuleID && ids[i] <= lastSetupRuleID { - return nil, fmt.Errorf("%q %w", item, errSetupRuleID) - } - } - - return ids, nil -} - -// countryCodes are the two-letter codes ISO 3166-1 assigns today, and XK, -// the code in common use for Kosovo. golang.org/x/text/language cannot -// check them: it also takes withdrawn codes such as su, and reserved ones -// such as ac, as countries. -const countryCodes = ` -AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ -BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ -CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ -DE DJ DK DM DO DZ -EC EE EG EH ER ES ET -FI FJ FK FM FO FR -GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY -HK HM HN HR HT HU -ID IE IL IM IN IO IQ IR IS IT -JE JM JO JP -KE KG KH KI KM KN KP KR KW KY KZ -LA LB LC LI LK LR LS LT LU LV LY -MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ -NA NC NE NF NG NI NL NO NP NR NU NZ -OM -PA PE PF PG PH PK PL PM PN PR PS PT PW PY -QA -RE RO RS RU RW -SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ -TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ -UA UG UM US UY UZ -VA VC VE VG VI VN VU -WF WS -XK -YE YT -ZA ZM ZW -` - -// parseCountries reads a comma-separated list of country codes in either -// case, and returns them in capitals. -func parseCountries(value string) ([]string, error) { - items, err := parseList(value) - if err != nil { - return nil, err - } - - countries := make([]string, 0, len(items)) - - for _, item := range items { - country, err := parseCountry(item) - if err != nil { - return nil, err - } - - countries = append(countries, country) - } - - return countries, nil -} - -// parseCountry reads a country code in either case, and returns it in -// capitals. -func parseCountry(value string) (string, error) { - country := strings.ToUpper(value) - if !slices.Contains(strings.Fields(countryCodes), country) { - return "", fmt.Errorf("%q %w", value, errNotCountry) - } - - return country, nil -} - -// The tags a request can get that the settings can name, as waf -// writes them. The prefixes are followed by a value. const ( - ASNTagPrefix = "asn=" - CountryTagPrefix = "country=" - DNSBLTagPrefix = "dnsbl=" - PrefixTagPrefix = "prefix=" - AbuseIPDBTag = "abuseipdb" - // ExploitSignatureTag is the tag of a request a rule file or the Core - // Rule Set matched. - ExploitSignatureTag = "exploitsignature" + // maxLabelLength is the most characters a label of a DNS name may have. + maxLabelLength = 63 + // labelChars are the characters a label of a DNS zone may hold. + labelChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-" ) -// parseTagLists reads a comma-separated list of name=URL pairs, such as -// torexit=https://example.com/tor.txt, and returns each list with its name -// in lower case. A name can hold a =, as vpn=mullvad does, and ends at the -// = before http. A name or a URL listed twice is an error. -func parseTagLists(value string) ([]reputation.TagList, error) { - items, err := parseList(value) - if err != nil { - return nil, err +// isZone reports whether each label of zone is as parseZones takes it. It +// also checks the hostnames of SWW_WAF_APPS, which are DNS names too. +func isZone(zone string) bool { + for label := range strings.SplitSeq(zone, ".") { + badChar := strings.ContainsFunc(label, func(char rune) bool { + return !strings.ContainsRune(labelChars, char) + }) + + if label == "" || len(label) > maxLabelLength || badChar || + strings.HasPrefix(label, "-") || strings.HasSuffix(label, "-") { + return false + } } - lists := make([]reputation.TagList, 0, len(items)) - - for _, item := range items { - end := strings.Index(item, "=http") - if end <= 0 || !isListURL(item[end+1:]) { - return nil, fmt.Errorf("%q %w", item, errNotTagList) - } - - list := reputation.TagList{ - Name: strings.ToLower(item[:end]), - URL: item[end+1:], - } - - if slices.ContainsFunc(lists, func(earlier reputation.TagList) bool { - return earlier.Name == list.Name || earlier.URL == list.URL - }) { - return nil, fmt.Errorf("%q %w", item, errListedTwice) - } - - lists = append(lists, list) - } - - return lists, nil + return true } -// parseTags reads a comma-separated list of tags, which parseTag reads. A -// tag listed twice is an error, which shows the tag as -// reputation.MaskZoneKey does. -func parseTags(value string) ([]string, error) { - items, err := parseList(value) - if err != nil { - // parseList's error, for an empty item, shows the whole value, keys - // included. - return nil, errEmptyItem - } +var ( + errNotBanResponse = errors.New("is not 403, 429 or close") +) - tags := make([]string, 0, len(items)) +// banResponse reads a setting that is how a refused client is answered. +func (e *environment) banResponse(name, defaultValue string) int { + status, err := parseBanResponse(e.value(name, defaultValue)) + e.check(name, err) - for _, item := range items { - tag, err := parseTag(item) - if err != nil { - return nil, err - } - - if slices.Contains(tags, tag) { - return nil, fmt.Errorf( - "%q %w", - reputation.MaskZoneKey(item), - errListedTwice, - ) - } - - tags = append(tags, tag) - } - - return tags, nil + return status } -// parseTag reads a tag in either case, with spaces around it, and returns it -// as a request carries it: in lower case, but for a country code, which is in -// capitals, an asn= tag's number without leading zeros, and a prefix= tag's -// netblock as routewatch writes it. An asn=, country= or prefix= tag must name -// a number, a country or a netblock, so that a mistyped one stops the start -// instead of never matching. -func parseTag(value string) (string, error) { - tag := strings.ToLower(strings.TrimSpace(value)) - - if digits, isASN := strings.CutPrefix(tag, ASNTagPrefix); isASN { - number, err := strconv.ParseUint(digits, 10, 32) - if err != nil { - return "", fmt.Errorf("%q %w", value, errNotASNTag) - } - - return ASNTagPrefix + strconv.FormatUint(number, 10), nil +// parseBanResponse reads how a refused client is answered: 403, 429, or +// close, which is 0. +func parseBanResponse(value string) (int, error) { + switch value { + case "403": + return http.StatusForbidden, nil + case "429": + return http.StatusTooManyRequests, nil + case "close": + return 0, nil + default: + return 0, fmt.Errorf("%q %w", value, errNotBanResponse) } - - if code, isCountry := strings.CutPrefix(tag, CountryTagPrefix); isCountry { - country, err := parseCountry(code) - if err != nil { - return "", err - } - - return CountryTagPrefix + country, nil - } - - if text, isPrefix := strings.CutPrefix(tag, PrefixTagPrefix); isPrefix { - netblock, err := netip.ParsePrefix(text) - if err != nil || netblock != netblock.Masked() { - return "", fmt.Errorf("%q %w", value, errNotPrefixTag) - } - - return PrefixTagPrefix + netblock.String(), nil - } - - return tag, nil } -// parseTagPercents reads a comma-separated list of tags, each with : and a -// percentage from 1 to 99, such as vpn:50 or asn=14061:25. A tag listed -// twice is an error, and so is ExploitSignatureTag, which a request gets -// only after the rate limits have counted it. -func parseTagPercents(value string) (map[string]int64, error) { - items, err := parseList(value) - if err != nil { - // parseList's error, for an empty item, shows the whole value, keys - // included. - return nil, errEmptyItem - } +const ( + ipv4Bits = 32 +) - percents := make(map[string]int64, len(items)) +var ( + errNotV4Prefix = errors.New( + "is not the length of an IPv4 netblock, from 0 to 32, such as 24") +) - for _, item := range items { - colon := strings.LastIndex(item, ":") - if colon <= 0 { - return nil, fmt.Errorf( - "%q %w", - reputation.MaskZoneKey(item), - errNotTagPercentItem, - ) - } +// v4Prefix reads a setting that is the length of an IPv4 netblock. +func (e *environment) v4Prefix(name, defaultValue string) int { + length, err := parseV4Prefix(e.value(name, defaultValue)) + e.check(name, err) - tag, err := parseTag(item[:colon]) - if err != nil { - return nil, err - } - - percent, err := strconv.ParseInt(item[colon+1:], 10, 64) - if err != nil || percent < 1 || percent > 99 { - return nil, fmt.Errorf( - "%q %w", - reputation.MaskZoneKey(item[colon+1:]), - errNotTagPercent, - ) - } - - if tag == ExploitSignatureTag { - return nil, fmt.Errorf("%q %w", tag, errExploitTag) - } - - if _, listed := percents[tag]; listed { - // tag, since item[:colon] may end in a space, which MaskZoneKey - // would not mask. - return nil, fmt.Errorf( - "%q %w", - reputation.MaskZoneKey(tag), - errListedTwice, - ) - } - - percents[tag] = percent - } - - return percents, nil + return length } -// parsePercents reads a comma-separated list of items, each an AS number -// or a country, which parseCode reads, then : and a percentage, such as -// AS64496:50 or cn:25, and returns each one's percentage. An empty value -// is an empty list. An AS number or country listed twice is an error. -func parsePercents( - value string, parseCode func(string) (string, error), -) (map[string]int64, error) { - items, err := parseList(value) - if err != nil { - return nil, err +// parseV4Prefix reads the length of an IPv4 netblock, from 0 to 32. +func parseV4Prefix(value string) (int, error) { + n, err := strconv.Atoi(value) + if err != nil || n < 0 || n > ipv4Bits { + return 0, fmt.Errorf("%q %w", value, errNotV4Prefix) } - percents := make(map[string]int64, len(items)) - - for _, item := range items { - codeText, percentText, found := strings.Cut(item, ":") - if !found { - return nil, fmt.Errorf("%q %w", item, errNotPercentItem) - } - - code, err := parseCode(codeText) - if err != nil { - return nil, err - } - - percent, err := reputation.ParsePercent(percentText) - if err != nil { - return nil, err - } - - if _, listed := percents[code]; listed { - return nil, fmt.Errorf("%q %w", codeText, errListedTwice) - } - - percents[code] = percent - } - - return percents, nil + return n, nil } -// parseHeaderNames reads a comma-separated list of header names in either -// case, and returns them in lower case. Host and Transfer-Encoding are -// refused: Go's HTTP server takes them out of the request's headers. -func parseHeaderNames(value string) ([]string, error) { - items, err := parseList(value) - if err != nil { - return nil, err - } +// listURLs reads a setting that is a list of the URLs lists are fetched +// from. It is empty by default. +func (e *environment) listURLs(name string) []string { + urls, err := parseListURLs(e.value(name, "")) + e.check(name, err) - headers := make([]string, 0, len(items)) - - for _, item := range items { - if !httpguts.ValidHeaderFieldName(item) { - return nil, fmt.Errorf("%q %w", item, errNotHeaderName) - } - - header := strings.ToLower(item) - switch header { - case "host": - return nil, fmt.Errorf( - "%q %w; the request's host is the field host", - item, - errHeaderTakenOut, - ) - case "transfer-encoding": - return nil, fmt.Errorf("%q %w", item, errHeaderTakenOut) - } - - headers = append(headers, header) - } - - return headers, nil -} - -// parseLogRemoteURL reads where every log line is also sent: -// syslog+udp, syslog+tcp or syslog+tls, a host and a port from 1 to -// 65535, and nothing else. -func parseLogRemoteURL(value string) (*url.URL, error) { - remote, err := url.Parse(value) - if err != nil { - return nil, fmt.Errorf("%q %w", value, errNotLogRemoteURL) - } - - schemes := []string{ - remotelog.SchemeUDP, - remotelog.SchemeTCP, - remotelog.SchemeTLS, - } - port, err := strconv.ParseUint(remote.Port(), 10, 16) - - onlySchemeHostAndPort := slices.Contains(schemes, remote.Scheme) && - remote.Hostname() != "" && err == nil && port != 0 && - remote.User == nil && remote.Opaque == "" && - (remote.Path == "" || remote.Path == "/") && - remote.RawQuery == "" && remote.Fragment == "" - if !onlySchemeHostAndPort { - return nil, fmt.Errorf("%q %w", value, errNotLogRemoteURL) - } - - return remote, nil -} - -// parseFacility reads the name of a syslog facility, and returns its -// number, as RFC 5424 numbers them. -func parseFacility(value string) (int, error) { - //nolint:mnd // the facilities' numbers in RFC 5424 - number, known := map[string]int{ - "kern": 0, "user": 1, "mail": 2, "daemon": 3, "auth": 4, "syslog": 5, - "lpr": 6, "news": 7, "uucp": 8, "cron": 9, "authpriv": 10, "ftp": 11, - "local0": 16, "local1": 17, "local2": 18, "local3": 19, - "local4": 20, "local5": 21, "local6": 22, "local7": 23, - }[value] - if !known { - return 0, fmt.Errorf("%q %w", value, errNotFacility) - } - - return number, nil -} - -// parseWebhookURL reads where each alert is posted: http or https, a -// host, and an optional port from 1 to 65535, path and query, without a -// user or a fragment. It returns the URL, and how the log shows it: its -// scheme and host, and ******** in place of its path and query, if it has -// either. An error shows no part of the value. An empty value is no URL. -func parseWebhookURL(value string) (*url.URL, string, error) { - if value == "" { - return nil, "", nil - } - - webhook, err := url.Parse(value) - if err != nil || !isHTTPURL(webhook) { - return nil, "", errNotWebhookURL - } - - logged := webhook.Scheme + "://" + webhook.Host - if webhook.Path != "" || webhook.RawQuery != "" { - logged += "/" + masked - } - - return webhook, logged, nil -} - -// isHTTPURL reports whether u is http or https, with a host, and an -// optional port from 1 to 65535, path and query, without a user or a -// fragment. -func isHTTPURL(u *url.URL) bool { - port, err := strconv.ParseUint(u.Port(), 10, 16) - - return (u.Scheme == "http" || u.Scheme == "https") && - u.Hostname() != "" && - (u.Port() == "" || (err == nil && port != 0)) && - u.User == nil && - u.Opaque == "" && - u.Fragment == "" -} - -// isListURL reports whether value is a URL a list can be fetched from, as -// isHTTPURL says. -func isListURL(value string) bool { - u, err := url.Parse(value) - - return err == nil && isHTTPURL(u) + return urls } // parseListURLs reads a comma-separated list of the URLs lists are fetched @@ -2423,21 +2288,111 @@ func parseListURLs(value string) ([]string, error) { return urls, nil } +var ( + errNotAnHourOrMore = errors.New( + "is not a duration of 1h or more, such as 24h", + ) +) + +// refresh reads the setting that is how long after a list was last +// fetched or tried it is fetched again: a duration of an hour or more, +// since the Spamhaus lists may be fetched no more often, which cannot be +// off. +func (e *environment) refresh(name, defaultValue string) time.Duration { + value := e.value(name, defaultValue) + + duration, err := parseDuration(value) + if err != nil || duration < time.Hour { + e.check(name, fmt.Errorf("%q %w", value, errNotAnHourOrMore)) + } + + return duration +} + +// ReputationAction is what is done with a client a reputation source +// lists, a value of SWW_WAF_BLOCKLIST_ACTION and of +// SWW_WAF_DNSBL_ABUSEIPDB_ACTION. +type ReputationAction string + +// The values of SWW_WAF_BLOCKLIST_ACTION and SWW_WAF_DNSBL_ABUSEIPDB_ACTION. +const ( + // ReputationActionDeny refuses the client's requests. + ReputationActionDeny ReputationAction = "deny" + // ReputationActionLimit gives the client a percentage of every limit, + // written limit: in the setting. + ReputationActionLimit ReputationAction = "limit" + // ReputationActionLog only notes the hit in the log line and raises an + // alert. + ReputationActionLog ReputationAction = "log" +) + +var ( + errNotAction = errors.New( + "is not deny, limit: such as limit:25, or log") +) + +// action reads a setting that is what is done with a client a blocklist +// or a DNSBL zone lists: deny, log, or limit:, which it returns +// as limit and the percentage. +func (e *environment) action( + name, defaultValue string, +) (ReputationAction, int64) { + value := ReputationAction(e.value(name, defaultValue)) + if value == ReputationActionDeny || value == ReputationActionLog { + return value, 0 + } + + percentText, isLimit := strings.CutPrefix( + string(value), string(ReputationActionLimit)+":", + ) + + percent, err := reputation.ParsePercent(percentText) + if !isLimit || err != nil { + e.check(name, fmt.Errorf("%q %w", value, errNotAction)) + } + + return ReputationActionLimit, percent +} + const ( // maxZoneLength is the most characters a DNSBL zone may have: 253, the // most a DNS name may have, less the 64 that come before the zone in // the name an IPv6 client is asked about by, its 32 hex digits each // followed by a dot. maxZoneLength = 189 - // maxLabelLength is the most characters a label of a DNS name may have. - maxLabelLength = 63 - // labelChars are the characters a label of a DNS zone may hold. - labelChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-" - // dnsPort is the port a resolver is asked on when SWW_WAF_DNSBL_RESOLVER - // gives none. - dnsPort = 53 ) +var ( + errNotZone = errors.New( + "is not a DNS zone such as dnsbl.dronebl.org", + ) + errZoneTooLong = errors.New( + "is longer than 189 characters, too long for the " + + "names IPv6 clients are asked about by", + ) +) + +// zones reads the setting that is the list of DNSBL zones. It is empty by +// default. The log shows each zone with its key masked, as +// reputation.MaskZoneKey masks it. +func (e *environment) zones(name string) []string { + value, _ := e.lookup(name) + zones, err := parseZones(value) + e.check(name, err) + + logged := make([]string, len(zones)) + for i, zone := range zones { + logged[i] = reputation.MaskZoneKey(zone) + } + + e.settings = append( + e.settings, + slog.String(name, strings.Join(logged, ",")), + ) + + return zones +} + // parseZones reads a comma-separated list of DNSBL zones, each a DNS name // such as dnsbl.dronebl.org: labels separated by dots, each of 1 to 63 // letters, digits and hyphens, neither starting nor ending with a hyphen, @@ -2480,21 +2435,26 @@ func parseZones(value string) ([]string, error) { return zones, nil } -// isZone reports whether each label of zone is as parseZones takes it. It -// also checks the hostnames of SWW_WAF_APPS, which are DNS names too. -func isZone(zone string) bool { - for label := range strings.SplitSeq(zone, ".") { - badChar := strings.ContainsFunc(label, func(char rune) bool { - return !strings.ContainsRune(labelChars, char) - }) +const ( + // dnsPort is the port a resolver is asked on when SWW_WAF_DNSBL_RESOLVER + // gives none. + dnsPort = 53 +) - if label == "" || len(label) > maxLabelLength || badChar || - strings.HasPrefix(label, "-") || strings.HasSuffix(label, "-") { - return false - } - } +var ( + errNotResolver = errors.New( + "is not an IP address with an optional port, " + + "such as 192.0.2.53 or [2001:db8::53]:5353", + ) +) - return true +// resolver reads the setting that is the resolver the DNSBL zones are +// asked through, the zero AddrPort while it is unset or empty. +func (e *environment) resolver(name string) netip.AddrPort { + resolver, err := parseResolver(e.value(name, "")) + e.check(name, err) + + return resolver } // parseResolver reads the resolver the DNSBL zones are asked through: an @@ -2519,6 +2479,138 @@ func parseResolver(value string) (netip.AddrPort, error) { return netip.AddrPortFrom(addr, dnsPort), nil } +// secret reads a setting that is a secret another service gave, such as +// an ntfy token or an AbuseIPDB key, "" while it is unset. It is sent in +// a header, which cannot hold a control character, so one in it is an +// error. The log shows ******** in place of a value that is not empty, and +// an error shows none of it. +func (e *environment) secret(name string) string { + value, _ := e.lookup(name) + + logged := "" + if value != "" { + logged = masked + } + + e.settings = append(e.settings, slog.String(name, logged)) + + if strings.ContainsFunc(value, unicode.IsControl) { + e.check(name, errControlCharacter) + } + + return value +} + +const ( + // minAbuseIPDBThreshold is the lowest score AbuseIPDB's blacklist can + // be cut at. + minAbuseIPDBThreshold = 25 +) + +var ( + errNotAbuseIPDBThreshold = errors.New( + "is not a whole number from 25 to 100, the scores AbuseIPDB's " + + "blacklist can be cut at", + ) +) + +// abuseIPDBThreshold reads the setting that is the least score of the +// clients on AbuseIPDB's blacklist: a whole number from 25 to 100, the +// range AbuseIPDB takes. +func (e *environment) abuseIPDBThreshold(name, defaultValue string) int64 { + value := e.value(name, defaultValue) + + threshold, err := strconv.ParseInt(value, 10, 64) + if err != nil || threshold < minAbuseIPDBThreshold || threshold > 100 { + e.check(name, fmt.Errorf("%q %w", value, errNotAbuseIPDBThreshold)) + } + + return threshold +} + +var ( + errNotWholeUnits = errors.New( + "is not a whole number above zero, such as 30, that fits a duration", + ) +) + +// wholeUnits reads a setting that is a whole number above zero of unit, +// such as minutes or days, and returns it as a duration. +func (e *environment) wholeUnits( + name, defaultValue string, unit time.Duration, +) time.Duration { + value := e.value(name, defaultValue) + + n, err := strconv.ParseInt(value, 10, 64) + if err != nil || n <= 0 || n > math.MaxInt64/int64(unit) { + e.check(name, fmt.Errorf("%q %w", value, errNotWholeUnits)) + } + + return time.Duration(n) * unit +} + +var ( + errNotWebhookURL = errors.New( + "is not an http or https URL without a user or a fragment, " + + "such as https://alerts.example/sww") +) + +// webhookURL reads a setting that is a URL each alert is posted to: +// SWW_WAF_ALERT_WEBHOOK_URL, SWW_WAF_ALERT_SLACK_WEBHOOK_URL or +// SWW_WAF_ALERT_NTFY_URL. Unset or empty, it is nil, and no alert is posted +// there. The log shows ******** in place of its path and query, and an +// error shows none of it, since a webhook or an ntfy topic can carry its +// secret there. +func (e *environment) webhookURL(name string) *url.URL { + value, _ := e.lookup(name) + webhook, logged, err := parseWebhookURL(value) + e.settings = append(e.settings, slog.String(name, logged)) + e.check(name, err) + + return webhook +} + +// parseWebhookURL reads where each alert is posted: http or https, a +// host, and an optional port from 1 to 65535, path and query, without a +// user or a fragment. It returns the URL, and how the log shows it: its +// scheme and host, and ******** in place of its path and query, if it has +// either. An error shows no part of the value. An empty value is no URL. +func parseWebhookURL(value string) (*url.URL, string, error) { + if value == "" { + return nil, "", nil + } + + webhook, err := url.Parse(value) + if err != nil || !isHTTPURL(webhook) { + return nil, "", errNotWebhookURL + } + + logged := webhook.Scheme + "://" + webhook.Host + if webhook.Path != "" || webhook.RawQuery != "" { + logged += "/" + masked + } + + return webhook, logged, nil +} + +var ( + errNotWebhookHeader = errors.New( + "is not a header name followed by : and the header's value, " + + "such as Authorization:Bearer ") +) + +// webhookHeaders reads the setting that is the headers sent with each +// alert. The log shows each header's value as ********, since a header +// such as Authorization carries a secret. +func (e *environment) webhookHeaders(name string) http.Header { + value, _ := e.lookup(name) + headers, logged, err := parseWebhookHeaders(value) + e.settings = append(e.settings, slog.String(name, logged)) + e.check(name, err) + + return headers +} + // parseWebhookHeaders reads a comma-separated list of headers, each its // name, :, and its value, and returns them, and how the log shows them, // with each value as ********. An error names the item by its place in @@ -2551,6 +2643,27 @@ func parseWebhookHeaders(value string) (http.Header, string, error) { return headers, strings.Join(logged, ","), nil } +var ( + errNotAlertEvent = errors.New( + "is not ban, permanent_ban, core_rule_set_match, anomaly, reputation_hit, " + + "source_failure or file_error", + ) +) + +// alertEvents reads the setting that is the events alerts are sent for, +// every event by default. +func (e *environment) alertEvents(name string) []alerts.Event { + every := make([]string, 0, len(alerts.Events())) + for _, event := range alerts.Events() { + every = append(every, string(event)) + } + + events, err := parseAlertEvents(e.value(name, strings.Join(every, ","))) + e.check(name, err) + + return events +} + // parseAlertEvents reads a comma-separated list of the events alerts can // be sent for. func parseAlertEvents(value string) ([]alerts.Event, error) { @@ -2572,36 +2685,100 @@ func parseAlertEvents(value string) ([]alerts.Event, error) { return events, nil } -// parseNumberOrOff reads a whole number above zero, or off, which is 0. -func parseNumberOrOff(value string) (int, error) { - if value == off { - return 0, nil - } +// duration reads a setting that is a duration. +func (e *environment) duration(name, defaultValue string) time.Duration { + duration, err := parseDuration(e.value(name, defaultValue)) + e.check(name, err) + return duration +} + +// 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 { + 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), + } +} + +var ( + errNotV6Prefix = errors.New( + "is not the length of an IPv6 netblock, from 0 to 128, such as 48") +) + +// v6Prefix reads a setting that is the length of an IPv6 netblock. +func (e *environment) v6Prefix(name, defaultValue string) int { + length, err := parseV6Prefix(e.value(name, defaultValue)) + e.check(name, err) + + return length +} + +// parseV6Prefix reads the length of an IPv6 netblock, from 0 to 128. +func parseV6Prefix(value string) (int, error) { n, err := strconv.Atoi(value) - if err != nil || n <= 0 { - return 0, fmt.Errorf("%q %w", value, errNotNumberOrOff) + if err != nil || n < 0 || n > ipv6Bits { + return 0, fmt.Errorf("%q %w", value, errNotV6Prefix) } return n, nil } -// appNameMaxLength is the most characters RFC 5424 allows in an -// APP-NAME. -const appNameMaxLength = 48 +var ( + errNotNamedNetblock = errors.New( + "is not a name, = and a netblock, such as office=203.0.113.0/24") +) -// isAppName reports whether value can be an APP-NAME: 1 to -// appNameMaxLength printable ASCII characters, none of them a space. -func isAppName(value string) bool { - if value == "" || len(value) > appNameMaxLength { - return false - } +// namedNetblocks reads a setting that is a list of named netblocks. It is +// empty by default. +func (e *environment) namedNetblocks(name string) []anomaly.NamedNetblock { + named, err := parseNamedNetblocks(e.value(name, "")) + e.check(name, err) - for _, char := range []byte(value) { - if char < '!' || char > '~' { - return false + return named +} + +// parseNamedNetblocks reads a comma-separated list of named netblocks, +// each a name, = and a netblock, such as office=203.0.113.0/24. An empty +// value is an empty list. A name listed twice is an error. +func parseNamedNetblocks(value string) ([]anomaly.NamedNetblock, error) { + items, err := parseList(value) + if err != nil { + return nil, err + } + + named := make([]anomaly.NamedNetblock, 0, len(items)) + + for _, item := range items { + name, netblockText, found := strings.Cut(item, "=") + name = strings.TrimSpace(name) + + if !found || name == "" { + return nil, fmt.Errorf("%q %w", item, errNotNamedNetblock) } + + netblock, err := reputation.ParseNetblock( + strings.TrimSpace(netblockText), + ) + if err != nil { + return nil, err + } + + if slices.ContainsFunc(named, func(n anomaly.NamedNetblock) bool { + return n.Name == name + }) { + return nil, fmt.Errorf("%q %w", name, errListedTwice) + } + + named = append( + named, + anomaly.NamedNetblock{Name: name, Netblock: netblock}, + ) } - return true + return named, nil }