/streaks learned the ?source= filter the observation and alert listings learned in OPEN-33, and standingStreaks narrows with the rows while trackedSources stays the map size. The console narrows observations and streaks to one key from a box or from a click on any key in any table, prints the filtered total under each, and says the words 'no recorded observations for <key>' instead of drawing an empty box. The first version appended '&source=' to a bare path, which is a different route: the edge answered 404 and the streak line printed 'unknown scope'. The console gate caught it; the API tests did not, because they call the API. Hence withSrc, which decides the separator per path.
171 lines
7.8 KiB
C#
171 lines
7.8 KiB
C#
using System.Net;
|
|
using System.Text.Json;
|
|
|
|
namespace OpenWaf.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// The streak map is the one part of the state that lives only in memory, so
|
|
/// until now the console could only say how many sources it was tracking — not
|
|
/// which one is three refusals from a ban. These tests pin the read that fixed,
|
|
/// and the two properties it inherits: it is behind the admin token, and
|
|
/// fumbling it must never ban the person fumbling it.
|
|
/// </summary>
|
|
public sealed class AdminStreaksTests
|
|
{
|
|
private const string Building = "203.0.113.180";
|
|
private const string Recovered = "203.0.113.181";
|
|
private const string Second = "203.0.113.182";
|
|
|
|
private static async Task GetAsync(EdgeFixture f, string path, string sourceIp)
|
|
{
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, path);
|
|
request.Headers.TryAddWithoutValidation("X-Forwarded-For", sourceIp);
|
|
using var response = await f.Client.SendAsync(request);
|
|
}
|
|
|
|
private static async Task<(HttpStatusCode Status, JsonElement Body)> StreaksAsync(
|
|
EdgeFixture f, bool withToken = true)
|
|
{
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, "/admin-api/streaks");
|
|
if (withToken) request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken);
|
|
using var response = await f.Client.SendAsync(request);
|
|
var body = response.StatusCode == HttpStatusCode.OK
|
|
? JsonDocument.Parse(await response.Content.ReadAsStringAsync()).RootElement
|
|
: default;
|
|
return (response.StatusCode, body);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_source_mid_streak_is_listed_with_its_current_count_and_the_threshold_it_is_rising_toward()
|
|
{
|
|
await using var f = new EdgeFixture(threshold: 5);
|
|
f.Upstream.StatusFor = path => path == "/ok" ? 200 : 404;
|
|
|
|
for (var i = 0; i < 3; i++) await GetAsync(f, "/probe", Building);
|
|
await GetAsync(f, "/probe", Recovered);
|
|
await GetAsync(f, "/ok", Recovered);
|
|
|
|
var (status, root) = await StreaksAsync(f);
|
|
Assert.Equal(HttpStatusCode.OK, status);
|
|
Assert.Equal("this-process", root.GetProperty("scope").GetString());
|
|
Assert.Equal(5, root.GetProperty("threshold").GetInt32());
|
|
Assert.Equal(2, root.GetProperty("trackedSources").GetInt32());
|
|
|
|
var streaks = root.GetProperty("streaks");
|
|
Assert.Equal(1, streaks.GetArrayLength());
|
|
var only = streaks[0];
|
|
Assert.Equal(Building, only.GetProperty("sourceKey").GetString());
|
|
Assert.Equal(3, only.GetProperty("consecutiveRefusals").GetInt32());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task The_streak_listing_is_admin_only()
|
|
{
|
|
await using var f = new EdgeFixture();
|
|
await GetAsync(f, "/probe", Building);
|
|
|
|
var (status, _) = await StreaksAsync(f, withToken: false);
|
|
Assert.Equal(HttpStatusCode.Unauthorized, status);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Fumbling_the_streak_listing_never_bans_the_operator_reading_it()
|
|
{
|
|
await using var f = new EdgeFixture(threshold: 3);
|
|
|
|
for (var i = 0; i < 8; i++)
|
|
{
|
|
var (status, _) = await StreaksAsync(f);
|
|
Assert.Equal(HttpStatusCode.OK, status);
|
|
}
|
|
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, "/probe");
|
|
request.Headers.TryAddWithoutValidation("X-Forwarded-For", Building);
|
|
using var proxied = await f.Client.SendAsync(request);
|
|
Assert.Equal(HttpStatusCode.NotFound, proxied.StatusCode);
|
|
}
|
|
|
|
// ── OPEN-36: the streak map learned the same filter the observation and
|
|
// alert listings learned in OPEN-33, for the same reason. Measured before
|
|
// the change: GET /admin-api/streaks?source=203.0.113.180 answered 200 with
|
|
// every standing source and standingStreaks counting all of them, because
|
|
// the endpoint had never heard of the parameter and Minimal APIs drop what a
|
|
// handler does not declare.
|
|
private static async Task<(HttpStatusCode Status, string Text, JsonElement Body)> StreaksAtAsync(
|
|
EdgeFixture f, string url)
|
|
{
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, url);
|
|
request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken);
|
|
using var response = await f.Client.SendAsync(request);
|
|
var text = await response.Content.ReadAsStringAsync();
|
|
return (response.StatusCode, text, response.StatusCode == HttpStatusCode.OK
|
|
? JsonDocument.Parse(text).RootElement
|
|
: default);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_source_filter_narrows_the_streaks_and_the_standing_count_with_them()
|
|
{
|
|
await using var f = new EdgeFixture(threshold: 5);
|
|
f.Upstream.StatusFor = path => path == "/ok" ? 200 : 404;
|
|
|
|
for (var i = 0; i < 3; i++) await GetAsync(f, "/probe", Building);
|
|
for (var i = 0; i < 2; i++) await GetAsync(f, "/probe", Second);
|
|
await GetAsync(f, "/probe", Recovered);
|
|
await GetAsync(f, "/ok", Recovered);
|
|
|
|
var (_, _, all) = await StreaksAtAsync(f, "/admin-api/streaks");
|
|
Assert.Equal(2, all.GetProperty("standingStreaks").GetInt32());
|
|
// With no filter the answer names no filter at all — the field is there
|
|
// and null, so a reader never has to guess whether an absent key means
|
|
// "unfiltered" or "this build forgot to say".
|
|
Assert.True(all.GetProperty("sourceFilter").ValueKind is JsonValueKind.Null);
|
|
|
|
var (status, text, one) = await StreaksAtAsync(f, "/admin-api/streaks?source=" + Building);
|
|
Assert.Equal(HttpStatusCode.OK, status);
|
|
Assert.Equal("this-process", one.GetProperty("scope").GetString());
|
|
Assert.Contains(Building, text);
|
|
// The rows narrowed, so the count narrowed. A page showing one source
|
|
// and reporting the whole map's standing count is the same lie the
|
|
// X-Truncated fix removed one level up.
|
|
Assert.Equal(1, one.GetProperty("streaks").GetArrayLength());
|
|
Assert.Equal(Building, one.GetProperty("streaks")[0].GetProperty("sourceKey").GetString());
|
|
Assert.Equal(1, one.GetProperty("standingStreaks").GetInt32());
|
|
// trackedSources stays the map size: "one of these many" is only honest
|
|
// if the many is still printed next to the one.
|
|
Assert.Equal(3, one.GetProperty("trackedSources").GetInt32());
|
|
Assert.Equal(Building, one.GetProperty("sourceFilter").GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_source_with_nothing_standing_answers_an_empty_page_and_says_so_with_a_zero()
|
|
{
|
|
await using var f = new EdgeFixture(threshold: 5);
|
|
f.Upstream.StatusFor = path => path == "/ok" ? 200 : 404;
|
|
|
|
for (var i = 0; i < 3; i++) await GetAsync(f, "/probe", Building);
|
|
await GetAsync(f, "/probe", Recovered);
|
|
await GetAsync(f, "/ok", Recovered);
|
|
|
|
var (_, _, one) = await StreaksAtAsync(f, "/admin-api/streaks?source=" + Recovered);
|
|
Assert.Equal(0, one.GetProperty("streaks").GetArrayLength());
|
|
Assert.Equal(0, one.GetProperty("standingStreaks").GetInt32());
|
|
// The map still holds the address — that is the difference between "this
|
|
// source was never seen" and "this source was seen and last saw a clean
|
|
// answer", and the console prints both numbers.
|
|
Assert.Equal(2, one.GetProperty("trackedSources").GetInt32());
|
|
Assert.Equal(Recovered, one.GetProperty("sourceFilter").GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task An_empty_source_is_refused_rather_than_read_as_every_streak()
|
|
{
|
|
await using var f = new EdgeFixture(threshold: 5);
|
|
for (var i = 0; i < 3; i++) await GetAsync(f, "/probe", Building);
|
|
|
|
var (status, text, _) = await StreaksAtAsync(f, "/admin-api/streaks?source=");
|
|
Assert.Equal(HttpStatusCode.BadRequest, status);
|
|
Assert.Contains("source must not be empty", text);
|
|
}
|
|
}
|