Files
openwaf/dotnet/OpenWaf.Tests.Unit/PolicyBoundsTests.cs
T
Pavlo Ratushnyi 52f4167867 A policy value nobody believed was stored, echoed back, and obeyed
The admin API answered 200 to anything. Measured on the shipped image
(openwaf:ci) before this commit:

  PUT {"streakThreshold":0}          -> 200, streakThreshold still 3, 0 log lines
  PUT {"maxTrackedSources":0}        -> 200, unchanged
  PUT {"banDurationMinutes":-1}      -> 200, unchanged
  PUT {"streakWindowMinutes":0}      -> 200, unchanged
  PUT {"refusalRange":"not-a-range"} -> 200, unchanged
  PUT {"engineBlockStatus":999}      -> 200, and the edge answered HTTP/1.1 999
  PUT {"engineBlockStatus":1000}     -> 200, and every banned request died as
                                       "curl: (1) Unsupported HTTP/1 subversion
                                        in response" — no HTTP response at all
  PUT {"ipv4KeyPrefixLength":0}      -> 200, one 404 banned the key "*", and the
                                       next three never-seen addresses were
                                       refused on their first request
  PUT {"ipv4KeyPrefixLength":999}    -> 200, GET echoed 999, enforcement used 32

Two different failures hid behind that 200. A value the code did not want was
dropped without a word, so the operator read "OK" over a policy that had not
moved. A value the code did want was stored and obeyed literally, and one of
those (engineBlockStatus=1000) takes the site down for every banned visitor
from one admin call.

So the ranges now live in one place, PolicyBounds, next to the reason for each
one, and three layers read it:

  * the admin endpoint answers 400 naming the key and the range — the same
    shape mode, geo and the state TTL already used;
  * the loader ignores a row outside the range and logs the key and the value,
    for a backup restored or a row edited by hand;
  * DecisionEngine refuses to put an unsendable status on the wire at all, and
    answers 403 instead.

ipv4KeyPrefixLength keeps a floor of 8 rather than 0, because 0 is not a coarse
grouping, it is the literal key "*" — one streak and one ban shared by every
IPv4 address on the internet.

Tests: 46 unit (the bounds table, the "*" mechanism, the wire guard) and 18
integration (400 by name, a refused write changing nothing, an accepted value
reaching the wire as 451, a hand-edited row ignored). Teeth, each measured by
removing the layer: no API validation -> 16 integration tests fail; no wire
guard -> 3 unit tests fail; no loader gate -> 1 integration test fails.
2026-10-01 23:51:30 +02:00

144 lines
6.2 KiB
C#

