using System.Net; using System.Text.Json; using Microsoft.Extensions.DependencyInjection; using OpenWaf.Domain.Security; using OpenWaf.Infrastructure.Persistence; namespace OpenWaf.Tests.Integration; /// /// A query parameter the endpoint has never heard of used to be dropped without /// a trace, and the answer kept its shape. Measured on the shipped image /// (openwaf:ci, 2026-10-02) with 16 recorded sources: /// /// GET /admin-api/observations?source=203.0.113.11 /// → 200, 16 rows over 16 different addresses, X-Total-Count: 16 /// GET /admin-api/observations?src=203.0.113.11 /// → 200, the same 16 rows /// /// Nothing said the filter had not been applied. An operator investigating one /// address was handed fifteen other addresses and a total that matched those /// fifteen, which is the same shape as the bug OPEN-23 found one level up — an /// unknown `?source=` VALUE quietly meaning "operator". Minimal APIs bind the /// parameters a handler declares and discard the rest, so the fix has to be a /// refusal that names the parameter and lists what the endpoint accepts, plus a /// filter that actually filters when the question is a fair one to ask. /// public sealed class QueryHonestyTests { 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, string Text, string? Total, string? Truncated)> GetAsync( 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); return (response.StatusCode, await response.Content.ReadAsStringAsync(), Header(response, "X-Total-Count"), Header(response, "X-Truncated")); } private static async Task SeedObservationsAsync(EdgeFixture f, params string[] ips) { foreach (var ip in ips) await f.SendAsync(f.Client, "/nope", ip); await f.FlushObservationsAsync(); } [Theory] [InlineData("/admin-api/observations")] [InlineData("/admin-api/alerts")] [InlineData("/admin-api/blocks")] [InlineData("/admin-api/allow")] [InlineData("/admin-api/streaks")] public async Task A_parameter_the_endpoint_never_implemented_is_refused_and_names_itself(string path) { await using var f = new EdgeFixture(threshold: 9); var r = await GetAsync(f, $"{path}?zzz=1"); Assert.Equal(HttpStatusCode.BadRequest, r.Status); Assert.Contains("unknown query parameter 'zzz'", r.Text); // The refusal has to be actionable: naming the bad parameter without // naming the good ones leaves the operator guessing which spelling works. Assert.Contains("limit", r.Text); } [Fact] public async Task A_source_filter_returns_only_that_source_and_counts_only_that_source() { await using var f = new EdgeFixture(threshold: 9); await SeedObservationsAsync(f, "203.0.113.201", "203.0.113.202", "203.0.113.203"); var all = await GetAsync(f, "/admin-api/observations?limit=1000"); Assert.Equal("3", all.Total); var one = await GetAsync(f, "/admin-api/observations?limit=1000&source=203.0.113.202"); Assert.Equal(HttpStatusCode.OK, one.Status); // The total answers the question that was asked. Counting the table while // showing one row would put a 3 next to a single row and make X-Truncated // claim a cut that never happened. Assert.Equal("1", one.Total); Assert.Equal("false", one.Truncated); var body = JsonDocument.Parse(one.Text).RootElement; Assert.Equal(1, body.GetArrayLength()); Assert.Equal("203.0.113.202", body[0].GetProperty("sourceKey").GetString()); } [Fact] public async Task A_source_with_no_rows_answers_empty_and_says_the_total_is_zero() { await using var f = new EdgeFixture(threshold: 9); await SeedObservationsAsync(f, "203.0.113.201"); var r = await GetAsync(f, "/admin-api/observations?limit=1000&source=198.51.100.77"); Assert.Equal(HttpStatusCode.OK, r.Status); Assert.Equal("0", r.Total); Assert.Equal("[]", r.Text); } [Fact] public async Task An_empty_source_is_refused_rather_than_read_as_every_source() { await using var f = new EdgeFixture(threshold: 9); await SeedObservationsAsync(f, "203.0.113.201"); var r = await GetAsync(f, "/admin-api/observations?limit=1000&source="); Assert.Equal(HttpStatusCode.BadRequest, r.Status); Assert.Contains("source must not be empty", r.Text); } [Fact] public async Task The_alert_listing_filters_by_source_and_leaves_the_others_out() { await using var f = new EdgeFixture(threshold: 9); var now = DateTimeOffset.UtcNow; using (var scope = f.Factory.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); for (var i = 1; i <= 3; i++) db.Alerts.Add(new EdgeAlert { SourceKey = i == 2 ? "203.0.113.212" : "203.0.113.211", Kind = "server_error_streak", Count = i, CreatedAt = now.AddMinutes(-i), Detail = "seeded for the filter" }); await db.SaveChangesAsync(); } var all = await GetAsync(f, "/admin-api/alerts?limit=1000"); Assert.Equal("3", all.Total); var one = await GetAsync(f, "/admin-api/alerts?limit=1000&source=203.0.113.212"); Assert.Equal("1", one.Total); var body = JsonDocument.Parse(one.Text).RootElement; Assert.Equal(1, body.GetArrayLength()); Assert.Equal("203.0.113.212", body[0].GetProperty("sourceKey").GetString()); } [Theory] [InlineData("/admin-api/blocks?source=engine&live=false&limit=1&offset=0")] [InlineData("/admin-api/allow?live=false&limit=500")] [InlineData("/admin-api/observations?limit=2&offset=1")] [InlineData("/admin-api/streaks?limit=1000")] public async Task The_parameters_the_endpoint_does_implement_keep_working(string url) { // The refusal is only worth having if it never fires on a parameter the // endpoint really does honour — the console and both smoke gates call // these with live/source/limit/offset every run. await using var f = new EdgeFixture(threshold: 9); var r = await GetAsync(f, url); Assert.NotEqual(HttpStatusCode.BadRequest, r.Status); Assert.DoesNotContain("unknown query parameter", r.Text); } }