Files
openwaf/scripts/smoke.sh
T
Pavlo Ratushnyi 718dcaaba3 a second token can read the operator's surfaces and change none of them
One admin secret meant one thing: whatever could read /admin-api/state could also delete every block and rewrite the policy. A Prometheus scrape therefore held a key that can lift a ban, a second operator checking why a visitor was refused held a key that can change what the edge enforces, and OPEN-61's export put a whole edge's config behind the same single GET. The operator's question was never "does this thing know the secret" but "what is it allowed to do", and the shipped answer could only be all or nothing.

OpenWaf:Admin:ReadOnlyToken (env OPENWAF_READ_ONLY_TOKEN) is the second level. Unset is the default and the whole of the previous behaviour. The rulings:

 * A known token without the right is answered 403, not 401, and the body names the key that would grant the write. Collapsing the two sentences sends someone debugging a failing script to check the secret when what is wrong is the endpoint it was sent to.
 * The line is drawn by the request method, not by a flag per endpoint: GET/HEAD/OPTIONS read, everything else writes. A write endpoint added tomorrow is behind the rule the moment it is written; a per-endpoint flag would fail the other way, and one forgotten flag would open the write.
 * GET /admin-api/export is the one read held to the admin token. It is a read and it is still the whole config, so it is the surface where "can read" and "can take" are the same act - and the dashboard token is exactly the one that must not be able to lift a fleet's config.
 * A read-only token sees byte-identical answers to the admin token. Read-only is about what a token may do, not about a weaker view: two people comparing answers must not get different answers because of who asked.
 * A read-only token equal to the admin token (compared after trimming, because env files add newlines) aborts startup naming both keys and never the secret. /admin-api/state answers readOnlyTokenConfigured, so the operator can check the key took effect instead of guessing from a 401.

What the run caught, in order: (1) A real defect, and the interesting one. On the shipped image the startup refusal printed its reason and then the process sat at 100% CPU inside the runtime's crash path - the container stayed "Up (unhealthy)" forever, no exit code, nothing for a supervisor to react to. Refusing to start is only useful if the process leaves, so the refusal now writes the reason to stderr and returns 1; measured again on the image, it exits in 2 s with code 1 and the same sentence. (2) The gate's own assertion for that case had the same blind spot as the bug: waiting on a foreground docker compose run cannot tell "refused" from "hung", so the container is now started detached and polled for an exit within 30 s, and the assertion is that it left. (3) Two smoke asserts were wrong rather than the product: the blocks listing field is sourceKey, not ip, so the first version read an empty answer, and a two-code assertion compared a newline-separated pair against a space-separated literal. (4) An integration assert compared JsonElement.ToString() against "true" - a bool element prints True, so it now reads GetBoolean().

Gates on this tree: build 0 warnings / 0 errors, unit 307/307, integration 286/286 in 26s, smoke 105 passed / 0 failed (23 new), image 262 MB, compose-smoke 261 passed / 0 failed with 0 containers left behind (18 new, against the shipped image), console 90 assertions / 0 failed.

Refs OPEN-62.
2026-10-05 04:57:59 +02:00

