Files
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

52 lines
2.0 KiB
C#

using System.Net;
using System.Net.Sockets;
namespace OpenWaf.Domain.Engine;
/// <summary>
/// CIDR / address matching for the allowlist. The allowlist is evaluated
/// against the raw peer address rather than the normalised source key, so an
/// operator can write "198.51.100.0/24" and mean exactly that, without having
/// to know the engine's key granularity.
/// </summary>
public static class NetworkMatch
{
public static bool Matches(string? address, string match)
{
if (string.IsNullOrWhiteSpace(address) || string.IsNullOrWhiteSpace(match)) return false;
var slash = match.IndexOf('/');
if (slash < 0)
{
return SourceKey.TryParse(address, out _, out var a)
&& SourceKey.TryParse(match, out _, out var b)
&& a.Equals(b);
}
var networkPart = match[..slash];
if (!SourceKey.TryParse(address, out _, out var addr)) return false;
if (!IPAddress.TryParse(networkPart.Trim(), out var network)) return false;
if (!int.TryParse(match[(slash + 1)..].Trim(), out var prefix)) return false;
if (addr.AddressFamily != network.AddressFamily)
{
// A v6 address written in mapped form against a v4 rule (and the
// reverse) is a common operator typo; compare the mapped forms.
if (addr.IsIPv4MappedToIPv6 && network.AddressFamily == AddressFamily.InterNetwork)
addr = addr.MapToIPv4();
else if (network.IsIPv4MappedToIPv6 && addr.AddressFamily == AddressFamily.InterNetwork)
network = network.MapToIPv4();
else
return false;
}
if (prefix < 0) return false;
var max = addr.AddressFamily == AddressFamily.InterNetwork ? 32 : 128;
if (prefix > max) return false;
var masked = SourceKey.Mask(network, prefix);
var addrMasked = SourceKey.Mask(addr, prefix);
return masked.Equals(addrMasked);
}
}