using OpenWaf.Domain.Policy; using OpenWaf.Domain.Security; namespace OpenWaf.Domain.Engine; /// /// What the volume pillar would answer for one named request, read off the /// counters without moving them (OPEN-69). /// /// The preview endpoint used to answer every pillar and stay silent about this /// one, because the count lives in the guard rather than in the snapshot. The /// silence was not neutral: the volume pillar is the pillar operators most often /// read as a ban, and the number they are choosing is exactly the number the /// preview could not comment on. /// /// The one invariant the whole type exists to keep: asking does not spend. /// A preview that answered by counting its own question would refuse the next real /// request because somebody pressed a button, and the tool that was supposed to /// prevent an over-tight budget would itself be the cause. /// public static class VolumePreview { /// The volume pillar is off: nothing is counted anywhere on this /// edge, so there is no budget to be over. public const string VerdictNotCounted = "not_counted"; /// No address was given, and a budget is a claim about a source. public const string VerdictNoSource = "no_source"; /// A counter for this source is already past a budget the request /// falls inside, so in Enforce mode this request is the one that gets 429. public const string VerdictRefusedNow = "refused_now"; /// A counter is past its budget and the mode is Observe: the crossing /// is real and alerting, and this request would still be served. Reading /// "over budget" as "would be refused" is how an observe budget turns into a /// surprise outage the moment the mode is switched. public const string VerdictOverBudgetNotEnforced = "over_budget_not_enforced"; /// The request would be counted and allowed: the budget is standing /// between this source and a refusal, and the counters say how far away. public const string VerdictWouldBeCounted = "would_be_counted"; public static VolumeAnswer For( EdgePolicy policy, EdgeSnapshot snapshot, string path, string? sourceKey, RateTracker rates, RateRuleTracker rateRules, DateTimeOffset now, bool reached) { var counting = policy.Rate != RateMode.Off && !string.IsNullOrEmpty(sourceKey); EdgeBudgetPreview? edge = null; var overEdge = false; if (counting) { // The same read the operator's rate listing makes, aimed at one // source: it reports the running window and nothing else, because a // window that has already run out is about to restart at zero and // "0 of 20" from a dead window is a different answer from "0 of 20". var live = rates.LiveRates(now, policy, 1, 0, sourceKey, out _); if (live.Count > 0) { var w = live[0]; overEdge = w.OverBudget; edge = new EdgeBudgetPreview( w.Count, policy.RateLimitPerWindow, Math.Round(policy.RateWindow.TotalSeconds, 1), w.WindowStart, w.WindowEnd, SecondsLeft(w.WindowEnd, now), w.OverBudget); } } var budgets = new List(); var overPath = false; if (counting) { // The same matcher the guard uses, in the same order: a preview that // matched paths differently from the enforcement would be a preview of // a different edge. foreach (var rule in snapshot.RateRules) { if (!rule.IsLive(now) || !PathPattern.Matches(rule.Pattern, path)) continue; var seen = rateRules.Peek(rule, sourceKey!, now); if (seen is null) { budgets.Add(new PathBudgetPreview(rule.Id, rule.Pattern, rule.LimitPerWindow, rule.WindowSeconds, false, null, false, null, null)); continue; } if (seen.OverBudget) overPath = true; budgets.Add(new PathBudgetPreview(rule.Id, rule.Pattern, rule.LimitPerWindow, rule.WindowSeconds, true, seen.Count, seen.OverBudget, seen.WindowEnd, SecondsLeft(seen.WindowEnd, now))); } } var crossed = overEdge || overPath; var verdict = policy.Rate == RateMode.Off ? VerdictNotCounted : string.IsNullOrEmpty(sourceKey) ? VerdictNoSource : !crossed ? VerdictWouldBeCounted : policy.Rate == RateMode.Enforce ? VerdictRefusedNow : VerdictOverBudgetNotEnforced; return new VolumeAnswer( policy.Rate.ToString(), "this-process", verdict, reached, edge, budgets, NoteFor(verdict, reached, overEdge, budgets)); } /// The sentence that keeps the three silences apart: the pillar is /// off, the request never reaches the pillar, and the pillar is reached with /// nothing counted yet. private static string NoteFor(string verdict, bool reached, bool overEdge, IReadOnlyList budgets) { if (verdict == VerdictNotCounted) return "the volume pillar is off: nothing is counted on this edge, so no budget can be crossed"; if (verdict == VerdictNoSource) return "no address was given, and a budget is a claim about a source"; if (!reached) return "an earlier pillar answers this request, so the volume pillar would not be reached and nothing would be counted for it"; var which = overEdge ? "the edge-wide budget" : budgets.FirstOrDefault(b => b.OverBudget) is { } over ? $"the path budget for {over.Pattern}" : null; if (which is null) return "the counters live in this process, so N edges allow N budgets; a path budget pauses only the path it names and writes no ban"; return verdict == VerdictOverBudgetNotEnforced ? $"{which} is already crossed for this source, and Observe mode enforces nothing: the request is served and one alert per source per window is raised" : $"{which} is already crossed for this source: this request is answered 429 for the rest of that window, and a path budget pauses only the path it names"; } private static int SecondsLeft(DateTimeOffset windowEnd, DateTimeOffset now) => Math.Max(0, (int)Math.Ceiling((windowEnd - now).TotalSeconds)); } /// The edge-wide window this source is inside, or null when it has none /// running — which is not the same as a window at zero. public sealed record EdgeBudgetPreview( int Count, int LimitPerWindow, double WindowSeconds, DateTimeOffset WindowStartsAt, DateTimeOffset WindowEndsAt, int SecondsLeft, bool OverBudget); /// One live path budget whose pattern matches the asked path. /// counted=false means the rule is in force for this path but this source /// has no window on it yet — the difference between "0 of 2" and "nothing yet". public sealed record PathBudgetPreview( long Id, string Pattern, int LimitPerWindow, int WindowSeconds, bool Counted, int? Count, bool OverBudget, DateTimeOffset? WindowEndsAt, int? SecondsLeft); /// The volume answer of a dry run. public sealed record VolumeAnswer( string Mode, string Scope, string Verdict, bool Reached, EdgeBudgetPreview? EdgeBudget, IReadOnlyList PathBudgets, string Note);