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

49 lines
2.2 KiB
C#

namespace OpenWaf.Domain.Security;
/// <summary>
/// One blocked source — either an operator's manual entry or an engine ban.
/// Keyed by the normalised source key (see <see cref="Engine.SourceKey"/>),
/// not by the raw peer address, so a ban written by the engine and a block
/// typed by the operator land on the same row shape and are compared on the
/// same key at the decision point.
/// </summary>
public sealed class BlockEntry
{
public long Id { get; init; }
/// <summary>Normalised source key: IPv4 dotted quad, or IPv6 /64 prefix
/// written as "a:b:c:d::/64". Unique together with <see cref="Source"/>.</summary>
public string SourceKey { get; set; } = string.Empty;
public BlockSource Source { get; set; }
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
/// <summary>Engine bans only. Null means "does not expire" — which only an
/// operator entry may have.</summary>
public DateTimeOffset? ExpiresAt { get; set; }
/// <summary>Operator free text, or the engine's machine-readable
/// explanation ("12 consecutive refusals 400-499 in 5 min, last 404 /wp-login.php").</summary>
public string? Reason { get; set; }
/// <summary>Per-entry override for the block response. When set, the edge
/// answers 301 to this URL instead of the kind's default. Operators use it
/// to send scanners to someone else's bandwidth; the engine never sets it.</summary>
public string? RedirectTo { get; set; }
// ---- evidence, so an automatic ban is explainable from stored rows ----
public int? LastStatusCode { get; set; }
public string? LastPath { get; set; }
public string? LastUserAgent { get; set; }
public string? LastCountryCode { get; set; }
public string? LastCity { get; set; }
public string? LastRegion { get; set; }
public int? ConsecutiveRefusals { get; set; }
/// <summary>Whether the ban is live right now. An operator entry (no
/// expiry) is live until deleted; an engine ban stops being live when it
/// ages out, without anyone having to delete it.</summary>
public bool IsLive(DateTimeOffset now) => ExpiresAt is null || ExpiresAt > now;
}