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/.
185 lines
7.2 KiB
C#
185 lines
7.2 KiB
C#
using OpenWaf.Domain.Engine;
|
|
using OpenWaf.Domain.Policy;
|
|
using OpenWaf.Domain.Security;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
public sealed class DecisionEngineTests
|
|
{
|
|
private static readonly DateTimeOffset Now = new(2026, 10, 1, 12, 0, 0, TimeSpan.Zero);
|
|
|
|
private static BlockEntry Manual(string key, string? redirect = null, DateTimeOffset? expires = null) => new()
|
|
{
|
|
SourceKey = key,
|
|
Source = BlockSource.Operator,
|
|
CreatedAt = Now.AddHours(-3),
|
|
ExpiresAt = expires,
|
|
RedirectTo = redirect,
|
|
Reason = "operator: repeated scraping"
|
|
};
|
|
|
|
private static BlockEntry Ban(string key, DateTimeOffset expires) => new()
|
|
{
|
|
SourceKey = key,
|
|
Source = BlockSource.Engine,
|
|
CreatedAt = Now.AddMinutes(-1),
|
|
ExpiresAt = expires,
|
|
Reason = "10 consecutive refusals (400-499) within 5 min; last 404 /wp-login.php"
|
|
};
|
|
|
|
private static EdgeSnapshot Snapshot(
|
|
EdgePolicy policy,
|
|
IReadOnlyDictionary<string, BlockEntry>? bans = null,
|
|
IReadOnlyList<AllowEntry>? allow = null,
|
|
IReadOnlyDictionary<string, CountryRule>? countries = null) => new()
|
|
{
|
|
GeneratedAt = Now,
|
|
Policy = policy,
|
|
EngineBans = bans ?? new Dictionary<string, BlockEntry>(),
|
|
AllowEntries = allow ?? [],
|
|
Countries = countries ?? new Dictionary<string, CountryRule>()
|
|
};
|
|
|
|
private static Dictionary<string, BlockEntry> Map(params BlockEntry[] entries)
|
|
=> entries.ToDictionary(e => e.SourceKey, StringComparer.Ordinal);
|
|
|
|
[Fact]
|
|
public void Nothing_matched_passes()
|
|
{
|
|
var d = DecisionEngine.Decide(Snapshot(EdgePolicy.Default), "203.0.113.9", "/index.html", Now);
|
|
Assert.Equal(EdgeVerdict.Pass, d.Verdict);
|
|
}
|
|
|
|
[Fact]
|
|
public void An_engine_ban_is_enforced_only_in_Enforce_mode()
|
|
{
|
|
var ban = Ban("203.0.113.9", Now.AddMinutes(30));
|
|
|
|
foreach (var mode in new[] { EnforcementMode.Off, EnforcementMode.Observe })
|
|
{
|
|
var snapshot = Snapshot(new EdgePolicy { Mode = mode }, Map(ban));
|
|
Assert.Equal(EdgeVerdict.Pass, DecisionEngine.Decide(snapshot, "203.0.113.9", "/x", Now).Verdict);
|
|
}
|
|
|
|
var enforcing = Snapshot(new EdgePolicy { Mode = EnforcementMode.Enforce }, Map(ban));
|
|
var decision = DecisionEngine.Decide(enforcing, "203.0.113.9", "/x", Now);
|
|
|
|
Assert.Equal(EdgeVerdict.BlockEngine, decision.Verdict);
|
|
Assert.Equal(403, decision.StatusCode);
|
|
Assert.Contains("consecutive refusals", decision.Reason);
|
|
Assert.Equal(ban.ExpiresAt, decision.ExpiresAt);
|
|
}
|
|
|
|
[Fact]
|
|
public void An_expired_ban_stops_blocking_without_anyone_deleting_it()
|
|
{
|
|
var snapshot = Snapshot(new EdgePolicy { Mode = EnforcementMode.Enforce }, Map(Ban("203.0.113.9", Now.AddMinutes(-1))));
|
|
Assert.Equal(EdgeVerdict.Pass, DecisionEngine.Decide(snapshot, "203.0.113.9", "/x", Now).Verdict);
|
|
}
|
|
|
|
[Fact]
|
|
public void An_operator_block_never_expires_and_answers_301_by_default()
|
|
{
|
|
var snapshot = Snapshot(EdgePolicy.Default, allow: []) with
|
|
{
|
|
OperatorBlocks = Map(Manual("203.0.113.9"))
|
|
};
|
|
|
|
var decision = DecisionEngine.Decide(snapshot, "203.0.113.9", "/x", Now);
|
|
|
|
Assert.Equal(EdgeVerdict.BlockOperator, decision.Verdict);
|
|
Assert.Equal(301, decision.StatusCode);
|
|
Assert.False(string.IsNullOrWhiteSpace(decision.Location));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_per_entry_redirect_overrides_the_kind_default()
|
|
{
|
|
var snapshot = Snapshot(EdgePolicy.Default) with
|
|
{
|
|
OperatorBlocks = Map(Manual("203.0.113.9", "https://example.net/honeypot"))
|
|
};
|
|
|
|
var decision = DecisionEngine.Decide(snapshot, "203.0.113.9", "/x", Now);
|
|
Assert.Equal("https://example.net/honeypot", decision.Location);
|
|
}
|
|
|
|
[Fact]
|
|
public void The_allowlist_beats_an_operator_block_a_ban_and_a_geo_deny_together()
|
|
{
|
|
var policy = new EdgePolicy { Mode = EnforcementMode.Enforce, Geo = GeoMode.DenyList };
|
|
var snapshot = new EdgeSnapshot
|
|
{
|
|
GeneratedAt = Now,
|
|
Policy = policy,
|
|
OperatorBlocks = Map(Manual("203.0.113.9")),
|
|
EngineBans = Map(Ban("203.0.113.9", Now.AddMinutes(30))),
|
|
AllowEntries = [new AllowEntry { Match = "203.0.113.0/24", Note = "office" }],
|
|
Countries = new Dictionary<string, CountryRule> { ["UA"] = new() { Code = "UA", IsAllowed = false } }
|
|
};
|
|
|
|
var decision = DecisionEngine.Decide(snapshot, "203.0.113.9", "/x", Now, "UA");
|
|
|
|
Assert.Equal(EdgeVerdict.PassAllowlisted, decision.Verdict);
|
|
Assert.Equal("office", decision.Reason);
|
|
}
|
|
|
|
[Fact]
|
|
public void An_expired_allow_entry_stops_exempting()
|
|
{
|
|
var snapshot = Snapshot(EdgePolicy.Default, allow:
|
|
[new AllowEntry { Match = "203.0.113.9", ExpiresAt = Now.AddMinutes(-1) }]) with
|
|
{
|
|
OperatorBlocks = Map(Manual("203.0.113.9"))
|
|
};
|
|
|
|
Assert.Equal(EdgeVerdict.BlockOperator, DecisionEngine.Decide(snapshot, "203.0.113.9", "/x", Now).Verdict);
|
|
}
|
|
|
|
[Fact]
|
|
public void Deny_list_rejects_with_451_and_allow_list_rejects_everything_else()
|
|
{
|
|
var countries = new Dictionary<string, CountryRule> { ["DE"] = new() { Code = "DE", IsAllowed = false } };
|
|
|
|
var deny = DecisionEngine.Decide(
|
|
Snapshot(new EdgePolicy { Geo = GeoMode.DenyList }, countries: countries), "203.0.113.9", "/x", Now, "DE");
|
|
Assert.Equal(EdgeVerdict.GeoReject, deny.Verdict);
|
|
Assert.Equal(451, deny.StatusCode);
|
|
|
|
var allowList = DecisionEngine.Decide(
|
|
Snapshot(new EdgePolicy { Geo = GeoMode.AllowList }, countries: countries), "203.0.113.9", "/x", Now, "FR");
|
|
Assert.Equal(EdgeVerdict.GeoReject, allowList.Verdict);
|
|
}
|
|
|
|
[Fact]
|
|
public void Geo_off_never_rejects_even_when_a_country_is_denied()
|
|
{
|
|
var countries = new Dictionary<string, CountryRule> { ["DE"] = new() { Code = "DE", IsAllowed = false } };
|
|
var decision = DecisionEngine.Decide(
|
|
Snapshot(new EdgePolicy { Geo = GeoMode.Off }, countries: countries), "203.0.113.9", "/x", Now, "DE");
|
|
|
|
Assert.Equal(EdgeVerdict.Pass, decision.Verdict);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("/admin-api/blocks")]
|
|
[InlineData("/healthz")]
|
|
[InlineData("/readyz")]
|
|
[InlineData("/openapi/v1.json")]
|
|
public void The_recovery_hatch_stays_open_for_a_self_blocked_operator(string path)
|
|
{
|
|
var snapshot = Snapshot(EdgePolicy.Default) with { OperatorBlocks = Map(Manual("203.0.113.9")) };
|
|
Assert.Equal(EdgeVerdict.PassBypassed, DecisionEngine.Decide(snapshot, "203.0.113.9", path, Now).Verdict);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_blocked_ipv6_source_is_matched_on_its_sixty_four_not_its_address()
|
|
{
|
|
var ban = Ban("2001:db8:1234:5678::/64", Now.AddMinutes(30));
|
|
var snapshot = Snapshot(new EdgePolicy { Mode = EnforcementMode.Enforce }, Map(ban));
|
|
|
|
Assert.Equal(EdgeVerdict.BlockEngine, DecisionEngine.Decide(snapshot, "2001:db8:1234:5678::cafe", "/x", Now).Verdict);
|
|
Assert.Equal(EdgeVerdict.Pass, DecisionEngine.Decide(snapshot, "2001:db8:4321:8765::cafe", "/x", Now).Verdict);
|
|
}
|
|
}
|