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.
51 lines
2.6 KiB
C#
51 lines
2.6 KiB
C#
using OpenWaf.Domain.Policy;
|
|
using OpenWaf.Domain.Security;
|
|
|
|
namespace OpenWaf.Domain.Engine;
|
|
|
|
/// <summary>
|
|
/// The immutable state the decision point reads. Built off the request path
|
|
/// on a TTL and swapped atomically, so a request never touches the database.
|
|
///
|
|
/// Blocks are split by provenance rather than by table: the two kinds are
|
|
/// looked up in a fixed order and a clear operation must be able to name one
|
|
/// kind without touching the other.
|
|
/// </summary>
|
|
public sealed record EdgeSnapshot
|
|
{
|
|
public required DateTimeOffset GeneratedAt { get; init; }
|
|
public required EdgePolicy Policy { get; init; }
|
|
|
|
public IReadOnlyDictionary<string, BlockEntry> OperatorBlocks { get; init; }
|
|
= new Dictionary<string, BlockEntry>(StringComparer.Ordinal);
|
|
public IReadOnlyDictionary<string, BlockEntry> EngineBans { get; init; }
|
|
= new Dictionary<string, BlockEntry>(StringComparer.Ordinal);
|
|
/// <summary>The operator's range bans (OPEN-66), in the prefix-bucketed
|
|
/// shape the request path can afford to ask. Kept out of
|
|
/// <see cref="OperatorBlocks"/> because a range is not a key: it is a
|
|
/// containment claim, and matching it by key would answer "is this address
|
|
/// exactly this network address" rather than "is it inside it".</summary>
|
|
public NetworkBanIndex OperatorRanges { get; init; } = NetworkBanIndex.Empty;
|
|
|
|
public IReadOnlyList<AllowEntry> AllowEntries { get; init; } = [];
|
|
public IReadOnlyDictionary<string, CountryRule> Countries { get; init; }
|
|
= new Dictionary<string, CountryRule>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
/// <summary>Operator-written request rules, in the order the operator wrote
|
|
/// them. The first live match wins, so the listing an operator reads is the
|
|
/// evaluation order — a list whose answer depended on insertion order
|
|
/// hidden inside a dictionary would be unreadable.</summary>
|
|
public IReadOnlyList<RequestRule> RequestRules { get; init; } = [];
|
|
|
|
/// <summary>The operator's per-path budgets (OPEN-68), in the order they were
|
|
/// written. They are not part of <see cref="DecisionEngine"/>: a budget is a
|
|
/// count, and a count has to be kept while the requests arrive rather than
|
|
/// read from a snapshot built between them. The snapshot carries the rules so
|
|
/// the guard sees the same list the operator sees, and sees it without a
|
|
/// database read.</summary>
|
|
public IReadOnlyList<RateRule> RateRules { get; init; } = [];
|
|
|
|
public static EdgeSnapshot Empty(EdgePolicy policy)
|
|
=> new() { GeneratedAt = DateTimeOffset.MinValue, Policy = policy };
|
|
}
|