A scanner rotating through a rented /29 used to cost one row and one write per address: POST /admin-api/blocks answered "not an IP address" for a CIDR while the allowlist control beside it accepted one, and the config export could not carry the operator's answer either. A CIDR now has its own table and is enforced by containment. The rulings: - One list, two shapes. GET /admin-api/blocks merges address rows and range rows and says which is which with `kind`; DELETE ?key= takes the CIDR as it was typed; ?source=engine still cannot reach an operator's range, while ?source=operator clears both operator shapes. - Containment is checked on the raw peer address, the same field the allowlist is matched on, after the exact operator block and before the engine's own ban. The verdict stays block_operator, so nothing downstream had to learn a new reason; the decision trace names the range that answered, and an allow entry still outranks a range. - What is written is normalised before it is stored: 203.0.113.37/28 is stored as 203.0.113.32/28, a /32 or /128 is stored as the bare address it already was, and the full-width prefix is refused on meaning rather than on form — "0.0.0.0/0" covers every address there is, including the one this was written from; that is a shutdown, not a ban on a source. - A request pays for the number of distinct prefix lengths, not for the number of ranges: the index buckets by prefix and asks a set for the request address masked to it, most specific first, and an expired row does not stop the search for a broader live one. - Export carries ranges in the blocks section and import lands them idempotently, so an edge's decisions survive the move. /state counts them apart from address blocks, and the console prints the shape beside the row. What the run caught: - BlockRow read Range?.Field ?? Entry!.Field. The two rows are mutually exclusive, so a range with no expiry, reason or redirect — the common case — dereferenced the row that is not there and GET /admin-api/blocks answered 500. - NetworkBanIndex filed a row under "a.b.c.d/24" while the lookup compared a masked address with no prefix, so no range ever matched: ten unit tests red before the bucket key was fixed to the network address alone. - smoke and compose both wrote their JSON bodies with escaped quotes straight inside a command substitution, which truncates the body and answers 400 — the hazard this file's own header warns about. The payloads are variables now. - Scene ordering in smoke: the range scene clears the engine's list wholesale, which took the expired scanner row a later scene asserts on. It runs after those scenes now, and says why in the file. - Test-side, worth keeping: 203.0.113.37/28 is not the network 203.0.113.16/28 names, so an assertion that writes one and expects the other is testing the mask, not the match. build 0 warnings / 0 errors, unit 334/334, integration 312/312 in 31 s, console 105 assertions 0 failed, smoke passed 137 failed 0, compose 285 passed 0 failed against the built image at 262 MB. Refs OPEN-66.
91 lines
3.8 KiB
C#
91 lines
3.8 KiB
C#
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; }
|
|
|
|
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;
|
|
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 _);
|
|
}
|