The edge-wide budget is one number for the whole site, and for a login path that number is always wrong: a hundred requests a minute does not stop a credential-stuffing run that makes twenty attempts a minute — twenty looks like a person — while five a minute locks an honest office behind one NAT out of every page they own. The only way to make both statements true is to price the paths differently. The rulings this had to make, and the answer each one got: * A rate rule is not a path rule. A path rule refuses a request because of what it asked for; a budget refuses it because of how often that source asked. The pattern language is shared because the thing being described — this path and what hangs off it — is the same. * Crossing a budget writes no engine ban. The claim is about one path, so a ban would refuse the rest of the site as punishment for a login-page rate. The answer is 429 with Retry-After for the rest of that source's window, and the reason names the rule, its count, its limit and the pattern it was written for. * The counters are a separate tracker, not a scope inside RateTracker. The edge-wide counter reads its limit and window from the policy; a rule carries its own. One map whose parameters change per entry is a map whose eviction and idle rules stop meaning anything. * The budgets hang off rateMode rather than getting a switch of their own: Enforce refuses, Observe counts and raises one rate_breach alert per source per window naming the pattern, Off counts nothing at all. A half-counting edge is the state an operator cannot reason about. * A second live budget for one pattern is a 409 — two live numbers for one path mean the stricter one wins by accident rather than by the operator's choice. * The bounds are the policy's own (1..100000 requests, 1..3600 seconds), so one vocabulary of numbers covers both budgets and a document cannot bring in a budget the API would have refused. * The listing answers with three counters — sources counted, windows running, over budget now — because "nobody has blown this" and "nobody can" look identical on paper and are different decisions. What the runs caught, in order: * The smoke scene first ran on 198.51.100.0/24, which an earlier scene of the same gate allowlists. An allowlisted source never reaches the counter, so the scene would have printed a row of 200s while measuring nothing. It runs on TEST-NET-1 now. * The console scene drew three 404s of its own and the streak pillar banned the address, so the reopen assertion read the streak's 403 instead of the absence of the budget. The engine list is cleared before that probe and the comment says which pillar the clearing belongs to. * The compose assertion compared a JSON boolean against the word "yes". * The change-log vocabulary test still listed six sections; there are seven now. * A unit expectation counted requests where the tracker holds windows: one (rule, source) pair is one window, and the count inside it is what the budget is measured against. Gates at this commit, each stage on its own: build 0 warnings / 0 errors, unit 361/361, integration 337/337 in 30 s, console 120 assertions 0 failed, smoke passed 169 failed 0, compose 300 passed 0 failed against the built image at 262 MB, 0 containers left behind. Refs OPEN-68.
161 lines
9.5 KiB
Bash
Executable File
161 lines
9.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# The console gate: start the stack, seed the two things the page has to show
|
|
# (a source sitting at 2 of a threshold of 3, and an operator block with a
|
|
# reason), drive the page in a real Chromium, tear the stack down.
|
|
#
|
|
# Needs docker and playwright-core. Set PLAYWRIGHT_CORE=/path/to/playwright-core
|
|
# if it is not installed locally. The script does not skip when playwright is
|
|
# missing — it exits non-zero, because a gate that reports success having
|
|
# asserted nothing is worse than no gate.
|
|
#
|
|
# OPENWAF_ADMIN_TOKEN=… SCREENSHOT=/tmp/console.png ./scripts/console-smoke.sh
|
|
# SEED_ONLY=1 … same, but leave the seeded stack for something else to drive
|
|
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
|
|
export OPENWAF_ADMIN_TOKEN="${OPENWAF_ADMIN_TOKEN:?set OPENWAF_ADMIN_TOKEN before running, or run scripts/ci-gates.sh (it defaults a throwaway for the stack it starts itself)}"
|
|
EDGE=${EDGE:-http://127.0.0.1:8080}
|
|
SEED_SOURCE=203.0.113.55
|
|
SEED_BLOCK=203.0.113.77
|
|
# Per run: the alerts table is ordered newest first and the data volume survives
|
|
# `compose down`, so a fixed address here would let last week's alert satisfy the
|
|
# assertion. A fresh one makes the check about this run.
|
|
SEED_ERROR="203.0.113.$(( 140 + RANDOM % 60 ))"
|
|
# An allow entry whose minute has passed by the time the page is driven. Seeded
|
|
# before the two waits this script already pays, so it costs no extra second and
|
|
# produces the state the shipped image printed as a live exemption: the row is
|
|
# still in the table, the edge stopped honouring it a minute ago. Chosen per run,
|
|
# so a leftover row in the data volume cannot satisfy the assertion.
|
|
SEED_EXPIRED_CIDR="203.0.113.$(( 240 + RANDOM % 10 ))/32"
|
|
export EDGE_URL="$EDGE" SEED_SOURCE SEED_BLOCK SEED_ERROR SEED_MARKUP_PATH SEED_MARKUP_BLOCK SEED_EXPIRED_CIDR SEED_NETBLOCK SEED_NETBLOCK_FRESH SEED_PATH_BUDGET_PATH SEED_PATH_BUDGET_IP
|
|
|
|
admin() { curl -s -m 10 -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" "$@"; }
|
|
|
|
cleanup() {
|
|
admin -X PUT -H 'Content-Type: application/json' \
|
|
-d '{"mode":"Enforce","geo":"Off","streakThreshold":10,"trustProxyHeaders":false}' "$EDGE/admin-api/policy" >/dev/null 2>&1
|
|
admin -X DELETE "$EDGE/admin-api/blocks?source=operator" >/dev/null 2>&1
|
|
# The gate probes 404 paths, and the seed sets the streak threshold to 3, so
|
|
# the gate can ban its own probe address. An engine ban lives in the data
|
|
# volume and outlives `compose down`, and the next run's first assertion is
|
|
# "before the block, this address reaches the upstream" — which a leftover ban
|
|
# answers with 403 for the whole ban duration.
|
|
admin -X DELETE "$EDGE/admin-api/blocks?source=engine" >/dev/null 2>&1
|
|
# The page can write allow entries. The gate removes the one it added, but if
|
|
# it failed before that step the entry would outlive the run inside the data
|
|
# volume and quietly change the next run — so clear them here too.
|
|
# live=false: the default listing hides the expired rows, and an expired row
|
|
# left in the volume quietly changes what the next run's table shows.
|
|
for id in $(admin "$EDGE/admin-api/allow?live=false&limit=2000" | tr ',' '\n' | sed -n 's/.*"id":\([0-9]*\).*/\1/p'); do
|
|
admin -X DELETE "$EDGE/admin-api/allow/$id" >/dev/null 2>&1
|
|
done
|
|
# The gate sets a US country rule and removes it again. PUT allowed=true would
|
|
# leave a row behind that only says "not denied"; DELETE /countries/{code} takes
|
|
# the rule out, which matters because the row lives in the data volume and
|
|
# outlives `compose down`.
|
|
admin -X DELETE "$EDGE/admin-api/countries/US" >/dev/null 2>&1
|
|
# The page can write path rules, and a rule outlives `compose down` in the
|
|
# data volume — where a leftover block rule would refuse a path for every run
|
|
# after this one. live=false so an expired rule is cleared too.
|
|
for id in $(admin "$EDGE/admin-api/rules?live=false&limit=2000" | tr ',' '\n' | sed -n 's/.*"id":\([0-9]*\).*/\1/p'); do
|
|
admin -X DELETE "$EDGE/admin-api/rules/$id" >/dev/null 2>&1
|
|
done
|
|
[ "${KEEP_UP:-0}" = 1 ] || docker compose down >/dev/null 2>&1
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
echo '- starting the stack'
|
|
docker compose up -d --build >/dev/null 2>&1 || { echo 'compose up failed'; exit 1; }
|
|
for _ in $(seq 1 120); do
|
|
[ "$(docker compose ps -q edge | xargs docker inspect --format '{{.State.Health.Status}}')" = healthy ] && break
|
|
sleep 0.5
|
|
done
|
|
|
|
# Threshold 3 and trusted proxy headers, so two refusals from one address show
|
|
# as "2 / 3" without tripping the ban. The policy lives in the shared database
|
|
# and reaches the guard on the next snapshot refresh — hence the wait, which is
|
|
# the same TTL the multi-instance measurement is about.
|
|
admin -X PUT -H 'Content-Type: application/json' \
|
|
-d '{"mode":"Enforce","geo":"Off","streakThreshold":3,"trustProxyHeaders":true}' "$EDGE/admin-api/policy" >/dev/null
|
|
# The expiring exemption is seeded here, ahead of the waits below, so it has aged
|
|
# out by the time the page is driven without adding a second to the gate. The API
|
|
# cannot be asked for an expiry in the past — expiresInMinutes is 1..5,256,000 —
|
|
# so waiting is the only honest way to produce this state.
|
|
admin -X POST -H 'Content-Type: application/json' \
|
|
-d "{\"match\":\"$SEED_EXPIRED_CIDR\",\"note\":\"console gate: expired exemption\",\"expiresInMinutes\":1}" \
|
|
"$EDGE/admin-api/allow" >/dev/null
|
|
sleep 31
|
|
|
|
echo '- seeding what the page must show'
|
|
for _ in 1 2; do curl -s -o /dev/null -H "X-Forwarded-For: $SEED_SOURCE" "$EDGE/probe"; done
|
|
curl -s -o /dev/null -H 'X-Forwarded-For: 8.8.8.8' "$EDGE/no-such-page"
|
|
admin -X POST -H 'Content-Type: application/json' \
|
|
-d "{\"ip\":\"$SEED_BLOCK\",\"reason\":\"console gate: operator block\"}" "$EDGE/admin-api/blocks" >/dev/null
|
|
|
|
# Markup in a path an anonymous visitor sends, and markup in a reason the
|
|
# operator types. Both end up in a table cell. The page renders every API value
|
|
# through esc(), and this is the assertion that keeps it that way: the only
|
|
# browser in this system holding the admin token is the one that renders them,
|
|
# so an unescaped cell is a stored XSS against the operator. The path is
|
|
# percent-encoded on the wire and arrives decoded — that is what the edge
|
|
# stores as lastPath and what the table then prints.
|
|
SEED_MARKUP_SOURCE="203.0.113.$(( 180 + RANDOM % 30 ))"
|
|
SEED_MARKUP_BLOCK="203.0.113.$(( 215 + RANDOM % 30 ))"
|
|
SEED_MARKUP_PATH="/xss-probe-$(( RANDOM % 32768 ))/%3Cimg%20src%3D1%20alt%3Dprobe%20onerror%3D%22document.title%3D%27XSS%27%22%3E"
|
|
curl -s -o /dev/null -H "X-Forwarded-For: $SEED_MARKUP_SOURCE" "$EDGE$SEED_MARKUP_PATH"
|
|
admin -X POST -H 'Content-Type: application/json' \
|
|
-d "{\"ip\":\"$SEED_MARKUP_BLOCK\",\"reason\":\"<svg onload=document.title='XSS2'> probe\"}" \
|
|
"$EDGE/admin-api/blocks" >/dev/null
|
|
|
|
# The netblock watch (OPEN-67): two engine bans inside one /24, produced the only
|
|
# way they can be produced — by traffic — and the watch threshold lowered to two,
|
|
# so that two of them are a candidate. The block is TEST-NET-1 rather than the
|
|
# /24 the rest of this gate probes, because taking the offer means banning a
|
|
# whole block for the length of the scene.
|
|
SEED_NETBLOCK=192.0.2.0/24
|
|
SEED_NETBLOCK_FRESH=192.0.2.203
|
|
|
|
# A request budget for one path (OPEN-68). The path is chosen per run so the row
|
|
# the page writes is this run's even on a volume that kept an earlier one, and the
|
|
# address is one no other scene of this gate probes.
|
|
SEED_PATH_BUDGET_PATH="/budget-probe-$(( RANDOM % 32768 ))"
|
|
SEED_PATH_BUDGET_IP=192.0.2.210
|
|
curl -s -X PUT -H "X-Admin-Token: $OPENWAF_ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
|
-d '{"netblockCandidateBans":2}' "$EDGE/admin-api/policy" >/dev/null
|
|
for src in 192.0.2.201 192.0.2.202; do
|
|
for _ in 1 2 3; do curl -s -o /dev/null -H "X-Forwarded-For: $src" "$EDGE/probe"; done
|
|
done
|
|
|
|
# An alert the page must show. /boom answers 502 (deploy/upstream.conf), and this
|
|
# gate already set the threshold to 3, so four of them from one address write
|
|
# server_error_streak rows. The alert is written after the response is, hence the
|
|
# short wait before the page reads it.
|
|
for _ in 1 2 3 4; do curl -s -o /dev/null -H "X-Forwarded-For: $SEED_ERROR" "$EDGE/boom"; done
|
|
sleep 2
|
|
|
|
# Durable observations are written by a background loop (flush every 15 s, at
|
|
# most once per source per 30 s), so the observations table needs the same wait
|
|
# the policy write needed. Without it the page is correct and the gate is wrong.
|
|
sleep 35
|
|
|
|
# SEED_ONLY=1 stops after the seed and leaves the stack running: the page is not
|
|
# driven from here. That is what scripts/ci-linux-rehearsal.sh uses to drive it
|
|
# from a container that has a real Chromium, and it is also what you want when
|
|
# debugging the page by hand against a stack that already has something to show.
|
|
if [ "${SEED_ONLY:-0}" = 1 ]; then
|
|
# The driver lives in another container (scripts/ci-linux-rehearsal.sh runs the
|
|
# page in a browser image), so it cannot inherit these values. Writing them out
|
|
# is what keeps the alert assertion honest: SEED_ERROR is chosen per run, and an
|
|
# empty one on the driver side would silently weaken the check to "any alert".
|
|
printf 'EDGE_URL=%s\nSEED_SOURCE=%s\nSEED_BLOCK=%s\nSEED_ERROR=%s\nSEED_MARKUP_PATH=%s\nSEED_MARKUP_BLOCK=%s\nSEED_EXPIRED_CIDR=%s\nSEED_NETBLOCK=%s\nSEED_NETBLOCK_FRESH=%s\nSEED_PATH_BUDGET_PATH=%s\nSEED_PATH_BUDGET_IP=%s\n' \
|
|
"$EDGE" "$SEED_SOURCE" "$SEED_BLOCK" "$SEED_ERROR" "$SEED_MARKUP_PATH" "$SEED_MARKUP_BLOCK" "$SEED_EXPIRED_CIDR" "$SEED_NETBLOCK" "$SEED_NETBLOCK_FRESH" \
|
|
"$SEED_PATH_BUDGET_PATH" "$SEED_PATH_BUDGET_IP" > "${SEED_FILE:-console-seed.env}"
|
|
echo "- SEED_ONLY=1: stack is up and seeded at $EDGE; seed values in ${SEED_FILE:-console-seed.env}"
|
|
trap - EXIT
|
|
exit 0
|
|
fi
|
|
|
|
echo '- driving the page'
|
|
node scripts/console-check.cjs
|