diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 66d3149..5852fba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -108,9 +108,11 @@ jobs: # summary looks fine. This drives it in a real Chromium. # # Honest status: this job has never run on a runner, like the rest of the - # file. What was verified is the script it calls — scripts/console-smoke.sh - # passed locally on the commit that added it (playwright-core 1.61.1), and its - # negative control (a wrong token) exits 1 rather than reporting success. + # file. What was verified is the gate it calls — scripts/ci-gates.sh console + # passed locally (16 assertions, playwright-core 1.61.1), and its negative + # control (a wrong token) exits 1 rather than reporting success. The step + # calls the gate by name rather than retyping the script, so the CI definition + # and the operator command cannot drift. console: name: the operator page in a real browser runs-on: ubuntu-latest @@ -133,4 +135,20 @@ jobs: echo "PLAYWRIGHT_CORE=$(node -e 'console.log(require.resolve("playwright-core"))')" >> "$GITHUB_ENV" - name: Console smoke — the page, not only its endpoints - run: ./scripts/console-smoke.sh + env: + # Written into the job's workspace so the next step can publish it. + # A green console job with no picture leaves nothing to inspect once + # the runner is gone; a red one leaves the reviewer the page as it + # broke. console-check.cjs writes and .phone.png. + SCREENSHOT: console-evidence.png + run: ./scripts/ci-gates.sh console + + # always(): the failure case is the one worth looking at. + - name: Publish the console screenshots + if: always() + uses: actions/upload-artifact@v4 + with: + name: console-evidence + path: console-evidence*.png + if-no-files-found: warn + retention-days: 14 diff --git a/.gitignore b/.gitignore index bbb3d47..5ab6163 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ publish/ # local run artefacts openwaf.db* *.log + +# Written by scripts/console-smoke.sh when SCREENSHOT is a relative path. +console-evidence*.png diff --git a/Dockerfile b/Dockerfile index cc11aac..3f7cb70 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,7 +20,7 @@ ARG DOTNET_VERSION=10.0 # later stage, and the geoip stage re-declares these without defaults to pick # these values up. Declaring them after the build stage is what made the URL # expand to dbip-city-lite-.mmdb.gz and 404 in the geoip stage. -ARG DBIP_VERSION=2026-09 +ARG DBIP_VERSION=2026-10 ARG DBIP_EDITION=country FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS restore @@ -56,8 +56,17 @@ RUN mkdir -p /publish/data /publish/geoip # never the name, so either edition below works without a code change. # # DBIP_VERSION is pinned, which means the database ages with the image. /healthz -# reports the loaded database's build date so a stale one is visible at runtime, -# not only at build time; bump this argument in CI on a schedule. +# reports the loaded database's build date and a geoStale flag (90 days) so a +# stale one is visible at runtime, not only at build time. Bumping this argument +# on a schedule from CI is NOT implemented — the pin was moved by hand to +# 2026-10 on 2026-10-01, and the month before that it was 2026-09. +# +# One trap with the bump: docker-compose.yml mounts a named volume at +# /app/geoip, and Docker seeds a named volume from the image only the first +# time it is created. An existing deployment keeps the OLD database after a +# rebuild; `docker volume rm _openwaf-geoip` is what makes the new one +# land. /healthz's geoBuiltAt is how you check which one you are actually +# running. # # DBIP_EDITION=country is the default: the geo pillar decides on the ISO country # code, and country-lite is 4 MB compressed / 6 MB on disk. city-lite is 60 MB diff --git a/README.md b/README.md index 737c42a..b6984be 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,22 @@ long this process may keep enforcing a list that is already out of date: Database: `Database:Provider` = `sqlite` (default) or `postgres`, `ConnectionStrings__OpenWaf`. GeoIP: `GEOIP_MAXMIND_DB_PATH`. +### The geo database ages on purpose, and a rebuild does not replace it + +The image bakes a dbip-country-lite MMDB pinned by `ARG DBIP_VERSION` +(currently `2026-10`, moved by hand on 2026-10-01; the bump is not automated in +CI). `/healthz` reports `geoBuiltAt`, `geoDatabase` and `geoStale` (over 90 +days), and `/admin-api/geo` reports the age in days, so a stale database is +visible at runtime rather than only at build time. + +`docker-compose.yml` mounts a named volume at `/app/geoip` so a redeploy does +not re-download 8 MB. Docker seeds a named volume from the image only the first +time the volume is created, so **an existing deployment keeps the old database +after a rebuild**. Measured on 2026-10-01: the same rebuilt image reported +`geoBuiltAt: 2026-09-01` against the existing volume and `2026-10-01` after +`docker volume rm openwaf_openwaf-geoip`. That is the command to reach for when +the pin moves. + ## Schema Migrations are the schema of record for PostgreSQL, which is the provider a @@ -168,7 +184,12 @@ GET /admin-api/state snapshot age, tracked sources, counts `GET /admin` serves a single page over those endpoints — mode and snapshot age, live bans with their reasons, the streak standing in this process right now (current count against the threshold, not the historical maximum), the sources -being watched, and the countries seen. +being watched, and the countries seen — counted per ISO code, with a bar per +country sized against the busiest one. That bar is the honest limit of the word +"map" here: the admin API exposes counts per country code, not coordinates, and +the runtime image is distroless with no external assets, so there is no world +outline and no tile server. A geographic map would be a decoration this page +could not load at the moment it is wanted. It decides nothing: every control on it is one call to the API above. It is on the always-pass list, so an operator who is themselves blocked can still reach the page that clears it, and it is never observed, so fumbling it cannot build a @@ -186,16 +207,26 @@ cannot mean different things. ``` scripts/ci-gates.sh all # restore, build, unit, integration, smoke, image, compose-smoke scripts/ci-gates.sh unit # or any one gate on its own -OPENWAF_ADMIN_TOKEN=… scripts/console-smoke.sh +scripts/ci-gates.sh console # the page, in a real Chromium (not part of `all`) ``` -The last one is the console gate: it starts the stack, seeds a source sitting at -2 of a threshold of 3 and an operator block with a reason, drives `/admin` in a -real Chromium, and asserts the page authenticated, rendered both tables, kept -the token out of the markup and threw no javascript. It needs `playwright-core` -(`PLAYWRIGHT_CORE=/path/to/it`) and exits non-zero when it is missing — a gate -that reports success having asserted nothing is worse than no gate. Its negative -control (a wrong token) exits 1. +`OPENWAF_ADMIN_TOKEN` defaults inside `ci-gates.sh` to a throwaway for the stack +the gate starts itself and tears down on exit; the standalone scripts still +refuse to invent one, because run by hand they can be pointed at a real +deployment. + +The console gate starts the stack, seeds a source sitting at 2 of a threshold of +3 and an operator block with a reason, drives `/admin` in a real Chromium, and +asserts 16 things: the page authenticated, listed the streak as current-over- +threshold rather than as a historical maximum, listed the operator block with its +reason, listed observations and countries with bars that have real pixel widths, +kept the token out of the markup, and threw no javascript — then repeats the +pass at 375 px in dark mode, where the document must not scroll sideways and the +status line must clear WCAG AA (measured 11.85:1). It needs `playwright-core` +(`PLAYWRIGHT_CORE=/path/to/it`) and exits 2 when that is missing — a gate that +reports success having asserted nothing is worse than no gate. Its negative +control (a wrong token) exits 1. `SCREENSHOT=console-evidence.png` writes the +desktop and phone screenshots the CI job publishes as artifacts. Honest status of the workflow itself: it has never executed on a runner, because the repository has no remote. Everything above was run on a laptop, on the diff --git a/dotnet/OpenWaf.Edge/Admin/AdminConsole.cs b/dotnet/OpenWaf.Edge/Admin/AdminConsole.cs index d9906b3..ac3276d 100644 --- a/dotnet/OpenWaf.Edge/Admin/AdminConsole.cs +++ b/dotnet/OpenWaf.Edge/Admin/AdminConsole.cs @@ -71,6 +71,14 @@ internal static class AdminConsole .muted { opacity: .6 } #status { font-weight: bold; } .grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(22rem, 1fr)); } + /* The country table is the closest thing this page has to a map, and a column + of numbers does not show a distribution. One div per row, sized against the + busiest country: no tiles, no CDN, no asset file, because the runtime image + is distroless and the edge is usually the thing standing in front of a site + that is being attacked. A geographic map is deliberately NOT here — the + admin API exposes counts per ISO code, not coordinates, and a world outline + drawn from memory would be decoration that misleads. */ + .sharebar { display: block; height: .55em; background: currentColor; opacity: .45; min-width: 2px; } @@ -101,7 +109,8 @@ internal static class AdminConsole
sourceconsecutiveserver errorslast statuspathgeolast seen