using OpenWaf.Domain.Engine;
using OpenWaf.Domain.Policy;
using OpenWaf.Domain.Security;
namespace OpenWaf.Tests.Unit;
/// <summary>
/// The numbers that say what a policy value may be, and the guard that keeps a
/// value which slipped past them off the wire.
///
/// These exist because the shipped image used to answer 200 to everything.
/// Measured on openwaf:ci before the fix: streakThreshold=0, maxTrackedSources=0,
/// banDurationMinutes=-1, streakWindowMinutes=0 and refusalRange="not-a-range"
/// each returned 200 with the old policy in the body and no word that the write
/// had been ignored; engineBlockStatus=999 was obeyed literally (the edge
/// answered "HTTP/1.1 999"); engineBlockStatus=1000 was obeyed too and the
/// client got no HTTP response at all; ipv4KeyPrefixLength=0 was obeyed and one
/// 404 banned the key "*", which is every IPv4 address at once.
/// </summary>
public sealed class PolicyBoundsTests
{
private static readonly DateTimeOffset Now = new(2026, 10, 1, 12, 0, 0, TimeSpan.Zero);
[Theory]
[InlineData("streakThreshold", "0", false)]
[InlineData("streakThreshold", "-5", false)]
[InlineData("streakThreshold", "1", true)]
[InlineData("streakThreshold", "1000", true)]
[InlineData("streakThreshold", "1001", false)]
[InlineData("streakWindowMinutes", "0", false)]
[InlineData("streakWindowMinutes", "1440", true)]
[InlineData("streakWindowMinutes", "1441", false)]
[InlineData("banDurationMinutes", "-1", false)]
[InlineData("banDurationMinutes", "10080", true)]
[InlineData("banDurationMinutes", "10081", false)]
[InlineData("maxTrackedSources", "0", false)]
[InlineData("maxTrackedSources", "1000001", false)]
[InlineData("ipv4KeyPrefixLength", "0", false)]
[InlineData("ipv4KeyPrefixLength", "7", false)]
[InlineData("ipv4KeyPrefixLength", "8", true)]
[InlineData("ipv4KeyPrefixLength", "32", true)]
[InlineData("ipv4KeyPrefixLength", "999", false)]
[InlineData("ipv6KeyPrefixLength", "31", false)]
[InlineData("ipv6KeyPrefixLength", "64", true)]
[InlineData("ipv6KeyPrefixLength", "128", true)]
[InlineData("ipv6KeyPrefixLength", "999", false)]
[InlineData("engineBlockStatus", "1", false)]
[InlineData("engineBlockStatus", "200", false)]
[InlineData("engineBlockStatus", "403", true)]
[InlineData("engineBlockStatus", "451", true)]
[InlineData("engineBlockStatus", "599", true)]
[InlineData("engineBlockStatus", "999", false)]
[InlineData("engineBlockStatus", "1000", false)]
[InlineData("operatorBlockStatus", "301", true)]
[InlineData("operatorBlockStatus", "302", false)]
[InlineData("operatorBlockStatus", "403", true)]
[InlineData("refusalRange", "not-a-range", false)]
[InlineData("refusalRange", "400-499", true)]
[InlineData("refusalRange", "499-400", false)]
[InlineData("refusalRange", "600-700", false)]
[InlineData("serverErrorRange", "500-599", true)]
[InlineData("serverErrorRange", "0-2000", false)]
public void A_policy_row_is_judged_by_the_same_numbers_the_api_uses(string key, string value, bool usable)
=> Assert.Equal(usable, PolicyBounds.ValueOk(key, value));
[Fact]
public void Every_key_with_a_range_states_it_in_words_an_operator_can_act_on()
{
foreach (var key in new[]
{
"streakThreshold", "streakWindowMinutes", "banDurationMinutes", "maxTrackedSources",
"ipv4KeyPrefixLength", "ipv6KeyPrefixLength", "engineBlockStatus", "operatorBlockStatus",
"refusalRange", "serverErrorRange"
})
{
var range = PolicyBounds.RangeOf(key);
Assert.False(string.IsNullOrWhiteSpace(range), $"{key} has no range text");
Assert.True(range != "a defined value", $"{key} fell through to the placeholder");
}
}
[Fact]
public void A_prefix_of_zero_is_what_the_floor_exists_to_stop_because_it_is_one_source_for_the_whole_family()
{
// Not a claim that 0 is unreachable — it is a claim about what it means,
// measured on the shipped image: three addresses that had never been
// seen were refused on their first request once the key was "*".
var policy = new EdgePolicy { Ipv4KeyPrefixLength = 0 };
Assert.Equal("*", SourceKey.For("203.0.113.7", policy));
Assert.Equal("*", SourceKey.For("198.51.100.9", policy));
var honest = new EdgePolicy { Ipv4KeyPrefixLength = 8 };
Assert.Equal("203.0.0.0/8", SourceKey.For("203.0.113.7", honest));
Assert.True(
SourceKey.For("203.0.113.7", honest) != SourceKey.For("198.51.100.9", honest),
"an /8 must not merge two providers into one source");
}
private static EdgeSnapshot WithBan(EdgePolicy policy, string key) => new()
{
GeneratedAt = Now,
Policy = policy,
EngineBans = new Dictionary<string, BlockEntry>
{
[key] = new BlockEntry
{
SourceKey = key,
Source = BlockSource.Engine,
CreatedAt = Now.AddMinutes(-1),
ExpiresAt = Now.AddMinutes(30),
Reason = "3 consecutive refusals (400-499) within 5 min; last 404 /nope"
}
}
};
[Theory]
[InlineData(1000)]
[InlineData(1)]
[InlineData(0)]
public void A_status_the_wire_cannot_carry_is_answered_as_403_instead_of_nothing_at_all(int configured)
{
var decision = DecisionEngine.Decide(
WithBan(new EdgePolicy { Mode = EnforcementMode.Enforce, EngineBlockStatus = configured }, "203.0.113.7"),
"203.0.113.7", "/nope", Now);
Assert.Equal(EdgeVerdict.BlockEngine, decision.Verdict);
Assert.Equal(403, decision.StatusCode);
}
[Theory]
[InlineData(403)]
[InlineData(451)]
[InlineData(503)]
public void A_status_inside_the_range_is_answered_exactly_as_the_operator_wrote_it(int configured)
{
var decision = DecisionEngine.Decide(
WithBan(new EdgePolicy { Mode = EnforcementMode.Enforce, EngineBlockStatus = configured }, "203.0.113.7"),
"203.0.113.7", "/nope", Now);
Assert.Equal(EdgeVerdict.BlockEngine, decision.Verdict);
Assert.Equal(configured, decision.StatusCode);
}
}