Files
openwaf/scripts/smoke.sh
T
Pavlo Ratushnyi a3d0777492 openwaf: edge WAF with one decision point and a streak ban
Initial implementation of the OPEN-2 epic: a .NET 10 YARP edge that
decides from what the upstream answered.

Design (docs/ARCHITECTURE.md, tracker authority 4c9de3f6):
- one short-circuit point, ordered: always-pass prefixes -> allowlist ->
  operator block (301) -> engine ban (403 + Retry-After) -> geo (451)
- a post-response observer feeds a consecutive-refusal streak; N refusals
  in M minutes ban for BanDuration; 5xx alerts and never bans
- provenance on the block row, so clearing engine bans cannot touch an
  operator's entries; the toggle is Off/Observe/Enforce and changes no rows
- inherited protections from the reference deployment: one durable write
  per source per 30s, LRU-bounded streak map, no DB on the request path

Verification: 36 unit + 14 integration tests, plus scripts/smoke.sh (19
assertions against a running edge and a real upstream, including the
distroless --healthcheck mode).

Findings worth keeping (documented in docs/ARCHITECTURE.md):
- ctx.RequestAborted silently cancelled the ban write, because the
  observer runs after the response is written
- SQLite cannot translate DateTimeOffset comparisons; timestamps are
  stored as integers there and stay timestamptz on PostgreSQL
- CIDR masking order silently widened prefixes (198.51.100.24/19 -> /16)
- an empty string in appsettings.json defeats the env fallback (?? on "")
- concurrent ban writes raced into the unique index and lost evidence

Also moves the unrelated 2026-09-30 inference benchmarks out of the
project root into research/.
2026-10-01 02:13:01 +02:00

