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.
122 lines
7.6 KiB
C#
122 lines
7.6 KiB
C#
namespace OpenWaf.Domain.Policy;
|
|
|
|
/// <summary>
|
|
/// What a policy value is allowed to be, said once.
|
|
///
|
|
/// Before this existed the admin API answered 200 to anything. Two different
|
|
/// failures hid behind that:
|
|
///
|
|
/// * a value the code did not want was dropped without a word — the operator
|
|
/// saw "200 OK", the policy in the response was the old one, and nothing in
|
|
/// the body or the log said the write had been ignored;
|
|
/// * a value the code did want was stored and obeyed literally, including
|
|
/// engineBlockStatus=1000, which the edge then tried to put on the wire:
|
|
/// the client got no HTTP response at all ("curl: (1) Unsupported HTTP/1
|
|
/// subversion in response"), so one typo in one admin call turns every
|
|
/// banned visitor into a connection error.
|
|
///
|
|
/// So the range lives here, next to the reason for it, and both the admin
|
|
/// endpoint and the DB override path check the same numbers. A rejected value
|
|
/// is a 400 that names the key and the range — the same shape the mode and geo
|
|
/// fields already used, and the same shape the state TTL uses at startup.
|
|
/// </summary>
|
|
public static class PolicyBounds
|
|
{
|
|
/// <summary>N. 0 or negative is not "never ban" — that is mode=Off — it is a
|
|
/// streak that can never mature, which is exactly the silent no-op this
|
|
/// class exists to stop. The ceiling keeps one operator call from turning
|
|
/// the guard off by asking for a threshold no source will ever reach.</summary>
|
|
public const int StreakThresholdMin = 1, StreakThresholdMax = 1000;
|
|
|
|
/// <summary>M. Minutes, so the floor is well under a second and the ceiling
|
|
/// is a day: a longer window is not a streak, it is a permanent record, and
|
|
/// permanent records are what the observation table is for.</summary>
|
|
public const double StreakWindowMinutesMin = 0.01, StreakWindowMinutesMax = 1440;
|
|
|
|
/// <summary>How long a ban lives. Ceiling is a week, matching the retention
|
|
/// window: a ban that outlives its own evidence row is a ban nobody can
|
|
/// explain.</summary>
|
|
public const double BanDurationMinutesMin = 0.01, BanDurationMinutesMax = 10_080;
|
|
|
|
/// <summary>The in-memory map bound. Below 1 the guard tracks nobody; above
|
|
/// a million the operator has asked the edge to grow the process, which is
|
|
/// the denial-of-service this bound exists to prevent.</summary>
|
|
public const int MaxTrackedSourcesMin = 1, MaxTrackedSourcesMax = 1_000_000;
|
|
|
|
/// <summary>Source-key granularity. The floor is the point: a /0 key is the
|
|
/// literal string "*" and every IPv4 address on the internet shares one
|
|
/// streak and one ban — measured, one 404 from one address banned the key
|
|
/// "*" and the next three addresses were refused on their first request.
|
|
/// An /8 is the largest thing that is still one provider's allocation;
|
|
/// below that a single key is most of the address space. IPv6 keeps its
|
|
/// documented /64 default and allows down to an /32 provider block.</summary>
|
|
public const int Ipv4KeyPrefixLengthMin = 8, Ipv4KeyPrefixLengthMax = 32;
|
|
public const int Ipv6KeyPrefixLengthMin = 32, Ipv6KeyPrefixLengthMax = 128;
|
|
|
|
/// <summary>The status the edge answers for a ban. It has to be a status
|
|
/// that means "you are not getting this": 400-599. Anything else is either
|
|
/// unsendable (1000 closed the connection) or a lie (200 says the request
|
|
/// worked while the body says blocked_by_policy).</summary>
|
|
public const int BlockStatusMin = 400, BlockStatusMax = 599;
|
|
|
|
/// <summary>The one non-4xx the operator block may answer, because 301 is
|
|
/// the reference behaviour: a redirect to a honey-pot target. Any other
|
|
/// 3xx would be a redirect with no Location, which is not a redirect.</summary>
|
|
public const int OperatorRedirectStatus = 301;
|
|
|
|
/// <summary>A status the HTTP/1.1 wire format can carry. Kestrel accepts
|
|
/// 100-999 and nothing else; outside that the response never reaches the
|
|
/// client. This is the last line of defence, not the first — the API
|
|
/// refuses these values long before they get here.</summary>
|
|
public const int SendableStatusMin = 100, SendableStatusMax = 999;
|
|
|
|
/// <summary>A status range is a classifier over real responses. 100-599 is
|
|
/// the whole space of statuses a server can answer; a range outside it
|
|
/// matches nothing, which would silently retire a pillar.</summary>
|
|
public const int ClassifiableStatusMin = 100, ClassifiableStatusMax = 599;
|
|
|
|
public static bool StreakThresholdOk(int v) => v is >= StreakThresholdMin and <= StreakThresholdMax;
|
|
public static bool StreakWindowMinutesOk(double v) => v is >= StreakWindowMinutesMin and <= StreakWindowMinutesMax;
|
|
public static bool BanDurationMinutesOk(double v) => v is >= BanDurationMinutesMin and <= BanDurationMinutesMax;
|
|
public static bool MaxTrackedSourcesOk(int v) => v is >= MaxTrackedSourcesMin and <= MaxTrackedSourcesMax;
|
|
public static bool Ipv4KeyPrefixLengthOk(int v) => v is >= Ipv4KeyPrefixLengthMin and <= Ipv4KeyPrefixLengthMax;
|
|
public static bool Ipv6KeyPrefixLengthOk(int v) => v is >= Ipv6KeyPrefixLengthMin and <= Ipv6KeyPrefixLengthMax;
|
|
public static bool EngineBlockStatusOk(int v) => v is >= BlockStatusMin and <= BlockStatusMax;
|
|
public static bool OperatorBlockStatusOk(int v) =>
|
|
v == OperatorRedirectStatus || (v is >= BlockStatusMin and <= BlockStatusMax);
|
|
public static bool IsSendableStatus(int status) => status is >= SendableStatusMin and <= SendableStatusMax;
|
|
|
|
/// <summary>The range as an operator reads it in a 400 body.</summary>
|
|
public static string RangeOf(string key) => key switch
|
|
{
|
|
"streakThreshold" => $"{StreakThresholdMin}..{StreakThresholdMax}",
|
|
"streakWindowMinutes" => $"{StreakWindowMinutesMin}..{StreakWindowMinutesMax}",
|
|
"banDurationMinutes" => $"{BanDurationMinutesMin}..{BanDurationMinutesMax}",
|
|
"maxTrackedSources" => $"{MaxTrackedSourcesMin}..{MaxTrackedSourcesMax}",
|
|
"ipv4KeyPrefixLength" => $"{Ipv4KeyPrefixLengthMin}..{Ipv4KeyPrefixLengthMax}",
|
|
"ipv6KeyPrefixLength" => $"{Ipv6KeyPrefixLengthMin}..{Ipv6KeyPrefixLengthMax}",
|
|
"engineBlockStatus" => $"{BlockStatusMin}..{BlockStatusMax}",
|
|
"operatorBlockStatus" => $"{OperatorRedirectStatus} or {BlockStatusMin}..{BlockStatusMax}",
|
|
"refusalRange" or "serverErrorRange" =>
|
|
$"low-high with {ClassifiableStatusMin} <= low <= high <= {ClassifiableStatusMax}",
|
|
_ => "a defined value"
|
|
};
|
|
|
|
/// <summary>True when a stored row or a request field for this key is
|
|
/// usable. Unknown keys are the caller's business, not this one's.</summary>
|
|
public static bool ValueOk(string key, string? value) => value is not null && key switch
|
|
{
|
|
"streakThreshold" => int.TryParse(value, out var v) && StreakThresholdOk(v),
|
|
"streakWindowMinutes" => double.TryParse(value, out var v) && StreakWindowMinutesOk(v),
|
|
"banDurationMinutes" => double.TryParse(value, out var v) && BanDurationMinutesOk(v),
|
|
"maxTrackedSources" => int.TryParse(value, out var v) && MaxTrackedSourcesOk(v),
|
|
"ipv4KeyPrefixLength" => int.TryParse(value, out var v) && Ipv4KeyPrefixLengthOk(v),
|
|
"ipv6KeyPrefixLength" => int.TryParse(value, out var v) && Ipv6KeyPrefixLengthOk(v),
|
|
"engineBlockStatus" => int.TryParse(value, out var v) && EngineBlockStatusOk(v),
|
|
"operatorBlockStatus" => int.TryParse(value, out var v) && OperatorBlockStatusOk(v),
|
|
"refusalRange" or "serverErrorRange" => StatusRange.TryParse(value, out var r)
|
|
&& r.Low >= ClassifiableStatusMin && r.High <= ClassifiableStatusMax,
|
|
_ => true
|
|
};
|
|
}
|