using System.Linq; using System.Net.Sockets; using OpenWaf.Domain.Security; namespace OpenWaf.Domain.Engine; /// /// The operator's range bans, arranged so that a request pays for the number of /// distinct prefix lengths 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 . /// public sealed class NetworkBanIndex { private readonly Dictionary<(AddressFamily Family, int Prefix), Dictionary> _buckets; /// Bucket keys, longest prefix first, so the most specific claim /// about this address is the one that answers. private readonly (AddressFamily Family, int Prefix)[] _order; public static NetworkBanIndex Empty { get; } = new([]); /// How many ranges are held, live or not — the number an operator /// counts when they ask what this edge has written. public int Count { get; } /// 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. public IReadOnlyList Bans { get; } public NetworkBanIndex(IEnumerable bans) { var buckets = new Dictionary<(AddressFamily, int), Dictionary>(); 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(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(); } /// 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. 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; } /// Whether any live range covers this address. The preview surface /// asks this without wanting the row. public bool Covers(string? address, DateTimeOffset now) => TryMatch(address, now, out _); }