`research/2026-10-01-multi-instance/RESULTS.md` said, correctly at the time, that
the snapshot TTL "is not operator-tunable... so 'shorten the TTL' is currently a
rebuild, not a deployment change". OPEN-13 made it
`OpenWaf:State:RefreshTtlSeconds` (validated 1-300 s, startup aborts naming the
key outside that range). A claim that was true and is now false is still a claim
sitting in a document, so it is corrected in place, with what changed and when,
rather than quietly rewritten.
`run4.sh` measures the option that claim was blocking. Same stack and same method
as D3 — edge-b serves a steady 200-only stream from one source (a refusal stream
bans its own probe, which is the mistake `run2.sh` made), edge-a writes an
operator block at t0, the first sample edge-b answers as a refusal is the
propagation number. Two phases, and the TTL is read back from `/healthz` rather
than assumed from the environment, because a typo in the key name would otherwise
measure 30 s twice and report it as two different settings.
ttl=30 (default) samples=1223 over 44.1 s (~27.7 req/s) enforced 23 100 ms after the write
ttl=5 samples=1247 over 44.3 s (~28.1 req/s) enforced 3 367 ms after the write
**23.1 s → 3.4 s: a 6.9x cut**, and the 30 s phase independently reproduces D3's
23.7 s, which is the second time that number has come out the same.
What it does not say. The request path through edge-b during the same runs: p50
1.75 ms / p95 3.15 ms at TTL 30, p50 1.93 ms / p95 2.78 ms at TTL 5, n=200 each.
The two percentiles move in opposite directions, so the honest reading is "inside
noise", not "cheaper" — and a shorter TTL is more refresh round trips per process
per minute (12 against 2), cheap at the snapshot size that was measured (~3 ms of
queries at 2000 blocks) and unmeasured at any larger size.
For OPEN-12 this turns option (b) from an assumption into a measurement, and
leaves its limit exactly where it was: shortening the TTL fixes propagation, and
does nothing about D1 — four refusals split 2+2 across two edges at threshold 3
still produce no ban anywhere. The streak map is still the thing that would have
to move.
`docs/ARCHITECTURE.md` evolution triggers 2 and 3 updated with what is now true:
the geo-staleness trigger partly fired and was handled (including the case the
90-day runtime alarm does not catch), and the multi-instance trigger now carries
both reproduced findings and the measured TTL effect.
114 lines
7.4 KiB
Markdown
114 lines
7.4 KiB
Markdown
# Architecture — openwaf
|
|
|
|
Canonical record: Lease Vibe work item **OPEN-2**, `architecture_decision`
|
|
`4c9de3f6-ca26-4020-a16b-8f21e316c936` (v1, authored under a live claim). This
|
|
file is the repository-side copy; the tracker holds the versioned authority and
|
|
its audit history. Scope carried over from **OPEN-1** (Canceled 2026-08-12),
|
|
whose BA analysis this builds on rather than repeats.
|
|
|
|
## Decision
|
|
|
|
One decision point, three pillars, provenance-carrying blocks.
|
|
|
|
`EdgeGuardMiddleware` is the only component allowed to short-circuit. It reads
|
|
an immutable in-memory snapshot and evaluates, in a fixed order:
|
|
|
|
```
|
|
always-pass prefixes → allowlist → operator block → engine ban → geo policy
|
|
```
|
|
|
|
`ResponseObservationMiddleware` runs inside the guard, awaits the upstream,
|
|
classifies the status, and is the only thing that can create an engine ban.
|
|
|
|
### Rulings on the open questions OPEN-1 left
|
|
|
|
| Q | Ruling | Why |
|
|
|---|--------|-----|
|
|
| Q1 — does M bound the streak or the ban? | **Both, as two parameters.** `StreakWindow` bounds the streak; `BanDuration` is separate. | "N раз підряд на M хвилин" reads both ways; dropping one loses a knob the operator needs. |
|
|
| Q2 — `> 400` or `>= 400`? | **Inclusive of 400** (`400-499`). | A malformed-body probe answers 400 and must count. |
|
|
| Q3 — do 5xx count? | **Never a ban; always an alert.** | A broken deploy makes every honest client draw consecutive 5xx; the first bad release would ban the site's own visitors. |
|
|
| Q4 — what is the source key? | **Normalised IP**: IPv4 as-is, IPv6 grouped to `/64`, IPv4-mapped v6 collapsed. Allowlist matches the raw address instead. | A bot rotating inside one v6 allocation is one source; an operator writing a CIDR should mean exactly that. |
|
|
| Q5 — one block table or two? | **One table, `Source` provenance column.** | Manual and automatic coexist permanently; clearing one kind must never touch the other, and the admin UI should not have to know about two tables. |
|
|
| Q6 — does the 301 survive for automatic bans? | **No: engine bans answer 403 + `Retry-After`; operator blocks keep 301.** A per-entry `RedirectTo` overrides both. | A 301 is a good answer to a scanner and a baffling one to a customer whose office IP was caught. |
|
|
| Q7 — allowlist and override | **Allowlist outranks every pillar and survives the toggle; the always-pass prefixes are the self-recovery hatch.** | A ban with no exemption path eventually blocks someone who matters at the worst moment. |
|
|
| Q8 — retention | **Aggregates only, aged out by a worker** (`ObservationRetention`, `BanRetention`). | The reference repo left raw-IP TTL unresolved; storing no request log makes the question smaller. |
|
|
|
|
### Why not the alternatives
|
|
|
|
* **Three independent middlewares** (what the reference repo does today):
|
|
precedence becomes registration order, and an allowlisted operator in a
|
|
denied country gets whichever guard runs first.
|
|
* **Two block tables**: makes "clear engine bans" a table operation and invites
|
|
a future merge that loses data.
|
|
* **NativeAOT**: rejected in the reference and recorded there; not relitigated.
|
|
* **Keying the ban on ip+UA** (as the analytics `SessionHash` does): a bot that
|
|
varies its UA multiplies into many sources and never reaches the threshold.
|
|
* **Banning on 5xx because the original wording said `> 400`**: turns an outage
|
|
into a second, self-inflicted outage.
|
|
|
|
## Properties the tests hold
|
|
|
|
* A ban never feeds its own streak — the guard refuses before the observer runs.
|
|
* The admin surface is never observed either: an operator's syntax error in an
|
|
admin call must not lock them out.
|
|
* Memory is bounded (`MaxTrackedSources`, LRU + idle TTL); the request path
|
|
never touches the database; a stale snapshot is served while one background
|
|
refresh runs; a failed refresh keeps the previous snapshot (fail closed for
|
|
blocks, fail open for geo).
|
|
* Durable writes are coalesced to one per source per `ObservationWriteInterval`.
|
|
* Ban writes are serialised in-process: the observer runs after the response is
|
|
written, so the next request can arrive while the ban is still landing.
|
|
|
|
## What building it taught us about the design
|
|
|
|
Recorded because they are the places a reader will otherwise step in again.
|
|
|
|
1. **`ctx.RequestAborted` is the wrong token for post-response work.** The ban
|
|
write was silently cancelled: the client had already hung up, so the request
|
|
that *proved* the source abusive was the one that failed to record it.
|
|
2. **SQLite cannot compare a `DateTimeOffset`** — the provider stores it as text
|
|
and refuses the query at translation time, which kills every "is this ban
|
|
still live" and "has this row aged out" predicate. Timestamps are stored as
|
|
integers on SQLite and stay `timestamptz` on PostgreSQL.
|
|
3. **CIDR masking order matters.** Masking the partial octet after zeroing the
|
|
rest silently widens the prefix (`198.51.100.24/19` became `198.51.0.0`).
|
|
Caught by a unit test, not by review.
|
|
4. **An empty string in `appsettings.json` defeats the environment fallback**
|
|
(`??` does not fire on `""`), which is how a geo database and an admin token
|
|
go missing in a deployment that looks configured.
|
|
5. **The configuration binder appends to a collection default** rather than
|
|
replacing it, so a non-empty default produced a duplicated redirect target.
|
|
6. **Two requests finishing together both saw "no ban yet"** and raced into the
|
|
unique index; the loser's evidence was lost. Now serialised behind a gate.
|
|
|
|
## Evolution triggers
|
|
|
|
1. A shared-egress IPv4 (carrier NAT, office, campus) banned by honest traffic
|
|
→ the per-address v4 key is wrong and keying must move to session or
|
|
connection.
|
|
2. Geo enforced on a stale MMDB → partly fired and was handled. `/healthz`
|
|
reports `geoBuiltAt`/`geoStale`, `/admin-api/geo` reports the age, the console
|
|
shows which database the process is enforcing on, and `scripts/geo-db-freshness.sh`
|
|
plus `.github/workflows/geo-freshness.yml` compare the pin with what the vendor
|
|
serves. What the 90-day runtime alarm does **not** catch: a deployment one month
|
|
behind reports `geoStale: false` → measured, with the named volume holding
|
|
2026-09 while the Dockerfile pinned 2026-10, every other geo assertion still
|
|
green. `compose-smoke.sh` now asserts pin == loaded, and that is the assertion
|
|
which catches it.
|
|
3. Multi-instance edge → measured on `research/2026-10-01-multi-instance/`,
|
|
not guessed. Two findings, each reproduced twice:
|
|
* The streak is the problem, and it is not lag. Round-robin over two edges
|
|
multiplies the effective streak threshold by the instance count: four
|
|
refusals split 2+2 at threshold 3 produced no ban anywhere (D1).
|
|
* Propagation is TTL-bound, and the TTL is now a setting rather than a
|
|
rebuild. 23.1 s at the default 30 s under ~28 req/s (D4, reproducing the
|
|
23.7 s of D3), 3.4 s at `OpenWaf:State:RefreshTtlSeconds=5` → a 6.9x cut,
|
|
with no measurable change on the request path (p50 1.75 to 1.93 ms, p95 3.15
|
|
to 2.78 ms at n=200; the two move in opposite directions, so the honest
|
|
reading is "inside noise", not "cheaper").
|
|
The block list is already shared through the database; the streak map is not.
|
|
Move the streak map when the threshold has to be exact behind a load balancer
|
|
— it costs a round trip on a path whose measured p95 is ~3 ms.
|
|
4. A need for a raw request log → the GDPR question the reference repo never
|
|
closed comes back with sharper edges.
|