Files
openwaf-agent ac672d9908 A write endpoint dropped a parameter it never implemented and changed state anyway
OPEN-33 put a guard on the read listings. The image that carried it then
answered the same request two different ways depending on the verb:

  GET    /admin-api/blocks?zzz=1                    -> 400, names 'zzz'
  POST   /admin-api/blocks?zzz=1                    -> 204, ban written
  PUT    /admin-api/policy?zzz=1                    -> 200, policy changed
  PUT    /admin-api/countries/DE?allowed=true&zzz=1 -> 204
  DELETE /admin-api/blocks?key=...&zzz=1            -> 200 {"deleted":1}
  GET    /admin-api/countries?limit=2               -> 200, every row

The silent half was the half that changes state. A misspelled scoping
parameter on a write acts with a wider scope than the caller meant, and
answers as if the scope had applied.

The guard now covers every endpoint under /admin-api, and it runs before
any state change, so a rejected write cannot half-happen. The last line
above changed the design: the guard used to assume limit/offset were
always legal, which is the same bug one level down -- /countries has no
paging to honour, so ?limit=2 was accepted, dropped, and answered with
the whole table. Each endpoint now declares its own parameter surface
and the base set is only `token`, the one parameter real for every
endpoint because the auth layer reads it (OPEN-16).

Teeth: WriteQueryGuardTests (29) asserts the 400 per endpoint, that the
refusal names the parameter that was actually sent, that a refused write
changes nothing (block, policy, country rule), that the same request
without the unknown parameter is still served, and that ?token= still
works on a write. The compose gate grew a scene (8 assertions) that runs
the same measurements against the shipped image.

Measured on this tree: unit 128/128, integration 170/170, smoke 28/0,
compose 94/0 (enforcement lag 11 ms on Linux), console 58/0, image 261
MB. Linux rehearsal: all three stages PASS. act: 5 jobs succeeded, 0
failed, console-evidence.zip 534721 bytes.
2026-10-02 12:08:02 +02:00

204 lines
10 KiB
C#

