The multi-instance research measured the gap rather than guessing it: four refusals from one address, two per edge, at a threshold of 3, produced no ban anywhere, because the block list was shared through the database and the streak map was not (research/2026-10-01-multi-instance/, D1). OpenWaf:Policy: SharedStreaks=true moves the counters onto a row in source_streaks; the default stays the in-process map, because that is the deployment that loses nothing. The rules were extracted into StreakRules.Apply and both stores call it — the in-process map against a StreakState, the shared one against a row. A second copy of the streak semantics would put the drift back where this removes it. Version is a concurrency token, so two edges writing one source produce a retry against the winner's row rather than a silently dropped refusal; a clean answer on any edge ends the run on every edge, in one statement. The rows age out on the same ObservationRetentionDays window as observations. Both surfaces say which store answered: /admin-api/streaks scope is this-process or shared-database, /admin-api/state answers streakScope, and the console's streak note stops claiming "this edge process only" when it is not true. Found by running it for real rather than in-process: two edge processes on one SQLite file both create the schema on startup, and the loser of that race died in StartAsync with 'table "allow_entries" already exists' — hosting failed to start and the process never listened. The schema step now re-checks existence and continues, because what has to be true is that the schema exists, not that this process made it. Measured on this tree: build 0 warnings / 0 errors, unit 132/132, integration 191/191 in 32 s (6 new, two real edge instances on one SQLite file each), smoke 28/0, console 78 assertions 0 failed. Out of process, two edge binaries on one file at a 5 s TTL: A answered 404, 404; B answered 404 on the third refusal and 403 after it; both edges then reported consecutiveRefusals: 3 with scope: shared-database, and A started enforcing 4 s later. Before the schema fix the second process aborted on startup, which is how that fix was found.
54 lines
2.5 KiB
C#
54 lines
2.5 KiB
C#
namespace OpenWaf.Domain.Security;
|
|
|
|
/// <summary>
|
|
/// The streak counters for one source, held in the database instead of in one
|
|
/// process's memory (OPEN-52).
|
|
///
|
|
/// This is what makes a threshold of 3 mean three refusals across a fleet.
|
|
/// Measured in <c>research/2026-10-01-multi-instance/</c>: four refusals from
|
|
/// one address, two per edge, at a threshold of 3, produced no ban anywhere —
|
|
/// each edge was counting its own map. The block list was already shared
|
|
/// through the database; the streak map was the half that was not.
|
|
///
|
|
/// One row per source key, upserted rather than appended — the same shape that
|
|
/// keeps a scanner farm from turning the edge into an append-only log. The row
|
|
/// ages out on <see cref="LastSeenAt"/>: idle rows are dropped when they are
|
|
/// read past <see cref="EdgePolicy.StateIdleTtl"/> and pruned by the retention
|
|
/// worker, so the table is bounded by traffic, not by how long the process runs.
|
|
/// </summary>
|
|
public sealed class SourceStreak
|
|
{
|
|
public string SourceKey { get; init; } = string.Empty;
|
|
|
|
public int ConsecutiveRefusals { get; set; }
|
|
public int MaxConsecutiveRefusals { get; set; }
|
|
public int TotalRefusals { get; set; }
|
|
public int ConsecutiveServerErrors { get; set; }
|
|
public int MaxConsecutiveServerErrors { get; set; }
|
|
|
|
/// <summary>The count the last alert of each kind was raised at, for this
|
|
/// episode — the field that makes the alerts edge-triggered rather than
|
|
/// per-request. Shared, so two edges do not each raise their own first
|
|
/// alert for one outage.</summary>
|
|
public int LastAlertedRefusals { get; set; }
|
|
public int LastAlertedServerErrors { get; set; }
|
|
|
|
public DateTimeOffset? LastRefusalAt { get; set; }
|
|
public DateTimeOffset LastSeenAt { get; set; } = DateTimeOffset.UtcNow;
|
|
|
|
public int? LastStatusCode { get; set; }
|
|
public string? LastPath { get; set; }
|
|
public string? LastUserAgent { get; set; }
|
|
public string? LastCountryCode { get; set; }
|
|
public string? LastCity { get; set; }
|
|
public string? LastRegion { get; set; }
|
|
|
|
/// <summary>Optimistic concurrency. Two edges reading the same row and both
|
|
/// writing is the normal case behind a load balancer, not a corner: without
|
|
/// this, the second write silently drops the first edge's refusal and the
|
|
/// threshold is wrong again — the exact bug this table exists to fix. The
|
|
/// loser gets <c>DbUpdateConcurrencyException</c> and re-applies the rules
|
|
/// against the winner's row.</summary>
|
|
public int Version { get; set; }
|
|
}
|