diff --git a/dotnet/OpenWaf.Domain/Engine/StreakTracker.cs b/dotnet/OpenWaf.Domain/Engine/StreakTracker.cs index cf4879f..2bbff53 100644 --- a/dotnet/OpenWaf.Domain/Engine/StreakTracker.cs +++ b/dotnet/OpenWaf.Domain/Engine/StreakTracker.cs @@ -58,6 +58,12 @@ public sealed class StreakTracker state.TotalRefusals++; if (state.ConsecutiveRefusals > state.MaxConsecutiveRefusals) state.MaxConsecutiveRefusals = state.ConsecutiveRefusals; + // A refusal ends the server-error run, and with it the + // episode: without this, a source that alternates 5xx with + // 4xx keeps the "already alerted at 12" mark while its + // error count restarts at 1, and the next outage is silent + // until the count doubles the old mark. + state.LastAlertedServerErrors = 0; state.ConsecutiveServerErrors = 0; break; diff --git a/dotnet/OpenWaf.Tests.Unit/StreakTrackerTests.cs b/dotnet/OpenWaf.Tests.Unit/StreakTrackerTests.cs index b967395..92252d9 100644 --- a/dotnet/OpenWaf.Tests.Unit/StreakTrackerTests.cs +++ b/dotnet/OpenWaf.Tests.Unit/StreakTrackerTests.cs @@ -128,6 +128,35 @@ public sealed class StreakTrackerTests 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() {