Files
Pavlo Ratushnyi dd10dd9839 the edge notices the netblock behind its bans and offers it as a range
A scanner farm inside one /24 is twenty engine bans and twenty operator
decisions: the streak rule works per source, and nothing in the edge connected
the rows. OPEN-66 made a range enforceable; this is the half that notices the
range is there.

The rulings:

- The evidence is the edge's own live engine bans. An operator's row is a
  decision already taken, and an expired ban is a row the edge stopped
  honouring; neither is evidence about a netblock. Nothing new is observed and
  nothing new is stored.
- An offer is not a ban. A candidate changes nothing on the wire: the operator's
  POST /admin-api/blocks with the CIDR is what enforces it, and the console
  button is that same call. Twenty honest visitors of one provider and one farm
  inside one rented block produce the same number, and only a human tells them
  apart.
- Grouping is one allocation up from the key: v4 to /24, v6 to /48, because the
  v6 streak key is already a /64 and grouping there would only repeat the key.
  The count and both prefixes are policy keys with bounds, because a host that
  hands out /22s should be shown the block it can act on.
- A ban keyed on a block wider than the netblock prefix is skipped: that row
  already claims more than a /24, and counting it as one ban inside a /24 would
  invent a candidate out of it.
- Reading the list is not news — the scan that answers the surface raises no
  alert, so refreshing a page cannot manufacture alert rows. The timer's scan
  raises netblock_candidate through the same durable alert table and push queue
  as every other kind, at most once per block per six hours, and never for a
  block the operator has already banned.
- A failed scan retires the offers instead of repeating the last ones, and
  /admin-api/state says which of off / failed / found-nothing this is.

What the run caught:

- The console's "ban this range" button posted a body with no content type, and
  the edge refused it: the button did nothing and the page looked idle rather
  than wrong. It uses the page's own postJson now, and the console gate asserts
  the row it writes, the wire answer for a source inside it, and the offer
  turning into "already banned".
- The grouping parsed ban keys with SourceKey.TryParse, which fails on the
  "2001:db8:1:2::/64" spelling the engine stores v6 bans under — so no v6 ban
  was ever grouped. Keys are parsed as keys, prefix included.
- An assertion claimed 203.0.113.137 sits under 203.0.113.128/24. That is a /25:
  the arithmetic was wrong in the test, not in the mask.

build 0 warnings / 0 errors, unit 353/353, integration 326/326 in 31 s, console
114 assertions 0 failed, smoke passed 149 failed 0, compose 293 passed 0 failed
against the built image at 262 MB.

Refs OPEN-67.
2026-10-05 07:55:06 +02:00

250 lines
10 KiB
C#

