The streak engine raised an alert on every response once the count was past the threshold, and neither condition ends on its own: a 5xx never bans, and in Observe mode nothing bans at all, so the source stays observable on every request. Measured on the shipped image in the compose gate: 12 upstream 502s at N=3 wrote 10 alert rows, and 11 refusals in a dry run wrote 9. That is attacker-shaped growth kept for the whole retention window, and the operator's alert list (newest 100 in the console, limit=500 on the endpoint) showed one source and nothing else. Both kinds are now edge-triggered: one row on the crossing, then one per doubling. Same two measurements answer 3 and 2. A gap past the window or a clean answer ends the episode, so the next outage announces itself again — alerting once per source per process would hide an outage that got ten times worse while nobody was looking. Teeth, measured rather than asserted: with IsEscalation forced to the old level-triggered answer, exactly the four new unit tests and the two new integration tests fail, and the compose gate reports 10 and 9 and fails those two checks; the other 41 compose checks stay green. Gates on this commit — host (macOS): build 0 warnings, unit 58/58, integration 32/32, smoke 19/19, image 261 MB, compose-smoke 43/43. Linux rehearsal (scripts/ci-linux-rehearsal.sh): source, compose and console gates all PASS, exit 0.
52 lines
1.9 KiB
C#
52 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace OpenWaf.Domain.Engine;
|
|
|
|
/// <summary>
|
|
/// Live in-memory state for one source. Mutable and lock-guarded on purpose:
|
|
/// this object is touched on every response, so it must not allocate a fresh
|
|
/// record per request.
|
|
/// </summary>
|
|
public sealed class StreakState
|
|
{
|
|
public required string SourceKey { get; init; }
|
|
|
|
public int ConsecutiveRefusals;
|
|
public int MaxConsecutiveRefusals;
|
|
public int TotalRefusals;
|
|
public int ConsecutiveServerErrors;
|
|
public int MaxConsecutiveServerErrors;
|
|
|
|
/// <summary>The count the last alert of each kind was raised at, for this
|
|
/// episode. 0 means "no alert yet", which is what makes the crossing
|
|
/// itself always speak. Reset when the episode ends (a clean answer, or a
|
|
/// gap longer than the window), so the next episode gets its own alert.
|
|
/// See StreakTracker.IsEscalation for what counts as escalation.</summary>
|
|
public int LastAlertedRefusals;
|
|
public int LastAlertedServerErrors;
|
|
|
|
public DateTimeOffset? LastRefusalAt;
|
|
public DateTimeOffset LastSeenAt;
|
|
public int? LastStatusCode;
|
|
public string? LastPath;
|
|
public string? LastUserAgent;
|
|
public string? LastCountryCode;
|
|
public string? LastCity;
|
|
public string? LastRegion;
|
|
|
|
/// <summary>Position in the LRU list, so eviction is O(1) and a touch is
|
|
/// O(1). Null once the state has been evicted.</summary>
|
|
public LinkedListNode<string>? LruNode;
|
|
|
|
public bool HasEvidence => TotalRefusals > 0 || MaxConsecutiveServerErrors > 0;
|
|
|
|
public void Apply(RequestEvidence evidence)
|
|
{
|
|
if (evidence.Path is not null) LastPath = evidence.Path;
|
|
if (evidence.UserAgent is not null) LastUserAgent = evidence.UserAgent;
|
|
if (evidence.CountryCode is not null) LastCountryCode = evidence.CountryCode;
|
|
if (evidence.City is not null) LastCity = evidence.City;
|
|
if (evidence.Region is not null) LastRegion = evidence.Region;
|
|
}
|
|
}
|