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/.
70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using OpenWaf.Domain.Engine;
|
|
using OpenWaf.Domain.Policy;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
public sealed class SourceKeyTests
|
|
{
|
|
private static readonly EdgePolicy Policy = new();
|
|
|
|
[Fact]
|
|
public void Ipv4_is_its_own_source_by_default()
|
|
{
|
|
Assert.Equal("203.0.113.7", SourceKey.For("203.0.113.7", Policy));
|
|
}
|
|
|
|
[Fact]
|
|
public void Ipv6_addresses_inside_one_sixty_four_collapse_to_one_source()
|
|
{
|
|
var a = SourceKey.For("2001:db8:1234:5678::1", Policy);
|
|
var b = SourceKey.For("2001:db8:1234:5678:dead:beef:cafe:1", Policy);
|
|
|
|
Assert.Equal(a, b);
|
|
Assert.Equal("2001:db8:1234:5678::/64", a);
|
|
}
|
|
|
|
[Fact]
|
|
public void Different_ipv6_sixty_fours_stay_different_sources()
|
|
{
|
|
Assert.NotEqual(SourceKey.For("2001:db8:1234:5678::1", Policy),
|
|
SourceKey.For("2001:db8:1234:5679::1", Policy));
|
|
}
|
|
|
|
[Fact]
|
|
public void Ipv4_mapped_ipv6_is_the_same_source_as_the_ipv4()
|
|
{
|
|
Assert.Equal(SourceKey.For("203.0.113.7", Policy), SourceKey.For("::ffff:203.0.113.7", Policy));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_wider_ipv6_prefix_groups_more_of_the_allocation()
|
|
{
|
|
var policy = Policy with { Ipv6KeyPrefixLength = 48 };
|
|
Assert.Equal(SourceKey.For("2001:db8:1234:5678::1", policy),
|
|
SourceKey.For("2001:db8:1234:7777::1", policy));
|
|
}
|
|
|
|
[Fact]
|
|
public void Garbage_does_not_become_a_source_key_that_can_be_matched()
|
|
{
|
|
Assert.Equal("invalid:not-an-ip", SourceKey.For("not-an-ip", Policy));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("198.51.100.24", "198.51.100.0/24", true)]
|
|
[InlineData("198.51.100.24", "198.51.100.24", true)]
|
|
[InlineData("198.51.101.24", "198.51.100.0/24", false)]
|
|
[InlineData("2001:db8:ff00::1", "2001:db8:ff00::/48", true)]
|
|
[InlineData("2001:db8:ff01::1", "2001:db8:ff00::/48", false)]
|
|
[InlineData("::ffff:198.51.100.24", "198.51.100.0/24", true)]
|
|
public void Allowlist_matching(string address, string match, bool expected)
|
|
=> Assert.Equal(expected, NetworkMatch.Matches(address, match));
|
|
|
|
[Fact]
|
|
public void Masking_zeroes_only_the_host_part()
|
|
{
|
|
Assert.Equal("198.51.100.0", SourceKey.Mask(System.Net.IPAddress.Parse("198.51.100.24"), 24).ToString());
|
|
Assert.Equal("198.51.96.0", SourceKey.Mask(System.Net.IPAddress.Parse("198.51.100.24"), 19).ToString());
|
|
}
|
|
}
|