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.
182 lines
7.9 KiB
C#
182 lines
7.9 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) || !IsFullAddressSpelling(text))
|
|
{
|
|
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)
|
|
|| !IsFullAddressSpelling(networkPart))
|
|
{
|
|
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>Is this the whole dotted-quad spelling of a v4 address, or an
|
|
/// abbreviation the parser happens to accept?
|
|
///
|
|
/// <see cref="System.Net.IPAddress.TryParse"/> takes "203.0.113" as
|
|
/// 203.0.0.113 — three parts, the last one read as a 16-bit host portion. On
|
|
/// a read surface that is a wrong answer about an address that does not
|
|
/// exist, and the preview (OPEN-59) refuses it for that reason. On this path
|
|
/// it is worse, because the row gets written: an operator missing one octet
|
|
/// bans 203.0.0.113, a live address that was never the target, and the
|
|
/// listing shows a spelling nobody typed. The write path has to refuse the
|
|
/// same string the read surfaces refuse, or the two disagree about whether an
|
|
/// address exists and a preview answers about a row the operator cannot find.
|
|
///
|
|
/// v6 and IPv4-mapped v6 are not this rule's business: the dotted-quad count
|
|
/// is what a v4 address looks like written out, and "::ffff:203.0.113.7" is a
|
|
/// whole address by its own spelling.
|
|
/// </summary>
|
|
private static bool IsFullAddressSpelling(string? text)
|
|
{
|
|
if (!System.Net.IPAddress.TryParse(text, out var parsed)) return false;
|
|
if (parsed.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) return true;
|
|
return text!.Split('.').Length == 4;
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|