Files
Pavlo Ratushnyi dcf8a733a9 A healed storage failure returned the edge to ready without waiting for traffic
OPEN-34, measured on the shipped image before this change. With writes to
block_entries aborted, five refusals produced counted write failures and
/readyz 503. The triggers were then REMOVED — write path healthy, zero triggers
left — and no further request was sent from that source:

  t+30 s  /readyz 503   /state: banWriteFailures 3, bansNotWritten 3
  t+60 s  /readyz 503   unchanged
  t+90 s  /readyz 503   unchanged
  t+120 s /readyz 503   unchanged

The ban pillar clears only on a successful ban write, and the only thing that
produced one was the source crossing the threshold again. Readiness — the
signal an orchestrator uses to keep an edge in rotation — was therefore coupled
to attack traffic: the quieter the site, the longer a repaired edge sat out of
service, enforcing everything except one ban nobody had written.

The fix is on the write side, not on the gate. A ban the engine decided and
could not write is kept as a PendingBan and re-attempted by a hosted loop on the
same 15 s cadence the other writers use; the sentence still runs from the moment
the ban was decided, so a retry that lands late cannot quietly lengthen it, and a
pending ban whose sentence expired before it could be written is counted
(bansNeverEnforced) and reported rather than rewritten as a fresh, longer ban.
/state gained bansPendingRetry and bansNeverEnforced, because without the first
a healed storage failure is indistinguishable from a quiet site.

Teeth: PendingBanRetryTests (3) — the retry landing with no further request, the
hosted loop doing it on its own (nobody calls it in that test), and an expired
pending ban reported rather than written late. Compose gate gained a 5-assertion
scene against the shipped image: the failed ban is queued, readiness returns
after the repair with no traffic from that source, the ban reaches storage, the
queue empties, and the source that was never banned during the break answers 403
afterwards.

Gates at this commit — macOS host: unit 128/128, integration 141/141, smoke
28/0, compose 86/0, console 58/0, 0 containers left behind. Linux rehearsal,
all three stages: PASS — source (128/128, 141/141, 28/0), compose 86/0 with
enforcement lag 13 ms, console 58/0, 0 containers left behind.
2026-10-02 11:34:44 +02:00

63 lines
2.7 KiB
C#

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace OpenWaf.Infrastructure.Persistence;
/// <summary>
/// Re-attempts the bans the engine decided and could not write.
///
/// Without this, a ban-write failure is only repaired by the attacker coming
/// back. Measured on the shipped image (2026-10-02): writes to block_entries
/// aborted by triggers, five refusals, then the triggers REMOVED — write path
/// healthy, zero triggers — and no further traffic. /readyz answered 503 at
/// t+30, t+60, t+90 and t+120 s with banWriteFailures and bansNotWritten
/// unmoved, because the only event that clears the ban pillar is a successful
/// ban write and the only thing that produced one was a fresh threshold
/// crossing. Readiness — what an orchestrator uses to keep an edge in rotation
/// — was coupled to attack traffic.
///
/// The cadence matches the other writers (15 s) and the work is bounded by the
/// pending set, which is bounded by the number of distinct sources the engine
/// has decided to ban. A dead database cannot turn this into a hot loop over an
/// unbounded set; it retries the same small set, and the ban pillar's counter
/// already says that is happening.
/// </summary>
public sealed class PendingBanRetryWorker : BackgroundService
{
private static readonly TimeSpan Interval = TimeSpan.FromSeconds(15);
private readonly BlockStore _store;
private readonly ILogger<PendingBanRetryWorker> _log;
public PendingBanRetryWorker(BlockStore store, ILogger<PendingBanRetryWorker> log)
{
_store = store;
_log = log;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try { await Task.Delay(Interval, stoppingToken); }
catch (TaskCanceledException) { return; }
try
{
var written = await _store.RetryPendingBansAsync(stoppingToken);
if (written > 0)
_log.LogInformation("PendingBanRetry: {Count} ban(s) that had failed to reach storage are "
+ "now in force; readiness returns on this write, not on a timer.", written);
}
catch (OperationCanceledException) { return; }
catch (Exception ex)
{
// The retry pass failing is itself a write-path event, and the
// ban pillar's counter is what the operator sees; logging here
// keeps the loop alive so the next pass can try again.
_log.LogWarning(ex, "PendingBanRetry: the retry pass failed; {Count} ban(s) still pending.",
_store.PendingBanRetries);
}
}
}
}