478 lines
31 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
RO_TOKEN=smokereadonly
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"
# The content root is the directory that holds appsettings.json, and ASP.NET
# Core takes it from the current directory — which here is the repository root,
# where there is no appsettings.json. Measured: started this way the edge loaded
# no ReverseProxy section at all, logged "Request reached the end of the
# middleware pipeline without being handled by application code" for every
# request, and answered 404 for a path the upstream was serving with 200. Every
# smoke assertion below expects 404 or a refusal, so the gate kept passing on an
# edge that never proxied anything. Naming the content root is what makes the
# first line of this file's comment — "proxies to a real upstream" — true.
BIN_DIR="$(dirname "$BIN")"
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"}'
# The volume pillar (OPEN-56). A window long enough that the scene below cannot
# race it, and a budget small enough to cross in three requests.
RATE_ON_JSON='{"rateMode":"enforce","rateLimitPerWindow":2,"rateWindowSeconds":120}'
RATE_OFF_JSON='{"rateMode":"Off"}'
RATE_BAD_JSON='{"rateLimitPerWindow":0}'
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" \
OPENWAF_READ_ONLY_TOKEN="$RO_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" --contentRoot "$BIN_DIR" >/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
# The ban is written out of band, after the response that proved it has been
# sent, and the snapshot the guard reads is refreshed by that write. So the
# gate has to wait for the row to exist before it asks the wire whether the ban
# is enforced — otherwise it is racing a durable write and the answer depends on
# how busy the disk is. The wait is bounded, and a timeout leaves the ban
# missing, which is exactly what the assertion below then reports.
for _ in $(seq 1 40); do
curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=engine" \
| grep -q "$SCANNER" && break
sleep 0.25
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"])')"
# The refusal in the visitor's language (OPEN-57). A browser named text/html, so
# it gets sentences; the same refusal stays JSON for the client that did not ask.
check 'a browser gets a page it can read, not a JSON blob' 'text/html; charset=utf-8' \
"$(curl -s -o /dev/null -w '%{content_type}' -H 'Accept: text/html,*/*' \
-H "X-Forwarded-For: $SCANNER" "$(from anything)")"
check 'the page carries the reference an operator can look up' yes \
"$(curl -s -H 'Accept: text/html' -H "X-Forwarded-For: $SCANNER" "$(from anything)" \
| grep -q 'Reference:' && echo yes || echo no)"
check 'and the same refusal is still JSON for a client that parses it' blocked_by_policy \
"$(curl -s -H 'Accept: */*' -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 '- metrics (OPEN-58)'
# The pull surface. The integration tests hold the format; this holds the
# deployed shape — the content type a scraper negotiates on, the token that
# closes it, and the one property the whole feature exists for: an edge that is
# refusing a source can still be watched by that source's operator.
check 'the metrics surface is closed without the token' 401 \
"$(code "http://127.0.0.1:$EDGE_PORT/metrics")"
check 'metrics answers in the Prometheus exposition format' 'text/plain; version=0.0.4; charset=utf-8' \
"$(curl -s -o /dev/null -w '%{content_type}' -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/metrics")"
METRICS=$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/metrics")
msample() { printf '%s\n' "$METRICS" | awk -v p="$1" 'index($0,p)==1 { print $NF; exit }'; }
check 'the body declares a type for every family it samples' yes \
"$([ "$(printf '%s\n' "$METRICS" | grep -c '^# TYPE ')" -ge 20 ] && echo yes || echo no)"
check 'no value in the body is NaN or Inf' yes \
"$(printf '%s\n' "$METRICS" | grep -Eq 'NaN|Inf' && echo no || echo yes)"
BLOCK_ENGINE=$(msample 'openwaf_requests_total{verdict="block_engine"} ')
check 'the verdict counter counts the ban this scene just made' yes \
"$([ "${BLOCK_ENGINE:-0}" -gt 0 ] 2>/dev/null && echo yes || echo no)"
check 'the live blocks gauge counts the engine ban' 1 \
"$(msample 'openwaf_blocks_live{source="engine"} ')"
# The push queue (OPEN-60) is a table, so its numbers are publishable the same
# way. No endpoint is configured in this scene, so the honest answer is three
# zeros — and a gauge that is missing is a different failure from a gauge that
# says 0, which is why msample's empty answer fails the check.
check 'the push queue is published as gauges as well' '0 0 0 0' \
"$(msample 'openwaf_alert_push_pending ') $(msample 'openwaf_alert_push_delivered_total ') \
$(msample 'openwaf_alert_push_given_up_total ') $(msample 'openwaf_alert_push_attempts_total ')"
# The aggregate-only invariant, asserted over the whole body with the markers
# from the scene above actually in the database behind it.
check 'the body names no address, no path and no operator note' yes \
"$(printf '%s\n' "$METRICS" | grep -Eq "$SCANNER|wp-admin|confirmed scraper" && echo no || echo yes)"
check 'a source the edge is refusing can still be watched' 200 \
"$(code -H "X-Admin-Token: $TOKEN" -H "X-Forwarded-For: $SCANNER" "http://127.0.0.1:$EDGE_PORT/metrics")"
echo '- asking the edge what it would answer (OPEN-59)'
# The dry run. The assertion that matters is not the verdict — the scene above
# already proved the ban is enforced — it is that the preview answers about the
# named address and says which pillar spoke, including the ones that never got
# asked.
DECIDE=$(curl -s -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/decide?ip=$SCANNER&path=/anything")
check 'the preview answers with the verdict the wire gives' block_engine \
"$(printf '%s' "$DECIDE" | python3 -c 'import sys,json;print(json.load(sys.stdin)["verdict"])')"
check 'and names the pillar that spoke' engineBan \
"$(printf '%s' "$DECIDE" | python3 -c 'import sys,json;p=json.load(sys.stdin)["pillars"];print(next(r["pillar"] for r in p if r["outcome"]=="answered"))')"
check 'and says the geo pillar was never asked in this configuration' not_enforced_in_this_mode \
"$(printf '%s' "$DECIDE" | python3 -c 'import sys,json;p=json.load(sys.stdin)["pillars"];print(next(r["outcome"] for r in p if r["pillar"]=="geo"))')"
check 'a typo in the address is refused rather than answered with pass' 400 \
"$(code -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/decide?ip=203.0.113&path=/anything")"
check 'a parameter this surface never read is refused before it is ignored' 400 \
"$(code -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/decide?ip=$SCANNER&path=/anything&source=engine")"
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)")"
# The exemption listing used to be the only listing with no ceiling: every paging
# parameter was ignored and the response said nothing about how many rows there
# were. Measured on the shipped image: 1101 rows, 130,386 bytes, no X-Total-Count,
# and ?limit=10 answered all 1101.
check 'the allow listing says how many rows there are' yes \
"$(curl -s -D- -o /dev/null -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/allow" | grep -qi '^x-total-count:' && echo yes || echo no)"
check 'the allow listing refuses a limit nobody meant' 400 \
"$(code -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/allow?limit=0")"
check 'a page of one is a page of one' 1 \
"$(curl -s -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/allow?limit=1" | python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')"
# A path rule is the one pillar about the request rather than about what the
# upstream answered, so the smoke run asks the question the feature exists to
# answer: does a refused path reach the origin or not.
# Not $Bystander: the allowlist scene above exempted 198.51.100.0/24, and an
# exempt address is the one answer a path rule must not override.
RULE_IP=203.0.113.77
RULE_JSON='{"pattern":"/gate-blocked*","action":"block","note":"smoke: the request itself"}'
BAD_RULE_JSON='{"pattern":"gate-blocked","action":"block"}'
echo '- a path rule refuses the request itself'
check 'add a blocking path rule' 204 \
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$RULE_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/rules")"
check 'the path is refused rather than proxied' 403 \
"$(code -H "X-Forwarded-For: $RULE_IP" "$(from gate-blocked)")"
check 'a browser gets the same refusal in sentences' yes \
"$(curl -s -H 'Accept: text/html' -H "X-Forwarded-For: $RULE_IP" "$(from gate-blocked)" \
| grep -q 'This path is refused on this site.' && echo yes || echo no)"
check 'the refusal names the rule and not the pattern' yes \
"$(curl -s -H "X-Forwarded-For: $RULE_IP" "$(from gate-blocked)" | grep -q blocked_by_rule && echo yes || echo no)"
check 'a pattern that could never match is refused' 400 \
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$BAD_RULE_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/rules")"
check 'the rules listing holds the rule' 1 \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/rules" | python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')"
RULE_ID="$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/rules" | python3 -c 'import sys,json;print(json.load(sys.stdin)[0]["id"])')"
check 'the rule can be removed by id' 204 \
"$(code -X DELETE -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/rules/$RULE_ID")"
check 'and the path flows again once it is gone' 404 \
"$(code -H "X-Forwarded-For: $RULE_IP" "$(from gate-blocked)")"
# The volume pillar, and the one claim it exists to make: the origin is happy to
# serve this traffic, so no streak, no rule and no list says anything about it —
# only the count does. index.html is the 200 this upstream will give.
echo 'openwaf smoke' > /tmp/openwaf-upstream/index.html
RATE_IP=203.0.113.88
echo '- the operator config leaves the edge as one document (OPEN-61)'
# The transfer. A block is written, the whole config is exported, the block is
# deleted, and the exported document is imported back. The assertions are on the
# wire, not on the JSON: the document has to be enough to rebuild what the edge
# enforces, including the redirect the operator typed.
TRANSFER_IP=203.0.113.151
TRANSFER_JSON='{"ip":"203.0.113.151","reason":"written for the transfer","redirectTo":"https://honeypot.example.org/seen"}'
check 'the block this scene starts with is in force' 204 \
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$TRANSFER_JSON" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
check 'and the wire says so' 301 "$(code -H "X-Forwarded-For: $TRANSFER_IP" "$(from anything)")"
EXPORT_JSON="$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/export")"
check 'the export names the version this build reads' 1 \
"$(printf '%s' "$EXPORT_JSON" | python3 -c 'import sys,json;print(json.load(sys.stdin)["version"])')"
check 'the export counts its sections' yes \
"$(printf '%s' "$EXPORT_JSON" | python3 -c 'import sys,json;c=json.load(sys.stdin)["counts"];print("yes" if all(k in c for k in ("blocks","allow","rules","countries")) else "no")')"
check 'and says how many engine bans it deliberately left out' yes \
"$(printf '%s' "$EXPORT_JSON" | python3 -c 'import sys,json;print("yes" if isinstance(json.load(sys.stdin).get("engineBlocksLeftBehind"), int) else "no")')"
check 'the exported document carries no engine ban' yes \
"$(printf '%s' "$EXPORT_JSON" | python3 -c 'import sys,json;print("yes" if all("203.0.113.66" != b["ip"] for b in json.load(sys.stdin)["blocks"]) else "no")')"
check 'deleting the block takes it off the wire' 200 \
"$(code -X DELETE -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=operator&key=$TRANSFER_IP")"
check 'and the path flows again' 404 "$(code -H "X-Forwarded-For: $TRANSFER_IP" "$(from anything)")"
IMPORT_JSON="$(curl -s -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$EXPORT_JSON" "http://127.0.0.1:$EDGE_PORT/admin-api/import")"
check 'importing the document lands the block again' 1 \
"$(printf '%s' "$IMPORT_JSON" | python3 -c 'import sys,json;print(json.load(sys.stdin)["blocks"]["added"])')"
check 'and the wire enforces it, redirect and all' yes \
"$(curl -s -D- -o /dev/null -H "X-Forwarded-For: $TRANSFER_IP" "$(from anything)" \
| tr -d '\r' | grep -qi '^location: https://honeypot.example.org/seen' && echo yes || echo no)"
check 'importing the same document again adds nothing' '0 0' \
"$(curl -s -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$EXPORT_JSON" "http://127.0.0.1:$EDGE_PORT/admin-api/import" \
| python3 -c 'import sys,json;d=json.load(sys.stdin);print(d["blocks"]["added"], d["rules"]["added"])')"
BEFORE_IMPORT_BLOCKS="$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?live=false" \
| python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')"
check 'a document from another version is refused naming the version' yes \
"$(curl -s -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
-d '{"version":999,"blocks":[{"ip":"203.0.113.152"}]}' \
"http://127.0.0.1:$EDGE_PORT/admin-api/import" | grep -q '999' && echo yes || echo no)"
check 'and it wrote nothing' "$BEFORE_IMPORT_BLOCKS" \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?live=false" \
| python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')"
check 'the transfer row is removable again, so the run ends clean' 200 \
"$(code -X DELETE -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=operator&key=$TRANSFER_IP")"
echo '- a second token that can look but not change'
# The read-only token (OPEN-62). The interesting assertions are not the 200s —
# a read is a read — but the shape of the refusal: a known token that lacks the
# right answers 403 and names the key that would grant it, while an unknown
# token still answers 401. Those two sentences are the feature; collapsing them
# is what would make an operator debug the wrong thing.
check 'the state says a second token exists on this edge' True \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/state" \
| python3 -c 'import sys,json;print(json.load(sys.stdin)["readOnlyTokenConfigured"])')"
check 'the read-only token reads the state' 200 \
"$(code -H "X-Admin-Token: $RO_TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/state")"
check 'the read-only token reads the block list' 200 \
"$(code -H "X-Admin-Token: $RO_TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
check 'the read-only token reads the path rules' 200 \
"$(code -H "X-Admin-Token: $RO_TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/rules")"
check 'the read-only token reads the policy' 200 \
"$(code -H "X-Admin-Token: $RO_TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/policy")"
check 'the read-only token can watch /metrics' 200 \
"$(code -H "X-Admin-Token: $RO_TOKEN" "http://127.0.0.1:$EDGE_PORT/metrics")"
check 'a read-only token presented in the query string is served' 200 \
"$(code "http://127.0.0.1:$EDGE_PORT/admin-api/observations?token=$RO_TOKEN")"
RO_WRITE="$(curl -s -X POST -H "X-Admin-Token: $RO_TOKEN" -H 'Content-Type: application/json' \
-d '{"ip":"203.0.113.155","reason":"must not land"}' \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
check 'a read-only token cannot write a block' 403 \
"$(code -X POST -H "X-Admin-Token: $RO_TOKEN" -H 'Content-Type: application/json' \
-d '{"ip":"203.0.113.155","reason":"must not land"}' \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
check 'the refusal says the token is read-only, not unknown' yes \
"$(printf '%s' "$RO_WRITE" | python3 -c 'import sys,json;print("yes" if "read-only" in json.load(sys.stdin)["error"] else "no")')"
check 'and it names the key that would grant the write' yes \
"$(printf '%s' "$RO_WRITE" | grep -q 'OpenWaf__Admin__Token' && echo yes || echo no)"
check 'the write really did not land' 0 \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?live=false" \
| python3 -c 'import sys,json;print(len([b for b in json.load(sys.stdin) if b["sourceKey"]=="203.0.113.155"]))')"
check 'a read-only token cannot change the policy' 403 \
"$(code -X PUT -H "X-Admin-Token: $RO_TOKEN" -H 'Content-Type: application/json' --data-binary "$POLICY_OFF_JSON" \
"http://127.0.0.1:$EDGE_PORT/admin-api/policy")"
check 'a read-only token cannot delete a block' 403 \
"$(code -X DELETE -H "X-Admin-Token: $RO_TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=operator")"
check 'a read-only token cannot import a document' 403 \
"$(code -X POST -H "X-Admin-Token: $RO_TOKEN" -H 'Content-Type: application/json' \
-d '{"version":1,"blocks":[]}' "http://127.0.0.1:$EDGE_PORT/admin-api/import")"
check 'the export is not readable by the read-only token' 403 \
"$(code -H "X-Admin-Token: $RO_TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/export")"
check 'and the refusal says the document is a capability' yes \
"$(curl -s -H "X-Admin-Token: $RO_TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/export" \
| grep -q 'capability' && echo yes || echo no)"
check 'the admin token still writes what the other one cannot' 204 \
"$(code -X POST -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
-d '{"ip":"203.0.113.156","reason":"written by the admin token"}' \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
# Cleaned up straight away: the later sections count operator rows, and a row
# left behind by this scene would be read as one of theirs.
check 'and the admin-token row is removable again' 200 \
"$(code -X DELETE -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=operator&key=203.0.113.156")"
check 'an unknown token is still 401, not 403' 401 \
"$(code -H "X-Admin-Token: not-a-token" "http://127.0.0.1:$EDGE_PORT/admin-api/state")"
check 'and an unknown token cannot write either' 401 \
"$(code -X POST -H "X-Admin-Token: not-a-token" -H 'Content-Type: application/json' \
-d '{"ip":"203.0.113.157"}' "http://127.0.0.1:$EDGE_PORT/admin-api/blocks")"
# The configuration this build refuses to start with: a read-only token that is
# also the admin token. Started for real, on its own port and its own database,
# because "refused at startup" is only worth claiming if a process that is
# actually configured that way fails to come up. The message has to name both
# keys and must not repeat the secret — startup output is where secrets leak.
BAD_DB=/tmp/openwaf-smoke-bad-$$.db
BAD_LOG=/tmp/openwaf-smoke-bad-$$.log
rm -f "$BAD_DB"
ASPNETCORE_URLS="http://127.0.0.1:$((EDGE_PORT+7))" \
OPENWAF_ADMIN_TOKEN="$TOKEN" \
OPENWAF_READ_ONLY_TOKEN="$TOKEN" \
ConnectionStrings__OpenWaf="Data Source=$BAD_DB" \
dotnet "$BIN" --contentRoot "$BIN_DIR" >"$BAD_LOG" 2>&1
BAD_RC=$?
check 'an edge whose read-only token is also its admin token does not start' yes \
"$([ "$BAD_RC" -ne 0 ] && echo yes || echo no)"
check 'it names both keys in the reason' yes \
"$(grep -q 'OpenWaf__Admin__ReadOnlyToken' "$BAD_LOG" && grep -q 'OpenWaf__Admin__Token' "$BAD_LOG" && echo yes || echo no)"
check 'and it never prints the secret' yes \
"$(grep -q "$TOKEN" "$BAD_LOG" && echo no || echo yes)"
rm -f "$BAD_DB" "$BAD_DB-shm" "$BAD_DB-wal" "$BAD_LOG"
echo '- volume budget'
STATE0_JSON="$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/state")"
check 'the budget is off until an operator writes one' Off \
"$(printf '%s' "$STATE0_JSON" | python3 -c 'import sys,json;print(json.load(sys.stdin)["rateMode"])')"
check 'a written budget is live without a restart' Enforce \
"$(curl -s -X PUT -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$RATE_ON_JSON" "http://127.0.0.1:$EDGE_PORT/admin-api/policy" \
| python3 -c 'import sys,json;print(json.load(sys.stdin)["rateMode"])')"
check 'a budget outside its range is refused by name' 400 \
"$(code -X PUT -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$RATE_BAD_JSON" "http://127.0.0.1:$EDGE_PORT/admin-api/policy")"
check 'the first request inside the budget is proxied' 200 \
"$(code -H "X-Forwarded-For: $RATE_IP" "$(from index.html)")"
check 'so is the second' 200 \
"$(code -H "X-Forwarded-For: $RATE_IP" "$(from index.html)")"
check 'the third crosses the budget and is answered by the edge' 429 \
"$(code -H "X-Forwarded-For: $RATE_IP" "$(from index.html)")"
check 'the refusal says when the pause ends' yes \
"$(curl -s -D- -o /dev/null -H "X-Forwarded-For: $RATE_IP" "$(from index.html)" \
| grep -qi '^retry-after:' && echo yes || echo no)"
check 'the breach wrote an engine ban the operator can see' 1 \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=engine" \
| python3 -c "import sys,json;print(len([b for b in json.load(sys.stdin) if b['sourceKey']=='$RATE_IP']))")"
check 'and the ban says volume, not refusals' rate \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=engine" \
| python3 -c "import sys,json;print('rate' if any('rate policy' in (b.get('reason') or '') for b in json.load(sys.stdin) if b['sourceKey']=='$RATE_IP') else 'none')")"
check 'the listing says what the limiter counted' 3 \
"$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/rate?source=$RATE_IP" \
| python3 -c 'import sys,json;print(json.load(sys.stdin)["sources"][0]["count"])')"
check 'the operator can clear the ban the breach wrote' '{"deleted":1}' \
"$(curl -s -X DELETE -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/blocks?source=engine&key=$RATE_IP" | tr -d ' ')"
check 'the budget can be switched off again' Off \
"$(curl -s -X PUT -H "X-Admin-Token: $TOKEN" -H 'Content-Type: application/json' \
--data-binary "$RATE_OFF_JSON" "http://127.0.0.1:$EDGE_PORT/admin-api/policy" \
| python3 -c 'import sys,json;print(json.load(sys.stdin)["rateMode"])')"
rm -f /tmp/openwaf-upstream/index.html
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"])')"
# Each pillar keeps its last good value when its own read fails, which is the
# right failure posture and was, until recently, an unmentioned one. The healthy
# case has to be said with numbers too — silence and "0" look the same from
# outside, and only one of them is a claim.
STATE_JSON="$(curl -s -H "X-Admin-Token: $TOKEN" "http://127.0.0.1:$EDGE_PORT/admin-api/state")"
check 'the state counts failed pillar reads, and there are none' '0 0' \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;d=json.load(sys.stdin);print(d["snapshotRefreshFailures"], d["policyReloadFailures"])')"
check 'the state says the snapshot is inside its ttl' False \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;print(json.load(sys.stdin)["snapshotStale"])')"
# Whether a raised alert goes anywhere on its own. Off by default, and the
# surface has to say which of "no alerts" and "no push" it is answering.
check 'the state says alerts are not pushed anywhere by default' False \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;print(json.load(sys.stdin)["alertWebhookConfigured"])')"
# What the push queue holds (OPEN-60): owed, delivered, given up. The keys are
# asserted before the values, because "0" from a surface that never writes the
# queue is indistinguishable from "0" from one that does.
check 'the state names the push queue it keeps' True \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;d=json.load(sys.stdin);print(all(k in d for k in ("alertPushPending","alertPushDelivered","alertPushGivenUp","alertPushLastError","alertPushOldestPendingAt")))')"
check 'and with no endpoint configured it keeps nothing' '0 0 0' \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;d=json.load(sys.stdin);print(d["alertPushPending"], d["alertPushDelivered"], d["alertPushGivenUp"])')"
check 'the policy answer carries its provenance header' 0 \
"$(curl -s -D- -o /dev/null -H "X-Admin-Token: $TOKEN" \
"http://127.0.0.1:$EDGE_PORT/admin-api/policy" | tr -d '\r' \
| sed -n 's/^[Xx]-[Pp]olicy-[Rr]eload-[Ff]ailures: //p')"
# The write side of the same contract. A failed write is worse than a failed
# read — the row never existed and no later request brings it back — so the
# counters exist even in a clean run, where they read 0 0 0. A surface that
# answers "0" for a field it never publishes is indistinguishable from one that
# publishes it and means it, which is why the keys are asserted too.
check 'the state counts writes it could not persist, and there are none' '0 0 0 0' \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;d=json.load(sys.stdin);print(d["banWriteFailures"], d["observationWriteFailures"], d["alertWriteFailures"], d["retentionPruneFailures"])')"
check 'and it carries what was lost and when a write last landed' True \
"$(printf '%s' "$STATE_JSON" | python3 -c 'import sys,json;d=json.load(sys.stdin);print(all(k in d for k in ("bansNotWritten","banLastWriteAt","observationsLost","alertsLost","observationLastWriteAt","alertLastWriteAt","retentionLastPruneAt")))')"
check 'readiness is ready while the write path is healthy' 200 \
"$(code "http://127.0.0.1:$EDGE_PORT/readyz")"
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 ]