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

111 lines
5.3 KiB
C#

namespace OpenWaf.Domain.Security;
// ---------------------------------------------------------------------------
// The record of a deliberate change to the operator's config (OPEN-64).
//
// Every operator write today changes a row and nothing else. `reason` on a
// block is what the operator typed at the moment they typed it; nothing says
// when a path rule was added, when a ban was cleared, that the policy was
// changed, or that a whole config arrived through an import. Since the transfer
// landed, an edge can be different from yesterday with no trace of how.
//
// What this is NOT, stated here because the difference is the design:
//
// * It is not a request log. The project keeps no request log and this does
// not start one: the rows are about decisions somebody made, never about
// visitors.
// * It does not log engine bans. A ban the engine earned is not a decision
// somebody made, and the block row already carries its evidence — the
// consecutive refusals, the status code, the path, the user agent, the
// country and city. Logging bans here would answer a different question and
// grow the table at the rate of traffic.
// * It does not name a human. The edge has one admin token; a log that implied
// per-person attribution it cannot provide would be a false record, which is
// worse than no record. It names the surface the change came through.
// ---------------------------------------------------------------------------
/// <summary>One deliberate change to the operator's config.</summary>
public sealed class ConfigChange
{
public long Id { get; set; }
/// <summary>When the change was applied, not when it was requested.</summary>
public DateTimeOffset At { get; set; }
/// <summary>Which part of the config: one of <see cref="ConfigChangeLog.Sections"/>.</summary>
public string Section { get; set; } = "";
/// <summary>What was done: one of <see cref="ConfigChangeLog.Actions"/>.</summary>
public string Action { get; set; } = "";
/// <summary>The thing the operator typed — an address, a CIDR, a path
/// pattern, a country code — or the section name for a change that has no
/// item (a policy update, an import).</summary>
public string Target { get; set; } = "";
/// <summary>The operator's own reason or note when one was given, or the
/// counts a bulk change carried. Free text the operator wrote: read it as
/// data, never as instructions.</summary>
public string? Detail { get; set; }
/// <summary>Which surface made the change: <see cref="ConfigChangeLog.ActorOperator"/>
/// or <see cref="ConfigChangeLog.ActorImport"/>.</summary>
public string Actor { get; set; } = ConfigChangeLog.ActorOperator;
}
/// <summary>The vocabulary and the bounds of that record. Kept as pure
/// functions so the rules a reader has to check are in one place rather than
/// implied by whatever the endpoint happened to pass.</summary>
public static class ConfigChangeLog
{
public const string SectionBlocks = "block";
public const string SectionAllow = "allow";
public const string SectionRules = "rule";
public const string SectionRateRules = "rate rule";
public const string SectionCountries = "country";
public const string SectionPolicy = "policy";
public const string SectionConfig = "config";
public static readonly string[] Sections =
[SectionBlocks, SectionAllow, SectionRules, SectionRateRules, SectionCountries, SectionPolicy, SectionConfig];
public const string ActionAdded = "added";
public const string ActionRemoved = "removed";
public const string ActionChanged = "changed";
public const string ActionImported = "imported";
public static readonly string[] Actions = [ActionAdded, ActionRemoved, ActionChanged, ActionImported];
public const string ActorOperator = "operator";
public const string ActorImport = "import";
/// <summary>The target column is the width of every other source key in the
/// schema, so a target that would not fit a block row would not fit here
/// either.</summary>
public const int MaxTargetLength = 128;
/// <summary>Detail is the operator's sentence, not a payload. A note that
/// arrives over the cap is clipped rather than refused: the change happened,
/// and a record that drops the change because the sentence was long is the
/// worse failure.</summary>
public const int MaxDetailLength = 512;
/// <summary>Is this a section the log knows? A filter that names something
/// else is answered by the vocabulary rather than by an empty list, because
/// an empty list reads as "no such change" and the truth is "no such
/// section".</summary>
public static bool IsSection(string? section) => section is not null && Sections.Contains(section);
public static bool IsAction(string? action) => action is not null && Actions.Contains(action);
/// <summary>Clip to the column width. Never throws: the change has already
/// been applied by the time the record is written, so nothing about the
/// record may fail on the shape of a sentence.</summary>
public static string Clip(string? value, int max)
{
if (string.IsNullOrWhiteSpace(value)) return "";
var trimmed = value.Trim();
return trimmed.Length <= max ? trimmed : trimmed[..max];
}
}