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.
28 lines
1.2 KiB
C#
28 lines
1.2 KiB
C#
namespace OpenWaf.Domain.Policy;
|
|
|
|
/// <summary>Inclusive HTTP status range used to classify a response.</summary>
|
|
public readonly record struct StatusRange(int Low, int High)
|
|
{
|
|
public bool Contains(int status) => status >= Low && status <= High;
|
|
|
|
public override string ToString() => $"{Low}-{High}";
|
|
|
|
/// <summary>Configuration path: a value that does not parse is not a
|
|
/// reason to refuse to start, so it falls back. The admin API does not use
|
|
/// this — it uses <see cref="TryParse"/> and answers 400, because an
|
|
/// operator's write deserves to be told, not quietly ignored.</summary>
|
|
public static StatusRange Parse(string? value, StatusRange fallback)
|
|
=> TryParse(value, out var parsed) ? parsed : fallback;
|
|
|
|
public static bool TryParse(string? value, out StatusRange range)
|
|
{
|
|
range = default;
|
|
if (string.IsNullOrWhiteSpace(value)) return false;
|
|
var parts = value.Split('-', StringSplitOptions.TrimEntries);
|
|
if (parts.Length != 2) return false;
|
|
if (!int.TryParse(parts[0], out var lo) || !int.TryParse(parts[1], out var hi) || lo > hi) return false;
|
|
range = new StatusRange(lo, hi);
|
|
return true;
|
|
}
|
|
}
|