From b6126c2fd2b618cd1440170da881d7320bfeb2c6 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Thu, 1 Oct 2026 16:24:02 +0200 Subject: [PATCH] Two rules that had only ever been proven inside a test process MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Server errors alert, they never ban` and `Observe mode records the decision and blocks nothing` are the two claims that protect honest traffic from this WAF: a bad deploy must not lock out the site's own visitors, and a dry run must be judgeable before it is allowed to act. Both were proven in-process, against `FakeUpstream`, in `EdgeBlockingTests`. Neither had ever been proven against the image users actually run. And `GET /admin-api/alerts` appeared in **zero** gate scripts — the operator-visible half of the alerting pillar had no gate at all, which is how the console gate found its first bug: everything below the surface looks fine while nothing reads it. `deploy/upstream.conf` is nginx's own default plus one path that answers 502, mounted read-only into the compose upstream. `scripts/compose-smoke.sh` grew two sections, 28 → **35 assertions**: - server errors alert, and never ban a 502 reaches the client through the edge / four 502s did not ban the source that saw them / no engine ban written / a server_error_streak alert exists - Observe mode records the decision and blocks nothing five refusals still reach the upstream / no ban written / the dry run is visible as a candidate_ban alert Numbers: host **35 passed, 0 failed**; the same script in the Linux rehearsal (`docker:cli` against the host daemon) **35 passed, 0 failed**. Two lessons about counting, both written into the script: - The data volume survives `compose down`, so an absolute alert count asserts history rather than this run. Alerts are counted as a delta now. - The alert is written *after* the response is, so a count read the instant the last request returns undercounts. The first version of the measurement line printed 1 where the settled number is 2. A number that is wrong by a race is worse than no number, so the count is taken after a pause. The measurement itself is the finding: **4 requests past N=3 wrote 2 alert rows** — one row per response past the threshold, not one per source. A real 5xx storm therefore writes at request rate. Retention ages alerts out (`BanRetention`), so they do not accumulate forever, but the write rate is a number an operator should know before their upstream falls over. Honest about the fixture: the 502 is nginx returning it directly, not an application failing behind nginx. The edge's classifier sees a status code on a socket, which is exactly what the rule is about; the fixture does not simulate a crashed process, and nothing should read it as if it did. --- deploy/upstream.conf | 30 +++++++++++++++++ docker-compose.yml | 6 ++++ scripts/compose-smoke.sh | 70 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 deploy/upstream.conf diff --git a/deploy/upstream.conf b/deploy/upstream.conf new file mode 100644 index 0000000..747152b --- /dev/null +++ b/deploy/upstream.conf @@ -0,0 +1,30 @@ +# The demo upstream in docker-compose.yml. Deliberately close to nginx's own +# default so the compose file still demonstrates the honest case: an application +# the edge knows nothing about, where a missing page is a 404 and the edge has no +# opinion about it. +# +# One addition: /boom answers 502. Not because the demo is broken, but because +# the "server errors alert, they never ban" rule is the most important safety +# property in this project — a bad deploy must not lock out the site's own +# visitors — and until now it was proven only in-process, against a fake +# upstream. scripts/compose-smoke.sh hits /boom three times from one source and +# asserts the source is still not banned and that an alert exists. In the shipped +# image, over a socket. +# +# Nothing in this file is used by the edge. It is the upstream's own config. +server { + listen 80; + server_name _; + + location / { + root /usr/share/nginx/html; + index index.html; + } + + # 502 on purpose, for the gate. A real 502 is the upstream failing; nginx + # returning one directly is the same status on the same wire, which is all + # the edge's classifier sees. + location = /boom { + return 502; + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 89e79ef..0720525 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,6 +33,12 @@ services: upstream: image: nginx:alpine restart: unless-stopped + # deploy/upstream.conf is nginx's own default plus one path that answers 502, + # so scripts/compose-smoke.sh can prove "server errors alert and never ban" + # against the deployed image instead of only against a fake upstream in a + # test. Remove this mount and the demo still works; that assertion fails. + volumes: + - ./deploy/upstream.conf:/etc/nginx/conf.d/default.conf:ro volumes: openwaf-data: diff --git a/scripts/compose-smoke.sh b/scripts/compose-smoke.sh index 267a068..e742b3a 100755 --- a/scripts/compose-smoke.sh +++ b/scripts/compose-smoke.sh @@ -36,13 +36,19 @@ admin() { curl -s -m 10 -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$@"; } # per assertion — sed over JSON silently returns the first match of anything. jcount() { python3 -c "import json,sys d=json.load(sys.stdin); print(sum(1 for b in d if $1))"; } -jget() { python3 -c "import json,sys +jget() { python3 -c "import json,sys d=json.load(sys.stdin) for o in d: if $1: print(o.get('$2') or ''); break"; } -code() { curl -s -m 10 -o /dev/null -w '%{http_code}' "$@"; } -hdr() { curl -s -m 10 -D - -o /dev/null "$@" | tr -d '\r'; } -xff() { printf 'X-Forwarded-For: %s' "$1"; } +code() { curl -s -m 10 -o /dev/null -w '%{http_code}' "$@"; } +hdr() { curl -s -m 10 -D - -o /dev/null "$@" | tr -d '\r'; } +xff() { printf 'X-Forwarded-For: %s' "$1"; } +# Alerts are counted as a delta (see the alert sections): the data volume +# survives `compose down`, so an absolute count would be history, not this run. +# jcount's loop variable is `b` (jget's is `o`) — the two helpers read the same +# JSON with different names, and mixing them up is a NameError inside an +# assertion, which is exactly the kind of gate that reports nothing. +alerts_for() { admin "$EDGE/admin-api/alerts?limit=500" | jcount "b['kind']=='$1' and b['sourceKey']=='$2'"; } cleanup() { admin -X PUT -H 'Content-Type: application/json' \ @@ -142,6 +148,62 @@ admin -X DELETE "$EDGE/admin-api/allow/$ALLOW_ID" >/dev/null check 'block returns when the allow entry goes' '301' "$(code -H "$(xff "$OFFICE")" "$EDGE/any/path")" admin -X DELETE "$EDGE/admin-api/blocks?source=operator" >/dev/null +echo '- server errors alert, and never ban' +# The most important safety property in this project: a broken deploy must not +# lock out the site's own visitors. Until now it was proven only in-process, +# against a fake upstream. deploy/upstream.conf makes the demo upstream answer 502 +# on /boom, so the shipped image is what gets tested here. +ERR_SRC=203.0.113.88 +admin -X PUT -H 'Content-Type: application/json' \ + -d '{"mode":"Enforce","geo":"Off","streakThreshold":3,"trustProxyHeaders":true}' "$EDGE/admin-api/policy" >/dev/null +# Counted as a delta, not an absolute: the data volume survives `compose down`, +# so an alert from yesterday's run would otherwise satisfy the assertion and the +# gate would be checking history instead of this run. +BEFORE_ERR=$(alerts_for server_error_streak "$ERR_SRC") +last='' +for _ in 1 2 3 4; do last=$(code -H "$(xff "$ERR_SRC")" "$EDGE/boom"); done +check 'a 502 from the upstream reaches the client through the edge' '502' "$last" +check 'four 502s did not ban the source that saw them' '404' "$(code -H "$(xff "$ERR_SRC")" "$EDGE/no-such-page")" +check 'and no engine ban was written for it' '0' "$(admin "$EDGE/admin-api/blocks" | jcount "b['sourceKey']=='$ERR_SRC'")" +ACOUNT=0 +for _ in $(seq 1 20); do + ACOUNT=$(alerts_for server_error_streak "$ERR_SRC") + [ "${ACOUNT:-0}" -gt "${BEFORE_ERR:-0}" ] && break + sleep 0.5 +done +ERR_DELTA=$(( ${ACOUNT:-0} - ${BEFORE_ERR:-0} )) +check 'a server_error_streak alert was written for that source' 'yes' "$([ "$ERR_DELTA" -ge 1 ] && echo yes || echo no)" +# Not an assertion, a measurement. The alert fires on every response *past* the +# threshold, so a 5xx storm writes one row per request rather than one per +# source. The count is taken after a pause on purpose: the alert is written after +# the response is, so a count read the instant the last request returns +# undercounts — the first version of this line read 1 where the settled number is +# 2, and a number that is wrong by a race is worse than no number. Retention ages +# alerts out (BanRetention), but the write rate is what an operator should see. +sleep 2 +ERR_SETTLED=$(alerts_for server_error_streak "$ERR_SRC") +printf ' info alert rows for 4 requests past the threshold: %s (N=3 — one per response past N)\n' \ + "$(( ${ERR_SETTLED:-0} - ${BEFORE_ERR:-0} ))" + +echo '- Observe mode records the decision and blocks nothing' +# The claim is that a dry run is *judgeable*: without the alert there is nothing +# to judge, and without the missing ban there is nothing safe about the dry run. +OBS_SRC=203.0.113.120 +admin -X PUT -H 'Content-Type: application/json' \ + -d '{"mode":"Observe","geo":"Off","streakThreshold":3,"trustProxyHeaders":true}' "$EDGE/admin-api/policy" >/dev/null +BEFORE_OBS=$(alerts_for candidate_ban "$OBS_SRC") +for _ in 1 2 3 4 5; do code -H "$(xff "$OBS_SRC")" "$EDGE/no-such-page" >/dev/null; done +check 'five refusals in Observe mode still reach the upstream' '404' "$(code -H "$(xff "$OBS_SRC")" "$EDGE/no-such-page")" +check 'and no engine ban was written in Observe mode' '0' "$(admin "$EDGE/admin-api/blocks" | jcount "b['sourceKey']=='$OBS_SRC'")" +OBS_ALERT=no +for _ in $(seq 1 20); do + [ "$(alerts_for candidate_ban "$OBS_SRC")" -gt "${BEFORE_OBS:-0}" ] && OBS_ALERT=yes && break + sleep 0.5 +done +check 'the dry run is visible as a candidate_ban alert' 'yes' "$OBS_ALERT" +admin -X PUT -H 'Content-Type: application/json' \ + -d '{"mode":"Enforce","geo":"Off","streakThreshold":3,"trustProxyHeaders":true}' "$EDGE/admin-api/policy" >/dev/null + echo '- country deny list' check 'the geo source reaches the upstream while geo is off' '404' "$(code -H "$(xff "$GEO_IP")" "$EDGE/no-such-page")" # The observation stream is deliberately coalesced (one durable upsert per