Review finding on the edge-trigger rule. The Refusal branch zeroes ConsecutiveServerErrors without clearing LastAlertedServerErrors, so a source alternating 5xx with 4xx kept a stale "already alerted at 12" mark while its error count restarted at 1: the next twelve broken answers stayed silent until the count doubled the stale mark. One refusal inside the window was enough. Now the mark goes with the run it describes. A_refusal_ends_the_server_error_ episode_so_the_next_outage_is_not_silent pins it: 3 alerts, one refusal, 3 more alerts. With the reset removed exactly that test fails (1 failed, 58 passed). Gates: build 0 warnings, unit 59/59, integration 32/32, smoke 19/19.
243 lines
8.1 KiB
C#
243 lines
8.1 KiB
C#
using OpenWaf.Domain.Engine;
|
|
using OpenWaf.Domain.Policy;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
public sealed class StreakTrackerTests
|
|
{
|
|
private static readonly DateTimeOffset T0 = new(2026, 10, 1, 12, 0, 0, TimeSpan.Zero);
|
|
private static readonly EdgePolicy Policy = new() { StreakThreshold = 3, StreakWindow = TimeSpan.FromMinutes(5) };
|
|
|
|
private static StreakOutcome Refusal(StreakTracker t, string key, DateTimeOffset at, int status = 404)
|
|
=> t.Observe(key, status, ResponseClass.Refusal, Policy, at);
|
|
|
|
[Fact]
|
|
public void Three_refusals_in_a_row_ban()
|
|
{
|
|
var t = new StreakTracker();
|
|
Assert.False(Refusal(t, "1.1.1.1", T0).BanTriggered);
|
|
Assert.False(Refusal(t, "1.1.1.1", T0.AddSeconds(10)).BanTriggered);
|
|
Assert.True(Refusal(t, "1.1.1.1", T0.AddSeconds(20)).BanTriggered);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_gap_longer_than_the_window_restarts_the_streak()
|
|
{
|
|
var t = new StreakTracker();
|
|
Refusal(t, "1.1.1.1", T0);
|
|
Refusal(t, "1.1.1.1", T0.AddMinutes(1));
|
|
var afterGap = Refusal(t, "1.1.1.1", T0.AddMinutes(1).AddMinutes(6));
|
|
|
|
Assert.Equal(1, afterGap.ConsecutiveRefusals);
|
|
Assert.False(afterGap.BanTriggered);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_successful_response_resets_the_streak_because_pidryad_means_consecutive()
|
|
{
|
|
var t = new StreakTracker();
|
|
Refusal(t, "1.1.1.1", T0);
|
|
Refusal(t, "1.1.1.1", T0.AddSeconds(1));
|
|
t.Observe("1.1.1.1", 200, ResponseClass.Neutral, Policy, T0.AddSeconds(2));
|
|
|
|
Assert.False(Refusal(t, "1.1.1.1", T0.AddSeconds(3)).BanTriggered);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_run_of_server_errors_alerts_and_never_bans()
|
|
{
|
|
var t = new StreakTracker();
|
|
var alerts = 0;
|
|
StreakOutcome last = default;
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
last = t.Observe("1.1.1.1", 502, ResponseClass.ServerError, Policy, T0.AddSeconds(i));
|
|
if (last.AlertTriggered) alerts++;
|
|
}
|
|
|
|
// N=3: the crossing at 3 and the first doubling at 6. Ten responses
|
|
// used to raise ten alerts; the count is what the storm test below
|
|
// pins down, here it is only the shape: it speaks, and it never bans.
|
|
Assert.Equal(2, alerts);
|
|
Assert.False(last.BanTriggered);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_storm_of_server_errors_alerts_on_the_crossing_and_then_only_as_it_doubles()
|
|
{
|
|
var t = new StreakTracker();
|
|
var alerts = 0;
|
|
for (var i = 0; i < 1_000; i++)
|
|
{
|
|
if (t.Observe("1.1.1.1", 502, ResponseClass.ServerError, Policy, T0.AddSeconds(i)).AlertTriggered)
|
|
alerts++;
|
|
}
|
|
|
|
// 3, 6, 12, 24, 48, 96, 192, 384, 768. One thousand broken responses
|
|
// from one source used to cost 998 alert rows, kept for the whole
|
|
// retention window, and the operator's alert list (newest 100) showed
|
|
// nothing but that one source.
|
|
Assert.Equal(9, alerts);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_storm_that_gaps_past_the_window_is_a_new_episode_and_alerts_again()
|
|
{
|
|
var t = new StreakTracker();
|
|
var alerts = 0;
|
|
for (var i = 0; i < 3; i++)
|
|
{
|
|
if (t.Observe("1.1.1.1", 502, ResponseClass.ServerError, Policy, T0.AddSeconds(i)).AlertTriggered)
|
|
alerts++;
|
|
}
|
|
|
|
// The upstream recovers, then breaks again an hour later. The streak
|
|
// restarts at 1, so the second episode has to be able to speak too:
|
|
// an alert rule that only fires once per source per process lifetime
|
|
// would leave the second outage invisible.
|
|
for (var i = 0; i < 3; i++)
|
|
{
|
|
var at = T0.AddHours(1).AddSeconds(i);
|
|
if (t.Observe("1.1.1.1", 502, ResponseClass.ServerError, Policy, at).AlertTriggered)
|
|
alerts++;
|
|
}
|
|
|
|
Assert.Equal(2, alerts);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_clean_answer_ends_the_episode_so_the_next_storm_alerts_again()
|
|
{
|
|
var t = new StreakTracker();
|
|
var alerts = 0;
|
|
for (var i = 0; i < 4; i++)
|
|
{
|
|
if (t.Observe("1.1.1.1", 502, ResponseClass.ServerError, Policy, T0.AddSeconds(i)).AlertTriggered)
|
|
alerts++;
|
|
}
|
|
|
|
t.Observe("1.1.1.1", 200, ResponseClass.Neutral, Policy, T0.AddSeconds(10));
|
|
|
|
for (var i = 0; i < 3; i++)
|
|
{
|
|
var at = T0.AddSeconds(11 + i);
|
|
if (t.Observe("1.1.1.1", 502, ResponseClass.ServerError, Policy, at).AlertTriggered)
|
|
alerts++;
|
|
}
|
|
|
|
Assert.Equal(2, alerts);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_refusal_ends_the_server_error_episode_so_the_next_outage_is_not_silent()
|
|
{
|
|
var t = new StreakTracker();
|
|
var first = 0;
|
|
for (var i = 0; i < 12; i++)
|
|
{
|
|
if (t.Observe("1.1.1.1", 502, ResponseClass.ServerError, Policy, T0.AddSeconds(i)).AlertTriggered)
|
|
first++;
|
|
}
|
|
|
|
// One refusal inside the window. It zeroes the error counter, so the
|
|
// episode is over — and the alert mark has to go with it, or the next
|
|
// twelve broken answers stay silent because the stale mark says
|
|
// "already alerted at 12" and the fresh count only reaches 12.
|
|
Refusal(t, "1.1.1.1", T0.AddSeconds(20));
|
|
|
|
var second = 0;
|
|
for (var i = 0; i < 12; i++)
|
|
{
|
|
var at = T0.AddSeconds(21 + i);
|
|
if (t.Observe("1.1.1.1", 502, ResponseClass.ServerError, Policy, at).AlertTriggered)
|
|
second++;
|
|
}
|
|
|
|
Assert.Equal(3, first);
|
|
Assert.Equal(3, second);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_dry_run_announces_a_candidate_ban_once_and_then_only_as_it_doubles()
|
|
{
|
|
var t = new StreakTracker();
|
|
var banTriggered = 0;
|
|
var announced = 0;
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
var o = t.Observe("1.1.1.1", 404, ResponseClass.Refusal, Policy, T0.AddSeconds(i));
|
|
if (o.BanTriggered) banTriggered++;
|
|
if (o.BanAlertDue) announced++;
|
|
}
|
|
|
|
// The gap between these two numbers is the whole point. BanTriggered is
|
|
// what the middleware uses to decide, and in Observe mode it was also
|
|
// what it used to write the candidate_ban row, so the safest mode was
|
|
// the one an attacker could grow the alert table with.
|
|
Assert.Equal(8, banTriggered);
|
|
Assert.Equal(2, announced);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_server_error_does_not_extend_a_refusal_streak()
|
|
{
|
|
var t = new StreakTracker();
|
|
Refusal(t, "1.1.1.1", T0);
|
|
t.Observe("1.1.1.1", 500, ResponseClass.ServerError, Policy, T0.AddSeconds(1));
|
|
var next = Refusal(t, "1.1.1.1", T0.AddSeconds(2));
|
|
|
|
Assert.Equal(2, next.ConsecutiveRefusals);
|
|
}
|
|
|
|
[Fact]
|
|
public void Sources_are_counted_separately()
|
|
{
|
|
var t = new StreakTracker();
|
|
Refusal(t, "1.1.1.1", T0);
|
|
Refusal(t, "1.1.1.1", T0.AddSeconds(1));
|
|
var other = Refusal(t, "2.2.2.2", T0.AddSeconds(2));
|
|
|
|
Assert.False(other.BanTriggered);
|
|
Assert.Equal(1, other.ConsecutiveRefusals);
|
|
}
|
|
|
|
[Fact]
|
|
public void The_map_is_bounded_so_a_scanner_cannot_grow_the_process()
|
|
{
|
|
var policy = Policy with { MaxTrackedSources = 100 };
|
|
var t = new StreakTracker();
|
|
t.Configure(policy);
|
|
|
|
for (var i = 0; i < 5_000; i++)
|
|
{
|
|
t.Observe($"10.0.{i / 256}.{i % 256}", 404, ResponseClass.Refusal, policy, T0);
|
|
}
|
|
|
|
Assert.True(t.TrackedCount <= 100, $"tracked {t.TrackedCount}");
|
|
}
|
|
|
|
[Fact]
|
|
public void Idle_states_age_out()
|
|
{
|
|
var policy = Policy with { StateIdleTtl = TimeSpan.FromMinutes(1) };
|
|
var t = new StreakTracker();
|
|
t.Observe("1.1.1.1", 404, ResponseClass.Refusal, policy, T0);
|
|
|
|
t.Snapshot(T0.AddMinutes(2), policy);
|
|
|
|
Assert.Equal(0, t.TrackedCount);
|
|
}
|
|
|
|
[Fact]
|
|
public void Snapshots_are_copies_not_live_state()
|
|
{
|
|
var t = new StreakTracker();
|
|
Refusal(t, "1.1.1.1", T0);
|
|
var before = t.Peek("1.1.1.1")!;
|
|
Refusal(t, "1.1.1.1", T0.AddSeconds(1));
|
|
|
|
Assert.Equal(1, before.TotalRefusals);
|
|
Assert.Equal(2, t.Peek("1.1.1.1")!.TotalRefusals);
|
|
}
|
|
}
|