Files
Pavlo Ratushnyi 6a65cbf344 the dry run says what the volume pillar would answer for that source
`GET /admin-api/decide` answers what would happen to a named request and which
pillar said so, and it stayed silent about the one pillar whose number the
operator is choosing. The count is kept in the guard rather than read off a
snapshot, so the preview printed `rate: not_evaluated` and left "would this source
be rate-limited" as the one question the surface exists to answer could not ask.

The rulings this had to make, and the answer each one got:

* Reading, never spending, is the invariant. The section is built from peek reads
  of both trackers, so N dry runs leave `count` exactly where it was and write no
  alert. A preview that answered by counting its own question would refuse the next
  real request because somebody pressed a button — the tool for finding an
  over-tight budget would itself be the cause of it.
* Four silences stay apart: `not_counted` (the pillar is off, so there is no budget
  to cross), `no_source` (no address was asked, and a budget is a claim about a
  source), `refused_now`, `would_be_counted`. A fifth, `over_budget_not_enforced`,
  exists because "over budget" and "would be refused" are two answers separated by
  the mode, and collapsing them is how an observe budget becomes a surprise the day
  someone switches it on.
* `reached: false` says an earlier pillar answers the request, so the guard would
  never consult the counter for it. The volume section still reports the counters —
  they are true — but the note stops talking about a budget this request would
  never touch.
* A budget in force for the path with nothing counted for this source yet prints
  `counted: false`, not `count: 0`. "0 of 2" and "nothing yet" are answers an
  operator acts on differently.
* The path budgets are matched by the same `PathPattern.Matches`, in the same
  order, as the guard: a preview that matched differently would be a preview of a
  different edge.
* The counters are this process's (`scope: "this-process"`), said in the answer
  rather than left for the operator to remember.
* The `rate` pillar row keeps `not_evaluated` — the engine really did not evaluate
  it — and its note now points at the section that did, instead of ending as a
  silence.

What the runs caught, in order:

* The first version answered `refused_now` for an Observe crossing. The counters
  were right and the sentence was wrong: observe enforces nothing. The mode is now
  part of the verdict.
* An integration expectation asserted the note of the `rate` pillar row after a
  crossing, and read the engine ban the crossing itself had written instead — the
  request was no longer reaching the volume pillar. The assertion moved to the
  preview taken before the crossing, and the ban case now asserts the honest note.
* Rewording that note broke an older preview test that keyed on the word "preview"
  in it; the wording keeps the word, and the older test is the one that caught the
  rewording.
* The smoke scene compared a listing's over-budget count against a request count,
  which would have passed for the wrong reason; it now asserts the pair
  (tracked, over) before and after six previews.

Gates at this commit, each stage on its own: build 0 warnings / 0 errors, unit
371/371, integration 345/345 in 43 s, console 122 assertions 0 failed, smoke
passed 182 failed 0, compose 304 passed 0 failed against the built image at
262 MB, 0 containers left behind.

Refs OPEN-69.
2026-10-05 09:14:09 +02:00

200 lines
8.1 KiB
C#