130 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# End-to-end smoke test against a real edge and a real upstream.
#
# The integration tests prove the decision logic in isolation; this proves the
# deployed shape: the binary starts, proxies to an upstream it knows nothing
# about, bans a source that drew enough refusals, answers the way the design
# says it should, and healthchecks itself the way a shell-less distroless image
# needs.
#
# Sources are simulated with X-Forwarded-For and TrustProxyHeaders=on. That is
# not a shortcut: it is the only way to exercise "this source is banned, that
# one is not" over a loopback connection, and it doubles as a check that the
# trusted-proxy path is wired.
set -uo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
EDGE_PORT=${EDGE_PORT:-18088}
UP_PORT=${UP_PORT:-18900}
DB=/tmp/openwaf-smoke-$$.db
TOKEN=smoketoken
BIN="$ROOT/dotnet/OpenWaf.Edge/bin/Release/net10.0/OpenWaf.Edge.dll"
[ -f "$BIN" ] || BIN="$ROOT/dotnet/OpenWaf.Edge/bin/Debug/net10.0/OpenWaf.Edge.dll"
export HEALTHCHECK_PORT="$EDGE_PORT"
SCANNER=203.0.113.66
OFFICER=198.51.100.7
Bystander=198.51.100.99
# Payloads live in single-quoted variables: embedding them in the command
# substitutions below with escaped quotes is exactly the kind of thing that
# silently truncates a body.
BLOCK_JSON='{"ip":"198.51.100.7","reason":"confirmed scraper","redirectTo":"https://honeypot.example.org/"}'
ALLOW_JSON='{"match":"198.51.100.0/24","note":"office"}'
POLICY_OFF_JSON='{"mode":"Off"}'
cleanup() {
[ -n "${EDGE_PID:-}" ] && kill "$EDGE_PID" 2>/dev/null
[ -n "${UP_PID:-}" ] && kill "$UP_PID" 2>/dev/null
rm -f "$DB" "$DB-shm" "$DB-wal"
}
trap cleanup EXIT
mkdir -p /tmp/openwaf-upstream
python3 -m http.server "$UP_PORT" --bind 127.0.0.1 -d /tmp/openwaf-upstream >/dev/null 2>&1 &
UP_PID=$!
rm -f "$DB"
ASPNETCORE_URLS="http://127.0.0.1:$EDGE_PORT" \
OPENWAF_ADMIN_TOKEN="$TOKEN" \
ConnectionStrings__OpenWaf="Data Source=$DB" \
OpenWaf__Policy__StreakThreshold=3 \
OpenWaf__Policy__Mode=Enforce \
OpenWaf__Policy__BanDuration="00:00:06" \
OpenWaf__Policy__TrustProxyHeaders=true \
ReverseProxy__Clusters__upstream__Destinations__upstream1__Address="http://127.0.0.1:$UP_PORT/" \
dotnet "$BIN" >/tmp/openwaf-smoke.log 2>&1 &
EDGE_PID=$!
for _ in $(seq 1 60); do
curl -fsS "http://127.0.0.1:$EDGE_PORT/healthz" >/dev/null 2>&1 && break
sleep 0.25
done
pass=0; fail=0
check() {
if [ "$2" = "$3" ]; then pass=$((pass+1)); printf ' ok %-56s %s\n' "$1" "$3"
else fail=$((fail+1)); printf ' FAIL %-56s expected %s, got %s\n' "$1" "$2" "$3"; fi
}
code() { curl -s -o /dev/null -w '%{http_code}' "$@"; }
from() { printf 'http://127.0.0.1:%s/%s' "$EDGE_PORT" "$1"; } # usage: from path + -H "X-Forwarded-For: ip"
echo "edge :$EDGE_PORT upstream :$UP_PORT db $DB"
echo '- streak ban'
for i in 1 2 3; do
check "probe $i from the scanner reaches the upstream" 404 \
"$(code -H "X-Forwarded-For: $SCANNER" "$(from wp-admin)")"
done
check '4th request from the scanner is refused by the edge' 403 \
"$(code -H "X-Forwarded-For: $SCANNER" "$(from anything)")"
check 'a bystander is untouched' 404 \
"$(code -H "X-Forwarded-For: $Bystander" "$(from anything)")"
check 'the refusal explains itself' blocked_by_policy \
"$(curl -s -H "X-Forwarded-For: $SCANNER" "$(from anything)" | python3 -c 'import sys,json;print(json.load(sys.stdin)["error"])')"
check 'the ban carries a Retry-After' yes \
"$(curl -s -D- -o /dev/null -H "X-Forwarded-For: $SCANNER" "$(from anything)" | grep -qi '^retry-after:' && echo yes || echo no)"
echo '- operator list'
check 'add an operator block' 204 \
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$BLOCK_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
check 'an operator block answers 301 with Location' https://honeypot.example.org/ \
"$(curl -s -o /dev/null -w '%{redirect_url}' -H "X-Forwarded-For: $OFFICER" "$(from anything)")"
echo '- allowlist outranks everything'
check 'allow the office range' 204 \
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$ALLOW_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/allow")"
check 'the blocked office address gets through' 404 \
"$(code -H "X-Forwarded-For: $OFFICER" "$(from anything)")"
check 'the scanner outside that range stays banned' 403 \
"$(code -H "X-Forwarded-For: $SCANNER" "$(from anything)")"
echo '- expiry and clearing'
sleep 7
check 'the engine ban lifted itself' 404 "$(code -H "X-Forwarded-For: $SCANNER" "$(from wp-admin)")"
check 'clearing engine bans leaves operator rows' '{"deleted":1}' \
"$(curl -s -X DELETE -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=engine" | tr -d ' ')"
check 'an operator row survives it' 1 \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=operator" | python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')"
echo '- admin surface'
check 'no token is refused' 401 "$(code "http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
check 'the policy is readable' Enforce \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/policy" | python3 -c 'import sys,json;print(json.load(sys.stdin)["mode"])')"
check 'switching the engine off is accepted' Off \
"$(curl -s -X PUT -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' --data-binary "$POLICY_OFF_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/policy" | python3 -c 'import sys,json;print(json.load(sys.stdin)["mode"])')"
echo '- healthcheck mode (a shell-less image can only ask the binary)'
dotnet "$BIN" --healthcheck; check 'healthcheck exit code' 0 "$?"
echo
echo "passed $pass, failed $fail"
[ "$fail" -eq 0 ]