Files
Pavlo Ratushnyi 336c2f74a4 A query parameter the endpoint never implemented was dropped without a trace
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

The filter was neither applied nor refused. It was absent: minimal APIs bind
the parameters a handler declares and discard the rest, so an operator
investigating one address was handed fifteen other addresses and a total that
matched those fifteen. Every list endpoint behaved this way. This is the same
shape as the bug OPEN-23 found one level up, where an unknown ?source= VALUE
meant "operator" and a typo in a DELETE wiped the manual list.

Two changes, because a refusal alone would have hidden a fair question:

- TryKnownQuery refuses any query parameter the endpoint has not heard of,
  naming the parameter and listing what that endpoint accepts. `token` is in
  the known set on purpose: the admin auth layer reads it from the query
  string and OPEN-16 ruled that curl-friendly form stays supported. Three
  existing integration tests call /admin-api/blocks?token=… and were right to
  — a guard that does not know the whole surface refuses requests that work,
  which is a worse bug than the one it prevents.
- /observations and /alerts gained ?source=, and X-Total-Count counts the
  filtered set rather than the table, so the header answers the question that
  was asked instead of putting a 16 next to a single row. An empty ?source= is
  refused rather than read as "every source".

Teeth: QueryHonestyTests (13) — integration 125 -> 138, and the three tests
the guard broke on the first run are the proof it bites. Compose gate gained a
scene with 7 assertions (74 -> 81): the filter returns one source and no
other, the total is the filtered total, ?zzz=1 and ?src= are refused by name
on two endpoints, and the parameters the endpoint really does honour still
answer 200.

Gates at this commit — macOS host: unit 128/128, integration 138/138, smoke
28/0, compose 81/0, console 58/0. Linux rehearsal: source PASS (128/128,
138/138, 28/0), compose 81/0, enforcement lag 12 ms.
2026-10-02 10:35:38 +02:00

154 lines
6.7 KiB
C#

using System.Net;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using OpenWaf.Domain.Security;
using OpenWaf.Infrastructure.Persistence;
namespace OpenWaf.Tests.Integration;
/// <summary>
/// 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.
/// </summary>
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<OpenWafDbContext>();
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);
}
}