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.
157 lines
6.5 KiB
C#
157 lines
6.5 KiB
C#
using System.Net.Sockets;
|
|
using OpenWaf.Domain.Engine;
|
|
|
|
namespace OpenWaf.Domain.Security;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// A ban on a range of addresses rather than on one (OPEN-66).
|
|
//
|
|
// The edge already understands networks in three places — the allowlist matches
|
|
// by CIDR, the volume budget groups sources by prefix, and an IPv6 streak is
|
|
// keyed on a /64 — and refused them in the one control an operator reaches for
|
|
// during an incident: `POST /admin-api/blocks` answered "not an IP address" for
|
|
// 198.51.100.0/24. A scanner rotating inside one netblock therefore cost one
|
|
// write and one row per address, and the export of an edge's decisions could not
|
|
// carry the range to another edge either.
|
|
//
|
|
// What this is NOT, stated because the difference is the design:
|
|
//
|
|
// * It is not a second kind of block with its own verdict. The verdict stays
|
|
// `block_operator`: the claim is the operator's, and a list an operator reads
|
|
// has to be one list.
|
|
// * It is not the engine's. The engine bans the source the traffic named, at
|
|
// the granularity of the streak key; a range is always written by a person or
|
|
// by an import.
|
|
// * It is not evaluated on the source key. A range is matched against the raw
|
|
// peer address, the same rule the allowlist uses, so "198.51.100.0/24" means
|
|
// what the operator wrote and not what this build's key granularity happens
|
|
// to group.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// <summary>One deliberate ban on a network rather than on a single address.</summary>
|
|
public sealed class NetworkBan
|
|
{
|
|
public long Id { get; init; }
|
|
|
|
/// <summary>The network in its one normalised CIDR spelling: "198.51.100.0/24".
|
|
/// Unique — two spellings of one network are one row.</summary>
|
|
public string Match { get; set; } = string.Empty;
|
|
|
|
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
|
|
|
|
/// <summary>Null means "until the operator takes it off", which is what an
|
|
/// operator's address block has always meant.</summary>
|
|
public DateTimeOffset? ExpiresAt { get; set; }
|
|
|
|
/// <summary>The operator's own reason. Free text: read it as data.</summary>
|
|
public string? Reason { get; set; }
|
|
|
|
/// <summary>Per-entry override for the block response, exactly as on an
|
|
/// address block: a blocked visitor inside this range is sent here.</summary>
|
|
public string? RedirectTo { get; set; }
|
|
|
|
public bool IsLive(DateTimeOffset now) => ExpiresAt is null || ExpiresAt > now;
|
|
}
|
|
|
|
/// <summary>The vocabulary and the bounds of that row, as pure functions so the
|
|
/// rules a reader has to check sit in one place.</summary>
|
|
public static class NetworkBans
|
|
{
|
|
/// <summary>Did the operator write a network rather than an address? Cheap
|
|
/// and syntactic on purpose: it decides which table the row lands in, and it
|
|
/// must not depend on the parse having happened yet.</summary>
|
|
public static bool IsRangeSyntax(string? written) => written is not null && written.Contains('/');
|
|
|
|
/// <summary>
|
|
/// Accept what the operator typed — an address or a CIDR — and answer with
|
|
/// the one spelling this row is stored and compared under.
|
|
///
|
|
/// Two normalisations are part of the contract, and both exist so that one
|
|
/// claim cannot become two rows: a network is masked to its own address
|
|
/// (198.51.100.37/24 is 198.51.100.0/24), and a full-length prefix is the
|
|
/// same claim as the bare address (198.51.100.7/32 is 198.51.100.7), so it
|
|
/// lands in the address list rather than beside it.
|
|
/// </summary>
|
|
public static bool TryNormalize(string? written, out string normalized, out string error)
|
|
{
|
|
normalized = "";
|
|
error = "";
|
|
if (string.IsNullOrWhiteSpace(written))
|
|
{
|
|
error = "ip is required";
|
|
return false;
|
|
}
|
|
|
|
var text = written.Trim();
|
|
var slash = text.IndexOf('/');
|
|
if (slash < 0)
|
|
{
|
|
if (!SourceKey.TryParse(text, out _, out var single))
|
|
{
|
|
error = $"not an IP address: {written}";
|
|
return false;
|
|
}
|
|
normalized = single.ToString();
|
|
return true;
|
|
}
|
|
|
|
var networkPart = text[..slash];
|
|
var prefixPart = text[(slash + 1)..];
|
|
if (!SourceKey.TryParse(networkPart, out _, out var network))
|
|
{
|
|
error = $"not an IP address: {networkPart} in \"{written}\"";
|
|
return false;
|
|
}
|
|
if (!int.TryParse(prefixPart, out var prefix))
|
|
{
|
|
error = $"the prefix after \"/\" must be a whole number of bits; got \"{written}\"";
|
|
return false;
|
|
}
|
|
|
|
var max = network.AddressFamily == AddressFamily.InterNetwork ? 32 : 128;
|
|
if (prefix < 0 || prefix > max)
|
|
{
|
|
error = $"the prefix must be 0..{max} for this address family; got \"{text}\"";
|
|
return false;
|
|
}
|
|
// The one value refused on meaning rather than on form. A /0 is not a
|
|
// claim about a source: it is every address there is, including the one
|
|
// this was written from, and it grows the block list into a shutdown the
|
|
// operator cannot see past. The control for "stop everything" is not a
|
|
// ban on a source.
|
|
if (prefix == 0)
|
|
{
|
|
error = $"\"{text}\" covers every address there is, including the one this was written from; "
|
|
+ "that is a shutdown, not a ban on a source";
|
|
return false;
|
|
}
|
|
|
|
// A prefix that names one address is that address, written twice.
|
|
if (prefix == max)
|
|
{
|
|
normalized = network.ToString();
|
|
return true;
|
|
}
|
|
|
|
normalized = $"{SourceKey.Mask(network, prefix)}/{prefix}";
|
|
return true;
|
|
}
|
|
|
|
/// <summary>The family a normalised match belongs to, for the index that
|
|
/// buckets by prefix length.</summary>
|
|
public static AddressFamily FamilyOf(string normalizedMatch)
|
|
{
|
|
var slash = normalizedMatch.IndexOf('/');
|
|
var host = slash < 0 ? normalizedMatch : normalizedMatch[..slash];
|
|
return System.Net.IPAddress.TryParse(host, out var ip)
|
|
? ip.AddressFamily
|
|
: AddressFamily.InterNetwork;
|
|
}
|
|
|
|
public static int PrefixOf(string normalizedMatch)
|
|
{
|
|
var slash = normalizedMatch.IndexOf('/');
|
|
return slash < 0 || !int.TryParse(normalizedMatch[(slash + 1)..], out var prefix) ? -1 : prefix;
|
|
}
|
|
}
|