using OpenWaf.Domain.Engine;
using OpenWaf.Domain.Policy;
using OpenWaf.Domain.Security;
namespace OpenWaf.Tests.Unit;
/// <summary>
/// The volume half of the dry run (OPEN-69). The preview used to answer every
/// pillar and stay silent about the one whose number the operator is choosing, so
/// these tests hold the two things that make the section worth having: it answers
/// with the four different silences kept apart, and it never spends what it reads.
/// </summary>
public sealed class VolumePreviewTests
{
private static readonly DateTimeOffset T0 = new(2026, 10, 5, 12, 0, 0, TimeSpan.Zero);
private static EdgePolicy Policy(RateMode mode = RateMode.Enforce, int limit = 2, int windowSeconds = 60) =>
new() { Rate = mode, RateLimitPerWindow = limit, RateWindow = TimeSpan.FromSeconds(windowSeconds) };
private static EdgeSnapshot Snapshot(EdgePolicy policy, params RateRule[] rules) =>
new() { Policy = policy, RateRules = rules, GeneratedAt = T0 };
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 With_the_pillar_off_the_answer_is_not_counted_rather_than_zero_of_the_limit()
{
var rates = new RateTracker();
var policy = Policy(RateMode.Off);
var answer = VolumePreview.For(policy, Snapshot(policy, Rule(1, "/login*", 2)), "/login", "a",
rates, new RateRuleTracker(), T0, true);
Assert.Equal(VolumePreview.VerdictNotCounted, answer.Verdict);
Assert.Null(answer.EdgeBudget);
Assert.Empty(answer.PathBudgets);
Assert.Contains("nothing is counted", answer.Note);
}
[Fact]
public void A_question_about_the_path_alone_says_a_budget_is_a_claim_about_a_source()
{
var policy = Policy();
var answer = VolumePreview.For(policy, Snapshot(policy, Rule(1, "/login*", 2)), "/login", null,
new RateTracker(), new RateRuleTracker(), T0, true);
Assert.Equal(VolumePreview.VerdictNoSource, answer.Verdict);
Assert.Empty(answer.PathBudgets);
}
[Fact]
public void The_edge_wide_budget_that_is_already_crossed_answers_refused_now_with_the_time_left()
{
var rates = new RateTracker();
var policy = Policy(limit: 2);
rates.Record("a", policy, T0);
rates.Record("a", policy, T0.AddSeconds(1));
rates.Record("a", policy, T0.AddSeconds(2));
var answer = VolumePreview.For(policy, Snapshot(policy), "/index", "a",
rates, new RateRuleTracker(), T0.AddSeconds(3), true);
Assert.Equal(VolumePreview.VerdictRefusedNow, answer.Verdict);
Assert.NotNull(answer.EdgeBudget);
Assert.Equal(3, answer.EdgeBudget!.Count);
Assert.Equal(2, answer.EdgeBudget.LimitPerWindow);
Assert.True(answer.EdgeBudget.OverBudget);
Assert.Equal(57, answer.EdgeBudget.SecondsLeft);
Assert.Contains("edge-wide budget", answer.Note);
}
[Fact]
public void A_path_budget_names_the_path_it_was_written_for_in_the_answer()
{
var rules = new RateRuleTracker();
var policy = Policy(limit: 1000);
var rule = Rule(7, "/login*", 2);
rules.Record(rule, "a", T0);
rules.Record(rule, "a", T0.AddSeconds(1));
rules.Record(rule, "a", T0.AddSeconds(2));
var answer = VolumePreview.For(policy, Snapshot(policy, rule), "/login", "a",
new RateTracker(), rules, T0.AddSeconds(3), true);
Assert.Equal(VolumePreview.VerdictRefusedNow, answer.Verdict);
// The edge-wide budget is not the one crossed, and the note says which is.
Assert.Null(answer.EdgeBudget);
var seen = Assert.Single(answer.PathBudgets);
Assert.Equal(7, seen.Id);
Assert.Equal("/login*", seen.Pattern);
Assert.True(seen.Counted);
Assert.Equal(3, seen.Count);
Assert.True(seen.OverBudget);
Assert.Contains("/login*", answer.Note);
}
[Fact]
public void An_observe_budget_that_is_crossed_says_so_without_promising_a_refusal()
{
var rules = new RateRuleTracker();
var policy = Policy(RateMode.Observe, limit: 1000);
var rule = Rule(1, "/login*", 2);
for (var i = 0; i < 3; i++) rules.Record(rule, "a", T0.AddSeconds(i));
var answer = VolumePreview.For(policy, Snapshot(policy, rule), "/login", "a",
new RateTracker(), rules, T0.AddSeconds(3), true);
// "Over budget" and "would be refused" are two answers, and the mode is
// what separates them. Collapsing them is how an observe budget becomes a
// surprise the day someone switches it on.
Assert.Equal(VolumePreview.VerdictOverBudgetNotEnforced, answer.Verdict);
Assert.Contains("Observe mode enforces nothing", answer.Note);
}
[Fact]
public void Only_the_budgets_whose_pattern_matches_the_asked_path_are_answered()
{
var policy = Policy(limit: 1000);
var answer = VolumePreview.For(policy,
Snapshot(policy, Rule(1, "/login*", 2), Rule(2, "/api/*", 5)), "/login", "a",
new RateTracker(), new RateRuleTracker(), T0, true);
var seen = Assert.Single(answer.PathBudgets);
Assert.Equal("/login*", seen.Pattern);
Assert.Equal(VolumePreview.VerdictWouldBeCounted, answer.Verdict);
}
[Fact]
public void A_budget_in_force_for_the_path_with_nothing_counted_yet_is_not_a_zero()
{
var policy = Policy(limit: 1000);
var answer = VolumePreview.For(policy, Snapshot(policy, Rule(1, "/login*", 2)), "/login", "a",
new RateTracker(), new RateRuleTracker(), T0, true);
var seen = Assert.Single(answer.PathBudgets);
Assert.False(seen.Counted);
Assert.Null(seen.Count);
Assert.False(seen.OverBudget);
}
[Fact]
public void A_request_an_earlier_pillar_answers_never_reaches_the_counter()
{
var rates = new RateTracker();
var policy = Policy(limit: 2);
rates.Record("a", policy, T0);
rates.Record("a", policy, T0.AddSeconds(1));
rates.Record("a", policy, T0.AddSeconds(2));
var answer = VolumePreview.For(policy, Snapshot(policy), "/index", "a",
rates, new RateRuleTracker(), T0.AddSeconds(3), reached: false);
Assert.False(answer.Reached);
Assert.Contains("an earlier pillar answers this request", answer.Note);
}
[Fact]
public void Asking_the_preview_does_not_spend_the_budget_it_is_asking_about()
{
var rates = new RateTracker();
var rules = new RateRuleTracker();
var policy = Policy(limit: 2);
var rule = Rule(1, "/login*", 2);
rates.Record("a", policy, T0);
rules.Record(rule, "a", T0);
var snapshot = Snapshot(policy, rule);
for (var i = 0; i < 5; i++)
{
var answer = VolumePreview.For(policy, snapshot, "/login", "a",
rates, rules, T0.AddSeconds(1), true);
Assert.Equal(1, answer.EdgeBudget!.Count);
Assert.Equal(1, answer.PathBudgets[0].Count);
}
// The next real request is the second of the window, not the sixth: a
// preview that counted its own question would have refused it.
Assert.Equal(2, rates.Record("a", policy, T0.AddSeconds(2)).Count);
Assert.Equal(2, rules.Record(rule, "a", T0.AddSeconds(2)).Count);
}
[Fact]
public void A_window_that_has_already_run_out_is_not_reported_as_a_running_budget()
{
var rates = new RateTracker();
var rules = new RateRuleTracker();
var policy = Policy(limit: 2, windowSeconds: 10);
var rule = Rule(1, "/login*", 2, windowSeconds: 10);
rates.Record("a", policy, T0);
rules.Record(rule, "a", T0);
var answer = VolumePreview.For(policy, Snapshot(policy, rule), "/login", "a",
rates, rules, T0.AddSeconds(30), true);
Assert.Null(answer.EdgeBudget);
Assert.False(answer.PathBudgets[0].Counted);
Assert.Equal(VolumePreview.VerdictWouldBeCounted, answer.Verdict);
}
}