Files
openwaf/dotnet/OpenWaf.Edge/Configuration/EdgePolicyOptions.cs
T
Pavlo Ratushnyi a3d0777492 openwaf: edge WAF with one decision point and a streak ban
Initial implementation of the OPEN-2 epic: a .NET 10 YARP edge that
decides from what the upstream answered.

Design (docs/ARCHITECTURE.md, tracker authority 4c9de3f6):
- one short-circuit point, ordered: always-pass prefixes -> allowlist ->
  operator block (301) -> engine ban (403 + Retry-After) -> geo (451)
- a post-response observer feeds a consecutive-refusal streak; N refusals
  in M minutes ban for BanDuration; 5xx alerts and never bans
- provenance on the block row, so clearing engine bans cannot touch an
  operator's entries; the toggle is Off/Observe/Enforce and changes no rows
- inherited protections from the reference deployment: one durable write
  per source per 30s, LRU-bounded streak map, no DB on the request path

Verification: 36 unit + 14 integration tests, plus scripts/smoke.sh (19
assertions against a running edge and a real upstream, including the
distroless --healthcheck mode).

Findings worth keeping (documented in docs/ARCHITECTURE.md):
- ctx.RequestAborted silently cancelled the ban write, because the
  observer runs after the response is written
- SQLite cannot translate DateTimeOffset comparisons; timestamps are
  stored as integers there and stay timestamptz on PostgreSQL
- CIDR masking order silently widened prefixes (198.51.100.24/19 -> /16)
- an empty string in appsettings.json defeats the env fallback (?? on "")
- concurrent ban writes raced into the unique index and lost evidence

Also moves the unrelated 2026-09-30 inference benchmarks out of the
project root into research/.
2026-10-01 02:13:01 +02:00

61 lines
3.0 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;
// 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; } = [];
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,
DefaultRedirectTargets = DefaultRedirectTargets.Length == 0
? new[] { "https://www.iana.org/help/example-domains" }
: DefaultRedirectTargets
};
/// <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;
}
}