Files
Pavlo Ratushnyi 7beaa7e463 a source's request budget can be written for one path
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.
2026-10-05 08:40:58 +02:00

64 lines
3.2 KiB
C#

namespace OpenWaf.Domain.Security;
/// <summary>
/// A request budget written for one path rather than for the whole edge (OPEN-68).
///
/// The edge-wide budget (OPEN-56) is one number for the whole site, and for a
/// login path that number is always wrong: 100 requests a minute does not stop a
/// credential-stuffing run that makes twenty attempts a minute — twenty looks
/// like a person — while 5 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, which is what this row does: a glob over the request
/// path, its own limit, its own window.
///
/// What it is not:
/// * It is not a path rule (<see cref="RequestRule"/>). A path rule refuses a
/// request because of what it asked for; a rate rule 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.
/// * It is not a ban. Crossing a path budget pauses that source on that path
/// for the rest of that window and nothing more. The claim is about one path,
/// and writing an engine ban for it would refuse the visitor the rest of the
/// site as punishment for a login-page rate.
/// </summary>
public sealed class RateRule
{
public long Id { get; init; }
/// <summary>The budget a rule gets when the request names none. The same
/// numbers the edge-wide pillar ships with, so a rule written without
/// numbers behaves like the pillar the operator already has.</summary>
public const int DefaultLimitPerWindow = 20;
/// <summary>And its window, for the same reason.</summary>
public const int DefaultWindowSeconds = 60;
/// <summary>Glob over the request path — the same
/// <see cref="PathPattern"/> matcher as a path rule: anchored,
/// case-insensitive, <c>*</c> and <c>?</c> only, never a regular expression.</summary>
public string Pattern { get; set; } = string.Empty;
/// <summary>How many requests one source may make to the matched paths inside
/// <see cref="WindowSeconds"/>. The request that crosses it is the breach.</summary>
public int LimitPerWindow { get; set; } = DefaultLimitPerWindow;
/// <summary>How long that budget lasts, per source: the window starts when
/// that source first asks, exactly as the edge-wide window does.</summary>
public int WindowSeconds { get; set; } = DefaultWindowSeconds;
public string? Note { get; set; }
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
/// <summary>A budget written for an incident stops being true when the
/// incident ends. Same rule as a path rule's expiry.</summary>
public DateTimeOffset? ExpiresAt { get; set; }
public bool IsLive(DateTimeOffset now) => ExpiresAt is null || ExpiresAt > now;
/// <summary>The window as a span, with the same floor the edge-wide budget
/// uses: a zero or negative window is not "no wait", it is a counter that
/// resets between the check and the answer.</summary>
public TimeSpan Window => WindowSeconds > 0 ? TimeSpan.FromSeconds(WindowSeconds) : TimeSpan.FromSeconds(1);
}