A scanner farm inside one /24 is twenty engine bans and twenty operator decisions: the streak rule works per source, and nothing in the edge connected the rows. OPEN-66 made a range enforceable; this is the half that notices the range is there. The rulings: - The evidence is the edge's own live engine bans. An operator's row is a decision already taken, and an expired ban is a row the edge stopped honouring; neither is evidence about a netblock. Nothing new is observed and nothing new is stored. - An offer is not a ban. A candidate changes nothing on the wire: the operator's POST /admin-api/blocks with the CIDR is what enforces it, and the console button is that same call. Twenty honest visitors of one provider and one farm inside one rented block produce the same number, and only a human tells them apart. - Grouping is one allocation up from the key: v4 to /24, v6 to /48, because the v6 streak key is already a /64 and grouping there would only repeat the key. The count and both prefixes are policy keys with bounds, because a host that hands out /22s should be shown the block it can act on. - A ban keyed on a block wider than the netblock prefix is skipped: that row already claims more than a /24, and counting it as one ban inside a /24 would invent a candidate out of it. - Reading the list is not news — the scan that answers the surface raises no alert, so refreshing a page cannot manufacture alert rows. The timer's scan raises netblock_candidate through the same durable alert table and push queue as every other kind, at most once per block per six hours, and never for a block the operator has already banned. - A failed scan retires the offers instead of repeating the last ones, and /admin-api/state says which of off / failed / found-nothing this is. What the run caught: - The console's "ban this range" button posted a body with no content type, and the edge refused it: the button did nothing and the page looked idle rather than wrong. It uses the page's own postJson now, and the console gate asserts the row it writes, the wire answer for a source inside it, and the offer turning into "already banned". - The grouping parsed ban keys with SourceKey.TryParse, which fails on the "2001:db8:1:2::/64" spelling the engine stores v6 bans under — so no v6 ban was ever grouped. Keys are parsed as keys, prefix included. - An assertion claimed 203.0.113.137 sits under 203.0.113.128/24. That is a /25: the arithmetic was wrong in the test, not in the mask. build 0 warnings / 0 errors, unit 353/353, integration 326/326 in 31 s, console 114 assertions 0 failed, smoke passed 149 failed 0, compose 293 passed 0 failed against the built image at 262 MB. Refs OPEN-67.
144 lines
7.7 KiB
C#
144 lines
7.7 KiB
C#
namespace OpenWaf.Domain.Policy;
|
|
|
|
/// <summary>
|
|
/// The whole edge policy as one immutable snapshot, read from configuration
|
|
/// and from the <c>policy_settings</c> row (the DB wins where a key is set,
|
|
/// so the operator can change the toggle without a redeploy).
|
|
///
|
|
/// Q1 is settled here: the streak window (M) and the ban duration are two
|
|
/// separate parameters. "N раз підряд на M хвилин" bounds the streak; how
|
|
/// long a ban then lasts is a different question and gets its own knob.
|
|
/// </summary>
|
|
public sealed record EdgePolicy
|
|
{
|
|
public EnforcementMode Mode { get; init; } = EnforcementMode.Observe;
|
|
|
|
/// <summary>N — consecutive refusals that trigger a ban.</summary>
|
|
public int StreakThreshold { get; init; } = 10;
|
|
|
|
/// <summary>M — the gap between two refusals that still counts as the same
|
|
/// streak. A gap longer than this restarts the streak at 1.</summary>
|
|
public TimeSpan StreakWindow { get; init; } = TimeSpan.FromMinutes(5);
|
|
|
|
/// <summary>How long an engine ban stays live.</summary>
|
|
public TimeSpan BanDuration { get; init; } = TimeSpan.FromMinutes(60);
|
|
|
|
/// <summary>Q2: the refusal class is inclusive of 400 — a malformed-body
|
|
/// probe answers 400 and must count.</summary>
|
|
public StatusRange RefusalRange { get; init; } = new(400, 499);
|
|
|
|
/// <summary>Q3: 5xx is an alert, never a ban.</summary>
|
|
public StatusRange ServerErrorRange { get; init; } = new(500, 599);
|
|
|
|
/// <summary>Q4: source key granularity. IPv6 is grouped to /64 so a bot
|
|
/// rotating addresses inside one provider allocation is one source, and a
|
|
/// whole office behind one v4 address stays one source (accepted cost,
|
|
/// documented, mitigated by the allowlist).</summary>
|
|
public int Ipv4KeyPrefixLength { get; init; } = 32;
|
|
public int Ipv6KeyPrefixLength { get; init; } = 64;
|
|
|
|
/// <summary>Hard bound on the in-memory streak map. Beyond it the oldest
|
|
/// sources are evicted, so a scanner farm cannot grow the process.
|
|
/// (LEAS-785 is the cautionary tale: an unbounded ConcurrentDictionary.)</summary>
|
|
public int MaxTrackedSources { get; init; } = 10_000;
|
|
|
|
/// <summary>Write-amplification cap: at most one durable observation per
|
|
/// source per this interval.</summary>
|
|
public TimeSpan ObservationWriteInterval { get; init; } = TimeSpan.FromSeconds(30);
|
|
|
|
/// <summary>A source state nobody has touched for this long is evicted.</summary>
|
|
public TimeSpan StateIdleTtl { get; init; } = TimeSpan.FromHours(2);
|
|
|
|
/// <summary>Where the streak counters live (OPEN-52). False — the default —
|
|
/// is the in-process map: no I/O on the request path, and a threshold that
|
|
/// is per instance (measured: 2+2 over two edges at a threshold of 3 banned
|
|
/// nobody). True moves the same rules to a database row, so N edges mature
|
|
/// one streak, at the cost of a round trip per observed response.
|
|
/// Startup-time only: it decides which store the process registers, and a
|
|
/// policy that changed underneath a running process would leave the streaks
|
|
/// in the store the old setting chose.</summary>
|
|
public bool SharedStreaks { get; init; }
|
|
|
|
/// <summary>What the edge does with a source that asked more often than the
|
|
/// budget allows (OPEN-56). Off by default: a limit nobody wrote is a
|
|
/// defence nobody asked for, and a false positive here locks out a whole
|
|
/// office behind one address.</summary>
|
|
public RateMode Rate { get; init; } = RateMode.Off;
|
|
|
|
/// <summary>The budget. Requests per source per window; the request that
|
|
/// crosses it is the breach. Meaningful only when
|
|
/// <see cref="Rate"/> is not Off, but kept valid regardless so a policy
|
|
/// that is switched on later cannot come up with a limit of zero, which
|
|
/// would refuse every request.</summary>
|
|
public int RateLimitPerWindow { get; init; } = 100;
|
|
|
|
/// <summary>How long a source's budget lasts. The window starts at that
|
|
/// source's first request, not on a global clock.</summary>
|
|
public TimeSpan RateWindow { get; init; } = TimeSpan.FromMinutes(1);
|
|
|
|
/// <summary>How long a ban's evidence row survives after it stops being live.</summary>
|
|
public TimeSpan BanRetention { get; init; } = TimeSpan.FromDays(7);
|
|
|
|
/// <summary>How long an observation row survives without traffic.</summary>
|
|
public TimeSpan ObservationRetention { get; init; } = TimeSpan.FromDays(7);
|
|
|
|
/// <summary>How long a change record survives (OPEN-64). Longer than the
|
|
/// observation window on purpose: the question the record answers — "when
|
|
/// did this rule get here" — stays worth asking about an incident that is
|
|
/// weeks old, and the row is about a decision, not about a source.</summary>
|
|
public TimeSpan ChangeRetention { get; init; } = TimeSpan.FromDays(30);
|
|
|
|
/// <summary>The other bound on the change log: how many rows it holds at
|
|
/// most, however young they are. An operator who scripts a thousand rule
|
|
/// edits should not be able to grow a table whose whole use is being
|
|
/// readable.</summary>
|
|
public int ChangeLogMaxRows { get; init; } = 5000;
|
|
|
|
/// <summary>How many live engine bans inside one block make that block worth
|
|
/// showing the operator as a range they could ban instead (OPEN-67). 0 turns
|
|
/// the watch off, and 0 is the honest way to say off: a threshold no edge
|
|
/// will ever reach is the same switch thrown with arithmetic while the
|
|
/// surface still answers. Nothing is banned by this number — it decides only
|
|
/// what is offered, and the operator decides the rest.</summary>
|
|
public int NetblockCandidateBans { get; init; } = 5;
|
|
|
|
/// <summary>The block a v4 ban is grouped into while looking for a
|
|
/// candidate. /24 is the allocation unit most providers hand out, so it is
|
|
/// the block an operator can act on with one row; a deployment whose host
|
|
/// hands out a /22 writes a smaller number and sees the bigger block.</summary>
|
|
public int NetblockPrefixV4 { get; init; } = 24;
|
|
|
|
/// <summary>The same for v6. The streak key is already a /64, so a candidate
|
|
/// at /64 would only ever repeat the key the ban is stored under; /48 is the
|
|
/// allocation one size up, which is where a rotating v6 farm first looks
|
|
/// like one thing rather than sixty-four rows.</summary>
|
|
public int NetblockPrefixV6 { get; init; } = 48;
|
|
|
|
public GeoMode Geo { get; init; } = GeoMode.Off;
|
|
|
|
/// <summary>Whether X-Forwarded-For / CF-Connecting-IP may define the
|
|
/// source address. Off by default: if the header is attacker-controlled
|
|
/// every request gets a fresh source key and no streak ever matures.</summary>
|
|
public bool TrustProxyHeaders { get; init; }
|
|
|
|
/// <summary>Response the edge gives an engine ban. 403, not 301: a 301 is
|
|
/// a good answer to a scanner and a baffling one to a customer whose
|
|
/// office IP was caught (Q6).</summary>
|
|
public int EngineBlockStatus { get; init; } = 403;
|
|
|
|
/// <summary>Response the edge gives an operator block when the entry has no
|
|
/// per-entry redirect. Defaults to the reference behaviour: 301 to a
|
|
/// honey-pot list, because scanners follow redirects and honest users
|
|
/// typed nothing.</summary>
|
|
public int OperatorBlockStatus { get; init; } = 301;
|
|
public IReadOnlyList<string> DefaultRedirectTargets { get; init; } =
|
|
new[] { "https://www.iana.org/help/example-domains" };
|
|
|
|
/// <summary>Paths that never consult the guard, so an operator who blocks
|
|
/// their own address can still reach the admin API and undo it.</summary>
|
|
public IReadOnlyList<string> AlwaysPassPrefixes { get; init; } =
|
|
new[] { "/admin-api", "/admin", "/healthz", "/readyz", "/metrics", "/openapi", "/swagger" };
|
|
|
|
public static EdgePolicy Default { get; } = new();
|
|
}
|