using System.Net; using System.Text.Json; namespace OpenWaf.Tests.Integration; /// /// A country rule is matched against the two-letter code the geo database /// returns, so a code made of any other two characters can never match /// anything. The shipped image checked only the length. Measured on openwaf:ci: /// PUT /admin-api/countries/12?allowed=false → 204 /// PUT /admin-api/countries/U.?allowed=false → 204 /// GET /admin-api/countries → "12: denied", "U.: denied", "US: allowed" /// probe from a source the database puts in the busiest country → 404, i.e. /// nothing was denied while the operator's table said two countries were. /// And there was no DELETE, so the row was permanent: the console gate's own /// comment used to read "a country rule cannot be deleted, only overwritten". /// public sealed class CountryRuleTests { private const string Scanner = "203.0.113.66"; private static async Task<(HttpStatusCode Status, string Body)> AdminAsync( EdgeFixture f, HttpMethod method, string url) { using var request = new HttpRequestMessage(method, url); request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken); using var response = await f.Client.SendAsync(request); return (response.StatusCode, await response.Content.ReadAsStringAsync()); } private static async Task CodesAsync(EdgeFixture f) { var (_, body) = await AdminAsync(f, HttpMethod.Get, "/admin-api/countries"); return JsonDocument.Parse(body).RootElement.EnumerateArray() .Select(e => e.GetProperty("code").GetString()!).ToArray(); } private static async Task ProbeAsync(EdgeFixture f, string? country) { using var request = new HttpRequestMessage(HttpMethod.Get, "/anything"); request.Headers.TryAddWithoutValidation("X-Forwarded-For", Scanner); if (country is not null) request.Headers.TryAddWithoutValidation("CF-IPCountry", country); using var response = await f.Client.SendAsync(request); return response.StatusCode; } [Theory] [InlineData("12")] [InlineData("U.")] [InlineData("??")] [InlineData("USA")] [InlineData("1A")] [InlineData("U S")] public async Task A_country_code_that_is_not_two_ASCII_letters_is_refused_by_name(string code) { await using var f = new EdgeFixture(geo: "DenyList"); var (status, body) = await AdminAsync(f, HttpMethod.Put, $"/admin-api/countries/{Uri.EscapeDataString(code)}?allowed=false"); Assert.Equal(HttpStatusCode.BadRequest, status); Assert.Contains("code must be a two-letter ISO country code", body); Assert.Contains(code, body); Assert.Empty(await CodesAsync(f)); } [Fact] public async Task A_lowercase_code_is_normalised_and_then_actually_enforced() { await using var f = new EdgeFixture(geo: "DenyList"); var (status, _) = await AdminAsync(f, HttpMethod.Put, "/admin-api/countries/de?allowed=false"); Assert.Equal(HttpStatusCode.NoContent, status); Assert.Equal(new[] { "DE" }, await CodesAsync(f)); Assert.Equal((HttpStatusCode)451, await ProbeAsync(f, "DE")); Assert.Equal(HttpStatusCode.NotFound, await ProbeAsync(f, "FR")); } [Fact] public async Task A_country_rule_can_be_removed_and_stops_being_enforced_on_the_wire() { await using var f = new EdgeFixture(geo: "DenyList"); Assert.Equal(HttpStatusCode.NoContent, (await AdminAsync(f, HttpMethod.Put, "/admin-api/countries/DE?allowed=false")).Status); Assert.Equal((HttpStatusCode)451, await ProbeAsync(f, "DE")); var (removed, _) = await AdminAsync(f, HttpMethod.Delete, "/admin-api/countries/DE"); Assert.Equal(HttpStatusCode.NoContent, removed); Assert.Empty(await CodesAsync(f)); // The rule is gone from the engine too, not only from the table — the // snapshot is invalidated by the delete, so the next request is decided // without it. Assert.Equal(HttpStatusCode.NotFound, await ProbeAsync(f, "DE")); } [Fact] public async Task Removing_a_rule_that_was_never_written_names_the_code_instead_of_passing_for_success() { await using var f = new EdgeFixture(geo: "DenyList"); var (status, body) = await AdminAsync(f, HttpMethod.Delete, "/admin-api/countries/ZZ"); Assert.Equal(HttpStatusCode.NotFound, status); // Parsed, not substring-matched: the quotes are inside a JSON string, so // a substring assertion here would have to quote the escaping too. Assert.Equal("no country rule for \"ZZ\"", JsonDocument.Parse(body).RootElement.GetProperty("error").GetString()); } [Fact] public async Task A_refused_country_code_leaves_the_rule_that_was_already_there_alone() { await using var f = new EdgeFixture(geo: "DenyList"); Assert.Equal(HttpStatusCode.NoContent, (await AdminAsync(f, HttpMethod.Put, "/admin-api/countries/DE?allowed=false")).Status); Assert.Equal(HttpStatusCode.BadRequest, (await AdminAsync(f, HttpMethod.Put, "/admin-api/countries/12?allowed=false")).Status); Assert.Equal(new[] { "DE" }, await CodesAsync(f)); Assert.Equal((HttpStatusCode)451, await ProbeAsync(f, "DE")); } }