Files
Pavlo Ratushnyi ddddfc864b recovered-partial: the blast-radius preview exists on disk with nobody's record on it
Recovered-partial, UNVERIFIED. These changes were in the working tree at session
start with no checkpoint record on any item (OPEN-75 carries no execution plan,
work result or handoff note), so they are preserved rather than built on.

Attribution: they are the OPEN-75 surface — GET /admin-api/impact, the console
panel that calls it, the write-path refusal of abbreviated v4 spellings, and the
smoke / compose / console scenes for it. The README section and endpoint-list
entry the item's definition of done asks for are not here.

Gate evidence taken against this state, not claimed for it:
  build (warnings-as-errors)  clean
  unit                        378/378
  integration                 403/405 — AlertPushQueueTests.Attempts_run_out_and_
                              the_surface_says_who_was_never_told and
                              PillarFailureVisibilityTests.The_snapshot_age_is_
                              measured_against_the_ttl_it_is_supposed_to_respect
                              fail here AND fail identically at 6f92d37 with
                              these changes absent, in a separate worktree, so
                              they are not this work's failures; their failure
                              site moves between runs (assertion vs fixture
                              StartServer), which is what an environment-sensitive
                              test looks like.
2026-10-05 18:20:42 +02:00

101 lines
4.4 KiB
C#

using System.Linq;
using System.Net.Sockets;
using OpenWaf.Domain.Security;
namespace OpenWaf.Domain.Engine;
/// <summary>
/// The operator's range bans, arranged so that a request pays for the number of
/// <em>distinct prefix lengths</em> and not for the number of ranges (OPEN-66).
///
/// The naive match is a scan: for every range, ask whether the address is inside
/// it. That is fine for three ranges and is a per-request cost that grows with
/// the operator's list, which is the shape this project refuses everywhere else.
/// Bucketing by prefix length turns each bucket into a set lookup: mask the
/// request address to that many bits and ask the set. A v4 address has 31 legal
/// prefix lengths and a v6 address 127, so the worst case is a bounded number of
/// hash lookups no matter how many rows the table holds.
///
/// Built with the snapshot, off the request path, and never mutated in place —
/// the same rule as every other part of <see cref="EdgeSnapshot"/>.
/// </summary>
public sealed class NetworkBanIndex
{
private readonly Dictionary<(AddressFamily Family, int Prefix), Dictionary<string, NetworkBan>> _buckets;
/// <summary>Bucket keys, longest prefix first, so the most specific claim
/// about this address is the one that answers.</summary>
private readonly (AddressFamily Family, int Prefix)[] _order;
public static NetworkBanIndex Empty { get; } = new([]);
/// <summary>How many ranges are held, live or not — the number an operator
/// counts when they ask what this edge has written.</summary>
public int Count { get; }
/// <summary>The ranges the index actually answers with, in the deduplicated
/// shape the request path sees. A count is not enough for the impact preview
/// (OPEN-75), which has to rebuild the index with one more range in it and
/// answer what that changes — and rebuilding from the rows the index was
/// handed rather than from the rows it kept would answer about a state the
/// wire is not running.</summary>
public IReadOnlyList<NetworkBan> Bans { get; }
public NetworkBanIndex(IEnumerable<NetworkBan> bans)
{
var buckets = new Dictionary<(AddressFamily, int), Dictionary<string, NetworkBan>>();
var total = 0;
foreach (var ban in bans)
{
var prefix = NetworkBans.PrefixOf(ban.Match);
if (prefix < 0) continue; // not a CIDR this code wrote: skip, do not guess
var key = (NetworkBans.FamilyOf(ban.Match), prefix);
if (!buckets.TryGetValue(key, out var set))
{
set = new Dictionary<string, NetworkBan>(StringComparer.Ordinal);
buckets[key] = set;
}
// The bucket already carries the prefix, so the row is filed under
// its network address alone — that is the string a masked request
// address is compared to.
var slash = ban.Match.IndexOf('/');
set[slash < 0 ? ban.Match : ban.Match[..slash]] = ban;
total++;
}
_buckets = buckets;
Count = total;
Bans = buckets.Values.SelectMany(v => v.Values).ToList();
var ordered = new List<(AddressFamily Family, int Prefix)>(buckets.Keys);
ordered.Sort((a, b) => b.Prefix.CompareTo(a.Prefix));
_order = ordered.ToArray();
}
/// <summary>The most specific live range that covers this address, if any.
/// An expired range answers nothing, and the search continues to a broader
/// live one rather than stopping at a dead row.</summary>
public bool TryMatch(string? address, DateTimeOffset now, out NetworkBan ban)
{
ban = null!;
if (_order.Length == 0) return false;
if (!SourceKey.TryParse(address, out _, out var normalized)) return false;
foreach (var key in _order)
{
if (key.Family != normalized.AddressFamily) continue;
var candidate = SourceKey.Mask(normalized, key.Prefix).ToString();
if (_buckets[key].TryGetValue(candidate, out var found) && found.IsLive(now))
{
ban = found;
return true;
}
}
return false;
}
/// <summary>Whether any live range covers this address. The preview surface
/// asks this without wanting the row.</summary>
public bool Covers(string? address, DateTimeOffset now) => TryMatch(address, now, out _);
}