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/.
48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
using OpenWaf.Domain.Engine;
|
|
|
|
namespace OpenWaf.Edge.Pipeline;
|
|
|
|
/// <summary>
|
|
/// Which address the edge treats as the source.
|
|
///
|
|
/// Trusting <c>X-Forwarded-For</c> is what makes the WAF work behind a CDN,
|
|
/// and it is also the single easiest way to make a ban list useless: if the
|
|
/// header is attacker-controlled, every request gets a fresh source key and no
|
|
/// streak ever reaches the threshold. So it is opt-in, and the operator is
|
|
/// expected to pair it with the CDN's own IP allowlist.
|
|
/// </summary>
|
|
public static class ClientIp
|
|
{
|
|
public static string? Resolve(HttpContext ctx, bool trustProxyHeaders)
|
|
{
|
|
if (trustProxyHeaders)
|
|
{
|
|
var cf = ctx.Request.Headers["CF-Connecting-IP"].ToString();
|
|
if (SourceKey.TryParse(cf, out _, out _)) return cf.Trim();
|
|
|
|
var real = ctx.Request.Headers["X-Real-IP"].ToString();
|
|
if (SourceKey.TryParse(real, out _, out _)) return real.Trim();
|
|
|
|
var xff = ctx.Request.Headers["X-Forwarded-For"].ToString();
|
|
if (!string.IsNullOrWhiteSpace(xff))
|
|
{
|
|
foreach (var hop in xff.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Reverse())
|
|
{
|
|
if (SourceKey.TryParse(hop, out _, out _)) return hop;
|
|
}
|
|
}
|
|
}
|
|
|
|
return ctx.Connection.RemoteIpAddress?.ToString();
|
|
}
|
|
|
|
/// <summary>Country as reported by an edge in front of us, when there is
|
|
/// one. Preferred over our own MMDB because it was resolved by whoever is
|
|
/// closest to the client.</summary>
|
|
public static string? HeaderCountry(HttpContext ctx)
|
|
{
|
|
var cf = ctx.Request.Headers["CF-IPCountry"].ToString();
|
|
return string.IsNullOrWhiteSpace(cf) || cf.Length != 2 ? null : cf.Trim().ToUpperInvariant();
|
|
}
|
|
}
|