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/.
44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace OpenWaf.Domain.Engine;
|
|
|
|
/// <summary>
|
|
/// Live in-memory state for one source. Mutable and lock-guarded on purpose:
|
|
/// this object is touched on every response, so it must not allocate a fresh
|
|
/// record per request.
|
|
/// </summary>
|
|
public sealed class StreakState
|
|
{
|
|
public required string SourceKey { get; init; }
|
|
|
|
public int ConsecutiveRefusals;
|
|
public int MaxConsecutiveRefusals;
|
|
public int TotalRefusals;
|
|
public int ConsecutiveServerErrors;
|
|
public int MaxConsecutiveServerErrors;
|
|
|
|
public DateTimeOffset? LastRefusalAt;
|
|
public DateTimeOffset LastSeenAt;
|
|
public int? LastStatusCode;
|
|
public string? LastPath;
|
|
public string? LastUserAgent;
|
|
public string? LastCountryCode;
|
|
public string? LastCity;
|
|
public string? LastRegion;
|
|
|
|
/// <summary>Position in the LRU list, so eviction is O(1) and a touch is
|
|
/// O(1). Null once the state has been evicted.</summary>
|
|
public LinkedListNode<string>? LruNode;
|
|
|
|
public bool HasEvidence => TotalRefusals > 0 || MaxConsecutiveServerErrors > 0;
|
|
|
|
public void Apply(RequestEvidence evidence)
|
|
{
|
|
if (evidence.Path is not null) LastPath = evidence.Path;
|
|
if (evidence.UserAgent is not null) LastUserAgent = evidence.UserAgent;
|
|
if (evidence.CountryCode is not null) LastCountryCode = evidence.CountryCode;
|
|
if (evidence.City is not null) LastCity = evidence.City;
|
|
if (evidence.Region is not null) LastRegion = evidence.Region;
|
|
}
|
|
}
|