using System.Net; using System.Text; using System.Text.Json; namespace OpenWaf.Tests.Integration; /// /// 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). /// 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 CountAsync(EdgeFixture f, string url) { var (_, body) = await AdminAsync(f, HttpMethod.Get, url); return JsonDocument.Parse(body).RootElement.GetArrayLength(); } private static async Task 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")); } }