The edge-wide budget is one number for the whole site, and for a login path that number is always wrong: a hundred requests a minute does not stop a credential-stuffing run that makes twenty attempts a minute — twenty looks like a person — while five a minute locks an honest office behind one NAT out of every page they own. The only way to make both statements true is to price the paths differently. The rulings this had to make, and the answer each one got: * A rate rule is not a path rule. A path rule refuses a request because of what it asked for; a budget refuses it because of how often that source asked. The pattern language is shared because the thing being described — this path and what hangs off it — is the same. * Crossing a budget writes no engine ban. The claim is about one path, so a ban would refuse the rest of the site as punishment for a login-page rate. The answer is 429 with Retry-After for the rest of that source's window, and the reason names the rule, its count, its limit and the pattern it was written for. * The counters are a separate tracker, not a scope inside RateTracker. The edge-wide counter reads its limit and window from the policy; a rule carries its own. One map whose parameters change per entry is a map whose eviction and idle rules stop meaning anything. * The budgets hang off rateMode rather than getting a switch of their own: Enforce refuses, Observe counts and raises one rate_breach alert per source per window naming the pattern, Off counts nothing at all. A half-counting edge is the state an operator cannot reason about. * A second live budget for one pattern is a 409 — two live numbers for one path mean the stricter one wins by accident rather than by the operator's choice. * The bounds are the policy's own (1..100000 requests, 1..3600 seconds), so one vocabulary of numbers covers both budgets and a document cannot bring in a budget the API would have refused. * The listing answers with three counters — sources counted, windows running, over budget now — because "nobody has blown this" and "nobody can" look identical on paper and are different decisions. What the runs caught, in order: * The smoke scene first ran on 198.51.100.0/24, which an earlier scene of the same gate allowlists. An allowlisted source never reaches the counter, so the scene would have printed a row of 200s while measuring nothing. It runs on TEST-NET-1 now. * The console scene drew three 404s of its own and the streak pillar banned the address, so the reopen assertion read the streak's 403 instead of the absence of the budget. The engine list is cleared before that probe and the comment says which pillar the clearing belongs to. * The compose assertion compared a JSON boolean against the word "yes". * The change-log vocabulary test still listed six sections; there are seven now. * A unit expectation counted requests where the tracker holds windows: one (rule, source) pair is one window, and the count inside it is what the budget is measured against. Gates at this commit, each stage on its own: build 0 warnings / 0 errors, unit 361/361, integration 337/337 in 30 s, console 120 assertions 0 failed, smoke passed 169 failed 0, compose 300 passed 0 failed against the built image at 262 MB, 0 containers left behind. Refs OPEN-68.
171 lines
6.5 KiB
C#
171 lines
6.5 KiB
C#
using OpenWaf.Domain.Engine;
|
|
using OpenWaf.Domain.Policy;
|
|
using OpenWaf.Domain.Security;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
/// <summary>
|
|
/// The per-path budget counter (OPEN-68), on its own. The integration tests ask
|
|
/// whether the edge refuses; these ask whether the counting itself is the thing
|
|
/// the operator wrote — one window per source, one breach per window, and a
|
|
/// memory bound that holds when the source keys do not stop coming.
|
|
/// </summary>
|
|
public sealed class RateRuleTrackerTests
|
|
{
|
|
private static readonly DateTimeOffset T0 = new(2026, 10, 5, 12, 0, 0, TimeSpan.Zero);
|
|
|
|
private static RateRule Rule(long id, string pattern, int limit, int windowSeconds = 60) =>
|
|
new() { Id = id, Pattern = pattern, LimitPerWindow = limit, WindowSeconds = windowSeconds };
|
|
|
|
[Fact]
|
|
public void The_window_starts_at_the_first_request_and_restarts_after_it_runs_out()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(1, "/login*", 3);
|
|
|
|
var first = tracker.Record(rule, "203.0.113.1", T0);
|
|
Assert.Equal(1, first.Count);
|
|
Assert.Equal(T0, first.WindowStart);
|
|
Assert.Equal(T0.AddSeconds(60), first.WindowEnd);
|
|
|
|
tracker.Record(rule, "203.0.113.1", T0.AddSeconds(10));
|
|
var inside = tracker.Record(rule, "203.0.113.1", T0.AddSeconds(20));
|
|
Assert.Equal(3, inside.Count);
|
|
Assert.False(inside.OverBudget);
|
|
|
|
// Still the same window: the budget is per window, not per minute.
|
|
var last = tracker.Record(rule, "203.0.113.1", T0.AddSeconds(59));
|
|
Assert.Equal(4, last.Count);
|
|
Assert.Equal(T0, last.WindowStart);
|
|
|
|
var after = tracker.Record(rule, "203.0.113.1", T0.AddSeconds(61));
|
|
Assert.Equal(1, after.Count);
|
|
Assert.Equal(T0.AddSeconds(61), after.WindowStart);
|
|
Assert.False(after.OverBudget);
|
|
}
|
|
|
|
[Fact]
|
|
public void Only_the_request_that_crosses_the_budget_starts_a_breach()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(1, "/login*", 2);
|
|
|
|
Assert.False(tracker.Record(rule, "a", T0).OverBudget);
|
|
Assert.False(tracker.Record(rule, "a", T0.AddSeconds(1)).OverBudget);
|
|
|
|
var crossed = tracker.Record(rule, "a", T0.AddSeconds(2));
|
|
Assert.True(crossed.OverBudget);
|
|
Assert.True(crossed.BreachStarted);
|
|
|
|
// Over budget is a state that goes on for the rest of the window; the
|
|
// breach is the one event in it. An alert per request is how a defence
|
|
// turns into write amplification.
|
|
var later = tracker.Record(rule, "a", T0.AddSeconds(3));
|
|
Assert.True(later.OverBudget);
|
|
Assert.False(later.BreachStarted);
|
|
}
|
|
|
|
[Fact]
|
|
public void Two_rules_count_the_same_source_separately()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var login = Rule(1, "/login*", 2);
|
|
var api = Rule(2, "/api/*", 5);
|
|
|
|
tracker.Record(login, "a", T0);
|
|
tracker.Record(login, "a", T0.AddSeconds(1));
|
|
var overLogin = tracker.Record(login, "a", T0.AddSeconds(2));
|
|
Assert.True(overLogin.OverBudget);
|
|
|
|
var underApi = tracker.Record(api, "a", T0.AddSeconds(3));
|
|
Assert.Equal(1, underApi.Count);
|
|
Assert.False(underApi.OverBudget);
|
|
|
|
// One window per (rule, source), not one per request: the count inside
|
|
// that window is what the budget is measured against.
|
|
Assert.Equal(1, tracker.TrackedCount(login.Id));
|
|
Assert.Equal(1, tracker.TrackedCount(api.Id));
|
|
}
|
|
|
|
[Fact]
|
|
public void The_cap_holds_the_newest_pairs_and_says_nothing_about_the_rest()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
tracker.Configure(new EdgePolicy { MaxTrackedSources = 3 });
|
|
var rule = Rule(1, "/x*", 10);
|
|
|
|
for (var i = 0; i < 5; i++)
|
|
tracker.Record(rule, $"10.0.0.{i}", T0.AddSeconds(i));
|
|
|
|
Assert.Equal(3, tracker.TrackedCount(rule.Id));
|
|
// The two oldest sources were dropped, so their next request starts a
|
|
// fresh window. That is the documented cost of a bounded counter, and it
|
|
// is the reason the cap is a policy key rather than a constant.
|
|
Assert.Equal(1, tracker.Record(rule, "10.0.0.0", T0.AddSeconds(10)).Count);
|
|
Assert.Equal(2, tracker.Record(rule, "10.0.0.4", T0.AddSeconds(11)).Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void Removing_a_rule_takes_its_counters_with_it()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(7, "/login*", 2);
|
|
tracker.Record(rule, "a", T0);
|
|
Assert.Equal(1, tracker.TrackedCount(7));
|
|
|
|
tracker.ForgetRule(7);
|
|
Assert.Equal(0, tracker.TrackedCount(7));
|
|
// A later rule reusing the id must not inherit a window it never started.
|
|
Assert.Equal(1, tracker.Record(Rule(7, "/login*", 2), "a", T0.AddSeconds(1)).Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void An_allowlisted_source_stops_being_counted()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(1, "/login*", 2);
|
|
tracker.Record(rule, "a", T0);
|
|
tracker.Record(rule, "b", T0);
|
|
Assert.Equal(2, tracker.TrackedCount(1));
|
|
|
|
tracker.ForgetSource("a");
|
|
Assert.Equal(1, tracker.TrackedCount(1));
|
|
}
|
|
|
|
[Fact]
|
|
public void The_live_listing_reports_the_windows_that_are_still_running()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(1, "/login*", 2);
|
|
tracker.Record(rule, "quiet", T0);
|
|
tracker.Record(rule, "loud", T0);
|
|
tracker.Record(rule, "loud", T0.AddSeconds(1));
|
|
tracker.Record(rule, "loud", T0.AddSeconds(2));
|
|
tracker.Record(rule, "loud", T0.AddSeconds(3));
|
|
// A window that has already run out is not "over budget now".
|
|
tracker.Record(rule, "early", T0.AddSeconds(-120));
|
|
|
|
var (live, over) = tracker.LiveCounts(1, 2, T0.AddSeconds(10));
|
|
// The run-out window is not "running", and the quiet source is running
|
|
// but not over: the pair is what tells an operator whether the number
|
|
// they wrote is a defence or their own traffic.
|
|
Assert.Equal(2, live);
|
|
Assert.Equal(1, over);
|
|
|
|
// Past every window, nothing is running and nothing is over.
|
|
Assert.Equal((0, 0), tracker.LiveCounts(1, 2, T0.AddSeconds(120)));
|
|
}
|
|
|
|
[Fact]
|
|
public void An_unidentifiable_source_is_not_counted()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(1, "/login*", 1);
|
|
|
|
var blank = tracker.Record(rule, "", T0);
|
|
Assert.Equal(0, blank.Count);
|
|
Assert.False(blank.OverBudget);
|
|
Assert.Equal(0, tracker.TrackedCount(1));
|
|
}
|
|
}
|