The multi-instance research measured the gap rather than guessing it: four refusals from one address, two per edge, at a threshold of 3, produced no ban anywhere, because the block list was shared through the database and the streak map was not (research/2026-10-01-multi-instance/, D1). OpenWaf:Policy: SharedStreaks=true moves the counters onto a row in source_streaks; the default stays the in-process map, because that is the deployment that loses nothing. The rules were extracted into StreakRules.Apply and both stores call it — the in-process map against a StreakState, the shared one against a row. A second copy of the streak semantics would put the drift back where this removes it. Version is a concurrency token, so two edges writing one source produce a retry against the winner's row rather than a silently dropped refusal; a clean answer on any edge ends the run on every edge, in one statement. The rows age out on the same ObservationRetentionDays window as observations. Both surfaces say which store answered: /admin-api/streaks scope is this-process or shared-database, /admin-api/state answers streakScope, and the console's streak note stops claiming "this edge process only" when it is not true. Found by running it for real rather than in-process: two edge processes on one SQLite file both create the schema on startup, and the loser of that race died in StartAsync with 'table "allow_entries" already exists' — hosting failed to start and the process never listened. The schema step now re-checks existence and continues, because what has to be true is that the schema exists, not that this process made it. Measured on this tree: build 0 warnings / 0 errors, unit 132/132, integration 191/191 in 32 s (6 new, two real edge instances on one SQLite file each), smoke 28/0, console 78 assertions 0 failed. Out of process, two edge binaries on one file at a 5 s TTL: A answered 404, 404; B answered 404 on the third refusal and 403 after it; both edges then reported consecutiveRefusals: 3 with scope: shared-database, and A started enforcing 4 s later. Before the schema fix the second process aborted on startup, which is how that fix was found.
99 lines
5.2 KiB
C#
99 lines
5.2 KiB
C#
using OpenWaf.Domain.Policy;
|
|
|
|
namespace OpenWaf.Edge.Configuration;
|
|
|
|
/// <summary>
|
|
/// The configuration-shaped mirror of <see cref="EdgePolicy"/>. It exists
|
|
/// because <c>appsettings.json</c> is strings: <see cref="StatusRange"/> is a
|
|
/// record struct the configuration binder cannot build from "400-499", and a
|
|
/// TimeSpan written as "00:05:00" in a deployment file is friendlier than a
|
|
/// number of minutes. One explicit mapping, rather than a binder that quietly
|
|
/// leaves a default in place when the shape is wrong.
|
|
/// </summary>
|
|
internal sealed class EdgePolicyOptions
|
|
{
|
|
public string Mode { get; set; } = "Observe";
|
|
public int StreakThreshold { get; set; } = 10;
|
|
public string StreakWindow { get; set; } = "00:05:00";
|
|
public string BanDuration { get; set; } = "01:00:00";
|
|
public string RefusalRange { get; set; } = "400-499";
|
|
public string ServerErrorRange { get; set; } = "500-599";
|
|
public string Geo { get; set; } = "Off";
|
|
public bool TrustProxyHeaders { get; set; }
|
|
public int MaxTrackedSources { get; set; } = 10_000;
|
|
public int Ipv4KeyPrefixLength { get; set; } = 32;
|
|
public int Ipv6KeyPrefixLength { get; set; } = 64;
|
|
public int EngineBlockStatus { get; set; } = 403;
|
|
public int OperatorBlockStatus { get; set; } = 301;
|
|
|
|
// Where the streak counters live. Not a runtime knob: it selects which
|
|
// store the process registers at startup, so changing it is a redeploy.
|
|
// Off by default — the in-process map costs nothing on the request path,
|
|
// and a single-edge deployment loses nothing by it.
|
|
public bool SharedStreaks { get; set; }
|
|
// Empty by default: the configuration binder appends to an existing
|
|
// collection rather than replacing it, so a non-empty default here would
|
|
// leave a duplicate target in every deployment.
|
|
public string[] DefaultRedirectTargets { get; set; } = [];
|
|
|
|
// Retention is the privacy promise this project makes: aggregates about a
|
|
// source outlive the traffic that produced them by exactly this long
|
|
// (OPEN-19). It used to be a constant on the domain record, so the operator
|
|
// who wanted either end of that promise — a shorter window, or an incident
|
|
// from nine days ago that is still explainable — had no lever at all.
|
|
// Two keys, not one: an observation ages out a fact about a source, while a
|
|
// ban's evidence row stays readable after the ban itself stopped being
|
|
// live. They shared 7 days by coincidence, not by design.
|
|
// Out-of-range refuses startup rather than falling back to 7. A silently
|
|
// widened window keeps rows a month longer than the deployment asked for,
|
|
// and nothing on any surface would say so — the same class of lie this
|
|
// project keeps chasing elsewhere.
|
|
public int ObservationRetentionDays { get; set; } = 7;
|
|
public int BanRetentionDays { get; set; } = 7;
|
|
|
|
public const int RetentionDaysFloor = 1;
|
|
public const int RetentionDaysCeiling = 365;
|
|
|
|
public EdgePolicy ToPolicy() => new()
|
|
{
|
|
Mode = Enum.TryParse<EnforcementMode>(Mode, true, out var m) ? m : EnforcementMode.Observe,
|
|
StreakThreshold = StreakThreshold > 0 ? StreakThreshold : 10,
|
|
StreakWindow = ParseWindow(StreakWindow, TimeSpan.FromMinutes(5)),
|
|
BanDuration = ParseWindow(BanDuration, TimeSpan.FromHours(1)),
|
|
RefusalRange = StatusRange.Parse(RefusalRange, new StatusRange(400, 499)),
|
|
ServerErrorRange = StatusRange.Parse(ServerErrorRange, new StatusRange(500, 599)),
|
|
Geo = Enum.TryParse<GeoMode>(Geo, true, out var g) ? g : GeoMode.Off,
|
|
TrustProxyHeaders = TrustProxyHeaders,
|
|
MaxTrackedSources = MaxTrackedSources > 0 ? MaxTrackedSources : 10_000,
|
|
Ipv4KeyPrefixLength = Ipv4KeyPrefixLength,
|
|
Ipv6KeyPrefixLength = Ipv6KeyPrefixLength,
|
|
EngineBlockStatus = EngineBlockStatus,
|
|
OperatorBlockStatus = OperatorBlockStatus,
|
|
SharedStreaks = SharedStreaks,
|
|
DefaultRedirectTargets = DefaultRedirectTargets.Length == 0
|
|
? new[] { "https://www.iana.org/help/example-domains" }
|
|
: DefaultRedirectTargets,
|
|
ObservationRetention = RetentionDays(ObservationRetentionDays, "OpenWaf:Policy:ObservationRetentionDays"),
|
|
BanRetention = RetentionDays(BanRetentionDays, "OpenWaf:Policy:BanRetentionDays")
|
|
};
|
|
|
|
/// <summary>Whole days, 1..365. Anything else is a deployment mistake and
|
|
/// is named as one at startup.</summary>
|
|
private static TimeSpan RetentionDays(int days, string configKey)
|
|
{
|
|
if (days < RetentionDaysFloor || days > RetentionDaysCeiling)
|
|
throw new InvalidOperationException(
|
|
$"{configKey} must be a whole number of days between {RetentionDaysFloor} and "
|
|
+ $"{RetentionDaysCeiling}; got {days}");
|
|
return TimeSpan.FromDays(days);
|
|
}
|
|
|
|
/// <summary>Accepts both <c>00:05:00</c> and a bare number of minutes.</summary>
|
|
private static TimeSpan ParseWindow(string value, TimeSpan fallback)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return fallback;
|
|
if (TimeSpan.TryParse(value, out var ts)) return ts;
|
|
return double.TryParse(value, out var minutes) && minutes > 0 ? TimeSpan.FromMinutes(minutes) : fallback;
|
|
}
|
|
}
|