using OpenWaf.Domain.Engine;
using OpenWaf.Domain.Policy;
using OpenWaf.Domain.Security;
namespace OpenWaf.Tests.Unit;
/// <summary>
/// The netblock watch (OPEN-67): the edge's own live engine bans, grouped into
/// the block they all sit under, so the operator is offered one range instead of
/// twenty rows.
///
/// The arithmetic lives in <see cref="Netblocks"/> as pure functions, and these
/// tests are the whole reason it is pure: the claim "these bans are one network"
/// is exactly the kind of claim that is easy to write and easy to get wrong by
/// one bit. A candidate that is one address too wide bans the neighbours of the
/// attacker; a candidate that never appears leaves the operator counting rows by
/// hand.
/// </summary>
public sealed class NetblockWatchTests
{
private static EdgePolicy Policy(int threshold = 3, int v4 = 24, int v6 = 48) => new()
{
NetblockCandidateBans = threshold,
NetblockPrefixV4 = v4,
NetblockPrefixV6 = v6
};
private static IEnumerable<(string Key, DateTimeOffset CreatedAt)> Bans(params string[] keys)
{
var start = new DateTimeOffset(2026, 10, 5, 4, 0, 0, TimeSpan.Zero);
return keys.Select((k, i) => (k, start.AddMinutes(i)));
}
private static readonly DateTimeOffset Now = new(2026, 10, 5, 5, 0, 0, TimeSpan.Zero);
[Fact]
public void A_block_with_enough_bans_inside_it_is_a_candidate()
{
var found = Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), NetworkBanIndex.Empty, Now);
var candidate = Assert.Single(found);
Assert.Equal("203.0.113.0/24", candidate.Match);
Assert.Equal(24, candidate.Prefix);
Assert.Equal(3, candidate.Bans);
Assert.False(candidate.AlreadyBanned);
}
[Fact]
public void A_block_below_the_threshold_is_not_offered()
{
// Two bans in a /24 is two unrelated visitors of one hosting provider as
// often as it is a farm, and offering it would train the operator to
// ignore the table.
Assert.Empty(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11"), Policy(), NetworkBanIndex.Empty, Now));
}
[Fact]
public void Addresses_in_different_blocks_never_add_up_to_one_candidate()
{
Assert.Empty(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.114.12"), Policy(), NetworkBanIndex.Empty, Now));
}
[Fact]
public void A_threshold_of_zero_is_the_watch_off_rather_than_a_candidate_for_everything()
{
// 0 has to mean off. Read as "every block with zero or more bans", it
// would offer every /24 the edge has ever seen — the arithmetic version
// of a switch that is not there.
Assert.Empty(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(threshold: 0),
NetworkBanIndex.Empty, Now));
}
[Fact]
public void The_prefix_the_operator_set_is_the_block_the_candidate_is_written_as()
{
var found = Netblocks.Candidates(
Bans("198.51.100.17", "198.51.100.18", "198.51.100.19"), Policy(v4: 16),
NetworkBanIndex.Empty, Now);
Assert.Equal("198.51.0.0/16", Assert.Single(found).Match);
}
[Fact]
public void A_ban_whose_own_key_is_a_wider_block_is_not_evidence_about_one_netblock()
{
// The edge can be configured to key a streak on a block (ipv4KeyPrefixLength).
// Counting one such ban as "one ban inside 203.0.113.0/24" would invent a
// candidate out of a row that already claims more than a /24.
Assert.Empty(Netblocks.Candidates(
Bans("203.0.113.0/8", "203.0.113.0/8", "203.0.113.0/8"), Policy(), NetworkBanIndex.Empty, Now));
}
[Fact]
public void A_key_that_names_no_address_is_skipped_rather_than_grouped()
{
// The "*" a /0 source key becomes is not inside any block.
Assert.Equal(-1, Netblocks.KeyPrefix("*"));
Assert.Empty(Netblocks.Candidates(Bans("*", "*", "*"), Policy(), NetworkBanIndex.Empty, Now));
}
[Fact]
public void v6_bans_are_grouped_one_allocation_up_from_the_key_they_are_stored_under()
{
// The streak key is already a /64, so grouping at /64 would only repeat
// the key. /48 is where a rotating v6 farm first looks like one thing.
var found = Netblocks.Candidates(
Bans("2001:db8:1:1::/64", "2001:db8:1:2::/64", "2001:db8:1:3::/64"),
Policy(), NetworkBanIndex.Empty, Now);
Assert.Equal("2001:db8:1::/48", Assert.Single(found).Match);
}
[Fact]
public void A_range_the_operator_already_wrote_is_said_rather_than_offered_again()
{
var ranges = new NetworkBanIndex(new[]
{
new NetworkBan { Match = "203.0.113.0/24", CreatedAt = Now.AddDays(-1) }
});
var candidate = Assert.Single(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), ranges, Now));
Assert.True(candidate.AlreadyBanned);
}
[Fact]
public void A_narrower_range_does_not_cover_the_block_it_sits_inside()
{
var ranges = new NetworkBanIndex(new[]
{
new NetworkBan { Match = "203.0.113.16/28", CreatedAt = Now.AddDays(-1) }
});
var candidate = Assert.Single(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), ranges, Now));
Assert.False(candidate.AlreadyBanned);
}
[Fact]
public void A_broader_range_covers_the_block_and_closes_the_offer()
{
var ranges = new NetworkBanIndex(new[]
{
new NetworkBan { Match = "203.0.0.0/16", CreatedAt = Now.AddDays(-1) }
});
var candidate = Assert.Single(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), ranges, Now));
Assert.True(candidate.AlreadyBanned);
}
[Fact]
public void The_oldest_and_newest_ban_of_a_block_travel_with_the_count()
{
var candidate = Assert.Single(Netblocks.Candidates(
Bans("203.0.113.10", "203.0.113.11", "203.0.113.12"), Policy(), NetworkBanIndex.Empty, Now));
Assert.Equal(new DateTimeOffset(2026, 10, 5, 4, 0, 0, TimeSpan.Zero), candidate.FirstBanAt);
Assert.Equal(new DateTimeOffset(2026, 10, 5, 4, 2, 0, TimeSpan.Zero), candidate.LastBanAt);
}
[Fact]
public void The_sample_shows_a_few_of_the_bans_and_not_the_whole_block()
{
var keys = Enumerable.Range(10, 12).Select(i => $"203.0.113.{i}").ToArray();
var candidate = Assert.Single(Netblocks.Candidates(Bans(keys), Policy(), NetworkBanIndex.Empty, Now));
Assert.Equal(12, candidate.Bans);
Assert.Equal(Netblocks.SampleSize, candidate.Sample.Count);
Assert.Contains("203.0.113.10", candidate.Sample);
}
[Fact]
public void The_widest_farm_is_offered_first()
{
var keys = new List<string>();
keys.AddRange(Enumerable.Range(10, 5).Select(i => $"203.0.113.{i}"));
keys.AddRange(Enumerable.Range(20, 4).Select(i => $"198.51.100.{i}"));
var found = Netblocks.Candidates(Bans(keys.ToArray()), Policy(), NetworkBanIndex.Empty, Now);
Assert.Equal(2, found.Count);
Assert.Equal("203.0.113.0/24", found[0].Match);
Assert.Equal(5, found[0].Bans);
Assert.Equal("198.51.100.0/24", found[1].Match);
}
[Fact]
public void The_block_a_ban_sits_under_is_the_block_the_block_form_takes()
{
// The string on the surface has to be the string POST /admin-api/blocks
// accepts, or the offer and the control disagree.
var match = Netblocks.Covering("203.0.113.137", Policy());
Assert.Equal("203.0.113.128/25", Netblocks.Covering("203.0.113.137", Policy(v4: 25)));
Assert.Equal("203.0.113.0/24", match);
Assert.True(NetworkBans.TryNormalize(match, out var normalized, out _));
Assert.Equal(match, normalized);
}
[Fact]
public void The_prefix_a_ban_key_names_is_the_prefix_it_was_written_with()
{
Assert.Equal(32, Netblocks.KeyPrefix("203.0.113.10"));
Assert.Equal(64, Netblocks.KeyPrefix("2001:db8:1:2::/64"));
Assert.Equal(24, Netblocks.KeyPrefix("198.51.100.0/24"));
Assert.Equal(-1, Netblocks.KeyPrefix("not-an-address"));
Assert.Equal(-1, Netblocks.KeyPrefix(null));
}
[Fact]
public void The_defaults_are_the_allocation_sizes_this_project_assumes()
{
var policy = new EdgePolicy();
Assert.Equal(5, policy.NetblockCandidateBans);
Assert.Equal(24, policy.NetblockPrefixV4);
Assert.Equal(48, policy.NetblockPrefixV6);
}
[Fact]
public void The_bounds_refuse_a_block_that_is_most_of_the_address_space()
{
Assert.True(PolicyBounds.NetblockCandidateBansOk(0));
Assert.False(PolicyBounds.NetblockCandidateBansOk(-1));
Assert.False(PolicyBounds.NetblockCandidateBansOk(PolicyBounds.NetblockCandidateBansMax + 1));
// /8 is the floor and /0 is not: banning the floor is already a stretch,
// and banning below it is a shutdown dressed as a range — the same reason
// the block form itself refuses 0.0.0.0/0.
Assert.True(PolicyBounds.NetblockPrefixV4Ok(PolicyBounds.NetblockPrefixV4Min));
Assert.False(PolicyBounds.NetblockPrefixV4Ok(0));
Assert.False(PolicyBounds.NetblockPrefixV4Ok(32));
Assert.True(PolicyBounds.NetblockPrefixV6Ok(PolicyBounds.NetblockPrefixV6Min));
Assert.False(PolicyBounds.NetblockPrefixV6Ok(128));
}
[Fact]
public void A_stored_override_outside_the_bounds_is_named_rather_than_obeyed()
{
Assert.True(PolicyBounds.ValueOk("netblockCandidateBans", "0"));
Assert.False(PolicyBounds.ValueOk("netblockCandidateBans", "-3"));
Assert.True(PolicyBounds.ValueOk("netblockPrefixV4", "22"));
Assert.False(PolicyBounds.ValueOk("netblockPrefixV4", "33"));
Assert.False(PolicyBounds.ValueOk("netblockPrefixV6", "not-a-prefix"));
Assert.Contains("..", PolicyBounds.RangeOf("netblockPrefixV4"));
}
}