Files
openwaf/scripts/console-smoke.sh
T
Pavlo Ratushnyi 00f6dd9513 A block's redirect target went into a header unchecked, and one of them turned the block into a 500
Measured on the shipped image (openwaf:ci). POST /admin-api/blocks answered 204
for every one of these, and the next request from that source paid for it:

  "https://example.com/ok"               301 Location: https://example.com/ok
  "http://bad target"                    301 Location: http://bad target
  "http://x.invalid/<CRLF>X-Injected: 1" 500 Internal Server Error, Content-Length: 0
  "javascript:alert(1)"                  301 Location: javascript:alert(1)
  "not-a-url"                            301 Location: not-a-url

The CRLF row is the defect rather than the wart: Kestrel refuses to write the
header, so the operator's own block — written with a 204 — became a server error
for that visitor. Nothing was injected; the server validated the header, which
is the only part of the old behaviour that was safe. javascript: and data: are
absolute URIs too, and a redirect is a navigation, so those are an execution
request made on the operator's behalf by a string somebody typed into a form.

RedirectTarget.IsSendable is now the one judge: absolute, http(s), no control
characters or surrounding whitespace. The API answers 400 naming the field, and
DecisionEngine ignores a target it cannot send and answers the block's own
default instead — for a row that arrived by a restored backup or a hand edit,
where the API guard never ran.

The console gate also asserts the other half of the same boundary: every value
that came from the API is rendered through esc(), and two of those values are
not authored by the person reading the page — the path in an observation row is
whatever an anonymous visitor asked for, and a block reason is a form field. The
seed now sends a percent-encoded path carrying <img onerror=...> and an operator
block whose reason carries <svg onload=...>, and the page must show both as text
and create no elements from them.

  unit 105 -> 126 (+21: the target table, and the decision point's fallback)
  integration 55 -> 62 (+7: 400 by name for five targets, a valid target
    reaching the wire as 301, and a hand-written row with CRLF answering 301 to
    the default target instead of the 500 it used to answer)
  console 33 -> 37 assertions

Teeth, each measured by removing the thing: esc() reduced to identity in the
shipped page makes the console gate go red with 6 failures, and the page title
becomes "XSS" — the anonymous visitor's path executed script in the operator's
browser. Dropping the API guard fails exactly 5 integration tests; dropping the
decision-point guard fails exactly 6 unit tests and the 1 hand-written-row test.
2026-10-02 00:34:35 +02:00

114 lines
6.3 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 ))"
export EDGE_URL="$EDGE" SEED_SOURCE SEED_BLOCK SEED_ERROR SEED_MARKUP_PATH SEED_MARKUP_BLOCK
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 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.
for id in $(admin "$EDGE/admin-api/allow" | tr ',' '\n' | sed -n 's/.*"id":\([0-9]*\).*/\1/p'); do
admin -X DELETE "$EDGE/admin-api/allow/$id" >/dev/null 2>&1
done
# A country rule set by the gate cannot be deleted, only overwritten: the API
# has PUT /countries/{code}?allowed= and no DELETE. The gate puts US back to
# allowed=true, so the residue is a row that means "not denied", not a policy
# change — but it is residue, and the row outlives `compose down` because it
# lives in the data volume.
[ "${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
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
# 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\n' \
"$EDGE" "$SEED_SOURCE" "$SEED_BLOCK" "$SEED_ERROR" "$SEED_MARKUP_PATH" "$SEED_MARKUP_BLOCK" > "${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