Measured on the shipped image (openwaf:c28, the head tree), one allow entry
written for one minute against an operator block on the same address:
03:58:16 POST /admin-api/allow {"match":"192.168.65.1","expiresInMinutes":1} -> 204
03:58:20 GET /whatever -> 502 (passed to the upstream: the exemption is in force)
03:59:38 GET /whatever -> 301 (the minute has passed; the block is back)
03:59:39 GET /admin-api/allow -> 200, the row still listed, no liveness field
GET /admin-api/state -> "allowEntries":1
The engine was right and the surface was wrong: the operator's table said
"exempted" a minute after the edge stopped honouring it, and the pill's number
was a count of rows rather than a count of exemptions in force. Nothing prunes
those rows either — retention ages out observations, engine bans and alerts, and
an operator's own writes are not the machine's to delete — so the dead ones
accumulate exactly where they cannot be told apart from the live ones.
The same endpoint was also the last listing with no ceiling: 1,101 rows came back
in 130,386 bytes with no X-Total-Count and no X-Truncated, and every paging
parameter was ignored — ?limit=10, ?offset=1000, ?limit=0 and ?limit=100000 each
answered all 1,101.
Now: GET /admin-api/allow takes live (default true, live=false means "do not
filter", the same shape as /blocks), limit 1..2000 and offset, refuses a limit
nobody meant by name, and carries X-Total-Count and X-Truncated computed from the
same predicate as the rows. Each row carries live, computed from the same rule
the engine applies (AllowEntry.IsLive), and /state answers allowEntries as the
live count with allowEntriesExpired beside it. The page fetches live=false, marks
a dead row "EXPIRED, the edge no longer honours it", counts them under the table,
and its pill says "0 live - 1 expired".
Teeth, in this order: AllowListingHonestyTests (4) written first and run against
the unfixed tree — 4 failed, 0 passed, including "allowEntries expected 1, got 2"
and "X-Total-Count expected 5, got null". The wire assertions in the second test
passed before the fix and after it: the engine already knew. The console gate
seeds an allow entry with a one-minute TTL ahead of the waits it already pays, so
the expired state arrives for free, and asserts the dead row is listed and marked,
the live one is not, and the line under the table counts them; the two existing
row deletes are now aimed at a match instead of clicking the first remove button,
which with a second row in the table would have deleted whatever sorted first.
Numbers on this tree: host — build 0 warnings, unit 128/128, integration 113/113,
smoke 22 passed / 0 failed, compose 47 passed / 0 failed, console 50 assertions /
0 failed, image 261 MB. Linux rehearsal, each stage on its own — source in
mcr.microsoft.com/dotnet/sdk:10.0 (Ubuntu 24.04.5, SDK 10.0.401): unit 128/128,
integration 113/113, smoke 22/0; compose in docker:cli: 47/0, enforcement lag
11 ms; console in mcr.microsoft.com/playwright:v1.63.0-noble: 50/0.
182 lines
8.4 KiB
C#
182 lines
8.4 KiB
C#
using System.Net;
|
|
using System.Text.Json;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using OpenWaf.Domain.Security;
|
|
using OpenWaf.Infrastructure.Persistence;
|
|
|
|
namespace OpenWaf.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// An allow entry is the one rule that outranks every ban, so the listing of it
|
|
/// is the operator's answer to "who is exempt right now". Measured on the
|
|
/// shipped image (openwaf:c28, the head tree), with one entry added for one
|
|
/// minute against an operator block:
|
|
///
|
|
/// 03:58:16 POST /admin-api/allow {"match":"192.168.65.1","expiresInMinutes":1} → 204
|
|
/// 03:58:20 GET /whatever → 502 (passed to the upstream: the exemption is in force)
|
|
/// 03:59:38 GET /whatever → 301 (the exemption has expired; the block is back)
|
|
/// 03:59:39 GET /admin-api/allow → 200, the row still listed, no liveness field
|
|
/// /admin-api/state → "allowEntries":1
|
|
///
|
|
/// So the page said the address was exempted one minute after the edge stopped
|
|
/// honouring it, and the count the page shows is the count of rows, not the
|
|
/// count of exemptions in force. The same listing answered 1101 rows in 130,386
|
|
/// bytes with no X-Total-Count and no X-Truncated, and every paging parameter
|
|
/// was ignored: ?limit=10, ?offset=1000, ?limit=0 and ?limit=100000 each
|
|
/// returned all 1101. Nothing prunes expired allow rows either — the retention
|
|
/// worker ages out observations, engine bans and alerts — so the table only
|
|
/// grows, and the dead rows are the ones the operator cannot tell apart.
|
|
/// </summary>
|
|
public sealed class AllowListingHonestyTests
|
|
{
|
|
private const string Expired = "203.0.113.88";
|
|
private const string Live = "203.0.113.89";
|
|
|
|
private static string? Header(HttpResponseMessage r, string name)
|
|
=> r.Headers.TryGetValues(name, out var a) ? string.Join(",", a)
|
|
: r.Content.Headers.TryGetValues(name, out var b) ? string.Join(",", b)
|
|
: null;
|
|
|
|
private static async Task<(HttpStatusCode Status, JsonElement Body, string? Total, string? Truncated, string Text)> Listed(
|
|
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();
|
|
var body = response.StatusCode == HttpStatusCode.OK && text.Length > 0
|
|
? JsonDocument.Parse(text).RootElement
|
|
: default;
|
|
return (response.StatusCode, body, Header(response, "X-Total-Count"), Header(response, "X-Truncated"), text);
|
|
}
|
|
|
|
private static async Task<HttpStatusCode> FromAsync(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);
|
|
return response.StatusCode;
|
|
}
|
|
|
|
private static string[] MatchesOf(JsonElement body) =>
|
|
body.EnumerateArray().Select(e => e.GetProperty("match").GetString()!).ToArray();
|
|
|
|
/// <summary>
|
|
/// Rows are written straight to the database because the API cannot be asked
|
|
/// for an expiry in the past — expiresInMinutes is 1..5,256,000, which is the
|
|
/// fix from an earlier cycle and is not about to be loosened to make a test
|
|
/// easier. The backdated row is exactly what a minute of waiting produces.
|
|
/// </summary>
|
|
private static async Task SeedAllow(EdgeFixture f, string match, DateTimeOffset? expiresAt)
|
|
{
|
|
using var scope = f.Factory.Services.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<OpenWafDbContext>();
|
|
db.AllowEntries.Add(new AllowEntry
|
|
{
|
|
Match = match,
|
|
Note = "seeded",
|
|
CreatedAt = DateTimeOffset.UtcNow.AddMinutes(-120),
|
|
ExpiresAt = expiresAt
|
|
});
|
|
await db.SaveChangesAsync();
|
|
await f.Service<EdgeStateStore>().InvalidateAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task An_expired_exemption_is_out_of_the_default_listing_and_says_it_expired_in_the_full_one()
|
|
{
|
|
await using var f = new EdgeFixture();
|
|
await SeedAllow(f, Expired, DateTimeOffset.UtcNow.AddMinutes(-1));
|
|
await SeedAllow(f, Live, null);
|
|
|
|
var defaults = await Listed(f, "/admin-api/allow");
|
|
Assert.Equal(HttpStatusCode.OK, defaults.Status);
|
|
Assert.Equal(["203.0.113.89"], MatchesOf(defaults.Body));
|
|
Assert.Equal("1", defaults.Total);
|
|
Assert.Equal("false", defaults.Truncated);
|
|
|
|
var everything = await Listed(f, "/admin-api/allow?live=false");
|
|
Assert.Equal(["203.0.113.88", "203.0.113.89"], MatchesOf(everything.Body));
|
|
Assert.Equal("2", everything.Total);
|
|
var dead = everything.Body.EnumerateArray().Single(e => e.GetProperty("match").GetString() == Expired);
|
|
Assert.False(dead.GetProperty("live").GetBoolean());
|
|
var alive = everything.Body.EnumerateArray().Single(e => e.GetProperty("match").GetString() == Live);
|
|
Assert.True(alive.GetProperty("live").GetBoolean());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task The_allow_listing_and_the_wire_give_the_same_answer_about_an_expired_exemption()
|
|
{
|
|
await using var f = new EdgeFixture();
|
|
// An operator block on both addresses, so the only thing that can let
|
|
// either one through is its allow entry.
|
|
using (var scope = f.Factory.Services.CreateScope())
|
|
{
|
|
var store = scope.ServiceProvider.GetRequiredService<BlockStore>();
|
|
foreach (var ip in new[] { Expired, Live })
|
|
await store.AddOperatorAsync(ip, "c28 seed", null, DateTimeOffset.UtcNow);
|
|
}
|
|
await SeedAllow(f, Expired, DateTimeOffset.UtcNow.AddMinutes(-1));
|
|
await SeedAllow(f, Live, null);
|
|
|
|
Assert.Equal(HttpStatusCode.NotFound, await FromAsync(f, "/anything", Live));
|
|
Assert.Equal(HttpStatusCode.MovedPermanently, await FromAsync(f, "/anything", Expired));
|
|
|
|
// The number the page puts in its pill has to be the number of
|
|
// exemptions the edge is honouring, not the number of rows.
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, "/admin-api/state");
|
|
request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken);
|
|
using var response = await f.Client.SendAsync(request);
|
|
var state = JsonDocument.Parse(await response.Content.ReadAsStringAsync()).RootElement;
|
|
Assert.Equal(1, state.GetProperty("allowEntries").GetInt32());
|
|
Assert.Equal(1, state.GetProperty("allowEntriesExpired").GetInt32());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task The_allow_listing_is_capped_and_says_so_and_the_offset_reaches_the_rest()
|
|
{
|
|
await using var f = new EdgeFixture();
|
|
for (var i = 1; i <= 5; i++)
|
|
await SeedAllow(f, $"198.51.100.{i}", null);
|
|
|
|
var first = await Listed(f, "/admin-api/allow?limit=2");
|
|
Assert.Equal("5", first.Total);
|
|
Assert.Equal("true", first.Truncated);
|
|
Assert.Equal(2, MatchesOf(first.Body).Length);
|
|
|
|
var seen = new List<string>();
|
|
for (var offset = 0; offset < 5; offset++)
|
|
{
|
|
var page = await Listed(f, $"/admin-api/allow?limit=1&offset={offset}");
|
|
Assert.Equal("5", page.Total);
|
|
seen.AddRange(MatchesOf(page.Body));
|
|
}
|
|
Assert.Equal(5, seen.Distinct().Count());
|
|
|
|
var whole = await Listed(f, "/admin-api/allow?limit=2000");
|
|
Assert.Equal("false", whole.Truncated);
|
|
Assert.Equal(5, MatchesOf(whole.Body).Length);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_paging_limit_nobody_meant_on_the_allow_listing_is_refused_by_name()
|
|
{
|
|
await using var f = new EdgeFixture();
|
|
await SeedAllow(f, Live, null);
|
|
|
|
foreach (var (query, expected) in new[]
|
|
{
|
|
("?limit=0", "limit must be 1..2000; got 0"),
|
|
("?limit=-1", "limit must be 1..2000; got -1"),
|
|
("?limit=2001", "limit must be 1..2000; got 2001"),
|
|
("?offset=-1", "offset must be 0 or more; got -1")
|
|
})
|
|
{
|
|
var bad = await Listed(f, "/admin-api/allow" + query);
|
|
Assert.Equal(HttpStatusCode.BadRequest, bad.Status);
|
|
Assert.Contains(expected, bad.Text);
|
|
}
|
|
}
|
|
}
|