The budget listing has carried three numbers per budget since OPEN-68 — sources
counted, windows running, over budget now — and no name. "3 over budget" is the
half an operator cannot act on, and the only lever for one of those three was
`DELETE /admin-api/rate-rules/{id}`: take the budget off every source on the site
to release one office behind one NAT. The edge-wide budget never had this hole,
because /admin-api/rate names its sources.
* GET /admin-api/rate-rules/{id}/sources — who this budget is inside right now:
sourceKey, count against the limit, when the window ends and how many seconds
are left, overBudget. Busiest first, live windows only, paged with
X-Total-Count / X-Truncated like every other listing. `total` is the same
number the listing's `liveWindows` reports and the over-budget count matches
`overBudgetNow`: the names come from the same state as the three counters,
not from a second ledger.
* DELETE /admin-api/rate-rules/{id}/sources?source=<key> — drop one source's
window on one budget. Not an exemption, and the answer says so: the window
restarts at zero and the budget counts this source again from there, while
every other source stays paused. An operator who types an address gets the
key this edge files it under, and the answer names the key it dropped,
because the others sharing it are still paused. A second clear answers
`cleared: false` and writes no second record; the first is in the change log
under `rate rule / changed`.
* Console: a `who` button per budget row, and a `let out` button per source.
Rulings kept: the entries are source keys with the prefix rule printed beside
them (a /24 in the list is the point, not a bug); the limit is passed in from the
rule row rather than copied into the counter, so one number is enforced once;
reading spends nothing, so the page that exists to show a pause cannot cause one;
an empty list says which kind of empty it is — "counting, nobody inside a window"
and "this budget has expired" are different answers; and the read-only token gets
a 403 on the clear, not a 401, because the token is known and its level is the
answer.
What the run caught, on this tree: two console scenes failed with `answered 403`
because the gate's own probes are refusals and the streak pillar had banned the
probe addresses — a correct edge, and a scene that cannot retry past a one-hour
ban. Both now clear that one ban by name, leaving the engine bans the netblock
scene reads standing. The first run of the feature also caught the clear
reporting `cleared: false` after actually dropping the window: the derived-key
branch dropped the state and never set the flag.
build 0 warnings / 0 errors, unit 377/377, integration 365/365 in 31 s,
console 129 assertions / 0 failed, smoke passed 202 / failed 0, compose 317
passed / 0 failed against the built image at 262 MB.
Refs OPEN-71.
315 lines
13 KiB
C#
315 lines
13 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)));
|
|
}
|
|
|
|
// The dossier (OPEN-70) asks "which path budgets is this source already
|
|
// inside" without naming a path, because the operator is asking about the
|
|
// source, not about a request.
|
|
[Fact]
|
|
public void One_source_can_be_asked_which_budgets_are_counting_it()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var login = Rule(1, "/login*", 2);
|
|
var api = Rule(7, "/api/*", 5, windowSeconds: 120);
|
|
var other = Rule(9, "/other*", 5);
|
|
|
|
tracker.Record(login, "203.0.113.1", T0);
|
|
tracker.Record(login, "203.0.113.1", T0.AddSeconds(1));
|
|
tracker.Record(api, "203.0.113.1", T0.AddSeconds(2));
|
|
tracker.Record(login, "someone-else", T0);
|
|
tracker.Record(other, "someone-else", T0);
|
|
|
|
var rows = tracker.WindowsFor("203.0.113.1", T0.AddSeconds(3));
|
|
// Only this source's windows, ordered by rule id so two calls answer in
|
|
// the same order, and the window length travels with the row.
|
|
Assert.Equal(2, rows.Count);
|
|
Assert.Equal(1L, rows[0].RuleId);
|
|
Assert.Equal(2, rows[0].Count);
|
|
Assert.Equal(T0.AddSeconds(60), rows[0].WindowEnd);
|
|
Assert.Equal(60, rows[0].WindowSeconds);
|
|
Assert.Equal(7L, rows[1].RuleId);
|
|
Assert.Equal(1, rows[1].Count);
|
|
Assert.Equal(120, rows[1].WindowSeconds);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_window_that_has_run_out_is_not_a_budget_this_source_is_inside()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(1, "/login*", 5);
|
|
|
|
tracker.Record(rule, "203.0.113.1", T0);
|
|
Assert.Single(tracker.WindowsFor("203.0.113.1", T0.AddSeconds(59)));
|
|
// "0 of 5" from a dead window is a different claim from "nothing yet", and
|
|
// the dossier has to be able to tell them apart — so it reports neither.
|
|
Assert.Empty(tracker.WindowsFor("203.0.113.1", T0.AddSeconds(61)));
|
|
// A source nobody ever counted answers with nothing, not with a row at
|
|
// zero, and an empty key never reaches into the map at all.
|
|
Assert.Empty(tracker.WindowsFor("never-seen", T0));
|
|
Assert.Empty(tracker.WindowsFor("", T0));
|
|
}
|
|
|
|
[Fact]
|
|
public void Asking_which_budgets_are_counting_a_source_spends_none_of_them()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(1, "/login*", 2);
|
|
|
|
tracker.Record(rule, "203.0.113.1", T0);
|
|
tracker.Record(rule, "203.0.113.1", T0.AddSeconds(1));
|
|
tracker.WindowsFor("203.0.113.1", T0.AddSeconds(2));
|
|
tracker.WindowsFor("203.0.113.1", T0.AddSeconds(2));
|
|
|
|
// The dossier's whole rule: a page that answers by counting its own
|
|
// question would refuse the next real request because somebody looked.
|
|
var next = tracker.Record(rule, "203.0.113.1", T0.AddSeconds(3));
|
|
Assert.Equal(3, next.Count);
|
|
Assert.Equal(T0, next.WindowStart);
|
|
}
|
|
|
|
// Who a budget is pausing (OPEN-71). The listing has shown three numbers per
|
|
// budget and no name, so the operator could see "3 over budget" and had one
|
|
// lever for it: delete the budget for everyone.
|
|
[Fact]
|
|
public void One_budget_can_be_asked_which_sources_are_inside_it()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var login = Rule(1, "/login*", 2);
|
|
var api = Rule(7, "/api/*", 5);
|
|
|
|
tracker.Record(login, "203.0.113.1", T0);
|
|
tracker.Record(login, "203.0.113.1", T0.AddSeconds(1));
|
|
tracker.Record(login, "203.0.113.1", T0.AddSeconds(2)); // 3 of 2: over
|
|
tracker.Record(login, "203.0.113.2", T0); // 1 of 2: inside
|
|
tracker.Record(login, "203.0.113.3", T0.AddSeconds(-600)); // window ran out
|
|
tracker.Record(api, "203.0.113.1", T0); // a different budget
|
|
|
|
var rows = tracker.SourcesFor(1, 2, T0.AddSeconds(3), 10, 0);
|
|
// Busiest first, this budget only, live windows only, and the over-budget
|
|
// flag computed from the limit the caller passed — not from a copy.
|
|
Assert.Equal(2, rows.Count);
|
|
Assert.Equal("203.0.113.1", rows[0].SourceKey);
|
|
Assert.Equal(3, rows[0].Count);
|
|
Assert.True(rows[0].OverBudget);
|
|
Assert.Equal("203.0.113.2", rows[1].SourceKey);
|
|
Assert.False(rows[1].OverBudget);
|
|
Assert.Equal(2, tracker.LiveWindowCount(1, T0.AddSeconds(3)));
|
|
Assert.Equal(1, tracker.LiveWindowCount(7, T0.AddSeconds(3)));
|
|
}
|
|
|
|
[Fact]
|
|
public void The_source_list_pages_and_keeps_one_order_across_the_pages()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var rule = Rule(1, "/login*", 10);
|
|
for (var i = 0; i < 5; i++)
|
|
for (var n = 0; n <= i; n++)
|
|
tracker.Record(rule, $"203.0.113.{i}", T0.AddSeconds(n));
|
|
|
|
// Same count and same key order on every call, so a page boundary cannot
|
|
// show one source twice and hide another.
|
|
var first = tracker.SourcesFor(1, 10, T0.AddSeconds(10), 2, 0).Select(r => r.SourceKey).ToList();
|
|
var second = tracker.SourcesFor(1, 10, T0.AddSeconds(10), 2, 2).Select(r => r.SourceKey).ToList();
|
|
Assert.Equal(["203.0.113.4", "203.0.113.3"], first);
|
|
Assert.Equal(["203.0.113.2", "203.0.113.1"], second);
|
|
Assert.Equal(5, tracker.LiveWindowCount(1, T0.AddSeconds(10)));
|
|
Assert.Empty(tracker.SourcesFor(1, 10, T0.AddSeconds(10), 2, 5));
|
|
}
|
|
|
|
[Fact]
|
|
public void Dropping_one_source_lets_it_out_without_touching_the_others()
|
|
{
|
|
var tracker = new RateRuleTracker();
|
|
var login = Rule(1, "/login*", 1);
|
|
var api = Rule(7, "/api/*", 1);
|
|
|
|
tracker.Record(login, "office", T0);
|
|
tracker.Record(login, "office", T0.AddSeconds(1)); // over
|
|
tracker.Record(login, "attacker", T0);
|
|
tracker.Record(login, "attacker", T0.AddSeconds(1)); // over
|
|
tracker.Record(api, "office", T0);
|
|
|
|
Assert.True(tracker.ForgetRuleSource(1, "office"));
|
|
// Only this budget, only this source: the other source is still over it,
|
|
// and the same source is still counted on the other budget.
|
|
Assert.Equal(1, tracker.LiveWindowCount(1, T0.AddSeconds(2)));
|
|
Assert.Equal(["attacker"], tracker.SourcesFor(1, 1, T0.AddSeconds(2), 10, 0)
|
|
.Select(r => r.SourceKey));
|
|
Assert.Equal(1, tracker.LiveWindowCount(7, T0.AddSeconds(2)));
|
|
// A second drop has nothing to drop, and says so rather than answering
|
|
// the same way as the first.
|
|
Assert.False(tracker.ForgetRuleSource(1, "office"));
|
|
Assert.False(tracker.ForgetRuleSource(1, ""));
|
|
// And the source is not exempt: its next request starts a window at one.
|
|
var again = tracker.Record(login, "office", T0.AddSeconds(3));
|
|
Assert.Equal(1, again.Count);
|
|
Assert.False(again.OverBudget);
|
|
}
|
|
|
|
[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));
|
|
}
|
|
}
|