Compare commits

..
Author SHA1 Message Date
sneak a2705dfa7a waf: decide every visitor request within rproxy's timeout (closes #394)
check / check (push) Failing after 3m43s
The Core Rule Set is the one check whose time a visitor can stretch.
At paranoia level 4 it took over 2 s on a request with an 8 KiB
request line, three 8 KiB Cookie lines and a 64 KiB form body.
SWW_WAF_CORE_RULE_SET_BODY_INSPECTION_SIZE now takes at most 8K. The
Core Rule Set also reads no JSON body over 1 KiB, since Coraza turns
JSON into up to 8 times its size in names and values. That brings
such a request under 1 s.

A test per kind of input times the Core Rule Set on the worst request
within the limits, at the slowest settings. SPEC.md lists the limits
that bound it.

Model: opus-5-5
2026-11-15 18:43:33 +00:00
26 changed files with 138 additions and 244 deletions

No files matched your search

+7 -6
View File
@@ -304,7 +304,7 @@ Every setting the code reads, with its default:
| `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_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_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_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 `host1/waf`. | | `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_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_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_TRACKED_CLIENTS` | `20000` | The most clients kept, in memory and in `clients.json`; the least recently seen is dropped first. |
@@ -329,7 +329,7 @@ Every setting the code reads, with its default:
| `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_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_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_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` | `500` | 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_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` | `true`, or `false` while the lookup source is `off` | `true` passes the app the client's AS number and country in `X-Client-ASN` and `X-Client-Country`. | | `SWW_WAF_ADD_LOOKUP_HEADERS` | `true`, or `false` while the lookup source is `off` | `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_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_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`. |
@@ -530,7 +530,7 @@ services:
image: smallwebwaf image: smallwebwaf
environment: environment:
SWW_COMMAND: waf SWW_COMMAND: waf
SWW_WAF_INSTANCE_NAME: host1/waf SWW_WAF_INSTANCE_NAME: fsn1/waf
# Refuse nothing yet; log what enforce mode would have done. # Refuse nothing yet; log what enforce mode would have done.
SWW_WAF_MODE: observe SWW_WAF_MODE: observe
# Look addresses up in a local IPinfo Lite database, never GeoJS. # Look addresses up in a local IPinfo Lite database, never GeoJS.
@@ -818,9 +818,10 @@ on the host.
- `script/docker`: builds the image, whose build runs the tests and the linter - `script/docker`: builds the image, whose build runs the tests and the linter
first. `make docker`. first. `make docker`.
- `script/publish`: runs `script/docker`, whose build happens on the Docker host - `script/publish`: runs `script/docker`, whose build happens on the Docker host
named by `DOCKER_HOST`, then pushes the image to `PUBLISH_IMAGE`, such as named by `DOCKER_HOST` (`ssh://root@fsn1app1` unless set). It then pushes the
`registry.example.com/smallwebwaf`. Both variables are required. It refuses a image to `PUBLISH_IMAGE` (`git.eeqj.de/sneak/smallwebwaf` unless set). It
working tree with uncommitted changes. `make publish`. It pushes four tags: refuses a working tree with uncommitted changes. `make publish`. It pushes
four tags:
- the UTC date as `YYYY-MM-DD`; - the UTC date as `YYYY-MM-DD`;
- the short commit hash; - the short commit hash;
- the branch name; - the branch name;
+7 -12
View File
@@ -2405,12 +2405,12 @@ The Core Rule Set inspects the body a question carries only while
`rproxy` stops waiting for `waf` after `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`, by `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 default 2 seconds, and `SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION` then
decides. The longest a question waits on an outside source is decides. The longest a question waits on an outside source is
`SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 500 milliseconds, well `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 800 milliseconds, well
within that time. within that time.
GeoJS is the only outside source a question can wait on. A question waits only GeoJS is the only outside source a question can wait on. A question waits only
when step 6 says so, and for at most when step 6 says so, and for at most
`SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS`, by default 500 milliseconds. A `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, client without an answer by then counts as unknown. No question waits on these,
which run in the background: which run in the background:
@@ -2451,11 +2451,8 @@ It refuses a request with `431` for any of:
- a header line longer than 8 KiB. Each value of a header sent twice is a line - a header line longer than 8 KiB. Each value of a header sent twice is a line
of its own. `Host` counts as a line. of its own. `Host` counts as a line.
- more than one `Cookie` line. A browser sends all its cookies on one line, as - more than 50 cookies. Each part of its `Cookie` headers between semicolons
RFC 6265 requires. Go's server joins the `Cookie` lines of an HTTP/2 request counts as one, unless it is empty or only spaces and tabs.
into one before `rproxy` sees them.
- more than 50 cookies. Each part of its `Cookie` line between semicolons counts
as one, unless it is empty or only spaces and tabs.
- more than 100 header fields. A header sent twice counts twice. `Host` and - more than 100 header fields. A header sent twice counts twice. `Host` and
`Transfer-Encoding` do not count. `X-Request-ID` and `X-Forwarded-Proto` do. `Transfer-Encoding` do not count. `X-Request-ID` and `X-Forwarded-Proto` do.
@@ -2493,11 +2490,9 @@ body can add up to more than the body. From 1 KiB of JSON, Coraza makes at most
A request can reach all of these at once. At paranoia level 4, with no rule A request can reach all of these at once. At paranoia level 4, with no rule
switched off, the Core Rule Set takes under a second on one core of a current switched off, the Core Rule Set takes under a second on one core of a current
server to inspect it, even with every core busy. Its share of the default 2 server to inspect it. So `waf` answers it within the default 2 seconds, even
seconds is 1.3 seconds: the rest is the default 500 milliseconds of waiting for after the default 800 milliseconds of waiting for GeoJS. Enough such requests at
GeoJS and 200 milliseconds for the other checks. So `waf` answers it in time. once can still overload `waf` past `SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`.
Enough such requests at once can still overload `waf` past
`SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS`.
### The report ### The report
+2 -2
View File
@@ -34,7 +34,7 @@ on one line:
```json ```json
{ {
"instance": "host1/waf", "instance": "fsn1/waf",
"time": "2026-10-06T12:00:50.123461Z", "time": "2026-10-06T12:00:50.123461Z",
"event": "ban", "event": "ban",
"host": "app.example", "host": "app.example",
@@ -109,7 +109,7 @@ on one line:
## Slack and ntfy ## Slack and ntfy
Slack and ntfy get a message with a title and a text. The title is the instance Slack and ntfy get a message with a title and a text. The title is the instance
and the event, such as `host1/waf: ban`. The text is the `reason`, then one line and the event, such as `fsn1/waf: ban`. The text is the `reason`, then one line
for each of these that the alert has: for each of these that the alert has:
- the host, the client, the netblock and the country; - the host, the client, the netblock and the country;
+3 -3
View File
@@ -26,7 +26,7 @@ These questions write no line:
A `report` line: A `report` line:
``` ```
{"type":"request","waf_line":"report","time":"2026-10-03T12:00:00.123Z","instance":"host1/waf","client_ip":"203.0.113.9","method":"GET","scheme":"https","host":"app.example","path":"/","query":"","protocol":"HTTP/1.1","status":200,"request_bytes":0,"response_bytes":5120,"referer":"","user_agent":"curl/8.9.1","request_id":"7Q2NHZ4KJ3VXW5YB6R3MEFTD2A","peer_ip":"172.18.0.2","forwarded_for":"203.0.113.9","client_group":"203.0.113.9/32","asn":"AS64496","as_name":"Example Net","country":"DE","request_headers":{"accept":"*/*"},"action":"forward","counts":{"minute":1,"hour":12,"day":40,"minute_bytes":5120,"hour_bytes":61440,"day_bytes":204800},"core_rule_set_score":0,"duration_total_ms":3.217,"duration_checks_ms":0.041,"duration_core_rule_set_ms":0.029,"duration_upstream_total_ms":3.104} {"type":"request","waf_line":"report","time":"2026-10-03T12:00:00.123Z","instance":"fsn1/waf","client_ip":"203.0.113.9","method":"GET","scheme":"https","host":"app.example","path":"/","query":"","protocol":"HTTP/1.1","status":200,"request_bytes":0,"response_bytes":5120,"referer":"","user_agent":"curl/8.9.1","request_id":"7Q2NHZ4KJ3VXW5YB6R3MEFTD2A","peer_ip":"172.18.0.2","forwarded_for":"203.0.113.9","client_group":"203.0.113.9/32","asn":"AS64496","as_name":"Example Net","country":"DE","request_headers":{"accept":"*/*"},"action":"forward","counts":{"minute":1,"hour":12,"day":40,"minute_bytes":5120,"hour_bytes":61440,"day_bytes":204800},"core_rule_set_score":0,"duration_total_ms":3.217,"duration_checks_ms":0.041,"duration_core_rule_set_ms":0.029,"duration_upstream_total_ms":3.104}
``` ```
### Every line ### Every line
@@ -128,8 +128,8 @@ Every one of these lines has:
- `rule_files`: `rule_id` and `rule_action`, of the rule that refused it; - `rule_files`: `rule_id` and `rule_action`, of the rule that refused it;
- `header_limits`: for a request line, `request_line_bytes` and - `header_limits`: for a request line, `request_line_bytes` and
`request_line_bytes_limit`; for headers, `longest_header_line_bytes`, `request_line_bytes_limit`; for headers, `longest_header_line_bytes`,
`header_line_bytes_limit`, `fields`, `fields_limit`, `cookie_lines`, `header_line_bytes_limit`, `fields`, `fields_limit`, `cookies` and
`cookie_lines_limit`, `cookies` and `cookies_limit`; `cookies_limit`;
- `core_rule_set`: `score`, and `threshold`, - `core_rule_set`: `score`, and `threshold`,
`SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD`; `SWW_WAF_CORE_RULE_SET_SCORE_THRESHOLD`;
- `refusal_limit`: `window`, `count` and `limit`, of the refusal limit; - `refusal_limit`: `window`, `count` and `limit`, of the refusal limit;
+5 -8
View File
@@ -34,7 +34,7 @@ chooses between the two (see "The image's command" below).
such as `:9000`. Leave the host out (see "Ports" in [`SPEC.md`](../SPEC.md)). such as `:9000`. Leave the host out (see "Ports" in [`SPEC.md`](../SPEC.md)).
- `SWW_WAF_INSTANCE_NAME` (default: the container's host name): the `instance` - `SWW_WAF_INSTANCE_NAME` (default: the container's host name): the `instance`
name in each line `waf` logs, and in each alert and metric, such as name in each line `waf` logs, and in each alert and metric, such as
`host1/waf`. Valid UTF-8. No control character while `SWW_WAF_ALERT_NTFY_URL` `fsn1/waf`. Valid UTF-8. No control character while `SWW_WAF_ALERT_NTFY_URL`
is set. 1 to 48 printable ASCII characters without a space while is set. 1 to 48 printable ASCII characters without a space while
`SWW_WAF_LOG_REMOTE_URL` is set and `SWW_WAF_LOG_REMOTE_APP_NAME` is not. `SWW_WAF_LOG_REMOTE_URL` is set and `SWW_WAF_LOG_REMOTE_APP_NAME` is not.
- `SWW_WAF_MODE` (default `enforce`): `enforce` or `observe`. - `SWW_WAF_MODE` (default `enforce`): `enforce` or `observe`.
@@ -158,7 +158,7 @@ cut off; the client is banned.
- an anomaly threshold per AS number. - an anomaly threshold per AS number.
- `SWW_WAF_LOOKUP_DB_PATH` (default empty): the IPinfo Lite database file, in - `SWW_WAF_LOOKUP_DB_PATH` (default empty): the IPinfo Lite database file, in
its `.mmdb` form, for `SWW_WAF_LOOKUP_SOURCE=file`. its `.mmdb` form, for `SWW_WAF_LOOKUP_SOURCE=file`.
- `SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS` (default `500`): how long a - `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 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`. to GeoJS may take. A time in milliseconds; never `off`.
- `SWW_WAF_ADD_LOOKUP_HEADERS` (default `true`): `true` has `rproxy` pass the - `SWW_WAF_ADD_LOOKUP_HEADERS` (default `true`): `true` has `rproxy` pass the
@@ -422,12 +422,9 @@ A few limits are fixed rather than settings:
- at most 20,000 anomaly counters are kept; - at most 20,000 anomaly counters are kept;
- each alert destination's queue holds at most 1,000 alerts; - each alert destination's queue holds at most 1,000 alerts;
- `waf` holds at most 10,000 answered requests awaiting their reports; - `waf` holds at most 10,000 answered requests awaiting their reports;
- `waf` refuses a request with any of these (see "Answering in time" under - `waf` refuses a request with a request line or header line over 8 KiB, more
"`waf`" in [`SPEC.md`](../SPEC.md)): than 50 cookies or more than 100 header fields (see "Answering in time" under
- a request line or header line over 8 KiB; "`waf`" in [`SPEC.md`](../SPEC.md));
- more than one `Cookie` line;
- more than 50 cookies;
- more than 100 header fields;
- `rproxy` takes a request line and headers of at most 32 KiB, and closes a - `rproxy` takes a request line and headers of at most 32 KiB, and closes a
kept-open connection idle for 120 seconds; kept-open connection idle for 120 seconds;
- `rproxy` reads at most 1 MiB of the body of `waf`'s answer, and answers `502` - `rproxy` reads at most 1 MiB of the body of `waf`'s answer, and answers `502`
+1 -1
View File
@@ -12,7 +12,6 @@ require (
github.com/oschwald/maxminddb-golang/v2 v2.7.0 github.com/oschwald/maxminddb-golang/v2 v2.7.0
github.com/prometheus/client_golang v1.24.1 github.com/prometheus/client_golang v1.24.1
github.com/spf13/viper v1.21.0 github.com/spf13/viper v1.21.0
golang.org/x/sys v0.48.0
) )
require ( require (
@@ -48,6 +47,7 @@ require (
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/net v0.60.0 // indirect golang.org/x/net v0.60.0 // indirect
golang.org/x/sync v0.23.0 // indirect golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/text v0.42.0 // indirect golang.org/x/text v0.42.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect google.golang.org/protobuf v1.36.11 // indirect
rsc.io/binaryregexp v0.2.0 // indirect rsc.io/binaryregexp v0.2.0 // indirect
+10 -10
View File
@@ -39,7 +39,7 @@ const (
ntfyURL = "https://ntfy.example/sww-alerts" ntfyURL = "https://ntfy.example/sww-alerts"
ntfyToken = "tk_0123456789abcdefghijklmnopq" ntfyToken = "tk_0123456789abcdefghijklmnopq"
// instance is the instance name every alert gives. // instance is the instance name every alert gives.
instance = "host1/blog" instance = "fsn1app1/blog"
// started is when each test starts, as an alert gives it, and // started is when each test starts, as an alert gives it, and
// anHourOn an hour later. // anHourOn an hour later.
started = "2000-01-01T00:00:00Z" started = "2000-01-01T00:00:00Z"
@@ -1160,8 +1160,8 @@ func TestEachDestinationIsSentOnlyTheAlertsWaitingForIt(t *testing.T) {
synctest.Wait() synctest.Wait()
wantBodies(t, standIns[alerts.DestinationSlack], wantBodies(t, standIns[alerts.DestinationSlack],
`{"text":"*host1/blog: file_error*\nsecond"}`, `{"text":"*fsn1app1/blog: file_error*\nsecond"}`,
`{"text":"*host1/blog: file_error*\nthird"}`) `{"text":"*fsn1app1/blog: file_error*\nthird"}`)
wantBodies(t, standIns[alerts.DestinationNtfy], "fourth") wantBodies(t, standIns[alerts.DestinationNtfy], "fourth")
// alerts.json then lists Slack and ntfy alone, with no alert waiting. // alerts.json then lists Slack and ntfy alone, with no alert waiting.
@@ -1477,7 +1477,7 @@ func anAlertForEachEvent() []eventMessage {
Reason: "requests per hour over the limit of 10000", Reason: "requests per hour over the limit of 10000",
Detail: map[string]any{"mode": "observe"}, Detail: map[string]any{"mode": "observe"},
}, },
"host1/blog: ban", "default no_entry", "fsn1app1/blog: ban", "default no_entry",
"requests per hour over the limit of 10000\nclient: 203.0.113.9\n" + "requests per hour over the limit of 10000\nclient: 203.0.113.9\n" +
"netblock: 203.0.113.0/24\ncountry: DE\nmode: observe", "netblock: 203.0.113.0/24\ncountry: DE\nmode: observe",
}, },
@@ -1489,7 +1489,7 @@ func anAlertForEachEvent() []eventMessage {
), Country: "FR", ), Country: "FR",
Reason: "matched the rule env-file", Reason: "matched the rule env-file",
}, },
"host1/blog: permanent_ban", "high no_entry", "fsn1app1/blog: permanent_ban", "high no_entry",
"matched the rule env-file\nclient: 198.51.100.7\n" + "matched the rule env-file\nclient: 198.51.100.7\n" +
"netblock: 198.51.100.7/32\ncountry: FR", "netblock: 198.51.100.7/32\ncountry: FR",
}, },
@@ -1500,7 +1500,7 @@ func anAlertForEachEvent() []eventMessage {
Reason: "refused by the Core Rule Set", Reason: "refused by the Core Rule Set",
Host: "app.example", Host: "app.example",
}, },
"host1/blog: core_rule_set_match", "default shield", "fsn1app1/blog: core_rule_set_match", "default shield",
"refused by the Core Rule Set\nhost: app.example\nclient: 192.0.2.1\n" + "refused by the Core Rule Set\nhost: app.example\nclient: 192.0.2.1\n" +
"netblock: 192.0.2.1/32", "netblock: 192.0.2.1/32",
}, },
@@ -1509,7 +1509,7 @@ func anAlertForEachEvent() []eventMessage {
Event: alerts.EventAnomaly, Netblock: netip.MustParsePrefix("192.0.2.0/24"), Event: alerts.EventAnomaly, Netblock: netip.MustParsePrefix("192.0.2.0/24"),
Reason: "requests per minute over the threshold of 5000", Reason: "requests per minute over the threshold of 5000",
}, },
"host1/blog: anomaly", "high chart_with_upwards_trend", "fsn1app1/blog: anomaly", "high chart_with_upwards_trend",
"requests per minute over the threshold of 5000\nnetblock: 192.0.2.0/24", "requests per minute over the threshold of 5000\nnetblock: 192.0.2.0/24",
}, },
{ {
@@ -1518,7 +1518,7 @@ func anAlertForEachEvent() []eventMessage {
Netblock: netip.MustParsePrefix("192.0.2.2/32"), Netblock: netip.MustParsePrefix("192.0.2.2/32"),
Reason: "listed by a DNS blocklist", Reason: "listed by a DNS blocklist",
}, },
"host1/blog: reputation_hit", "low label", "fsn1app1/blog: reputation_hit", "low label",
"listed by a DNS blocklist\nclient: 192.0.2.2\nnetblock: 192.0.2.2/32", "listed by a DNS blocklist\nclient: 192.0.2.2\nnetblock: 192.0.2.2/32",
}, },
{ {
@@ -1526,7 +1526,7 @@ func anAlertForEachEvent() []eventMessage {
Event: alerts.EventSourceFailure, Reason: "asking GeoJS failed", Event: alerts.EventSourceFailure, Reason: "asking GeoJS failed",
Detail: map[string]any{"source": "geojs"}, Detail: map[string]any{"source": "geojs"},
}, },
"host1/blog: source_failure", "high warning", "fsn1app1/blog: source_failure", "high warning",
"asking GeoJS failed\nsource: geojs", "asking GeoJS failed\nsource: geojs",
}, },
{ {
@@ -1537,7 +1537,7 @@ func anAlertForEachEvent() []eventMessage {
"file": "/etc/sww/rules.d/50-app.rules", "error": "line 2: no action", "file": "/etc/sww/rules.d/50-app.rules", "error": "line 2: no action",
}, },
}, },
"host1/blog: file_error", "high warning", "fsn1app1/blog: file_error", "high warning",
"a rule file has an error, and the rules stay as they were\n" + "a rule file has an error, and the rules stay as they were\n" +
"file: /etc/sww/rules.d/50-app.rules\nerror: line 2: no action", "file: /etc/sww/rules.d/50-app.rules\nerror: line 2: no action",
}, },
+1 -1
View File
@@ -548,7 +548,7 @@ func WAFFromEnvironment(v *viper.Viper) (*WAF, error) {
LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""), LookupDBPath: env.value("SWW_WAF_LOOKUP_DB_PATH", ""),
ASNAndCountryLookupTimeout: env.milliseconds( ASNAndCountryLookupTimeout: env.milliseconds(
"SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS", "SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS",
"500", "800",
), ),
ExclusivelyAllowedCountries: env.countries( ExclusivelyAllowedCountries: env.countries(
"SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", ""), "SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES", ""),
+10 -10
View File
@@ -218,7 +218,7 @@ const (
// instance is an SWW_WAF_INSTANCE_NAME that is a valid app name too, and // instance is an SWW_WAF_INSTANCE_NAME that is a valid app name too, and
// remoteURL an SWW_WAF_LOG_REMOTE_URL, for the tests that send the lines. // remoteURL an SWW_WAF_LOG_REMOTE_URL, for the tests that send the lines.
const ( const (
instance = "host1/blog" instance = "fsn1app1/blog"
remoteURL = "syslog+udp://192.0.2.1:514" remoteURL = "syslog+udp://192.0.2.1:514"
) )
@@ -286,7 +286,7 @@ func TestDefaults(t *testing.T) {
RuleFilesEnabled: true, RuleFilesEnabled: true,
}) })
wantLookupSettings(t, cfg, config.WAF{ wantLookupSettings(t, cfg, config.WAF{
LookupSource: defaultLookupSource, ASNAndCountryLookupTimeout: 500 * time.Millisecond, LookupSource: defaultLookupSource, ASNAndCountryLookupTimeout: 800 * time.Millisecond,
AddLookupHeaders: true, AddLookupHeaders: true,
}) })
wantByteLimitSettings(t, cfg, config.WAF{ wantByteLimitSettings(t, cfg, config.WAF{
@@ -847,11 +847,11 @@ func TestInstanceNameAndLoggedHeadersAsSet(t *testing.T) {
t.Parallel() t.Parallel()
cfg := wafFromEnvironment(t, environment{ cfg := wafFromEnvironment(t, environment{
instanceName: "host1/blog", instanceName: "fsn1app1/blog",
logRequestHeaders: " Accept , X-Custom", logRequestHeaders: " Accept , X-Custom",
}) })
if cfg.InstanceName != "host1/blog" || if cfg.InstanceName != "fsn1app1/blog" ||
!slices.Equal( !slices.Equal(
cfg.LogRequestHeaders, cfg.LogRequestHeaders,
[]string{"accept", "x-custom"}, []string{"accept", "x-custom"},
@@ -1024,14 +1024,14 @@ func TestInstanceNameNotAnAppNameStopsTheStartOnlyWhileSending(
) { ) {
t.Parallel() t.Parallel()
const spaced = "host1 blog" const spaced = "fsn1 app1"
sending := environment{logRemoteURL: remoteURL, instanceName: spaced} sending := environment{logRemoteURL: remoteURL, instanceName: spaced}
_, err := config.WAFFromEnvironment(sending.viper()) _, err := config.WAFFromEnvironment(sending.viper())
want := logRemoteAppName + `: is unset, and ` + instanceName + want := logRemoteAppName + `: is unset, and ` + instanceName +
` "host1 blog", its default, is not 1 to 48 printable ASCII characters ` + ` "fsn1 app1", its default, is not 1 to 48 printable ASCII characters ` +
`without a space, such as blog` `without a space, such as blog`
if err == nil || err.Error() != want { if err == nil || err.Error() != want {
t.Errorf("error %v, want %s", err, want) t.Errorf("error %v, want %s", err, want)
@@ -1333,13 +1333,13 @@ func TestInstanceNameWithAControlCharacterStopsTheStartOnlyWithNtfySet(
) { ) {
t.Parallel() t.Parallel()
const name = "host1\r" const name = "fsn1app1\r"
_, err := config.WAFFromEnvironment(environment{ _, err := config.WAFFromEnvironment(environment{
instanceName: name, alertNtfyURL: "https://ntfy.example/sww-alerts", instanceName: name, alertNtfyURL: "https://ntfy.example/sww-alerts",
}.viper()) }.viper())
want := instanceName + `: "host1\r" holds a control character, such as the ` + want := instanceName + `: "fsn1app1\r" holds a control character, such as the ` +
`carriage return of a Windows line end, and is sent to ntfy in a header ` + `carriage return of a Windows line end, and is sent to ntfy in a header ` +
`while ` + alertNtfyURL + ` is set` `while ` + alertNtfyURL + ` is set`
if err == nil || err.Error() != want { if err == nil || err.Error() != want {
@@ -1407,7 +1407,7 @@ func TestLookupSettingsAsSet(t *testing.T) {
// With no client looked up, the headers are off unless set. // With no client looked up, the headers are off unless set.
cfg = wafFromEnvironment(t, environment{lookupSource: off}) cfg = wafFromEnvironment(t, environment{lookupSource: off})
wantLookupSettings(t, cfg, config.WAF{ wantLookupSettings(t, cfg, config.WAF{
LookupSource: off, ASNAndCountryLookupTimeout: 500 * time.Millisecond, LookupSource: off, ASNAndCountryLookupTimeout: 800 * time.Millisecond,
}) })
} }
@@ -2933,7 +2933,7 @@ func TestLogsEachSettingWithItsValue(t *testing.T) {
bytesCounted: "both", bytesCounted: "both",
lookupSource: defaultLookupSource, lookupSource: defaultLookupSource,
lookupDBPath: "", lookupDBPath: "",
asnAndCountryLookupTimeout: "500", asnAndCountryLookupTimeout: "800",
addLookupHeaders: enabled, addLookupHeaders: enabled,
allowedCountries: "", allowedCountries: "",
tagLists: "", tagLists: "",
+27 -94
View File
@@ -5,16 +5,12 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/netip" "net/netip"
"regexp"
"runtime"
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
"golang.org/x/sys/unix"
"sneak.berlin/go/smallwebwaf/internal/coreruleset" "sneak.berlin/go/smallwebwaf/internal/coreruleset"
) )
@@ -35,45 +31,12 @@ const (
mostJSONRead = 1 << 10 mostJSONRead = 1 << 10
) )
// The Core Rule Set has 1.3 seconds for one request. That is the default of // inspectionTimeLimit is the most time the Core Rule Set may take on one
// rproxy's SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, 2 seconds, less the default of // request. It is the default of rproxy's SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, 2
// SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, 500 milliseconds, and less 200 // seconds, less the default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, 800
// milliseconds, which a question can also wait for GeoJS, and less 200
// milliseconds for waf's other checks. // milliseconds for waf's other checks.
// const inspectionTimeLimit = time.Second
// A busy host slows all work, so the test does not compare an inspection with
// 1.3 seconds. It compares it with referenceTime, timed on the same thread
// just before and after. inspectionTimeLimitMultiple is how many times
// referenceTime an inspection may take.
//
// On the host that runs our checks, referenceTime took at least 78
// milliseconds, so 16 times it is about 1.25 seconds. With that host fully
// busy, no try of the slowest request took more than 6.7 times referenceTime.
const inspectionTimeLimitMultiple = 16
// referencePattern is what referenceTime matches. Like the Core Rule Set's
// rules, it is a regular expression with alternatives and repeats.
var referencePattern = regexp.MustCompile(
`(?i)(union|select|insert|delete)\s+\w+\s+(from|into)\s+\w+[0-9]+;`,
)
// referenceTime returns the processor time the calling thread takes to do a
// fixed amount of work unrelated to the Core Rule Set, so that a change to the
// rules does not change it. referencePattern matches nowhere in the text, so
// each match reads all of it.
func referenceTime(t *testing.T) time.Duration {
t.Helper()
referenceText := text(4 << 10)
began := threadCPUTime(t)
for range 200 {
if referencePattern.MatchString(referenceText) {
t.Fatal("the reference pattern matched")
}
}
return threadCPUTime(t) - began
}
// text returns n bytes of letters, digits and spaces, which the Core Rule Set // text returns n bytes of letters, digits and spaces, which the Core Rule Set
// takes longer over than one letter repeated. // takes longer over than one letter repeated.
@@ -221,22 +184,20 @@ const fullLine = lineLimit - 64
// worstCaseRequests are, for each kind of thing a visitor sends, the requests // worstCaseRequests are, for each kind of thing a visitor sends, the requests
// that keep the Core Rule Set busiest, within the bounds above. The last // that keep the Core Rule Set busiest, within the bounds above. The last
// holds the most of everything at once. waf refuses a second Cookie line, so // holds the most of everything at once: Cookie lines keep it busier than
// it has one, and User-Agent and Referer lines, which keep the Core Rule Set // other header lines, and the JSON body of the most elements under a key
// busier than other header lines. The JSON body of the most elements under a // keeps it busier than the other bodies.
// key keeps it busier than the other bodies.
func worstCaseRequests() []visitorRequest { func worstCaseRequests() []visitorRequest {
fullCookieLine := cookieLine(1)[:fullLine-len("Cookie: ")]
return append( return append(
append(worstCaseHeaders(), worstCaseBodies()...), append(worstCaseHeaders(), worstCaseBodies()...),
visitorRequest{ visitorRequest{
name: "the longest request line, Cookie, User-Agent and " + name: "the longest request line, three Cookie lines and body",
"Referer lines and body",
target: longestTarget()[:fullLine-len("POST HTTP/1.1")], target: longestTarget()[:fullLine-len("POST HTTP/1.1")],
header: http.Header{ header: http.Header{cookieHeader: {
cookieHeader: {cookieLine(1)[:fullLine-len("Cookie: ")]}, fullCookieLine, fullCookieLine, fullCookieLine,
"User-Agent": {headerValue("User-Agent", fullLine)}, }},
"Referer": {headerValue("Referer", fullLine)},
},
contentType: jsonType, body: jsonOfMostElementsUnderAKey(), contentType: jsonType, body: jsonOfMostElementsUnderAKey(),
}, },
) )
@@ -341,30 +302,11 @@ func worstCaseBodies() []visitorRequest {
} }
} }
// threadCPUTime returns the processor time the calling thread has used. // inspectionTime returns how long crs takes to inspect tc, and fails t unless
func threadCPUTime(t *testing.T) time.Duration { // it reads all of tc's body and Coraza parses it.
t.Helper()
var now unix.Timespec
err := unix.ClockGettime(unix.CLOCK_THREAD_CPUTIME_ID, &now)
if err != nil {
t.Fatalf("read the thread's processor time: %v", err)
}
return time.Duration(now.Nano())
}
// inspectionTime returns the processor time crs takes to inspect tc, and the
// mean of referenceTime just before and after. It fails t unless crs reads all
// of tc's body and Coraza parses it.
//
// All of it runs on this goroutine, kept on one thread, so the thread's
// processor time measures it. Time other work holds the processor does not
// count.
func inspectionTime( func inspectionTime(
t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest, t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest,
) (time.Duration, time.Duration) { ) time.Duration {
t.Helper() t.Helper()
req := httptest.NewRequestWithContext( req := httptest.NewRequestWithContext(
@@ -376,19 +318,11 @@ func inspectionTime(
req.Header.Set("Content-Type", tc.contentType) req.Header.Set("Content-Type", tc.contentType)
} }
runtime.LockOSThread() began := time.Now()
defer runtime.UnlockOSThread()
before := referenceTime(t)
began := threadCPUTime(t)
result, read, _ := crs.Inspect( result, read, _ := crs.Inspect(
req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body), req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body),
) )
took := threadCPUTime(t) - began took := time.Since(began)
reference := (before + referenceTime(t)) / 2
if string(read) != tc.body { if string(read) != tc.body {
t.Errorf("read %d bytes of %d", len(read), len(tc.body)) t.Errorf("read %d bytes of %d", len(read), len(tc.body))
@@ -399,13 +333,13 @@ func inspectionTime(
t.Errorf("Coraza could not parse the body") t.Errorf("Coraza could not parse the body")
} }
return took, reference return took
} }
// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so // TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so
// that the tests running in parallel do not slow it. Other work on the host // that the tests running in parallel do not slow it. Other work on the host
// can still slow one try more than the reference work around it, so a request // can still slow one try, so a request fails only when three tries in a row
// fails only when three tries in a row take too long. // take too long.
// //
//nolint:paralleltest // one at a time, as above //nolint:paralleltest // one at a time, as above
func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) { func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
@@ -419,19 +353,18 @@ func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
for _, tc := range worstCaseRequests() { for _, tc := range worstCaseRequests() {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
var took, reference time.Duration var took time.Duration
for range tries { for range tries {
took, reference = inspectionTime(t, crs, tc) took = inspectionTime(t, crs, tc)
if took <= inspectionTimeLimitMultiple*reference { if took <= inspectionTimeLimit {
return return
} }
} }
t.Errorf( t.Errorf(
"took %s on the last of %d tries, over %d times the %s "+ "took %s on the last of %d tries, over %s",
"of the reference work", took, tries, inspectionTimeLimit,
took, tries, inspectionTimeLimitMultiple, reference,
) )
}) })
} }
+1 -1
View File
@@ -44,7 +44,7 @@ import (
const ( const (
// started is the time a record made as a test starts gives. // started is the time a record made as a test starts gives.
started = "2000-01-01T00:00:00.000Z" started = "2000-01-01T00:00:00.000Z"
appName = "host1/blog" appName = "fsn1app1/blog"
// local0 is the number of the default facility, and local0Info the // local0 is the number of the default facility, and local0Info the
// priority of its records. // priority of its records.
local0 = 16 local0 = 16
+3 -3
View File
@@ -140,7 +140,7 @@ func TestProcessLinesAreMarkedProcessAndGiveTheInstance(t *testing.T) {
var out bytes.Buffer var out bytes.Buffer
requestlog.NewProcessLogger(&out, "host1/blog", slog.LevelInfo). requestlog.NewProcessLogger(&out, "fsn1app1/blog", slog.LevelInfo).
Info("starting", Info("starting",
"version", "v1") "version", "v1")
@@ -152,7 +152,7 @@ func TestProcessLinesAreMarkedProcessAndGiveTheInstance(t *testing.T) {
} }
if fields["type"] != "process" || if fields["type"] != "process" ||
fields["instance"] != "host1/blog" || fields["instance"] != "fsn1app1/blog" ||
fields["msg"] != "starting" || fields["msg"] != "starting" ||
fields["level"] != "INFO" || fields["level"] != "INFO" ||
fields["version"] != "v1" { fields["version"] != "v1" {
@@ -187,7 +187,7 @@ func TestProcessLoggerWritesTheMessagesAtItsLevelOrMoreSevere(t *testing.T) {
processLog := requestlog.NewProcessLogger( processLog := requestlog.NewProcessLogger(
&out, &out,
"host1/blog", "fsn1app1/blog",
level, level,
) )
for _, at := range levels { for _, at := range levels {
+5 -29
View File
@@ -1424,14 +1424,7 @@ func TestFiveFailedAsksKeepTheNextRequestFromAskingWAF(t *testing.T) {
failure(w, r) failure(w, r)
}, },
) )
nextID := make(chan string, 1) app := startApp(t, greet)
app := startApp(t, func(w http.ResponseWriter, r *http.Request) {
if answering.Load() {
nextID <- r.Header.Get(requestIDHeader)
}
greet(w, r)
})
addr, out := startRProxy( addr, out := startRProxy(
t, t,
standIn.URL, standIn.URL,
@@ -1441,36 +1434,19 @@ func TestFiveFailedAsksKeepTheNextRequestFromAskingWAF(t *testing.T) {
}, },
) )
// An ask can time out before it reaches the stand-in, so these
// may leave fewer than five questions, or leave some late.
for range 5 { for range 5 {
wantGreeting(t, get(t, addr, "/")) wantGreeting(t, get(t, addr, "/"))
} standIn.question(t)
// rproxy writes this line before it answers a request it kept
// from asking waf, so no wait is needed.
if out.has("the circuit breaker is open") {
t.Error("the circuit breaker opened before five asks failed")
} }
answering.Store(true) answering.Store(true)
wantGreeting(t, get(t, addr, "/")) wantGreeting(t, get(t, addr, "/"))
out.waitFor(t, "the circuit breaker is open") out.waitFor(t, "the circuit breaker is open")
// Had the next request asked waf, its question would have reached
// the stand-in before the request reached the app.
var id string
select { select {
case id = <-nextID: case <-standIn.questions:
default: t.Error("waf was asked, want it not to be")
t.Fatal("the app did not get the next request") case <-time.After(10 * pollInterval):
}
for len(standIn.questions) > 0 {
asked := <-standIn.questions
if asked.header.Get(requestIDHeader) == id {
t.Error("waf was asked, want it not to be")
}
} }
}) })
} }
+3 -3
View File
@@ -159,7 +159,7 @@ const filledAlertsJSON = `{
"waiting": { "waiting": {
"webhook": [ "webhook": [
{ {
"instance": "host1/blog", "instance": "fsn1app1/blog",
"time": "2026-10-06T00:00:00Z", "time": "2026-10-06T00:00:00Z",
"event": "ban", "event": "ban",
"client": "203.0.113.9", "client": "203.0.113.9",
@@ -174,7 +174,7 @@ const filledAlertsJSON = `{
"suppressed_repeats": 0 "suppressed_repeats": 0
}, },
{ {
"instance": "host1/blog", "instance": "fsn1app1/blog",
"time": "2026-10-06T00:00:00Z", "time": "2026-10-06T00:00:00Z",
"event": "file_error", "event": "file_error",
"client": "", "client": "",
@@ -2398,7 +2398,7 @@ func newParams(dir string) state.Params {
Events: alerts.Events(), Events: alerts.Events(),
Cooldown: 15 * time.Minute, Cooldown: 15 * time.Minute,
MaxPerHour: 2, MaxPerHour: 2,
Instance: "host1/blog", Instance: "fsn1app1/blog",
Now: midnight, Now: midnight,
}) })
+14 -14
View File
@@ -52,7 +52,7 @@ const (
adminSecret = "fedcba9876543210fedcba9876543210" adminSecret = "fedcba9876543210fedcba9876543210"
// instance is the SWW_WAF_INSTANCE_NAME the tests set where they look at // instance is the SWW_WAF_INSTANCE_NAME the tests set where they look at
// it. // it.
instance = "host1/blog" instance = "fsn1app1/blog"
// greeting is what the tests' app answers. // greeting is what the tests' app answers.
greeting = "hello from the app" greeting = "hello from the app"
// placed is the client the tests' lookup databases place. // placed is the client the tests' lookup databases place.
@@ -386,8 +386,8 @@ func TestEveryLogLineAndMetricCarriesTheInstanceName(t *testing.T) {
// the comments. // the comments.
for line := range strings.Lines(metrics) { for line := range strings.Lines(metrics) {
if !strings.HasPrefix(line, "#") && if !strings.HasPrefix(line, "#") &&
!strings.Contains(line, `instance="host1/blog"`) { !strings.Contains(line, `instance="fsn1app1/blog"`) {
t.Errorf("series %q, without instance=\"host1/blog\"", line) t.Errorf("series %q, without instance=\"fsn1app1/blog\"", line)
} }
} }
} }
@@ -734,12 +734,12 @@ func TestLookupDatabaseReplacedWhileRunningTakesEffect(t *testing.T) {
t, t,
url+"_sww/metrics", url+"_sww/metrics",
token, token,
`sww_lookup_database_read_failures_total{instance="host1/blog"} 1`, `sww_lookup_database_read_failures_total{instance="fsn1app1/blog"} 1`,
) )
lastRead = seriesValue( lastRead = seriesValue(
t, t,
metrics, metrics,
`sww_lookup_database_last_read_timestamp_seconds{instance="host1/blog"}`, `sww_lookup_database_last_read_timestamp_seconds{instance="fsn1app1/blog"}`,
) )
wantStatus(t, url, placed, http.StatusOK) wantStatus(t, url, placed, http.StatusOK)
@@ -801,7 +801,7 @@ func TestBlocklistTriesAndCopiesKeptInListsJSONAcrossRestarts(t *testing.T) {
// too. // too.
rateLimitExemptNets: placed + "," + localhost, rateLimitExemptNets: placed + "," + localhost,
} }
failures := `sww_reputation_failures_total{instance="host1/blog",` + failures := `sww_reputation_failures_total{instance="fsn1app1/blog",` +
`source="` + torURL + `"} ` `source="` + torURL + `"} `
// tor.txt cannot be fetched at first, which is counted. // tor.txt cannot be fetched at first, which is counted.
@@ -984,8 +984,8 @@ func TestStalledRemoteLogEndpointHoldsUpNoRequest(t *testing.T) {
// last. // last.
metrics := metricsText(t, url+"_sww/metrics", token) metrics := metricsText(t, url+"_sww/metrics", token)
for _, series := range []string{ for _, series := range []string{
`sww_remote_log_lines_sent_total{instance="host1/blog"} 0`, `sww_remote_log_lines_sent_total{instance="fsn1app1/blog"} 0`,
`sww_remote_log_lines_waiting{instance="host1/blog"} 1`, `sww_remote_log_lines_waiting{instance="fsn1app1/blog"} 1`,
} { } {
if !strings.Contains(metrics, "\n"+series+"\n") { if !strings.Contains(metrics, "\n"+series+"\n") {
t.Errorf("no %q in the metrics:\n%s", series, metrics) t.Errorf("no %q in the metrics:\n%s", series, metrics)
@@ -993,7 +993,7 @@ func TestStalledRemoteLogEndpointHoldsUpNoRequest(t *testing.T) {
} }
const dropped = "\nsww_remote_log_lines_dropped_total" + const dropped = "\nsww_remote_log_lines_dropped_total" +
`{instance="host1/blog"} ` `{instance="fsn1app1/blog"} `
if strings.Contains(metrics, dropped+"0\n") || if strings.Contains(metrics, dropped+"0\n") ||
!strings.Contains(metrics, dropped) { !strings.Contains(metrics, dropped) {
t.Errorf("no line dropped in the metrics:\n%s", metrics) t.Errorf("no line dropped in the metrics:\n%s", metrics)
@@ -1089,7 +1089,7 @@ func TestBanIsAlertedAndAnAlertNotSentIsKeptAcrossARestart(t *testing.T) {
runUntilStopped(t, env, func(url string) { runUntilStopped(t, env, func(url string) {
webhook.waitFor(t, "permanent_ban", true) webhook.waitFor(t, "permanent_ban", true)
const ofWebhook = `{destination="webhook",instance="host1/blog"}` const ofWebhook = `{destination="webhook",instance="fsn1app1/blog"}`
metrics := metricsWith(t, url+"_sww/metrics", token, metrics := metricsWith(t, url+"_sww/metrics", token,
"\nsww_alerts_sent_total"+ofWebhook+" 1\n") "\nsww_alerts_sent_total"+ofWebhook+" 1\n")
@@ -1176,13 +1176,13 @@ func TestBanIsAlertedToSlackAndNtfy(t *testing.T) {
err := json.Unmarshal([]byte(slackPost.body), &message) err := json.Unmarshal([]byte(slackPost.body), &message)
if err != nil || if err != nil ||
!strings.HasPrefix(message.Text, "*host1/blog: ban*\n") || !strings.HasPrefix(message.Text, "*fsn1app1/blog: ban*\n") ||
!strings.Contains(message.Text, "\nclient: "+client+"\n") { !strings.Contains(message.Text, "\nclient: "+client+"\n") {
t.Errorf("Slack was sent %s", slackPost.body) t.Errorf("Slack was sent %s", slackPost.body)
} }
ntfyPost := ntfy.firstPost(t) ntfyPost := ntfy.firstPost(t)
if ntfyPost.header.Get("Title") != "host1/blog: ban" || if ntfyPost.header.Get("Title") != "fsn1app1/blog: ban" ||
ntfyPost.header.Get("Authorization") != "Bearer "+ntfyToken || ntfyPost.header.Get("Authorization") != "Bearer "+ntfyToken ||
!strings.Contains(ntfyPost.body, "\nclient: "+client+"\n") { !strings.Contains(ntfyPost.body, "\nclient: "+client+"\n") {
t.Errorf("ntfy was sent %s, with the headers %v", ntfyPost.body, t.Errorf("ntfy was sent %s, with the headers %v", ntfyPost.body,
@@ -1194,9 +1194,9 @@ func TestBanIsAlertedToSlackAndNtfy(t *testing.T) {
// test process cannot fail the test. // test process cannot fail the test.
sent := []string{ sent := []string{
"\nsww_alerts_sent_total{destination=\"slack\"," + "\nsww_alerts_sent_total{destination=\"slack\"," +
"instance=\"host1/blog\"} 1\n", "instance=\"fsn1app1/blog\"} 1\n",
"\nsww_alerts_sent_total{destination=\"ntfy\"," + "\nsww_alerts_sent_total{destination=\"ntfy\"," +
"instance=\"host1/blog\"} 1\n", "instance=\"fsn1app1/blog\"} 1\n",
} }
metrics := metricsWith(t, url+"_sww/metrics", token, sent...) metrics := metricsWith(t, url+"_sww/metrics", token, sent...)
+1 -1
View File
@@ -18,7 +18,7 @@ const (
alertWebhookURL = "SWW_WAF_ALERT_WEBHOOK_URL" alertWebhookURL = "SWW_WAF_ALERT_WEBHOOK_URL"
alertMaxPerHour = "SWW_WAF_ALERT_MAX_PER_HOUR" alertMaxPerHour = "SWW_WAF_ALERT_MAX_PER_HOUR"
// alertInstance is the instance every alert of these tests gives. // alertInstance is the instance every alert of these tests gives.
alertInstance = "host1/blog" alertInstance = "fsn1app1/blog"
) )
func TestBanForABrokenLimitRaisesABanAlertWithItsNotes(t *testing.T) { func TestBanForABrokenLimitRaisesABanAlertWithItsNotes(t *testing.T) {
+4 -8
View File
@@ -33,7 +33,7 @@ const (
// SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, the time waf has to answer. // SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, the time waf has to answer.
answerTimeLimit = 2 * time.Second answerTimeLimit = 2 * time.Second
// defaultLookupWait is the default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS. // defaultLookupWait is the default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS.
defaultLookupWait = 500 * time.Millisecond defaultLookupWait = 800 * time.Millisecond
// defaultDNSBLAbuseIPDBWait is the default of SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS. // defaultDNSBLAbuseIPDBWait is the default of SWW_WAF_DNSBL_ABUSEIPDB_TIMEOUT_MS.
defaultDNSBLAbuseIPDBWait = 2 * time.Second defaultDNSBLAbuseIPDBWait = 2 * time.Second
// The header limits SPEC.md gives. // The header limits SPEC.md gives.
@@ -111,8 +111,7 @@ func TestHeaderLimitRefusalNamesItsHeuristicCountsAndLimits(t *testing.T) {
s.out.requestLine(t), s.out.requestLine(t),
"header_limits", "header_limits",
`{"longest_header_line_bytes": 40, "header_line_bytes_limit": 8192, `{"longest_header_line_bytes": 40, "header_line_bytes_limit": 8192,
"fields": 101, "fields_limit": 100, "cookie_lines": 0, "fields": 101, "fields_limit": 100, "cookies": 0, "cookies_limit": 50}`,
"cookie_lines_limit": 1, "cookies": 0, "cookies_limit": 50}`,
) )
} }
@@ -199,9 +198,6 @@ func TestAHeaderLineOverTheLimitIsRefusedWith431(t *testing.T) {
{"a header line over the limit", func(req *http.Request) { {"a header line over the limit", func(req *http.Request) {
req.Header.Set("X-Long", strings.Repeat("a", lineLimit+1-len("X-Long: "))) req.Header.Set("X-Long", strings.Repeat("a", lineLimit+1-len("X-Long: ")))
}, true}, }, true},
{"a Cookie line at the limit", func(req *http.Request) {
req.Header.Set(cookieHeader, "a="+strings.Repeat("a", lineLimit-len("Cookie: a=")))
}, false},
{"one cookie over the limit", func(req *http.Request) { {"one cookie over the limit", func(req *http.Request) {
req.Header.Set(cookieHeader, "a="+strings.Repeat("a", lineLimit)) req.Header.Set(cookieHeader, "a="+strings.Repeat("a", lineLimit))
}, true}, }, true},
@@ -271,8 +267,8 @@ func TestEachHeaderLimitRefusesARequestOverIt(t *testing.T) {
{"parts of no-break spaces count", func(req *http.Request) { {"parts of no-break spaces count", func(req *http.Request) {
req.Header.Set(cookieHeader, strings.Repeat(" ;", cookieLimit+1)) req.Header.Set(cookieHeader, strings.Repeat(" ;", cookieLimit+1))
}, true}, }, true},
{"two Cookie lines", func(req *http.Request) { {"cookies over the limit across two headers", func(req *http.Request) {
req.Header[cookieHeader] = []string{"a=1", "b=1"} req.Header[cookieHeader] = []string{cookies(cookieLimit), "last=1"}
}, true}, }, true},
{"cookies without a value count", func(req *http.Request) { {"cookies without a value count", func(req *http.Request) {
req.Header.Set(cookieHeader, strings.Repeat("a;", cookieLimit+1)) req.Header.Set(cookieHeader, strings.Repeat("a;", cookieLimit+1))
+9 -16
View File
@@ -10,15 +10,11 @@ import (
"sneak.berlin/go/smallwebwaf/internal/requestlog" "sneak.berlin/go/smallwebwaf/internal/requestlog"
) )
// The header limits: the most Cookie lines, cookies and header fields a request // The header limits: the most cookies and header fields a request may have,
// may have, and the longest its request line and each header line may be, in // and the longest its request line and each header line may be, in bytes, not
// bytes, not counting the line ending. Real clients rarely reach them. // counting the line ending. Real clients rarely reach them. maxLineBytes is
// maxLineBytes is what nginx allows by default. A browser sends all its cookies // what nginx allows by default.
// on one Cookie line, as RFC 6265 requires, and Go's server joins the Cookie
// lines of an HTTP/2 request into one. A second line would only give a visitor
// another 8 KiB for the Core Rule Set to read.
const ( const (
maxCookieLines = 1
maxCookies = 50 maxCookies = 50
maxHeaderFields = 100 maxHeaderFields = 100
maxLineBytes = 8 << 10 maxLineBytes = 8 << 10
@@ -26,11 +22,10 @@ const (
// headerLimits refuses a request with a request line longer than maxLineBytes, // headerLimits refuses a request with a request line longer than maxLineBytes,
// with 414. It refuses a request with a header line longer than maxLineBytes, // with 414. It refuses a request with a header line longer than maxLineBytes,
// more than maxHeaderFields header fields, more than maxCookieLines Cookie // more than maxHeaderFields header fields or more than maxCookies cookies, with
// lines or more than maxCookies cookies, with 431. Its figures are the request // 431. Its figures are the request line's length and its limit for 414. For
// line's length and its limit for 414. For 431 they are the longest header // 431 they are the longest header line, the header fields and the cookies,
// line, the header fields, the Cookie lines and the cookies, each with its // each with its limit.
// limit.
type headerLimits struct{} type headerLimits struct{}
func (headerLimits) name() string { return "header_limits" } func (headerLimits) name() string { return "header_limits" }
@@ -53,10 +48,9 @@ func (headerLimits) check(
longest := rq.longestHeaderLineBytes() longest := rq.longestHeaderLineBytes()
fields, cookies := rq.headerCounts() fields, cookies := rq.headerCounts()
cookieLines := len(rq.in.Header.Values("Cookie"))
if longest <= maxLineBytes && fields <= maxHeaderFields && if longest <= maxLineBytes && fields <= maxHeaderFields &&
cookieLines <= maxCookieLines && cookies <= maxCookies { cookies <= maxCookies {
return nil return nil
} }
@@ -66,7 +60,6 @@ func (headerLimits) check(
"longest_header_line_bytes": longest, "longest_header_line_bytes": longest,
"header_line_bytes_limit": maxLineBytes, "header_line_bytes_limit": maxLineBytes,
"fields": fields, "fields_limit": maxHeaderFields, "fields": fields, "fields_limit": maxHeaderFields,
"cookie_lines": cookieLines, "cookie_lines_limit": maxCookieLines,
"cookies": cookies, "cookies_limit": maxCookies, "cookies": cookies, "cookies_limit": maxCookies,
}, },
} }
+1 -1
View File
@@ -20,7 +20,7 @@ const (
// requestIDHeader carries the request's id. // requestIDHeader carries the request's id.
requestIDHeader = "X-Request-ID" requestIDHeader = "X-Request-ID"
// instance is the SWW_WAF_INSTANCE_NAME a test sets. // instance is the SWW_WAF_INSTANCE_NAME a test sets.
instance = "host1/blog" instance = "fsn1app1/blog"
// ipv6Client is a client on IPv6, and ipv6Group the netblock the rate // ipv6Client is a client on IPv6, and ipv6Group the netblock the rate
// limits count it as. // limits count it as.
ipv6Client = "2001:db8::7" ipv6Client = "2001:db8::7"
+4 -1
View File
@@ -2,6 +2,9 @@
# script/cibuild: run the CI build. It bootstraps first: a CI runner # script/cibuild: run the CI build. It bootstraps first: a CI runner
# checks out and runs this and nothing else, and script/fmt-check runs # checks out and runs this and nothing else, and script/fmt-check runs
# the formatter on the host, which a pristine checkout cannot do. # the formatter on the host, which a pristine checkout cannot do.
# --no-cache for the same reason as script/docker: the gate phases the
# final stage depends on are RUN steps, and a cached one is a check that
# did not run.
set -eu set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
@@ -17,7 +20,7 @@ main() {
# the version a build stage derives from the .git in the context. # the version a build stage derives from the .git in the context.
version="$(git describe --tags --always --dirty 2>/dev/null || true)" version="$(git describe --tags --always --dirty 2>/dev/null || true)"
[ -n "$version" ] || version="unknown" [ -n "$version" ] || version="unknown"
docker build \ docker build --no-cache \
--build-arg VERSION="$version" \ --build-arg VERSION="$version" \
-t "$("$SCRIPT_DIR/projectname")" . -t "$("$SCRIPT_DIR/projectname")" .
} }
+3 -1
View File
@@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh
# script/docker: build the Docker image tagged with the project name. # script/docker: build the Docker image tagged with the project name.
# Identical in all repos; the tag comes from script/projectname. # Identical in all repos; the tag comes from script/projectname.
# --no-cache because the gate phases the final stage depends on are RUN
# steps, and a cached one is a check that did not run.
set -eu set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
@@ -14,7 +16,7 @@ main() {
# the version a build stage derives from the .git in the context. # the version a build stage derives from the .git in the context.
version="$(git describe --tags --always --dirty 2>/dev/null || true)" version="$(git describe --tags --always --dirty 2>/dev/null || true)"
[ -n "$version" ] || version="unknown" [ -n "$version" ] || version="unknown"
docker build \ docker build --no-cache \
--build-arg VERSION="$version" \ --build-arg VERSION="$version" \
-t "$("$SCRIPT_DIR/projectname")" . -t "$("$SCRIPT_DIR/projectname")" .
} }
+2 -2
View File
@@ -170,8 +170,8 @@ main() {
trap cleanup EXIT trap cleanup EXIT
trap 'exit 1' HUP INT TERM trap 'exit 1' HUP INT TERM
docker build -t "$IMAGE" . docker build --no-cache -t "$IMAGE" .
docker build --build-arg SWW_IMAGE="$IMAGE" \ docker build --no-cache --build-arg SWW_IMAGE="$IMAGE" \
-t "$APP_IMAGE" deploy/example-app -t "$APP_IMAGE" deploy/example-app
docker network create "$NETWORK" >/dev/null docker network create "$NETWORK" >/dev/null
+3 -2
View File
@@ -5,7 +5,8 @@
# make its answer untrustworthy. # make its answer untrustworthy.
# #
# The phase is not the last stage in the file, so it is built only when # The phase is not the last stage in the file, so it is built only when
# --target names it. The tag makes each build replace the previous image # --target names it. --no-cache because a cached lint layer is a lint
# that did not run. The tag makes each build replace the previous image
# instead of leaving a dangling one behind. # instead of leaving a dangling one behind.
set -eu set -eu
@@ -14,7 +15,7 @@ ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
main() { main() {
cd "$ROOT" cd "$ROOT"
docker build \ docker build --no-cache \
--target lint \ --target lint \
-t "$("$SCRIPT_DIR/projectname")-lint" . -t "$("$SCRIPT_DIR/projectname")-lint" .
} }
+6 -12
View File
@@ -2,9 +2,8 @@
# script/publish: build the image on the Docker host and push it to the # script/publish: build the image on the Docker host and push it to the
# registry under four tags: the UTC date (YYYY-MM-DD), the short commit # registry under four tags: the UTC date (YYYY-MM-DD), the short commit
# hash, the branch name and latest. The build is script/docker's, so the # hash, the branch name and latest. The build is script/docker's, so the
# tests and the linter run first. DOCKER_HOST names the Docker host and # tests and the linter run first. The host and the registry name are
# PUBLISH_IMAGE the image in the registry. Both must be set; neither has # environment variables with defaults, for running it against another.
# a default.
# #
# The working tree must be clean: the hash tag names a commit, and an # The working tree must be clean: the hash tag names a commit, and an
# image built from uncommitted changes would carry a hash that does not # image built from uncommitted changes would carry a hash that does not
@@ -14,16 +13,11 @@ set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)" ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
main() { : "${DOCKER_HOST:=ssh://root@fsn1app1}"
if [ -z "${DOCKER_HOST:-}" ]; then : "${PUBLISH_IMAGE:=git.eeqj.de/sneak/smallwebwaf}"
echo "script/publish: DOCKER_HOST is not set; set it to the Docker host that builds the image, such as ssh://user@build.example.com" >&2 export DOCKER_HOST
exit 1
fi
if [ -z "${PUBLISH_IMAGE:-}" ]; then
echo "script/publish: PUBLISH_IMAGE is not set; set it to the image name in the registry, such as registry.example.com/smallwebwaf" >&2
exit 1
fi
main() {
cd "$ROOT" cd "$ROOT"
# Own line: a failing command substitution inside an argument does # Own line: a failing command substitution inside an argument does
+3 -2
View File
@@ -2,7 +2,8 @@
# script/test: run the test suite. Testing is a phase of the Dockerfile # script/test: run the test suite. Testing is a phase of the Dockerfile
# and this builds that phase alone, on the same terms as script/lint: # and this builds that phase alone, on the same terms as script/lint:
# --target because a phase that is not the last stage is built only when # --target because a phase that is not the last stage is built only when
# named, and a tag so each build replaces the previous image. # named, --no-cache because a cached test layer is a test that did not
# run, and a tag so each build replaces the previous image.
set -eu set -eu
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
@@ -10,7 +11,7 @@ ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
main() { main() {
cd "$ROOT" cd "$ROOT"
docker build \ docker build --no-cache \
--target test \ --target test \
-t "$("$SCRIPT_DIR/projectname")-test" . -t "$("$SCRIPT_DIR/projectname")-test" .
} }
+3 -1
View File
@@ -3,13 +3,15 @@
# the test phase of the Dockerfile checks. This builds the Dockerfile's # the test phase of the Dockerfile checks. This builds the Dockerfile's
# tidy-files stage, which holds the two files alone, and --output writes # tidy-files stage, which holds the two files alone, and --output writes
# them into the working tree. The build makes no image, so it has no tag. # them into the working tree. The build makes no image, so it has no tag.
# --no-cache because `go mod tidy` asks the module proxy, whose answers a
# cached layer would repeat.
set -eu set -eu
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)" ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
main() { main() {
cd "$ROOT" cd "$ROOT"
docker build \ docker build --no-cache \
--target tidy-files \ --target tidy-files \
--output "type=local,dest=$ROOT" . --output "type=local,dest=$ROOT" .
} }