Countries seen

-
codesourcesrefusalsrule
+
+
codesourcesrefusalsrefusals vs the busiestrule

Every action here is one call to the admin API. The page decides nothing.

@@ -129,8 +138,13 @@ function note(text, bad) { s.className = bad ? 'warn' : 'ok'; } +// c.v is text and goes through esc(); c.h is markup this file wrote itself (the +// country bar) and nothing else. Every value that came from the API is text. +const plural = (n, word) => (n === 1 ? word : word + 's'); + function row(cells) { - return '' + cells.map((c) => '' + esc(c.v) + '').join('') + ''; + return '' + cells.map((c) => '' + + (c.h !== undefined ? c.h : esc(c.v)) + '').join('') + ''; } async function load() { @@ -177,8 +191,24 @@ async function load() { + ' tracked sources have a streak standing; threshold ' + streaks.threshold + ' within ' + streaks.windowMinutes + ' min; map capped at ' + streaks.maxTrackedSources : 'unknown scope'; - el('countries').tBodies[0].innerHTML = (countries || []).map((c) => row([ + // The bar is a share of the busiest country, not of some absolute scale: + // what an operator wants out of this table is "where is the traffic coming + // from right now", and that is a comparison. Widths are percentages of the + // cell, so the table still fits a 375 px phone (the phone pass of the gate + // measures the document, not this column). + const seen = countries || []; + const busiest = seen.reduce((m, c) => Math.max(m, c.refusals || 0), 0); + const totalSources = seen.reduce((t, c) => t + (c.sources || 0), 0); + const totalRefusals = seen.reduce((t, c) => t + (c.refusals || 0), 0); + el('countrysum').textContent = seen.length + ? seen.length + ' ' + plural(seen.length, 'country') + ', ' + + totalSources + ' ' + plural(totalSources, 'source') + ', ' + + totalRefusals + ' ' + plural(totalRefusals, 'refusal') + ' total' + : 'no country-resolved sources yet'; + el('countries').tBodies[0].innerHTML = seen.map((c) => row([ { v: c.code }, { v: c.sources, n: true }, { v: c.refusals, n: true }, + { h: '' }, { v: allowed.has(c.code) ? (allowed.get(c.code) ? 'allowed' : 'denied') : '—' } ])).join('') || row([{ v: 'none' }]); note('refreshed ' + new Date().toLocaleTimeString(), false); diff --git a/scripts/ci-gates.sh b/scripts/ci-gates.sh index baa6a83..9deb3b6 100755 --- a/scripts/ci-gates.sh +++ b/scripts/ci-gates.sh @@ -5,13 +5,21 @@ # stops at the first failure. # # scripts/ci-gates.sh restore|build|unit|integration|smoke|image|compose-smoke|all +# scripts/ci-gates.sh console (needs a real Chromium; not part of `all`) # -# OPENWAF_ADMIN_TOKEN is required by the compose gate; the value is not a secret, -# it is a throwaway for a stack that exists only inside the run. +# OPENWAF_ADMIN_TOKEN defaults below to the same throwaway CI uses; set it to +# override. Nothing here talks to anything outside the run. set -euo pipefail cd "$(dirname "$0")/.." +# The compose gate needs an admin token for the stack it starts. It is not a +# secret — that stack exists only inside this run and is torn down on exit — but +# requiring it meant `ci-gates.sh all` stopped at the last gate for anyone who +# forgot to export one, which made the one-command entry point a lie. Same value +# CI uses, overridable by the caller. +export OPENWAF_ADMIN_TOKEN="${OPENWAF_ADMIN_TOKEN:-ci-only-throwaway}" + banner() { printf '\n\033[1m=== gate: %s ===\033[0m\n' "$1"; } restore() { banner restore; dotnet restore dotnet/OpenWaf.slnx; } @@ -32,6 +40,14 @@ image() { banner image; docker build -t openwaf:ci .; docker ima compose_smoke() { banner compose-smoke; ./scripts/compose-smoke.sh; } +# The browser gate over the operator page. It is a gate, so it lives here too and +# CI calls it by the same name. It is NOT part of `all`: it needs a real +# Chromium, and a machine without one exits 2 ("cannot run"), which is a +# different answer from 1 ("the page is wrong") and would otherwise make the +# one-command entry point unrunnable. Run it explicitly before pushing a change +# to the console. +console() { banner console; ./scripts/console-smoke.sh; } + case "${1:-all}" in restore) restore ;; build) build ;; @@ -40,12 +56,15 @@ case "${1:-all}" in smoke) smoke ;; image) image ;; compose-smoke) compose_smoke ;; + console) console ;; all) restore; build; unit; integration; smoke; image; compose_smoke printf '\nall gates passed\n' + printf 'NOT included above: console (needs a real Chromium; exits 2 without one).\n' + printf 'If the console changed, run: %s console\n' "$0" ;; *) - printf 'usage: %s restore|build|unit|integration|smoke|image|compose-smoke|all\n' "$0" >&2 + printf 'usage: %s restore|build|unit|integration|smoke|image|compose-smoke|console|all\n' "$0" >&2 exit 2 ;; esac diff --git a/scripts/compose-smoke.sh b/scripts/compose-smoke.sh index f633bf6..39395ba 100755 --- a/scripts/compose-smoke.sh +++ b/scripts/compose-smoke.sh @@ -17,7 +17,7 @@ set -uo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" || exit 1 -: "${OPENWAF_ADMIN_TOKEN:?set OPENWAF_ADMIN_TOKEN before running}" +: "${OPENWAF_ADMIN_TOKEN:?set OPENWAF_ADMIN_TOKEN before running, or run scripts/ci-gates.sh compose-smoke (it defaults a throwaway for the stack it starts itself)}" export OPENWAF_ADMIN_TOKEN EDGE=${EDGE:-http://127.0.0.1:8080} SCANNER=203.0.113.66 diff --git a/scripts/console-check.cjs b/scripts/console-check.cjs index dfbd568..59a86fa 100644 --- a/scripts/console-check.cjs +++ b/scripts/console-check.cjs @@ -29,6 +29,12 @@ try { process.exit(2); } +// A page that cannot be driven at all is the failure worth seeing, and a stack +// trace from a click timeout says nothing about what the page looked like. The +// first version of the country bars did exactly this: the class name collided +// with the toolbar's own .bar, the toolbar collapsed to 0.55em tall, and every +// assertion after the token click died. Best-effort picture on the way out — a +// screenshot failure must not mask the real error. const failures = []; const check = (name, ok, detail) => { console.log(` ${ok ? 'ok ' : 'FAIL'} ${name}${detail ? ' — ' + detail : ''}`); @@ -78,6 +84,19 @@ const rgb = (s) => s.match(/\d+/g).slice(0, 3).map(Number); JSON.stringify(blockRows.map((r) => [r[0], r[2]]))); check('durable observations are listed', obsRows > 0, `${obsRows} rows`); check('the per-country summary is listed', countryRows > 0, `${countryRows} rows`); + // The country table is the page's answer to "where is this coming from", and a + // column of numbers does not answer that. The bar has to be a real box with a + // real width — a span styled to nothing would still pass a text assertion. + const bars = await page.$$eval('#countries tbody tr .sharebar', (els) => els.map((e) => ({ + w: e.clientWidth, cell: e.parentElement.clientWidth, + }))); + const countrysum = (await page.textContent('#countrysum')).replace(/\s+/g, ' ').trim(); + check('the country bars are boxes with real width', + bars.length > 0 && bars.every((b) => b.w >= 2), JSON.stringify(bars)); + check('the busiest country fills its cell, so a bar is a share and not decoration', + bars.length > 0 && bars.some((b) => b.cell > 0 && b.w >= b.cell * 0.9), JSON.stringify(bars)); + check('the country table states what it counted', + /^\d+ countr(y|ies), \d+ sources?, \d+ refusals? total$/.test(countrysum), countrysum); check('the token never appears in the markup', !html.includes(TOKEN)); check('the page threw no javascript', jsProblems.length === 0, jsProblems.join(' | ')); @@ -115,6 +134,7 @@ const rgb = (s) => s.match(/\d+/g).slice(0, 3).map(Number); innerWidth: window.innerWidth, html: document.documentElement.outerHTML, streakRow, + barWidths: [...document.querySelectorAll('#countries tbody tr .sharebar')].map((e) => e.clientWidth), }; }); @@ -127,15 +147,38 @@ const rgb = (s) => s.match(/\d+/g).slice(0, 3).map(Number); m.streakRow.some((r) => r[0] === SEED_SOURCE && /2 \/ 3/.test(r[1] || '')), JSON.stringify(m.streakRow.map((r) => [r[0], r[1]]))); check('the token is not in the markup at phone width either', !m.html.includes(TOKEN)); + check('the country bars survive the phone layout', + m.barWidths.length > 0 && m.barWidths.every((w) => w >= 2), JSON.stringify(m.barWidths)); check('the page threw no javascript at phone width', phoneProblems.length === 0, phoneProblems.join(' | ')); if (SCREENSHOT) await phone.screenshot({ path: SCREENSHOT.replace(/(\.[a-z0-9]+)$/i, '.phone$1'), fullPage: true }); await phone.close(); } - await browser.close(); if (failures.length) { + // The two ways this gate goes red are different and both need a picture: + // "the page could not be driven" (the catch below) and "the page was driven + // and is wrong" (here). The first version only covered the first. + if (SCREENSHOT) { + try { + await page.screenshot({ path: SCREENSHOT.replace(/(\.[a-z0-9]+)$/i, '.failure$1'), fullPage: true }); + } catch (e) { console.error(' (no failure screenshot: ' + e.message + ')'); } + } console.error(`console check: ${failures.length} assertion(s) failed`); + await browser.close(); process.exit(1); } + await browser.close(); console.log('console check: all assertions passed'); -})().catch((e) => { console.error('console check failed to run:', e.message); process.exit(1); }); +})().catch(async (e) => { + console.error('console check failed to run:', e.message); + if (SCREENSHOT) { + try { + const b = await chromium.launch({ headless: true }); + const p2 = await b.newPage({ viewport: { width: 1280, height: 1100 } }); + await p2.goto(`${EDGE}/admin`, { waitUntil: 'domcontentloaded' }); + await p2.screenshot({ path: SCREENSHOT.replace(/(\.[a-z0-9]+)$/i, '.failure$1'), fullPage: true }); + await b.close(); + } catch (e2) { console.error(' (no failure screenshot: ' + e2.message + ')'); } + } + process.exit(1); +}); diff --git a/scripts/console-smoke.sh b/scripts/console-smoke.sh index d0b8179..90e324e 100755 --- a/scripts/console-smoke.sh +++ b/scripts/console-smoke.sh @@ -13,7 +13,7 @@ set -euo pipefail cd "$(dirname "$0")/.." -export OPENWAF_ADMIN_TOKEN="${OPENWAF_ADMIN_TOKEN:?set OPENWAF_ADMIN_TOKEN before running}" +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=http://127.0.0.1:8080 SEED_SOURCE=203.0.113.55 SEED_BLOCK=203.0.113.77