Every defence this edge had reads something that already happened: the status the application answered, the country the address sits in, a list a human wrote. A source asking for a cheap path two hundred times a minute produces no refusal, so no streak matures, no path rule names it, and the origin serves all two hundred. The only signal is the count, and a count has to be kept while the requests are arriving — which is why this pillar is the one piece of live state on the request path, and why it runs after the ordered decision instead of inside it. `rateMode` off|observe|enforce, `rateLimitPerWindow`, `rateWindowSeconds` (policy_settings, so no schema change). Off is the default: a limit nobody wrote is a defence nobody asked for, and a false positive here locks out a whole office behind one address. `observe` counts and raises one `rate_breach` alert per source per window. `enforce` answers the request that crosses with 429 + Retry-After — the surplus never reaches the origin — and writes an engine ban, so the stop outlives the window, is shared with the other edges and survives a restart; from then on the answer is the ban's own 403. One breach, one alert, one ban write per source per window: a flood is exactly the traffic that turns a per-request durable write into the thing that takes the database down. Exempt by construction rather than by a second copy of the precedence rules: the counter runs only on a plain Pass, so an always-pass prefix, an allowlisted address, a matched rule, a block or a geo refusal never reaches it. The budget counts exactly the requests that would have cost the origin. Costs, stated rather than hidden: the counter is per process, so N edges allow N times the budget (the same trade `SharedStreaks` made for the streak, at a round trip per request instead of per observed response); the window is fixed, not sliding, so the worst sustained rate is twice the limit across one boundary — a sliding counter needs one entry per request, which is a map that grows at attack rate. Two things the run caught rather than the design assumed: * The guard wrote the 429 and then called next() anyway, so YARP answered "the request cannot be forwarded, the response has already started". The pillar now says whether the request may go through. * `StreakTracker.Configure` was called by nobody outside its own tests, so `maxTrackedSources` bounded nothing in a running edge — the map grew to the built-in 10000 whatever the operator had asked for. Both maps are configured at warm-up and on a policy write now. And the smoke gate was not testing what its first line says. The edge was started from the repository root, where there is no appsettings.json, so it loaded no ReverseProxy section at all, logged "Request reached the end of the middleware pipeline without being handled by application code" for every request, and answered 404 for a path the upstream was serving with 200. Every assertion in the file expects a 404 or a refusal, so the gate kept passing on an edge that never proxied anything. Naming the content root fixes it; the streak scene also waits for the ban row to exist before asking the wire whether it is enforced, because the ban is written out of band after the response that proved it. Gates on this tree: build 0 warnings 0 errors, unit 207/207, integration 219/219 in 20 s, smoke passed 48 failed 0, console 90 assertions 0 failed, compose 209 passed 0 failed, image 261 MB. OPEN-56
209 lines
7.2 KiB
C#
209 lines
7.2 KiB
C#
using OpenWaf.Domain.Engine;
|
|
using OpenWaf.Domain.Policy;
|
|
using Xunit;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
/// <summary>
|
|
/// The volume counter's arithmetic (OPEN-56). The enforcement is the guard's
|
|
/// business and is tested end to end; what lives here is the part that has to
|
|
/// be exactly right and cannot be seen from the outside: when a window starts,
|
|
/// which single request is allowed to call itself the breach, and what happens
|
|
/// to the map when the traffic is the kind that exists to grow it.
|
|
/// </summary>
|
|
public sealed class RateTrackerTests
|
|
{
|
|
private static readonly DateTimeOffset T0 = new(2026, 10, 4, 12, 0, 0, TimeSpan.Zero);
|
|
|
|
private static EdgePolicy Policy(int limit = 3, int windowSeconds = 60) => new()
|
|
{
|
|
Rate = RateMode.Enforce,
|
|
RateLimitPerWindow = limit,
|
|
RateWindow = TimeSpan.FromSeconds(windowSeconds)
|
|
};
|
|
|
|
[Fact]
|
|
public void The_budget_counts_requests_and_not_responses()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 3);
|
|
|
|
for (var i = 1; i <= 3; i++)
|
|
{
|
|
var seen = tracker.Record("1.2.3.4", policy, T0.AddSeconds(i));
|
|
Assert.Equal(i, seen.Count);
|
|
Assert.False(seen.OverBudget);
|
|
Assert.False(seen.BreachStarted);
|
|
}
|
|
|
|
var fourth = tracker.Record("1.2.3.4", policy, T0.AddSeconds(10));
|
|
Assert.Equal(4, fourth.Count);
|
|
Assert.True(fourth.OverBudget);
|
|
Assert.True(fourth.BreachStarted);
|
|
}
|
|
|
|
[Fact]
|
|
public void Only_the_request_that_crosses_is_the_breach()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 2);
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
tracker.Record("1.2.3.4", policy, T0.AddSeconds(i));
|
|
|
|
// One alert and one ban write per source per window, not one per
|
|
// request: a flood is precisely the traffic that turns a per-request
|
|
// durable write into the thing that takes the database down.
|
|
var breaches = Enumerable.Range(0, 10)
|
|
.Select(i => tracker.Record("9.9.9.9", policy, T0.AddSeconds(i)))
|
|
.Count(r => r.BreachStarted);
|
|
Assert.Equal(1, breaches);
|
|
}
|
|
|
|
[Fact]
|
|
public void The_window_starts_at_the_sources_first_request_not_on_a_global_clock()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 1, windowSeconds: 60);
|
|
|
|
var late = tracker.Record("1.2.3.4", policy, T0.AddSeconds(59));
|
|
Assert.False(late.OverBudget);
|
|
Assert.Equal(T0.AddSeconds(59), late.WindowStart);
|
|
Assert.Equal(T0.AddSeconds(119), late.WindowEnd);
|
|
|
|
// A source that arrives just before the imaginary global boundary is not
|
|
// handed a one-second window: the limit is not a lottery on arrival time.
|
|
var over = tracker.Record("1.2.3.4", policy, T0.AddSeconds(60));
|
|
Assert.True(over.OverBudget);
|
|
Assert.Equal(T0.AddSeconds(59), over.WindowStart);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_new_window_restarts_the_count_and_the_source_flows_again()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 2, windowSeconds: 30);
|
|
|
|
tracker.Record("1.2.3.4", policy, T0);
|
|
tracker.Record("1.2.3.4", policy, T0.AddSeconds(1));
|
|
Assert.True(tracker.Record("1.2.3.4", policy, T0.AddSeconds(2)).OverBudget);
|
|
|
|
var next = tracker.Record("1.2.3.4", policy, T0.AddSeconds(31));
|
|
Assert.Equal(1, next.Count);
|
|
Assert.False(next.OverBudget);
|
|
Assert.Equal(T0.AddSeconds(31), next.WindowStart);
|
|
}
|
|
|
|
[Fact]
|
|
public void Each_source_has_its_own_window_and_its_own_count()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 1);
|
|
|
|
Assert.False(tracker.Record("1.2.3.4", policy, T0).OverBudget);
|
|
Assert.True(tracker.Record("1.2.3.4", policy, T0).OverBudget);
|
|
Assert.False(tracker.Record("5.6.7.8", policy, T0).OverBudget);
|
|
Assert.Equal(2, tracker.TrackedCount);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_budget_of_zero_is_not_a_limit_and_refuses_nothing()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = new EdgePolicy { Rate = RateMode.Enforce, RateLimitPerWindow = 0 };
|
|
|
|
for (var i = 0; i < 5; i++)
|
|
Assert.False(tracker.Record("1.2.3.4", policy, T0.AddSeconds(i)).OverBudget);
|
|
}
|
|
|
|
[Fact]
|
|
public void The_map_is_bounded_so_a_farm_of_new_addresses_cannot_grow_the_process()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 5);
|
|
tracker.Configure(policy with { MaxTrackedSources = 10 });
|
|
|
|
for (var i = 0; i < 500; i++)
|
|
tracker.Record($"10.0.0.{i % 251}", policy, T0.AddSeconds(i / 251));
|
|
|
|
Assert.Equal(10, tracker.TrackedCount);
|
|
}
|
|
|
|
[Fact]
|
|
public void An_idle_source_stops_being_counted()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 5);
|
|
|
|
tracker.Record("1.2.3.4", policy, T0);
|
|
Assert.Equal(1, tracker.TrackedCount);
|
|
|
|
tracker.LiveRates(T0.AddHours(3), policy with { StateIdleTtl = TimeSpan.FromMinutes(10) },
|
|
10, 0, out var live);
|
|
Assert.Equal(0, live);
|
|
Assert.Equal(0, tracker.TrackedCount);
|
|
}
|
|
|
|
[Fact]
|
|
public void The_listing_shows_only_windows_that_are_still_running_biggest_first()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 10, windowSeconds: 30);
|
|
|
|
for (var i = 0; i < 4; i++) tracker.Record("1.2.3.4", policy, T0);
|
|
for (var i = 0; i < 2; i++) tracker.Record("5.6.7.8", policy, T0);
|
|
// A window that has already run out is about to restart at zero.
|
|
tracker.Record("9.9.9.9", policy, T0.AddSeconds(-120));
|
|
|
|
var rows = tracker.LiveRates(T0.AddSeconds(10), policy, 10, 0, out var total);
|
|
Assert.Equal(2, total);
|
|
Assert.Equal(2, rows.Count);
|
|
Assert.Equal("1.2.3.4", rows[0].SourceKey);
|
|
Assert.Equal(4, rows[0].Count);
|
|
Assert.False(rows[0].OverBudget);
|
|
Assert.Equal(T0.AddSeconds(30), rows[0].WindowEnd);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_filtered_listing_counts_only_what_it_shows()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 10);
|
|
|
|
tracker.Record("1.2.3.4", policy, T0);
|
|
tracker.Record("5.6.7.8", policy, T0);
|
|
|
|
var rows = tracker.LiveRates(T0, policy, 10, 0, "5.6.7.8", out var total);
|
|
Assert.Single(rows);
|
|
Assert.Equal(1, total);
|
|
Assert.Equal(2, tracker.TrackedCount);
|
|
}
|
|
|
|
[Fact]
|
|
public void Forgetting_a_source_clears_its_window()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 1);
|
|
|
|
tracker.Record("1.2.3.4", policy, T0);
|
|
Assert.True(tracker.Record("1.2.3.4", policy, T0).OverBudget);
|
|
|
|
tracker.Forget("1.2.3.4");
|
|
Assert.Equal(0, tracker.TrackedCount);
|
|
Assert.False(tracker.Record("1.2.3.4", policy, T0).OverBudget);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_source_without_an_address_costs_nothing_and_claims_nothing()
|
|
{
|
|
var tracker = new RateTracker();
|
|
var policy = Policy(limit: 1);
|
|
|
|
var seen = tracker.Record("", policy, T0);
|
|
Assert.Equal(0, seen.Count);
|
|
Assert.False(seen.OverBudget);
|
|
Assert.False(seen.BreachStarted);
|
|
Assert.Equal(0, tracker.TrackedCount);
|
|
}
|
|
}
|