using System.Net;
using System.Text;
using System.Text.Json;
namespace OpenWaf.Tests.Integration;
/// <summary>
/// The query guard landed on the read endpoints first, and the shipped image then
/// answered the same request two different ways depending on the verb. Measured
/// on openwaf:ci (the tree at 336c2f7) with the guard in place:
///
/// GET /admin-api/blocks?zzz=1 -> 400, names 'zzz'
/// POST /admin-api/blocks?zzz=1 -> 204, and the ban was written
/// PUT /admin-api/policy?zzz=1 -> 200, and the policy changed
/// PUT /admin-api/countries/DE?allowed=true&zzz=1 -> 204
/// DELETE /admin-api/blocks?key=...&zzz=1 -> 200 {"deleted":1}
/// GET /admin-api/countries?limit=2 -> 200 with every row
///
/// The silent half was the half that changes state. A misspelled scoping
/// parameter on a write is not a cosmetic miss: `DELETE
/// /admin-api/blocks?src=engine` reads as "no source filter", which the endpoint
/// then refuses only because it also demands a key - while `?sourc=operator` next
/// to a key would have deleted the row the caller did not mean to touch. So the
/// guard covers every endpoint under /admin-api, and it runs before any state
/// change, so a rejected write cannot half-happen.
///
/// The second measurement is the one that changed the design: the guard used to
/// assume limit/offset were always legal, and that is the same bug one level
/// down. /admin-api/countries has no paging, so `?limit=2` was accepted, dropped,
/// and answered with all 3 rows. Each endpoint now declares its own parameter
/// surface and the guard's base set is only `token`, the one parameter that is
/// real for every endpoint because the auth layer reads it (OPEN-16).
/// </summary>
public sealed class WriteQueryGuardTests
{
private static async Task<(HttpStatusCode Status, string Body)> AdminAsync(
EdgeFixture f, HttpMethod method, string url, string? json = null)
{
using var request = new HttpRequestMessage(method, url);
request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken);
if (json is not null)
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
using var response = await f.Client.SendAsync(request);
return (response.StatusCode, await response.Content.ReadAsStringAsync());
}
private static async Task<int> CountAsync(EdgeFixture f, string url)
{
var (_, body) = await AdminAsync(f, HttpMethod.Get, url);
return JsonDocument.Parse(body).RootElement.GetArrayLength();
}
private static async Task<int> PolicyThresholdAsync(EdgeFixture f)
{
var (_, body) = await AdminAsync(f, HttpMethod.Get, "/admin-api/policy");
return JsonDocument.Parse(body).RootElement.GetProperty("streakThreshold").GetInt32();
}
[Theory]
[InlineData("POST", "/admin-api/blocks?zzz=1", "{\"ip\":\"203.0.113.77\"}")]
[InlineData("DELETE", "/admin-api/blocks?source=operator&zzz=1", null)]
[InlineData("DELETE", "/admin-api/blocks?key=203.0.113.77&zzz=1", null)]
[InlineData("POST", "/admin-api/allow?zzz=1", "{\"match\":\"203.0.113.78\"}")]
[InlineData("DELETE", "/admin-api/allow/1?zzz=1", null)]
[InlineData("PUT", "/admin-api/policy?zzz=1", "{\"streakThreshold\":7}")]
[InlineData("PUT", "/admin-api/countries/de?allowed=true&zzz=1", null)]
[InlineData("DELETE", "/admin-api/countries/de?zzz=1", null)]
public async Task A_write_refuses_a_query_parameter_it_never_implemented_and_names_it(
string method, string url, string? json)
{
await using var f = new EdgeFixture(threshold: 9);
var (status, body) = await AdminAsync(f, new HttpMethod(method), url, json);
Assert.Equal(HttpStatusCode.BadRequest, status);
Assert.Contains("unknown query parameter 'zzz'", body);
// Naming the bad parameter is half the answer; the operator also has to
// be able to see the spelling this endpoint really does take.
Assert.Contains("this endpoint accepts", body);
}
[Theory]
[InlineData("/admin-api/countries?limit=2", "limit")]
[InlineData("/admin-api/countries?offset=1", "offset")]
[InlineData("/admin-api/countries/summary?limit=2", "limit")]
[InlineData("/admin-api/policy?live=false", "live")]
[InlineData("/admin-api/state?zzz=1", "zzz")]
[InlineData("/admin-api/geo?zzz=1", "zzz")]
public async Task An_endpoint_without_paging_refuses_the_paging_parameters_it_never_implemented(
string url, string name)
{
await using var f = new EdgeFixture(threshold: 9, geo: "DenyList");
var (status, body) = await AdminAsync(f, HttpMethod.Get, url);
Assert.Equal(HttpStatusCode.BadRequest, status);
// The name has to be the one that was actually sent. A guard that reports
// a generic complaint is a 400 the operator still has to debug by hand.
Assert.Contains($"unknown query parameter '{name}'", body);
}
[Fact]
public async Task A_refused_write_changes_nothing_and_the_same_request_without_it_still_works()
{
await using var f = new EdgeFixture(threshold: 9);
var (status, _) = await AdminAsync(f, HttpMethod.Post, "/admin-api/blocks?zzz=1",
"{\"ip\":\"203.0.113.77\",\"reason\":\"guard probe\"}");
Assert.Equal(HttpStatusCode.BadRequest, status);
Assert.Equal(0, await CountAsync(f, "/admin-api/blocks?source=operator"));
// The refusal is about the parameter, not about the write: the identical
// body with no unknown parameter is served.
var (ok, _) = await AdminAsync(f, HttpMethod.Post, "/admin-api/blocks",
"{\"ip\":\"203.0.113.77\",\"reason\":\"guard probe\"}");
Assert.Equal(HttpStatusCode.NoContent, ok);
Assert.Equal(1, await CountAsync(f, "/admin-api/blocks?source=operator"));
}
[Fact]
public async Task A_refused_delete_leaves_the_row_it_was_asked_to_remove()
{
await using var f = new EdgeFixture(threshold: 9);
var (created, _) = await AdminAsync(f, HttpMethod.Post, "/admin-api/blocks",
"{\"ip\":\"203.0.113.79\"}");
Assert.Equal(HttpStatusCode.NoContent, created);
var (status, body) = await AdminAsync(f, HttpMethod.Delete,
"/admin-api/blocks?key=203.0.113.79&zzz=1");
Assert.Equal(HttpStatusCode.BadRequest, status);
// A count of what was removed may only be said by an endpoint that ran
// the removal - the same rule that made {"deleted":0} a 503 in OPEN-32.
Assert.DoesNotContain("deleted", body);
Assert.Equal(1, await CountAsync(f, "/admin-api/blocks?source=operator"));
}
[Fact]
public async Task A_refused_policy_write_leaves_the_policy_the_edge_is_enforcing()
{
await using var f = new EdgeFixture(threshold: 3);
Assert.Equal(3, await PolicyThresholdAsync(f));
var (status, _) = await AdminAsync(f, HttpMethod.Put, "/admin-api/policy?zzz=1",
"{\"streakThreshold\":7}");
Assert.Equal(HttpStatusCode.BadRequest, status);
Assert.Equal(3, await PolicyThresholdAsync(f));
var (ok, _) = await AdminAsync(f, HttpMethod.Put, "/admin-api/policy",
"{\"streakThreshold\":7}");
Assert.Equal(HttpStatusCode.OK, ok);
Assert.Equal(7, await PolicyThresholdAsync(f));
}
[Fact]
public async Task A_refused_country_write_writes_no_rule()
{
await using var f = new EdgeFixture(geo: "DenyList");
var (status, _) = await AdminAsync(f, HttpMethod.Put,
"/admin-api/countries/de?allowed=false&zzz=1");
Assert.Equal(HttpStatusCode.BadRequest, status);
Assert.Equal(0, await CountAsync(f, "/admin-api/countries"));
var (ok, _) = await AdminAsync(f, HttpMethod.Put, "/admin-api/countries/de?allowed=false");
Assert.Equal(HttpStatusCode.NoContent, ok);
Assert.Equal(1, await CountAsync(f, "/admin-api/countries"));
}
[Theory]
[InlineData("POST", "/admin-api/blocks", "{\"ip\":\"203.0.113.80\"}")]
[InlineData("DELETE", "/admin-api/blocks?source=operator", null)]
[InlineData("POST", "/admin-api/allow", "{\"match\":\"203.0.113.0/24\"}")]
[InlineData("DELETE", "/admin-api/allow/404", null)]
[InlineData("PUT", "/admin-api/countries/fr?allowed=true", null)]
[InlineData("DELETE", "/admin-api/countries/fr", null)]
[InlineData("GET", "/admin-api/countries", null)]
[InlineData("GET", "/admin-api/countries/summary", null)]
[InlineData("GET", "/admin-api/state", null)]
[InlineData("GET", "/admin-api/geo", null)]
public async Task The_parameters_each_endpoint_does_implement_keep_working(
string method, string url, string? json)
{
// The guard is only worth having if it never fires on a parameter the
// endpoint really honours. Both smoke gates, the console gate and the
// compose gate call these with source/key/live/allowed every run.
await using var f = new EdgeFixture(threshold: 9, geo: "DenyList");
var (status, body) = await AdminAsync(f, new HttpMethod(method), url, json);
Assert.NotEqual(HttpStatusCode.BadRequest, status);
Assert.DoesNotContain("unknown query parameter", body);
}
[Fact]
public async Task The_curl_friendly_token_still_works_on_a_write_endpoint()
{
// `token` is not a handler parameter, so a guard that only knows the
// handler's own signature would refuse the one request shape OPEN-16
// ruled has to keep working.
await using var f = new EdgeFixture(threshold: 9);
var (created, _) = await AdminAsync(f, HttpMethod.Post, "/admin-api/blocks",
"{\"ip\":\"203.0.113.81\"}");
Assert.Equal(HttpStatusCode.NoContent, created);
using var request = new HttpRequestMessage(HttpMethod.Delete,
$"/admin-api/blocks?token={EdgeFixture.AdminToken}&source=operator");
using var response = await f.Client.SendAsync(request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(0, await CountAsync(f, "/admin-api/blocks?source=operator"));
}
}