PUT /admin-api/countries/{code} checked one thing about the code: that it was
two characters long. Measured on the shipped image (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 geo database puts in a real country -> 404
A country rule is matched against the two-letter code the geo database returns,
so "12" and "U." can never match anything: the operator's table said two
countries were denied and the edge enforced nothing. With US denied for real the
same probe answers 451, which is the difference the table was hiding. 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", and the residue out
lived `compose down` because it lives in the data volume.
Now the code must be two ASCII letters or the write is refused by name:
"code must be a two-letter ISO country code; got \"12\"". DELETE
/admin-api/countries/{code} removes a rule and invalidates the snapshot, and
says `no country rule for "ZZ"` when there is nothing to remove. The page offers
"remove rule" on a row that has a rule, and the gate's cleanup deletes the US
rule it sets instead of leaving it behind.
The country part of the console gate was also fixed, and it was a gate bug worth
naming: it assumed a US rule already existed. With residue in the volume the
first click denied the country; on a fresh volume it allowed it, so the
assertion only ever passed on leftover state. It now starts from no rule and
asserts — → allowed → denied → removed.
Teeth, each reverted and counted, then restored: length-only code check -> 5
integration failures; DELETE endpoint removed -> 2; both out -> 9.
Gates on this tree: unit 128/128, integration 99/99, smoke passed 19 failed 0,
compose 43 passed 0 failed, console 44 assertions 0 failed, image 261 MB.
125 lines
5.3 KiB
C#
125 lines
5.3 KiB
C#
using System.Net;
|
|
using System.Text.Json;
|
|
|
|
namespace OpenWaf.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// 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".
|
|
/// </summary>
|
|
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<string[]> 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<HttpStatusCode> 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"));
|
|
}
|
|
}
|