GET /admin-api/blocks silently returned the newest 500 rows. The delete beside it is not capped, so an operator counting 500 rows on a page — or in the console — under-counts their own block list in one direction and over-trusts the clear button in the other. Found while measuring the snapshot refresh cost: 2 000 seeded blocks, 500 rows back, no word about it. The cap stays (a 2 000-row dump is not a page), but it now travels with the answer: X-Total-Count and X-Truncated headers, plus limit/offset (default 500, max 2000). The console renders the difference: showing the newest 500 of 2000, and clearing by provenance removes all of them, not just this page. Verified on the deployed image, not only in the fixture: with 600 blocks the default listing answers X-Total-Count: 600 / X-Truncated: true, and limit=1000 answers X-Truncated: false with all 600 rows. The first attempt at that check ran against a stale compose image — compose up without --build reuses it. The README and compose-smoke.sh both use --build; the check had to. Gates: 36 unit + 20 integration, 0 failed; smoke 19/19; compose-smoke 27/27; actionlint and shellcheck both 0.
183 lines
8.5 KiB
C#
183 lines
8.5 KiB
C#
namespace OpenWaf.Edge.Admin;
|
|
|
|
/// <summary>
|
|
/// The operator's console: one page, served by the edge itself, over the
|
|
/// endpoints in <see cref="AdminEndpoints"/>. It adds no decision logic — every
|
|
/// action on the page is one call to an endpoint that already exists.
|
|
///
|
|
/// Two properties are inherited from the admin API rather than invented here,
|
|
/// and both are load-bearing:
|
|
/// * <c>/admin</c> is on the always-pass list, so an operator who is themselves
|
|
/// blocked can still reach the page that clears it;
|
|
/// * the always-pass surface is never observed, so fumbling the page cannot
|
|
/// build a streak against the operator.
|
|
///
|
|
/// The token is never in a URL. A token in a query string survives in proxy
|
|
/// logs, browser history and Referer headers, which is exactly how an admin
|
|
/// token on a public edge leaks. It is typed into the page, kept in
|
|
/// sessionStorage for the tab, and sent as a header.
|
|
///
|
|
/// No external assets: the runtime image is distroless and the edge is often
|
|
/// the thing standing in front of a site that is under attack. A page that
|
|
/// needs a CDN is a page that fails at the moment it is wanted.
|
|
/// </summary>
|
|
internal static class AdminConsole
|
|
{
|
|
public static IEndpointRouteBuilder MapAdminConsole(this IEndpointRouteBuilder app)
|
|
{
|
|
// One endpoint, not two: "/admin" and "/admin/" are the same route to the
|
|
// matcher and registering both is an AmbiguousMatchException at request
|
|
// time (found by a test, not by reading). The catch-all is deliberate —
|
|
// this is a single page, so a mistyped subpath should show the page
|
|
// rather than be proxied to an upstream that has never heard of it.
|
|
app.MapGet("/admin/{**rest}", () => Results.Content(Page, "text/html; charset=utf-8"));
|
|
return app;
|
|
}
|
|
|
|
private const string Page = """
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="robots" content="noindex, nofollow">
|
|
<title>openwaf — operator</title>
|
|
<style>
|
|
:root { color-scheme: light dark; }
|
|
body { font: 14px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; margin: 0; padding: 1.2rem; }
|
|
h1 { font-size: 1rem; margin: 0 0 .2rem; }
|
|
h2 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; margin: 1.4rem 0 .4rem; opacity: .7; }
|
|
.bar { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: .6rem; }
|
|
input, select, button { font: inherit; padding: .3rem .5rem; }
|
|
button { cursor: pointer; }
|
|
table { border-collapse: collapse; width: 100%; }
|
|
th, td { text-align: left; padding: .25rem .5rem; border-bottom: 1px solid color-mix(in srgb, currentColor 18%, transparent); vertical-align: top; }
|
|
th { font-weight: normal; opacity: .6; }
|
|
.num { text-align: right; font-variant-numeric: tabular-nums; }
|
|
.pill { padding: .05rem .35rem; border: 1px solid currentColor; }
|
|
.warn { color: #b3261e; }
|
|
.ok { color: #186a3b; }
|
|
.muted { opacity: .6 }
|
|
#status { font-weight: bold; }
|
|
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(22rem, 1fr)); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>openwaf</h1>
|
|
<div class="bar">
|
|
<input id="token" type="password" placeholder="admin token" autocomplete="off" size="24">
|
|
<button id="save">use token</button>
|
|
<button id="refresh">refresh</button>
|
|
<select id="mode">
|
|
<option>Off</option><option>Observe</option><option>Enforce</option>
|
|
</select>
|
|
<button id="setMode">set mode</button>
|
|
<button id="clearEngine">clear engine bans</button>
|
|
<span id="status" class="muted">no token</span>
|
|
</div>
|
|
|
|
<div id="summary" class="muted">—</div>
|
|
|
|
<h2>Bans and blocks</h2>
|
|
<div id="blocknote" class="muted"></div>
|
|
<table id="blocks"><thead><tr><th>source</th><th>provenance</th><th>reason</th><th>live until</th><th class="num">last</th></tr></thead><tbody></tbody></table>
|
|
|
|
<h2>Sources the edge is watching</h2>
|
|
<table id="obs"><thead><tr><th>source</th><th class="num">consec.</th><th class="num">refusals</th><th>last status</th><th>path</th><th>geo</th></tr></thead><tbody></tbody></table>
|
|
|
|
<h2>Countries seen</h2>
|
|
<table id="countries"><thead><tr><th>code</th><th class="num">sources</th><th class="num">refusals</th><th>rule</th></tr></thead><tbody></tbody></table>
|
|
|
|
<p class="muted">Every action here is one call to the admin API. The page decides nothing.</p>
|
|
|
|
<script>
|
|
const el = (id) => document.getElementById(id);
|
|
const esc = (s) => String(s ?? '').replace(/[&<>"]/g, (c) => ({'&':'&','<':'<','>':'>','"':'"'}[c]));
|
|
let token = sessionStorage.getItem('openwaf.token') || '';
|
|
el('token').value = token;
|
|
|
|
async function apiRaw(path, opts = {}) {
|
|
const headers = Object.assign({}, opts.headers || {});
|
|
if (token) headers['X-Admin-Token'] = token;
|
|
const r = await fetch(path, Object.assign({}, opts, { headers }));
|
|
if (r.status === 401) { note('token rejected', true); throw new Error('401'); }
|
|
if (r.status === 404) { note('admin api disabled — set OpenWaf__Admin__Token', true); throw new Error('404'); }
|
|
const body = r.status === 204 ? null : await r.json();
|
|
return { body, total: r.headers.get('X-Total-Count'), truncated: r.headers.get('X-Truncated') === 'true' };
|
|
}
|
|
|
|
async function api(path, opts = {}) { return (await apiRaw(path, opts)).body; }
|
|
|
|
function note(text, bad) {
|
|
const s = el('status');
|
|
s.textContent = text;
|
|
s.className = bad ? 'warn' : 'ok';
|
|
}
|
|
|
|
function row(cells) {
|
|
return '<tr>' + cells.map((c) => '<td' + (c.n ? ' class="num"' : '') + '>' + esc(c.v) + '</td>').join('') + '</tr>';
|
|
}
|
|
|
|
async function load() {
|
|
if (!token) { note('no token', true); return; }
|
|
try {
|
|
const [state, blocksRes, obs, countries, rules] = await Promise.all([
|
|
api('/admin-api/state'), apiRaw('/admin-api/blocks?live=false'),
|
|
api('/admin-api/observations?limit=25'), api('/admin-api/countries/summary'),
|
|
api('/admin-api/countries')
|
|
]);
|
|
const blocks = blocksRes.body;
|
|
// The listing is capped server-side. Saying so is the difference between a
|
|
// console and a misleading one: the delete beside this table is not capped.
|
|
el('blocknote').textContent = blocksRes.truncated
|
|
? 'showing the newest ' + blocks.length + ' of ' + blocksRes.total + ' — clearing by provenance removes all ' + blocksRes.total + ', not just this page'
|
|
: blocksRes.total + (blocksRes.total === '1' ? ' entry' : ' entries');
|
|
const allowed = new Map((rules || []).map((r) => [r.code, r.isAllowed]));
|
|
el('summary').innerHTML =
|
|
'<span class="pill">mode ' + esc(state.mode) + '</span> ' +
|
|
'<span class="pill">snapshot ' + esc(state.snapshotAgeSeconds) + 's</span> ' +
|
|
'<span class="pill">tracked ' + esc(state.trackedSources) + '</span> ' +
|
|
'<span class="pill">engine bans ' + esc(state.engineBans) + '</span> ' +
|
|
'<span class="pill">operator blocks ' + esc(state.operatorBlocks) + '</span> ' +
|
|
'<span class="pill">allow entries ' + esc(state.allowEntries) + '</span>';
|
|
el('mode').value = state.mode;
|
|
el('blocks').tBodies[0].innerHTML = (blocks || []).map((b) => row([
|
|
{ v: b.sourceKey }, { v: b.source }, { v: b.reason },
|
|
{ v: b.live ? (b.expiresAt || '').replace('T', ' ').slice(0, 19) : 'expired' },
|
|
{ v: (b.lastStatusCode || '') + ' ' + (b.lastPath || ''), n: true }
|
|
])).join('') || row([{ v: 'none' }]);
|
|
el('obs').tBodies[0].innerHTML = (obs || []).map((o) => row([
|
|
{ v: o.sourceKey }, { v: o.maxConsecutiveRefusals, n: true }, { v: o.totalRefusals, n: true },
|
|
{ v: o.lastStatusCode }, { v: o.lastPath }, { v: [o.lastCountryCode, o.lastCity].filter(Boolean).join(' / ') }
|
|
])).join('') || row([{ v: 'none' }]);
|
|
el('countries').tBodies[0].innerHTML = (countries || []).map((c) => row([
|
|
{ v: c.code }, { v: c.sources, n: true }, { v: c.refusals, n: true },
|
|
{ v: allowed.has(c.code) ? (allowed.get(c.code) ? 'allowed' : 'denied') : '—' }
|
|
])).join('') || row([{ v: 'none' }]);
|
|
note('refreshed ' + new Date().toLocaleTimeString(), false);
|
|
} catch (e) { /* note() already said why */ }
|
|
}
|
|
|
|
el('save').onclick = () => {
|
|
token = el('token').value.trim();
|
|
sessionStorage.setItem('openwaf.token', token);
|
|
load();
|
|
};
|
|
el('refresh').onclick = load;
|
|
el('setMode').onclick = async () => {
|
|
await api('/admin-api/policy', { method: 'PUT', headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ mode: el('mode').value }) });
|
|
load();
|
|
};
|
|
el('clearEngine').onclick = async () => {
|
|
await api('/admin-api/blocks?source=engine', { method: 'DELETE' });
|
|
load();
|
|
};
|
|
setInterval(load, 5000);
|
|
load();
|
|
</script>
|
|
</body>
|
|
</html>
|
|
""";
|
|
}
|